summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/FormTools.java
blob: 46515043f67707f6c217b2b49dcfff41a817bc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package util;

// access the implementations via names
import com.sun.star.uno.XInterface;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XComponent;
import com.sun.star.drawing.XControlShape;
import com.sun.star.drawing.XDrawPage;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.awt.Size;
import com.sun.star.awt.Point;
import com.sun.star.awt.XControlModel;
import com.sun.star.container.XNameContainer;
import com.sun.star.container.XIndexContainer;
import com.sun.star.form.XFormsSupplier;
import com.sun.star.form.XForm;
import com.sun.star.form.XLoadable;
import com.sun.star.text.XTextDocument;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Type;

/**
 * contains helper methods forms
 */

public class FormTools {


    /**
     * creates a XControlShape
     *
     * @param oDoc the document
     * @param height the height of the shape
     * @param width the width of the shape
     * @param x the x-position of the shape
     * @param y the y-position of the shape
     * @param kind the kind of the shape
     * @return the created XControlShape
    */
    public static XControlShape createControlShape( XComponent oDoc, int height,
                                        int width, int x, int y, String kind ) {

         Size size = new Size();
        Point position = new Point();
        XControlShape oCShape = null;
        XControlModel aControl = null;

        //get MSF
        XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
                UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );

        try{
            Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
            Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
            XPropertySet model_props = (XPropertySet)
                    UnoRuntime.queryInterface(XPropertySet.class,aCon);
            model_props.setPropertyValue("DefaultControl","com.sun.star.form.control."+kind);
            aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
            oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
            size.Height = height;
            size.Width = width;
            position.X = x;
            position.Y = y;
            oCShape.setSize(size);
            oCShape.setPosition(position);
        } catch ( com.sun.star.uno.Exception e ) {
            // Some exception occures.FAILED
            System.out.println( "Couldn't create instance "+ e );
        }

        oCShape.setControl(aControl);

        return oCShape;
    } // finish createControlShape

    public static XControlShape createUnoControlShape( XComponent oDoc, int height,
                                        int width, int x, int y, String kind, String defControl ) {

         Size size = new Size();
        Point position = new Point();
        XControlShape oCShape = null;
        XControlModel aControl = null;

        //get MSF
           XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );

           try{
         Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
         Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
         XPropertySet model_props = (XPropertySet)
                        UnoRuntime.queryInterface(XPropertySet.class,aCon);
         model_props.setPropertyValue("DefaultControl","com.sun.star.awt."+defControl);
         aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
         oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
         size.Height = height;
         size.Width = width;
         position.X = x;
         position.Y = y;
         oCShape.setSize(size);
         oCShape.setPosition(position);


           } catch ( com.sun.star.uno.Exception e ) {
            // Some exception occures.FAILED
            System.out.println( "Couldn't create instance "+ e );
        }

        oCShape.setControl(aControl);

        return oCShape;
    } // finish createControlShape

    public static XControlShape createControlShapeWithDefaultControl( XComponent oDoc, int height,
                                        int width, int x, int y, String kind ) {

         Size size = new Size();
        Point position = new Point();
        XControlShape oCShape = null;
        XControlModel aControl = null;

        //get MSF
           XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );

           try{
         Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
         Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);

         aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
         oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
         size.Height = height;
         size.Width = width;
         position.X = x;
         position.Y = y;
         oCShape.setSize(size);
         oCShape.setPosition(position);


           } catch ( com.sun.star.uno.Exception e ) {
            // Some exception occures.FAILED
            System.out.println( "Couldn't create instance "+ e );
        }

        oCShape.setControl(aControl);

        return oCShape;
    } // finish createControlShape

    public static XInterface createControl( XComponent oDoc, String kind ) {

        XInterface oControl = null;

           XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
                UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );

           try{
            oControl = (XInterface) oDocMSF.createInstance(
                                        "com.sun.star.form.component."+kind);
           } catch ( Exception e ) {
            // Some exception occures.FAILED
            System.out.println( "Couldn't create instance "+ kind + ": "+ e );
        }
        return oControl;
    } // finish createControl

    public static XNameContainer getForms ( XDrawPage oDP )
    {
        XFormsSupplier oFS = (XFormsSupplier) UnoRuntime.queryInterface(
                                                    XFormsSupplier.class,oDP);
        return oFS.getForms();
    } //finish getForms

    public static XIndexContainer getIndexedForms ( XDrawPage oDP )
    {
        XFormsSupplier oFS = (XFormsSupplier) UnoRuntime.queryInterface(
                                                    XFormsSupplier.class,oDP);
        return (XIndexContainer)UnoRuntime.queryInterface( XIndexContainer.class,
            oFS.getForms() );
    } //finish getIndexedForms

    public static void insertForm ( XComponent aDoc, XNameContainer Forms,
                                                                String aName ) {
        try {
            XInterface oControl = createControl(aDoc, "Form");
            XForm oForm = (XForm) UnoRuntime.queryInterface(XForm.class, oControl);
            Forms.insertByName(aName,oForm);
        } catch ( Exception e ) {
            throw new IllegalArgumentException( "Couldn't insert Form" );
        }
    }

    public static XControlShape insertControlShape( XComponent oDoc, int height,
                                        int width, int x, int y, String kind ) {

        XControlShape aShape = createControlShape(oDoc,height,width,x,y,kind);
        XDrawPage oDP = DrawTools.getDrawPage(oDoc,0);
        DrawTools.getShapes(oDP).add(aShape);
        return aShape;
    }

    public static XLoadable bindForm( XTextDocument aDoc ) {
        XLoadable formLoader = null;

        try {
            Object aForm = FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0);
            XForm the_form = null;
            try {
                the_form = (XForm) AnyConverter.toObject(new Type(XForm.class), aForm);
            } catch (com.sun.star.lang.IllegalArgumentException iae) {
                System.out.println("### Couldn't convert Any");
            }
            XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
            formProps.setPropertyValue("DataSourceName","Bibliography");
            formProps.setPropertyValue("Command","biblio");
            formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
            formLoader = (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
        }
        catch (Exception ex) {
            System.out.println("Exception: "+ex);
            ex.printStackTrace(System.out);
        }

        return formLoader;
    }

    /**
    * Binds <code>'Standard'</code> form of <code>aDoc</code> Writer document
    * to the <code>tableName</code> table of <code>sourceName</code>
    * Data Source.
    * @param aDoc Writer document where DB controls are added.
    * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
    * @param tableName The name of the table to which controls are bound.
    * @return <code>com.sun.star.form.component.DatabaseForm</code> service
    * implementation which is the bound form inside the document.
    */
    public static XLoadable bindForm( XTextDocument aDoc, String sourceName, String tableName )
        throws com.sun.star.uno.Exception {

        XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
            FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0));
        XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
        formProps.setPropertyValue("DataSourceName",sourceName);
        formProps.setPropertyValue("Command",tableName);
        formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));

        return (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
    }

    public static XLoadable bindForm( XTextDocument aDoc, String formName ) {
        XLoadable formLoader = null;

        try {
            XForm the_form = (XForm) FormTools.getForms(WriterTools.getDrawPage(aDoc)).getByName(formName);
            XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
            formProps.setPropertyValue("DataSourceName","Bibliography");
            formProps.setPropertyValue("Command","biblio");
            formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
            formLoader = (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
        }
        catch (Exception ex) {
            System.out.println("Exception: "+ex);
            ex.printStackTrace(System.out);
        }

        return formLoader;
    }

    /**
    * Binds the form with the name specified of <code>aDoc</code> Writer document
    * to the <code>tableName</code> table of <code>sourceName</code>
    * Data Source.
    * @param aDoc Writer document where DB controls are added.
    * @param formName The name of the form to be bound.
    * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
    * @param tableName The name of the table to which controls are bound.
    * @return <code>com.sun.star.form.component.DatabaseForm</code> service
    * implementation which is the bound form inside the document.
    */
    public static XLoadable bindForm( XTextDocument aDoc, String formName, String sourceName,
        String tableName) throws com.sun.star.uno.Exception {

        XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
            FormTools.getForms(WriterTools.getDrawPage(aDoc)).getByName(formName));
        XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
        formProps.setPropertyValue("DataSourceName",sourceName);
        formProps.setPropertyValue("Command",tableName);
        formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));

        return (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
    }

    public static void switchDesignOf(XMultiServiceFactory xMSF, XTextDocument aDoc) {
    try {
        com.sun.star.frame.XController aController = aDoc.getCurrentController();
        com.sun.star.frame.XFrame aFrame = aController.getFrame();
        com.sun.star.frame.XDispatchProvider aDispProv = (com.sun.star.frame.XDispatchProvider)
                UnoRuntime.queryInterface(com.sun.star.frame.XDispatchProvider.class,aFrame);
        com.sun.star.util.URL aURL = new com.sun.star.util.URL();
        aURL.Complete = ".uno:SwitchControlDesignMode";

        Object instance = xMSF.createInstance("com.sun.star.util.URLTransformer");
        com.sun.star.util.XURLTransformer atrans =
                (com.sun.star.util.XURLTransformer)UnoRuntime.queryInterface(
                                    com.sun.star.util.XURLTransformer.class,instance);
        com.sun.star.util.URL[] aURLA = new com.sun.star.util.URL[1];
        aURLA[0] = aURL;
        atrans.parseStrict(aURLA);
        aURL = aURLA[0];

        com.sun.star.frame.XDispatch aDisp = (com.sun.star.frame.XDispatch)aDispProv.queryDispatch(aURL, "",
                                com.sun.star.frame.FrameSearchFlag.SELF |
                                    com.sun.star.frame.FrameSearchFlag.CHILDREN);

        com.sun.star.beans.PropertyValue[] noArgs = new com.sun.star.beans.PropertyValue[0];
        aDisp.dispatch(aURL, noArgs);
        } catch (Exception e) {
            System.out.println("******* Mist");
            e.printStackTrace();
            }
    }

}