1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir import com.sun.star.awt.XControl; 28*cdf0e10cSrcweir import com.sun.star.awt.XControlModel; 29*cdf0e10cSrcweir import com.sun.star.awt.XWindow; 30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 31*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 32*cdf0e10cSrcweir import com.sun.star.container.XIndexContainer; 33*cdf0e10cSrcweir import com.sun.star.form.FormComponentType; 34*cdf0e10cSrcweir import com.sun.star.form.XForm; 35*cdf0e10cSrcweir import com.sun.star.form.XFormController; 36*cdf0e10cSrcweir import com.sun.star.frame.XController; 37*cdf0e10cSrcweir import com.sun.star.frame.XDispatch; 38*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider; 39*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 40*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 41*cdf0e10cSrcweir import com.sun.star.util.URL; 42*cdf0e10cSrcweir import com.sun.star.util.XURLTransformer; 43*cdf0e10cSrcweir import com.sun.star.view.XControlAccess; 44*cdf0e10cSrcweir import com.sun.star.view.XFormLayerAccess; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir /**************************************************************************/ 48*cdf0e10cSrcweir /** provides a small wrapper around a document view 49*cdf0e10cSrcweir */ 50*cdf0e10cSrcweir class DocumentViewHelper 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir private XMultiServiceFactory m_orb; 53*cdf0e10cSrcweir private XController m_controller; 54*cdf0e10cSrcweir private DocumentHelper m_document; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 57*cdf0e10cSrcweir final protected XController getController() 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir return m_controller; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 63*cdf0e10cSrcweir final protected DocumentHelper getDocument() 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir return m_document; 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 69*cdf0e10cSrcweir public DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller ) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir m_orb = orb; 72*cdf0e10cSrcweir m_document = document; 73*cdf0e10cSrcweir m_controller = controller; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 77*cdf0e10cSrcweir /** Quick access to a given interface of the view 78*cdf0e10cSrcweir @param aInterfaceClass 79*cdf0e10cSrcweir the class of the interface which shall be returned 80*cdf0e10cSrcweir */ 81*cdf0e10cSrcweir public Object get( Class aInterfaceClass ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir return UnoRuntime.queryInterface( aInterfaceClass, m_controller ); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 87*cdf0e10cSrcweir /** retrieves a dispatcher for the given URL, obtained at the current view of the document 88*cdf0e10cSrcweir @param aURL 89*cdf0e10cSrcweir a one-element array. The first element must contain a valid 90*cdf0e10cSrcweir <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly 91*cdf0e10cSrcweir parsed. 92*cdf0e10cSrcweir @return 93*cdf0e10cSrcweir the dispatcher for the URL in question 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir public XDispatch getDispatcher( URL[] aURL ) throws java.lang.Exception 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir XDispatch xReturn = null; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // go get the current view 100*cdf0e10cSrcweir XController xController = (XController)get( XController.class ); 101*cdf0e10cSrcweir // go get the dispatch provider of it's frame 102*cdf0e10cSrcweir XDispatchProvider xProvider = (XDispatchProvider)UnoRuntime.queryInterface( 103*cdf0e10cSrcweir XDispatchProvider.class, xController.getFrame() ); 104*cdf0e10cSrcweir if ( null != xProvider ) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir // need an URLTransformer 107*cdf0e10cSrcweir XURLTransformer xTransformer = (XURLTransformer)UnoRuntime.queryInterface( 108*cdf0e10cSrcweir XURLTransformer.class, m_orb.createInstance( "com.sun.star.util.URLTransformer" ) ); 109*cdf0e10cSrcweir xTransformer.parseStrict( aURL ); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir xReturn = xProvider.queryDispatch( aURL[0], new String( ), 0 ); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir return xReturn; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 117*cdf0e10cSrcweir /** retrieves a dispatcher for the given URL, obtained at the current view of the document 118*cdf0e10cSrcweir */ 119*cdf0e10cSrcweir public XDispatch getDispatcher( String sURL ) throws java.lang.Exception 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir URL[] aURL = new URL[] { new URL() }; 122*cdf0e10cSrcweir aURL[0].Complete = sURL; 123*cdf0e10cSrcweir return getDispatcher( aURL ); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 127*cdf0e10cSrcweir /* retrieves the form controller belonging to a given logical form 128*cdf0e10cSrcweir */ 129*cdf0e10cSrcweir public XFormController getFormController( Object _form ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir XFormLayerAccess formLayer = (XFormLayerAccess)get( XFormLayerAccess.class ); 132*cdf0e10cSrcweir return formLayer.getFormController( (XForm)UnoRuntime.queryInterface( XForm.class, _form ) ); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 136*cdf0e10cSrcweir /** retrieves a control within the current view of a document 137*cdf0e10cSrcweir @param xModel 138*cdf0e10cSrcweir specifies the control model whose control should be located 139*cdf0e10cSrcweir @return 140*cdf0e10cSrcweir the control tied to the model 141*cdf0e10cSrcweir */ 142*cdf0e10cSrcweir public XControl getFormControl( XControlModel xModel ) throws com.sun.star.uno.Exception 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir // the current view of the document 145*cdf0e10cSrcweir XControlAccess xCtrlAcc = (XControlAccess)get( XControlAccess.class ); 146*cdf0e10cSrcweir // delegate the task of looking for the control 147*cdf0e10cSrcweir return xCtrlAcc.getControl( xModel ); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 151*cdf0e10cSrcweir public XControl getFormControl( Object aModel ) throws com.sun.star.uno.Exception 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel ); 154*cdf0e10cSrcweir return getFormControl( xModel ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 158*cdf0e10cSrcweir public Object getFormControl( Object aModel, Class aInterfaceClass ) throws com.sun.star.uno.Exception 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel ); 161*cdf0e10cSrcweir return UnoRuntime.queryInterface( aInterfaceClass, getFormControl( xModel ) ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 165*cdf0e10cSrcweir /** toggles the design mode of the form layer of active view of our sample document 166*cdf0e10cSrcweir */ 167*cdf0e10cSrcweir protected void toggleFormDesignMode( ) throws java.lang.Exception 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir // get a dispatcher for the toggle URL 170*cdf0e10cSrcweir URL[] aToggleURL = new URL[] { new URL() }; 171*cdf0e10cSrcweir aToggleURL[0].Complete = new String( ".uno:SwitchControlDesignMode" ); 172*cdf0e10cSrcweir XDispatch xDispatcher = getDispatcher( aToggleURL ); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir // dispatch the URL - this will result in toggling the mode 175*cdf0e10cSrcweir PropertyValue[] aDummyArgs = new PropertyValue[] { }; 176*cdf0e10cSrcweir xDispatcher.dispatch( aToggleURL[0], aDummyArgs ); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 180*cdf0e10cSrcweir /** sets the focus to a specific control 181*cdf0e10cSrcweir @param xModel 182*cdf0e10cSrcweir a control model. The focus is set to that control which is part of our view 183*cdf0e10cSrcweir and associated with the given model. 184*cdf0e10cSrcweir */ 185*cdf0e10cSrcweir public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir // look for the control from the current view which belongs to the model 188*cdf0e10cSrcweir XControl xControl = getFormControl( xModel ); 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir // the focus can be set to an XWindow only 191*cdf0e10cSrcweir XWindow xControlWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, 192*cdf0e10cSrcweir xControl ); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir // grab the focus 195*cdf0e10cSrcweir xControlWindow.setFocus(); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 199*cdf0e10cSrcweir /** sets the focus to the first control 200*cdf0e10cSrcweir */ 201*cdf0e10cSrcweir protected void grabControlFocus( ) throws java.lang.Exception 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir // the forms container of our document 204*cdf0e10cSrcweir XIndexContainer xForms = UNO.queryIndexContainer( m_document.getFormComponentTreeRoot( ) ); 205*cdf0e10cSrcweir // the first form 206*cdf0e10cSrcweir XIndexContainer xForm = UNO.queryIndexContainer( xForms.getByIndex( 0 ) ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir // the first control model which is no FixedText (FixedText's can't have the focus) 209*cdf0e10cSrcweir for ( int i = 0; i<xForm.getCount(); ++i ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir XPropertySet xControlProps = UNO.queryPropertySet( xForm.getByIndex( i ) ); 212*cdf0e10cSrcweir if ( FormComponentType.FIXEDTEXT != ((Short)xControlProps.getPropertyValue( "ClassId" )).shortValue() ) 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir XControlModel xControlModel = (XControlModel)UnoRuntime.queryInterface( 215*cdf0e10cSrcweir XControlModel.class, xControlProps ); 216*cdf0e10cSrcweir // set the focus to this control 217*cdf0e10cSrcweir grabControlFocus( xControlModel ); 218*cdf0e10cSrcweir // outta here 219*cdf0e10cSrcweir break; 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir // Note that we simply took the first control model from the hierarchy. This does state nothing 224*cdf0e10cSrcweir // about the location of the respective control in the view. A control model is tied to a control 225*cdf0e10cSrcweir // shape, and the shapes are where the geometry information such as position and size is hung up. 226*cdf0e10cSrcweir // So you could easily have a document where the first control model is bound to a shape which 227*cdf0e10cSrcweir // has a greater ordinate than any other control model. 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir }; 230*cdf0e10cSrcweir 231