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