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 package integration.forms; 24 25 /**************************************************************************/ 26 27 import com.sun.star.awt.XControl; 28 import com.sun.star.awt.XControlModel; 29 import com.sun.star.awt.XWindow; 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.container.XIndexContainer; 33 import com.sun.star.form.FormComponentType; 34 import com.sun.star.form.XForm; 35 import com.sun.star.form.runtime.XFormController; 36 import com.sun.star.frame.XController; 37 import com.sun.star.frame.XDispatch; 38 import com.sun.star.frame.XDispatchProvider; 39 import com.sun.star.lang.XMultiServiceFactory; 40 import com.sun.star.uno.UnoRuntime; 41 import com.sun.star.util.URL; 42 import com.sun.star.util.XURLTransformer; 43 import com.sun.star.view.XControlAccess; 44 import com.sun.star.view.XFormLayerAccess; 45 import org.openoffice.xforms.XMLDocument; 46 47 /**************************************************************************/ 48 /** provides a small wrapper around a document view 49 */ 50 public class DocumentViewHelper 51 { 52 private XMultiServiceFactory m_orb; 53 private XController m_controller; 54 private DocumentHelper m_document; 55 56 /* ------------------------------------------------------------------ */ getController()57 final protected XController getController() 58 { 59 return m_controller; 60 } 61 62 /* ------------------------------------------------------------------ */ getDocument()63 final protected DocumentHelper getDocument() 64 { 65 return m_document; 66 } 67 68 /* ------------------------------------------------------------------ */ DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller )69 public DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller ) 70 { 71 m_orb = orb; 72 m_document = document; 73 m_controller = controller; 74 } 75 76 /* ------------------------------------------------------------------ */ 77 /** Quick access to a given interface of the view 78 @param aInterfaceClass 79 the class of the interface which shall be returned 80 */ query( Class aInterfaceClass )81 public Object query( Class aInterfaceClass ) 82 { 83 return UnoRuntime.queryInterface( aInterfaceClass, m_controller ); 84 } 85 86 /* ------------------------------------------------------------------ */ 87 /** retrieves a dispatcher for the given URL, obtained at the current view of the document 88 @param aURL 89 a one-element array. The first element must contain a valid 90 <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly 91 parsed. 92 @return 93 the dispatcher for the URL in question 94 */ getDispatcher( URL[] aURL )95 public XDispatch getDispatcher( URL[] aURL ) throws java.lang.Exception 96 { 97 XDispatch xReturn = null; 98 99 // go get the current view 100 XController xController = (XController)query( XController.class ); 101 // go get the dispatch provider of it's frame 102 XDispatchProvider xProvider = (XDispatchProvider)UnoRuntime.queryInterface( 103 XDispatchProvider.class, xController.getFrame() ); 104 if ( null != xProvider ) 105 { 106 // need an URLTransformer 107 XURLTransformer xTransformer = (XURLTransformer)UnoRuntime.queryInterface( 108 XURLTransformer.class, m_orb.createInstance( "com.sun.star.util.URLTransformer" ) ); 109 xTransformer.parseStrict( aURL ); 110 111 xReturn = xProvider.queryDispatch( aURL[0], new String( ), 0 ); 112 } 113 return xReturn; 114 } 115 116 /* ------------------------------------------------------------------ */ 117 /** retrieves a dispatcher for the given URL, obtained at the current view of the document 118 */ getDispatcher( String url )119 public XDispatch getDispatcher( String url ) throws java.lang.Exception 120 { 121 URL[] aURL = new URL[] { new URL() }; 122 aURL[0].Complete = url; 123 return getDispatcher( aURL ); 124 } 125 126 /* ------------------------------------------------------------------ */ 127 /** dispatches the given URL into the view, if there's a dispatcher for it 128 129 @return 130 <TRUE/> if the URL was successfully dispatched 131 */ dispatch( String url )132 public boolean dispatch( String url ) throws java.lang.Exception 133 { 134 URL[] completeURL = new URL[] { new URL() }; 135 completeURL[0].Complete = url; 136 XDispatch dispatcher = getDispatcher( completeURL ); 137 if ( dispatcher == null ) 138 return false; 139 140 PropertyValue[] aDummyArgs = new PropertyValue[] { }; 141 dispatcher.dispatch( completeURL[0], aDummyArgs ); 142 return true; 143 } 144 145 /* ------------------------------------------------------------------ */ 146 /** retrieves a control within the current view of a document 147 @param xModel 148 specifies the control model whose control should be located 149 @return 150 the control tied to the model 151 */ getControl( XControlModel xModel )152 public XControl getControl( XControlModel xModel ) throws com.sun.star.uno.Exception 153 { 154 // the current view of the document 155 XControlAccess xCtrlAcc = (XControlAccess)query( XControlAccess.class ); 156 // delegate the task of looking for the control 157 return xCtrlAcc.getControl( xModel ); 158 } 159 160 /* ------------------------------------------------------------------ */ getControl( Object aModel )161 public XControl getControl( Object aModel ) throws com.sun.star.uno.Exception 162 { 163 XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel ); 164 return getControl( xModel ); 165 } 166 167 /* ------------------------------------------------------------------ */ getControl( Object aModel, Class aInterfaceClass )168 public Object getControl( Object aModel, Class aInterfaceClass ) throws com.sun.star.uno.Exception 169 { 170 XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel ); 171 return UnoRuntime.queryInterface( aInterfaceClass, getControl( xModel ) ); 172 } 173 174 /* ------------------------------------------------------------------ */ 175 /** retrieves the form controller for a given logical form 176 */ getFormController( XForm _form )177 public XFormController getFormController( XForm _form ) 178 { 179 XFormLayerAccess formLayerAccess = (XFormLayerAccess)query( XFormLayerAccess.class ); 180 return formLayerAccess.getFormController( _form ); 181 } 182 183 /* ------------------------------------------------------------------ */ 184 /** retrieves the form controller for a given logical form 185 */ getFormController( Object _form )186 public XFormController getFormController( Object _form ) 187 { 188 return getFormController( UnoRuntime.queryInterface( XForm.class, _form )); 189 } 190 191 /* ------------------------------------------------------------------ */ 192 /** toggles the design mode of the form layer of active view of our sample document 193 */ toggleFormDesignMode( )194 protected void toggleFormDesignMode( ) throws java.lang.Exception 195 { 196 if ( m_document instanceof XMLDocument ) 197 dispatch( ".uno:SwitchXFormsDesignMode" ); 198 else 199 dispatch( ".uno:SwitchControlDesignMode" ); 200 } 201 202 /* ------------------------------------------------------------------ */ 203 /** sets the focus to a specific control 204 @param xModel 205 a control model. The focus is set to that control which is part of our view 206 and associated with the given model. 207 */ grabControlFocus( Object xModel )208 public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception 209 { 210 // look for the control from the current view which belongs to the model 211 XControl xControl = getControl( xModel ); 212 213 // the focus can be set to an XWindow only 214 XWindow xControlWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, 215 xControl ); 216 217 // grab the focus 218 xControlWindow.setFocus(); 219 } 220 221 /* ------------------------------------------------------------------ */ 222 /** sets the focus to the first control 223 */ grabControlFocus( )224 protected void grabControlFocus( ) throws java.lang.Exception 225 { 226 // the forms container of our document 227 XIndexContainer xForms = dbfTools.queryIndexContainer( m_document.getFormComponentTreeRoot( ) ); 228 // the first form 229 XIndexContainer xForm = dbfTools.queryIndexContainer( xForms.getByIndex( 0 ) ); 230 231 // the first control model which is no FixedText (FixedText's can't have the focus) 232 for ( int i = 0; i<xForm.getCount(); ++i ) 233 { 234 XPropertySet xControlProps = dbfTools.queryPropertySet( xForm.getByIndex( i ) ); 235 if ( FormComponentType.FIXEDTEXT != ((Short)xControlProps.getPropertyValue( "ClassId" )).shortValue() ) 236 { 237 XControlModel xControlModel = (XControlModel)UnoRuntime.queryInterface( 238 XControlModel.class, xControlProps ); 239 // set the focus to this control 240 grabControlFocus( xControlModel ); 241 // outta here 242 break; 243 } 244 } 245 246 // Note that we simply took the first control model from the hierarchy. This does state nothing 247 // about the location of the respective control in the view. A control model is tied to a control 248 // shape, and the shapes are where the geometry information such as position and size is hung up. 249 // So you could easily have a document where the first control model is bound to a shape which 250 // has a greater ordinate than any other control model. 251 } 252 }; 253 254