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