summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/FormTools.java
blob: 2189612cd17157835fc58b2a87927300cdbe75c2 (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
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

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 )
        throws com.sun.star.uno.Exception
    {

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

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

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

        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 )
        throws com.sun.star.uno.Exception
    {

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

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

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

        oCShape.setControl(aControl);

        return oCShape;
    } // finish createControlShape

    public static XControlShape createControlShapeWithDefaultControl( XComponent oDoc, int height,
                                        int width, int x, int y, String kind )
        throws com.sun.star.uno.Exception
    {

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

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

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

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

        oCShape.setControl(aControl);

        return oCShape;
    } // finish createControlShape

    public static XInterface createControl( XComponent oDoc, String kind )
        throws com.sun.star.uno.Exception
    {
        XInterface oControl = null;

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

        oControl = (XInterface) oDocMSF.createInstance(
                                        "com.sun.star.form.component."+kind);
        return oControl;
    } // finish createControl

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

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

    public static void insertForm ( XComponent aDoc, XNameContainer Forms,
                                                                String aName )
        throws com.sun.star.uno.Exception
    {
        XInterface oControl = createControl(aDoc, "Form");
        XForm oForm = UnoRuntime.queryInterface(XForm.class, oControl);
        Forms.insertByName(aName,oForm);
    }

    public static XControlShape insertControlShape( XComponent oDoc, int height,
                                        int width, int x, int y, String kind )
        throws com.sun.star.uno.Exception
    {
        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 )
        throws com.sun.star.uno.Exception
    {
        XLoadable formLoader = null;

        Object aForm = FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0);
        XForm the_form = null;
        the_form = (XForm) AnyConverter.toObject(new Type(XForm.class), aForm);
        XPropertySet formProps = UnoRuntime.queryInterface(XPropertySet.class, the_form);
        formProps.setPropertyValue("DataSourceName","Bibliography");
        formProps.setPropertyValue("Command","biblio");
        formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
        formLoader = UnoRuntime.queryInterface(XLoadable.class, the_form);

        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 = UnoRuntime.queryInterface(XPropertySet.class, the_form);
        formProps.setPropertyValue("DataSourceName",sourceName);
        formProps.setPropertyValue("Command",tableName);
        formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));

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



    /**
    * 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 = UnoRuntime.queryInterface(XPropertySet.class, the_form);
        formProps.setPropertyValue("DataSourceName",sourceName);
        formProps.setPropertyValue("Command",tableName);
        formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));

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

    public static void switchDesignOf(XMultiServiceFactory xMSF, XTextDocument aDoc)
        throws com.sun.star.uno.Exception
    {
        com.sun.star.frame.XController aController = aDoc.getCurrentController();
        com.sun.star.frame.XFrame aFrame = aController.getFrame();
        com.sun.star.frame.XDispatchProvider aDispProv = 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 =
                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 = 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);
        util.utils.waitForEventIdle(xMSF); // async dispatch
    }

}