Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / qadevOOo / runner / util / FormTools.java
blob2189612cd17157835fc58b2a87927300cdbe75c2
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package util;
21 // access the implementations via names
22 import com.sun.star.uno.XInterface;
23 import com.sun.star.uno.UnoRuntime;
24 import com.sun.star.lang.XComponent;
25 import com.sun.star.drawing.XControlShape;
26 import com.sun.star.drawing.XDrawPage;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.awt.Size;
29 import com.sun.star.awt.Point;
30 import com.sun.star.awt.XControlModel;
31 import com.sun.star.container.XNameContainer;
32 import com.sun.star.container.XIndexContainer;
33 import com.sun.star.form.XFormsSupplier;
34 import com.sun.star.form.XForm;
35 import com.sun.star.form.XLoadable;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
41 /**
42 * contains helper methods forms
45 public class FormTools {
48 /**
49 * creates a XControlShape
51 * @param oDoc the document
52 * @param height the height of the shape
53 * @param width the width of the shape
54 * @param x the x-position of the shape
55 * @param y the y-position of the shape
56 * @param kind the kind of the shape
57 * @return the created XControlShape
59 public static XControlShape createControlShape( XComponent oDoc, int height,
60 int width, int x, int y, String kind )
61 throws com.sun.star.uno.Exception
64 Size size = new Size();
65 Point position = new Point();
66 XControlShape oCShape = null;
67 XControlModel aControl = null;
69 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
71 Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
72 Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
73 XPropertySet model_props = UnoRuntime.queryInterface(XPropertySet.class,aCon);
74 model_props.setPropertyValue("DefaultControl","com.sun.star.form.control."+kind);
75 aControl = UnoRuntime.queryInterface( XControlModel.class, aCon );
76 oCShape = UnoRuntime.queryInterface( XControlShape.class, oInt );
77 size.Height = height;
78 size.Width = width;
79 position.X = x;
80 position.Y = y;
81 oCShape.setSize(size);
82 oCShape.setPosition(position);
84 oCShape.setControl(aControl);
86 return oCShape;
87 } // finish createControlShape
89 public static XControlShape createUnoControlShape( XComponent oDoc, int height,
90 int width, int x, int y, String kind, String defControl )
91 throws com.sun.star.uno.Exception
94 Size size = new Size();
95 Point position = new Point();
96 XControlShape oCShape = null;
97 XControlModel aControl = null;
99 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
101 Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
102 Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
103 XPropertySet model_props = UnoRuntime.queryInterface(XPropertySet.class,aCon);
104 model_props.setPropertyValue("DefaultControl","com.sun.star.awt."+defControl);
105 aControl = UnoRuntime.queryInterface( XControlModel.class, aCon );
106 oCShape = UnoRuntime.queryInterface( XControlShape.class, oInt );
107 size.Height = height;
108 size.Width = width;
109 position.X = x;
110 position.Y = y;
111 oCShape.setSize(size);
112 oCShape.setPosition(position);
114 oCShape.setControl(aControl);
116 return oCShape;
117 } // finish createControlShape
119 public static XControlShape createControlShapeWithDefaultControl( XComponent oDoc, int height,
120 int width, int x, int y, String kind )
121 throws com.sun.star.uno.Exception
124 Size size = new Size();
125 Point position = new Point();
126 XControlShape oCShape = null;
127 XControlModel aControl = null;
129 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
131 Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
132 Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
134 aControl = UnoRuntime.queryInterface( XControlModel.class, aCon );
135 oCShape = UnoRuntime.queryInterface( XControlShape.class, oInt );
136 size.Height = height;
137 size.Width = width;
138 position.X = x;
139 position.Y = y;
140 oCShape.setSize(size);
141 oCShape.setPosition(position);
143 oCShape.setControl(aControl);
145 return oCShape;
146 } // finish createControlShape
148 public static XInterface createControl( XComponent oDoc, String kind )
149 throws com.sun.star.uno.Exception
151 XInterface oControl = null;
153 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
155 oControl = (XInterface) oDocMSF.createInstance(
156 "com.sun.star.form.component."+kind);
157 return oControl;
158 } // finish createControl
160 public static XNameContainer getForms ( XDrawPage oDP )
162 XFormsSupplier oFS = UnoRuntime.queryInterface(
163 XFormsSupplier.class,oDP);
164 return oFS.getForms();
165 } //finish getForms
167 private static XIndexContainer getIndexedForms ( XDrawPage oDP )
169 XFormsSupplier oFS = UnoRuntime.queryInterface(
170 XFormsSupplier.class,oDP);
171 return UnoRuntime.queryInterface( XIndexContainer.class,
172 oFS.getForms() );
173 } //finish getIndexedForms
175 public static void insertForm ( XComponent aDoc, XNameContainer Forms,
176 String aName )
177 throws com.sun.star.uno.Exception
179 XInterface oControl = createControl(aDoc, "Form");
180 XForm oForm = UnoRuntime.queryInterface(XForm.class, oControl);
181 Forms.insertByName(aName,oForm);
184 public static XControlShape insertControlShape( XComponent oDoc, int height,
185 int width, int x, int y, String kind )
186 throws com.sun.star.uno.Exception
188 XControlShape aShape = createControlShape(oDoc,height,width,x,y,kind);
189 XDrawPage oDP = DrawTools.getDrawPage(oDoc,0);
190 DrawTools.getShapes(oDP).add(aShape);
191 return aShape;
194 public static XLoadable bindForm( XTextDocument aDoc )
195 throws com.sun.star.uno.Exception
197 XLoadable formLoader = null;
199 Object aForm = FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0);
200 XForm the_form = null;
201 the_form = (XForm) AnyConverter.toObject(new Type(XForm.class), aForm);
202 XPropertySet formProps = UnoRuntime.queryInterface(XPropertySet.class, the_form);
203 formProps.setPropertyValue("DataSourceName","Bibliography");
204 formProps.setPropertyValue("Command","biblio");
205 formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
206 formLoader = UnoRuntime.queryInterface(XLoadable.class, the_form);
208 return formLoader;
212 * Binds <code>'Standard'</code> form of <code>aDoc</code> Writer document
213 * to the <code>tableName</code> table of <code>sourceName</code>
214 * Data Source.
215 * @param aDoc Writer document where DB controls are added.
216 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
217 * @param tableName The name of the table to which controls are bound.
218 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
219 * implementation which is the bound form inside the document.
221 public static XLoadable bindForm( XTextDocument aDoc, String sourceName, String tableName )
222 throws com.sun.star.uno.Exception {
224 XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
225 FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0));
226 XPropertySet formProps = UnoRuntime.queryInterface(XPropertySet.class, the_form);
227 formProps.setPropertyValue("DataSourceName",sourceName);
228 formProps.setPropertyValue("Command",tableName);
229 formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
231 return UnoRuntime.queryInterface(XLoadable.class, the_form);
237 * Binds the form with the name specified of <code>aDoc</code> Writer document
238 * to the <code>tableName</code> table of <code>sourceName</code>
239 * Data Source.
240 * @param aDoc Writer document where DB controls are added.
241 * @param formName The name of the form to be bound.
242 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
243 * @param tableName The name of the table to which controls are bound.
244 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
245 * implementation which is the bound form inside the document.
247 public static XLoadable bindForm( XTextDocument aDoc, String formName, String sourceName,
248 String tableName) throws com.sun.star.uno.Exception {
250 XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
251 FormTools.getForms(WriterTools.getDrawPage(aDoc)).getByName(formName));
252 XPropertySet formProps = UnoRuntime.queryInterface(XPropertySet.class, the_form);
253 formProps.setPropertyValue("DataSourceName",sourceName);
254 formProps.setPropertyValue("Command",tableName);
255 formProps.setPropertyValue("CommandType",Integer.valueOf(com.sun.star.sdb.CommandType.TABLE));
257 return UnoRuntime.queryInterface(XLoadable.class, the_form);
260 public static void switchDesignOf(XMultiServiceFactory xMSF, XTextDocument aDoc)
261 throws com.sun.star.uno.Exception
263 com.sun.star.frame.XController aController = aDoc.getCurrentController();
264 com.sun.star.frame.XFrame aFrame = aController.getFrame();
265 com.sun.star.frame.XDispatchProvider aDispProv = UnoRuntime.queryInterface(com.sun.star.frame.XDispatchProvider.class,aFrame);
266 com.sun.star.util.URL aURL = new com.sun.star.util.URL();
267 aURL.Complete = ".uno:SwitchControlDesignMode";
269 Object instance = xMSF.createInstance("com.sun.star.util.URLTransformer");
270 com.sun.star.util.XURLTransformer atrans =
271 UnoRuntime.queryInterface(
272 com.sun.star.util.XURLTransformer.class,instance);
273 com.sun.star.util.URL[] aURLA = new com.sun.star.util.URL[1];
274 aURLA[0] = aURL;
275 atrans.parseStrict(aURLA);
276 aURL = aURLA[0];
278 com.sun.star.frame.XDispatch aDisp = aDispProv.queryDispatch(aURL, "",
279 com.sun.star.frame.FrameSearchFlag.SELF |
280 com.sun.star.frame.FrameSearchFlag.CHILDREN);
282 com.sun.star.beans.PropertyValue[] noArgs = new com.sun.star.beans.PropertyValue[0];
283 aDisp.dispatch(aURL, noArgs);
284 util.utils.waitForEventIdle(xMSF); // async dispatch