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 .
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
;
42 * contains helper methods forms
45 public class FormTools
{
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
) {
62 Size size
= new Size();
63 Point position
= new Point();
64 XControlShape oCShape
= null;
65 XControlModel aControl
= null;
68 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
);
81 oCShape
.setSize(size
);
82 oCShape
.setPosition(position
);
83 } catch ( com
.sun
.star
.uno
.Exception e
) {
84 // Some exception occurs.FAILED
85 System
.out
.println( "Couldn't create instance "+ e
);
88 oCShape
.setControl(aControl
);
91 } // finish createControlShape
93 public static XControlShape
createUnoControlShape( XComponent oDoc
, int height
,
94 int width
, int x
, int y
, String kind
, String defControl
) {
96 Size size
= new Size();
97 Point position
= new Point();
98 XControlShape oCShape
= null;
99 XControlModel aControl
= null;
102 XMultiServiceFactory oDocMSF
= UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
105 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
106 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component."+kind
);
107 XPropertySet model_props
= UnoRuntime
.queryInterface(XPropertySet
.class,aCon
);
108 model_props
.setPropertyValue("DefaultControl","com.sun.star.awt."+defControl
);
109 aControl
= UnoRuntime
.queryInterface( XControlModel
.class, aCon
);
110 oCShape
= UnoRuntime
.queryInterface( XControlShape
.class, oInt
);
111 size
.Height
= height
;
115 oCShape
.setSize(size
);
116 oCShape
.setPosition(position
);
119 } catch ( com
.sun
.star
.uno
.Exception e
) {
120 // Some exception occurs.FAILED
121 System
.out
.println( "Couldn't create instance "+ e
);
124 oCShape
.setControl(aControl
);
127 } // finish createControlShape
129 public static XControlShape
createControlShapeWithDefaultControl( XComponent oDoc
, int height
,
130 int width
, int x
, int y
, String kind
) {
132 Size size
= new Size();
133 Point position
= new Point();
134 XControlShape oCShape
= null;
135 XControlModel aControl
= null;
138 XMultiServiceFactory oDocMSF
= UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
141 Object oInt
= oDocMSF
.createInstance("com.sun.star.drawing.ControlShape");
142 Object aCon
= oDocMSF
.createInstance("com.sun.star.form.component."+kind
);
144 aControl
= UnoRuntime
.queryInterface( XControlModel
.class, aCon
);
145 oCShape
= UnoRuntime
.queryInterface( XControlShape
.class, oInt
);
146 size
.Height
= height
;
150 oCShape
.setSize(size
);
151 oCShape
.setPosition(position
);
154 } catch ( com
.sun
.star
.uno
.Exception e
) {
155 // Some exception occurs.FAILED
156 System
.out
.println( "Couldn't create instance "+ e
);
159 oCShape
.setControl(aControl
);
162 } // finish createControlShape
164 public static XInterface
createControl( XComponent oDoc
, String kind
) {
166 XInterface oControl
= null;
168 XMultiServiceFactory oDocMSF
= UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oDoc
);
171 oControl
= (XInterface
) oDocMSF
.createInstance(
172 "com.sun.star.form.component."+kind
);
173 } catch ( Exception e
) {
174 // Some exception occurs.FAILED
175 System
.out
.println( "Couldn't create instance "+ kind
+ ": "+ e
);
178 } // finish createControl
180 public static XNameContainer
getForms ( XDrawPage oDP
)
182 XFormsSupplier oFS
= UnoRuntime
.queryInterface(
183 XFormsSupplier
.class,oDP
);
184 return oFS
.getForms();
187 public static XIndexContainer
getIndexedForms ( XDrawPage oDP
)
189 XFormsSupplier oFS
= UnoRuntime
.queryInterface(
190 XFormsSupplier
.class,oDP
);
191 return UnoRuntime
.queryInterface( XIndexContainer
.class,
193 } //finish getIndexedForms
195 public static void insertForm ( XComponent aDoc
, XNameContainer Forms
,
198 XInterface oControl
= createControl(aDoc
, "Form");
199 XForm oForm
= UnoRuntime
.queryInterface(XForm
.class, oControl
);
200 Forms
.insertByName(aName
,oForm
);
201 } catch ( Exception e
) {
202 throw new IllegalArgumentException( "Couldn't insert Form" );
206 public static XControlShape
insertControlShape( XComponent oDoc
, int height
,
207 int width
, int x
, int y
, String kind
) {
209 XControlShape aShape
= createControlShape(oDoc
,height
,width
,x
,y
,kind
);
210 XDrawPage oDP
= DrawTools
.getDrawPage(oDoc
,0);
211 DrawTools
.getShapes(oDP
).add(aShape
);
215 public static XLoadable
bindForm( XTextDocument aDoc
) {
216 XLoadable formLoader
= null;
219 Object aForm
= FormTools
.getIndexedForms(WriterTools
.getDrawPage(aDoc
)).getByIndex(0);
220 XForm the_form
= null;
222 the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class), aForm
);
223 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
224 System
.out
.println("### Couldn't convert Any");
226 XPropertySet formProps
= UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
227 formProps
.setPropertyValue("DataSourceName","Bibliography");
228 formProps
.setPropertyValue("Command","biblio");
229 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
230 formLoader
= UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
232 catch (Exception ex
) {
233 System
.out
.println("Exception: "+ex
);
234 ex
.printStackTrace(System
.err
);
241 * Binds <code>'Standard'</code> form of <code>aDoc</code> Writer document
242 * to the <code>tableName</code> table of <code>sourceName</code>
244 * @param aDoc Writer document where DB controls are added.
245 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
246 * @param tableName The name of the table to which controls are bound.
247 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
248 * implementation which is the bound form inside the document.
250 public static XLoadable
bindForm( XTextDocument aDoc
, String sourceName
, String tableName
)
251 throws com
.sun
.star
.uno
.Exception
{
253 XForm the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
254 FormTools
.getIndexedForms(WriterTools
.getDrawPage(aDoc
)).getByIndex(0));
255 XPropertySet formProps
= UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
256 formProps
.setPropertyValue("DataSourceName",sourceName
);
257 formProps
.setPropertyValue("Command",tableName
);
258 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
260 return UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
263 public static XLoadable
bindForm( XTextDocument aDoc
, String formName
) {
264 XLoadable formLoader
= null;
267 XForm the_form
= (XForm
) FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName(formName
);
268 XPropertySet formProps
= UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
269 formProps
.setPropertyValue("DataSourceName","Bibliography");
270 formProps
.setPropertyValue("Command","biblio");
271 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
272 formLoader
= UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
274 catch (Exception ex
) {
275 System
.out
.println("Exception: "+ex
);
276 ex
.printStackTrace(System
.err
);
283 * Binds the form with the name specified of <code>aDoc</code> Writer document
284 * to the <code>tableName</code> table of <code>sourceName</code>
286 * @param aDoc Writer document where DB controls are added.
287 * @param formName The name of the form to be bound.
288 * @param sourceName The name of DataSource in the <code>DatabaseContext</code>.
289 * @param tableName The name of the table to which controls are bound.
290 * @return <code>com.sun.star.form.component.DatabaseForm</code> service
291 * implementation which is the bound form inside the document.
293 public static XLoadable
bindForm( XTextDocument aDoc
, String formName
, String sourceName
,
294 String tableName
) throws com
.sun
.star
.uno
.Exception
{
296 XForm the_form
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
297 FormTools
.getForms(WriterTools
.getDrawPage(aDoc
)).getByName(formName
));
298 XPropertySet formProps
= UnoRuntime
.queryInterface(XPropertySet
.class, the_form
);
299 formProps
.setPropertyValue("DataSourceName",sourceName
);
300 formProps
.setPropertyValue("Command",tableName
);
301 formProps
.setPropertyValue("CommandType",new Integer(com
.sun
.star
.sdb
.CommandType
.TABLE
));
303 return UnoRuntime
.queryInterface(XLoadable
.class, the_form
);
306 public static void switchDesignOf(XMultiServiceFactory xMSF
, XTextDocument aDoc
) {
308 com
.sun
.star
.frame
.XController aController
= aDoc
.getCurrentController();
309 com
.sun
.star
.frame
.XFrame aFrame
= aController
.getFrame();
310 com
.sun
.star
.frame
.XDispatchProvider aDispProv
= UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XDispatchProvider
.class,aFrame
);
311 com
.sun
.star
.util
.URL aURL
= new com
.sun
.star
.util
.URL();
312 aURL
.Complete
= ".uno:SwitchControlDesignMode";
314 Object instance
= xMSF
.createInstance("com.sun.star.util.URLTransformer");
315 com
.sun
.star
.util
.XURLTransformer atrans
=
316 UnoRuntime
.queryInterface(
317 com
.sun
.star
.util
.XURLTransformer
.class,instance
);
318 com
.sun
.star
.util
.URL
[] aURLA
= new com
.sun
.star
.util
.URL
[1];
320 atrans
.parseStrict(aURLA
);
323 com
.sun
.star
.frame
.XDispatch aDisp
= aDispProv
.queryDispatch(aURL
, "",
324 com
.sun
.star
.frame
.FrameSearchFlag
.SELF
|
325 com
.sun
.star
.frame
.FrameSearchFlag
.CHILDREN
);
327 com
.sun
.star
.beans
.PropertyValue
[] noArgs
= new com
.sun
.star
.beans
.PropertyValue
[0];
328 aDisp
.dispatch(aURL
, noArgs
);
329 } catch (Exception e
) {
330 System
.out
.println("******* Mist");