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