1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package util;
29 
30 // access the implementations via names
31 import com.sun.star.uno.XInterface;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.drawing.XControlShape;
35 import com.sun.star.drawing.XDrawPage;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.awt.Size;
38 import com.sun.star.awt.Point;
39 import com.sun.star.awt.XControlModel;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.container.XIndexContainer;
42 import com.sun.star.form.XFormsSupplier;
43 import com.sun.star.form.XForm;
44 import com.sun.star.form.XLoadable;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 
50 /**
51  * contains helper methods forms
52  */
53 
54 public class FormTools {
55 
56 
57     /**
58      * creates a XControlShape
59      *
60      * @param oDoc the document
61      * @param height the height of the shape
62      * @param width the width of the shape
63      * @param x the x-position of the shape
64      * @param y the y-position of the shape
65      * @param kind the kind of the shape
66      * @return the created XControlShape
67     */
68     public static XControlShape createControlShape( XComponent oDoc, int height,
69                                         int width, int x, int y, String kind ) {
70 
71      	Size size = new Size();
72         Point position = new Point();
73         XControlShape oCShape = null;
74         XControlModel aControl = null;
75 
76         //get MSF
77         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
78                 UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
79 
80         try{
81             Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
82             Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
83             XPropertySet model_props = (XPropertySet)
84                     UnoRuntime.queryInterface(XPropertySet.class,aCon);
85             model_props.setPropertyValue("DefaultControl","com.sun.star.form.control."+kind);
86             aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
87             oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
88             size.Height = height;
89             size.Width = width;
90             position.X = x;
91             position.Y = y;
92             oCShape.setSize(size);
93             oCShape.setPosition(position);
94         } catch ( com.sun.star.uno.Exception e ) {
95             // Some exception occures.FAILED
96             System.out.println( "Couldn't create instance "+ e );
97         }
98 
99         oCShape.setControl(aControl);
100 
101         return oCShape;
102     } // finish createControlShape
103 
104 	public static XControlShape createUnoControlShape( XComponent oDoc, int height,
105                                         int width, int x, int y, String kind, String defControl ) {
106 
107      	Size size = new Size();
108         Point position = new Point();
109         XControlShape oCShape = null;
110         XControlModel aControl = null;
111 
112         //get MSF
113    		XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
114 
115    		try{
116          Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
117          Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
118          XPropertySet model_props = (XPropertySet)
119                         UnoRuntime.queryInterface(XPropertySet.class,aCon);
120          model_props.setPropertyValue("DefaultControl","com.sun.star.awt."+defControl);
121          aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
122          oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
123          size.Height = height;
124 		 size.Width = width;
125 		 position.X = x;
126 		 position.Y = y;
127 		 oCShape.setSize(size);
128 		 oCShape.setPosition(position);
129 
130 
131    		} catch ( com.sun.star.uno.Exception e ) {
132 			// Some exception occures.FAILED
133 			System.out.println( "Couldn't create instance "+ e );
134 		}
135 
136         oCShape.setControl(aControl);
137 
138         return oCShape;
139     } // finish createControlShape
140 
141 	public static XControlShape createControlShapeWithDefaultControl( XComponent oDoc, int height,
142                                         int width, int x, int y, String kind ) {
143 
144      	Size size = new Size();
145         Point position = new Point();
146         XControlShape oCShape = null;
147         XControlModel aControl = null;
148 
149         //get MSF
150    		XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
151 
152    		try{
153          Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape");
154          Object aCon = oDocMSF.createInstance("com.sun.star.form.component."+kind);
155 
156          aControl = (XControlModel) UnoRuntime.queryInterface( XControlModel.class, aCon );
157          oCShape = (XControlShape) UnoRuntime.queryInterface( XControlShape.class, oInt );
158          size.Height = height;
159 		 size.Width = width;
160 		 position.X = x;
161 		 position.Y = y;
162 		 oCShape.setSize(size);
163 		 oCShape.setPosition(position);
164 
165 
166    		} catch ( com.sun.star.uno.Exception e ) {
167 			// Some exception occures.FAILED
168 			System.out.println( "Couldn't create instance "+ e );
169 		}
170 
171         oCShape.setControl(aControl);
172 
173         return oCShape;
174     } // finish createControlShape
175 
176 	public static XInterface createControl( XComponent oDoc, String kind ) {
177 
178         XInterface oControl = null;
179 
180    		XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
181                 UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc );
182 
183    		try{
184 	        oControl = (XInterface) oDocMSF.createInstance(
185                                         "com.sun.star.form.component."+kind);
186    		} catch ( Exception e ) {
187 			// Some exception occures.FAILED
188 			System.out.println( "Couldn't create instance "+ kind + ": "+ e );
189 		}
190         return oControl;
191     } // finish createControl
192 
193     public static XNameContainer getForms ( XDrawPage oDP )
194     {
195 		XFormsSupplier oFS = (XFormsSupplier) UnoRuntime.queryInterface(
196                                                     XFormsSupplier.class,oDP);
197 		return oFS.getForms();
198     } //finish getForms
199 
200     public static XIndexContainer getIndexedForms ( XDrawPage oDP )
201     {
202 		XFormsSupplier oFS = (XFormsSupplier) UnoRuntime.queryInterface(
203                                                     XFormsSupplier.class,oDP);
204 		return (XIndexContainer)UnoRuntime.queryInterface( XIndexContainer.class,
205             oFS.getForms() );
206     } //finish getIndexedForms
207 
208     public static void insertForm ( XComponent aDoc, XNameContainer Forms,
209                                                                 String aName ) {
210         try {
211 		    XInterface oControl = createControl(aDoc, "Form");
212 		    XForm oForm = (XForm) UnoRuntime.queryInterface(XForm.class, oControl);
213             Forms.insertByName(aName,oForm);
214 		} catch ( Exception e ) {
215 			throw new IllegalArgumentException( "Couldn't insert Form" );
216 		}
217     }
218 
219 	public static XControlShape insertControlShape( XComponent oDoc, int height,
220                                         int width, int x, int y, String kind ) {
221 
222         XControlShape aShape = createControlShape(oDoc,height,width,x,y,kind);
223         XDrawPage oDP = DrawTools.getDrawPage(oDoc,0);
224         DrawTools.getShapes(oDP).add(aShape);
225         return aShape;
226     }
227 
228     public static XLoadable bindForm( XTextDocument aDoc ) {
229         XLoadable formLoader = null;
230 
231         try {
232             Object aForm = FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0);
233             XForm the_form = null;
234             try {
235                 the_form = (XForm) AnyConverter.toObject(new Type(XForm.class), aForm);
236             } catch (com.sun.star.lang.IllegalArgumentException iae) {
237                 System.out.println("### Couldn't convert Any");
238             }
239             XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
240             formProps.setPropertyValue("DataSourceName","Bibliography");
241             formProps.setPropertyValue("Command","biblio");
242             formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
243             formLoader = (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
244         }
245         catch (Exception ex) {
246             System.out.println("Exception: "+ex);
247             ex.printStackTrace(System.out);
248         }
249 
250         return formLoader;
251     }
252 
253 	/**
254 	* Binds <code>'Standard'</code> form of <code>aDoc</code> Writer document
255 	* to the <code>tableName</code> table of <code>sourceName</code>
256 	* Data Source.
257 	* @param aDoc Writer document where DB controls are added.
258 	* @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
259 	* @param tableName The name of the table to which controls are bound.
260 	* @return <code>com.sun.star.form.component.DatabaseForm</code> service
261 	* implementation which is the bound form inside the document.
262 	*/
263     public static XLoadable bindForm( XTextDocument aDoc, String sourceName, String tableName )
264     	throws com.sun.star.uno.Exception {
265 
266         XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
267             FormTools.getIndexedForms(WriterTools.getDrawPage(aDoc)).getByIndex(0));
268         XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
269         formProps.setPropertyValue("DataSourceName",sourceName);
270         formProps.setPropertyValue("Command",tableName);
271         formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
272 
273         return (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
274     }
275 
276     public static XLoadable bindForm( XTextDocument aDoc, String formName ) {
277         XLoadable formLoader = null;
278 
279         try {
280             XForm the_form = (XForm) FormTools.getForms(WriterTools.getDrawPage(aDoc)).getByName(formName);
281             XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
282             formProps.setPropertyValue("DataSourceName","Bibliography");
283             formProps.setPropertyValue("Command","biblio");
284             formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
285             formLoader = (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
286         }
287         catch (Exception ex) {
288             System.out.println("Exception: "+ex);
289             ex.printStackTrace(System.out);
290         }
291 
292         return formLoader;
293     }
294 
295 	/**
296 	* Binds the form with the name specified of <code>aDoc</code> Writer document
297 	* to the <code>tableName</code> table of <code>sourceName</code>
298 	* Data Source.
299 	* @param aDoc Writer document where DB controls are added.
300 	* @param formName The name of the form to be bound.
301 	* @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
302 	* @param tableName The name of the table to which controls are bound.
303 	* @return <code>com.sun.star.form.component.DatabaseForm</code> service
304 	* implementation which is the bound form inside the document.
305 	*/
306     public static XLoadable bindForm( XTextDocument aDoc, String formName, String sourceName,
307     	String tableName) throws com.sun.star.uno.Exception {
308 
309         XForm the_form = (XForm) AnyConverter.toObject(new Type(XForm.class),
310             FormTools.getForms(WriterTools.getDrawPage(aDoc)).getByName(formName));
311         XPropertySet formProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, the_form);
312         formProps.setPropertyValue("DataSourceName",sourceName);
313         formProps.setPropertyValue("Command",tableName);
314         formProps.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.TABLE));
315 
316         return (XLoadable) UnoRuntime.queryInterface(XLoadable.class, the_form);
317     }
318 
319     public static void switchDesignOf(XMultiServiceFactory xMSF, XTextDocument aDoc) {
320     try {
321         com.sun.star.frame.XController aController = aDoc.getCurrentController();
322         com.sun.star.frame.XFrame aFrame = aController.getFrame();
323         com.sun.star.frame.XDispatchProvider aDispProv = (com.sun.star.frame.XDispatchProvider)
324                 UnoRuntime.queryInterface(com.sun.star.frame.XDispatchProvider.class,aFrame);
325         com.sun.star.util.URL aURL = new com.sun.star.util.URL();
326         aURL.Complete = ".uno:SwitchControlDesignMode";
327 
328         Object instance = xMSF.createInstance("com.sun.star.util.URLTransformer");
329         com.sun.star.util.XURLTransformer atrans =
330                 (com.sun.star.util.XURLTransformer)UnoRuntime.queryInterface(
331                                     com.sun.star.util.XURLTransformer.class,instance);
332         com.sun.star.util.URL[] aURLA = new com.sun.star.util.URL[1];
333         aURLA[0] = aURL;
334         atrans.parseStrict(aURLA);
335         aURL = aURLA[0];
336 
337         com.sun.star.frame.XDispatch aDisp = (com.sun.star.frame.XDispatch)aDispProv.queryDispatch(aURL, "",
338         	                    com.sun.star.frame.FrameSearchFlag.SELF |
339                                     com.sun.star.frame.FrameSearchFlag.CHILDREN);
340 
341         com.sun.star.beans.PropertyValue[] noArgs = new com.sun.star.beans.PropertyValue[0];
342         aDisp.dispatch(aURL, noArgs);
343         } catch (Exception e) {
344             System.out.println("******* Mist");
345             e.printStackTrace();
346             }
347     }
348 
349 }
350