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