1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FormTools.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 // access the implementations via names
34 import com
.sun
.star
.uno
.XInterface
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.lang
.XComponent
;
37 import com
.sun
.star
.drawing
.XControlShape
;
38 import com
.sun
.star
.drawing
.XDrawPage
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.awt
.Size
;
41 import com
.sun
.star
.awt
.Point
;
42 import com
.sun
.star
.awt
.XControlModel
;
43 import com
.sun
.star
.container
.XNameContainer
;
44 import com
.sun
.star
.form
.XFormsSupplier
;
45 import com
.sun
.star
.form
.XForm
;
46 import com
.sun
.star
.form
.XLoadable
;
47 import com
.sun
.star
.text
.XTextDocument
;
48 import com
.sun
.star
.beans
.XPropertySet
;
49 import com
.sun
.star
.uno
.AnyConverter
;
50 import com
.sun
.star
.uno
.Type
;
53 * contains helper methods forms
56 public class FormTools
{
60 * creates a XControlShape
62 * @param oDoc the document
63 * @param height the height of the shape
64 * @param width the width of the shape
65 * @param x the x-position of the shape
66 * @param y the y-position of the shape
67 * @param kind the kind of the shape
68 * @return the created XControlShape
70 public static XControlShape
createControlShape( XComponent oDoc
, int height
,
71 int width
, int x
, int y
, String kind
) {
73 Size size
= new Size();
74 Point position
= new Point();
75 XControlShape oCShape
= null;
76 XControlModel aControl
= null;
79 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
)
80 UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
83 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
84 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component."+kind
);
85 XPropertySet model_props
= (XPropertySet
)
86 UnoRuntime
.queryInterface(XPropertySet
.class,aCon
);
87 model_props
.setPropertyValue("DefaultControl","com.sun.star.form.control."+kind
);
88 aControl
= (XControlModel
) UnoRuntime
.queryInterface( XControlModel
.class, aCon
);
89 oCShape
= (XControlShape
) UnoRuntime
.queryInterface( XControlShape
.class, oInt
);
94 oCShape
.setSize(size
);
95 oCShape
.setPosition(position
);
96 } catch ( com
.sun
.star
.uno
.Exception e
) {
97 // Some exception occures.FAILED
98 System
.out
.println( "Couldn't create instance "+ e
);
101 oCShape
.setControl(aControl
);
104 } // finish createControlShape
106 public static XControlShape
createUnoControlShape( XComponent oDoc
, int height
,
107 int width
, int x
, int y
, String kind
, String defControl
) {
109 Size size
= new Size();
110 Point position
= new Point();
111 XControlShape oCShape
= null;
112 XControlModel aControl
= null;
115 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
118 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
119 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component."+kind
);
120 XPropertySet model_props
= (XPropertySet
)
121 UnoRuntime
.queryInterface(XPropertySet
.class,aCon
);
122 model_props
.setPropertyValue("DefaultControl","com.sun.star.awt."+defControl
);
123 aControl
= (XControlModel
) UnoRuntime
.queryInterface( XControlModel
.class, aCon
);
124 oCShape
= (XControlShape
) UnoRuntime
.queryInterface( XControlShape
.class, oInt
);
125 size
.Height
= height
;
129 oCShape
.setSize(size
);
130 oCShape
.setPosition(position
);
133 } catch ( com
.sun
.star
.uno
.Exception e
) {
134 // Some exception occures.FAILED
135 System
.out
.println( "Couldn't create instance "+ e
);
138 oCShape
.setControl(aControl
);
141 } // finish createControlShape
143 public static XControlShape
createControlShapeWithDefaultControl( XComponent oDoc
, int height
,
144 int width
, int x
, int y
, String kind
) {
146 Size size
= new Size();
147 Point position
= new Point();
148 XControlShape oCShape
= null;
149 XControlModel aControl
= null;
152 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
) UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
155 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
156 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component."+kind
);
158 aControl
= (XControlModel
) UnoRuntime
.queryInterface( XControlModel
.class, aCon
);
159 oCShape
= (XControlShape
) UnoRuntime
.queryInterface( XControlShape
.class, oInt
);
160 size
.Height
= height
;
164 oCShape
.setSize(size
);
165 oCShape
.setPosition(position
);
168 } catch ( com
.sun
.star
.uno
.Exception e
) {
169 // Some exception occures.FAILED
170 System
.out
.println( "Couldn't create instance "+ e
);
173 oCShape
.setControl(aControl
);
176 } // finish createControlShape
178 public static XInterface
createControl( XComponent oDoc
, String kind
) {
180 XInterface oControl
= null;
182 XMultiServiceFactory oDocMSF
= (XMultiServiceFactory
)
183 UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
186 oControl
= (XInterface
) oDocMSF
.createInstance(
187 "com.sun.star.form.component."+kind
);
188 } catch ( Exception e
) {
189 // Some exception occures.FAILED
190 System
.out
.println( "Couldn't create instance "+ kind
+ ": "+ e
);
193 } // finish createControl
195 public static XNameContainer
getForms ( XDrawPage oDP
) {
196 XFormsSupplier oFS
= (XFormsSupplier
) UnoRuntime
.queryInterface(
197 XFormsSupplier
.class,oDP
);
198 return oFS
.getForms();
201 public static void insertForm ( XComponent aDoc
, XNameContainer Forms
,
204 XInterface oControl
= createControl(aDoc
, "Form");
205 XForm oForm
= (XForm
) UnoRuntime
.queryInterface(XForm
.class, oControl
);
206 Forms
.insertByName(aName
,oForm
);
207 } catch ( Exception e
) {
208 throw new IllegalArgumentException( "Couldn't insert Form" );
212 public static XControlShape
insertControlShape( XComponent oDoc
, int height
,
213 int width
, int x
, int y
, String kind
) {
215 XControlShape aShape
= createControlShape(oDoc
,height
,width
,x
,y
,kind
);
216 XDrawPage oDP
= DrawTools
.getDrawPage(oDoc
,0);
217 DrawTools
.getShapes(oDP
).add(aShape
);
221 public static XLoadable
bindForm( XTextDocument aDoc
) {
222 XLoadable formLoader
= null;
225 Object aForm
= FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName("Standard");
226 XForm the_form
= null;
228 the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class), aForm
);
229 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
230 System
.out
.println("### Couldn't convert Any");
232 XPropertySet formProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
233 formProps
.setPropertyValue("DataSourceName","Bibliography");
234 formProps
.setPropertyValue("Command","biblio");
235 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
236 formLoader
= (XLoadable
) UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
238 catch (Exception ex
) {
239 System
.out
.println("Exception: "+ex
);
240 ex
.printStackTrace(System
.out
);
247 * Binds <code>'Standard'</code> form of <code>aDoc</code> Writer document
248 * to the <code>tableName</code> table of <code>sourceName</code>
250 * @param aDoc Writer document where DB controls are added.
251 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
252 * @param tableName The name of the table to which controls are bound.
253 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
254 * implementation which is the bound form inside the document.
256 public static XLoadable
bindForm( XTextDocument aDoc
, String sourceName
, String tableName
)
257 throws com
.sun
.star
.uno
.Exception
{
259 XForm the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
260 FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName("Standard"));
261 XPropertySet formProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
262 formProps
.setPropertyValue("DataSourceName",sourceName
);
263 formProps
.setPropertyValue("Command",tableName
);
264 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
266 return (XLoadable
) UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
269 public static XLoadable
bindForm( XTextDocument aDoc
, String formName
) {
270 XLoadable formLoader
= null;
273 XForm the_form
= (XForm
) FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName(formName
);
274 XPropertySet formProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
275 formProps
.setPropertyValue("DataSourceName","Bibliography");
276 formProps
.setPropertyValue("Command","biblio");
277 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
278 formLoader
= (XLoadable
) UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
280 catch (Exception ex
) {
281 System
.out
.println("Exception: "+ex
);
282 ex
.printStackTrace(System
.out
);
289 * Binds the form with the name specified of <code>aDoc</code> Writer document
290 * to the <code>tableName</code> table of <code>sourceName</code>
292 * @param aDoc Writer document where DB controls are added.
293 * @param formName The name of the form to be bound.
294 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
295 * @param tableName The name of the table to which controls are bound.
296 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
297 * implementation which is the bound form inside the document.
299 public static XLoadable
bindForm( XTextDocument aDoc
, String formName
, String sourceName
,
300 String tableName
) throws com
.sun
.star
.uno
.Exception
{
302 XForm the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
303 FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName(formName
));
304 XPropertySet formProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
305 formProps
.setPropertyValue("DataSourceName",sourceName
);
306 formProps
.setPropertyValue("Command",tableName
);
307 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
309 return (XLoadable
) UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
312 public static void switchDesignOf(XMultiServiceFactory xMSF
, XTextDocument aDoc
) {
314 com
.sun
.star
.frame
.XController aController
= aDoc
.getCurrentController();
315 com
.sun
.star
.frame
.XFrame aFrame
= aController
.getFrame();
316 com
.sun
.star
.frame
.XDispatchProvider aDispProv
= (com
.sun
.star
.frame
.XDispatchProvider
)
317 UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XDispatchProvider
.class,aFrame
);
318 com
.sun
.star
.util
.URL aURL
= new com
.sun
.star
.util
.URL();
319 aURL
.Complete
= ".uno:SwitchControlDesignMode";
321 Object instance
= xMSF
.createInstance("com.sun.star.util.URLTransformer");
322 com
.sun
.star
.util
.XURLTransformer atrans
=
323 (com
.sun
.star
.util
.XURLTransformer
)UnoRuntime
.queryInterface(
324 com
.sun
.star
.util
.XURLTransformer
.class,instance
);
325 com
.sun
.star
.util
.URL
[] aURLA
= new com
.sun
.star
.util
.URL
[1];
327 atrans
.parseStrict(aURLA
);
330 com
.sun
.star
.frame
.XDispatch aDisp
= (com
.sun
.star
.frame
.XDispatch
)aDispProv
.queryDispatch(aURL
, "",
331 com
.sun
.star
.frame
.FrameSearchFlag
.SELF
|
332 com
.sun
.star
.frame
.FrameSearchFlag
.CHILDREN
);
334 com
.sun
.star
.beans
.PropertyValue
[] noArgs
= new com
.sun
.star
.beans
.PropertyValue
[0];
335 aDisp
.dispatch(aURL
, noArgs
);
336 } catch (Exception e
) {
337 System
.out
.println("******* Mist");