1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
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.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 
46 
47 /**************************************************************************/
48 /** provides a small wrapper around a document view
49 */
50 class DocumentViewHelper
51 {
52     private     XMultiServiceFactory	m_orb;
53     private     XController             m_controller;
54     private     DocumentHelper		m_document;
55 
56     /* ------------------------------------------------------------------ */
57     final protected XController getController()
58     {
59         return m_controller;
60     }
61 
62     /* ------------------------------------------------------------------ */
63     final protected DocumentHelper getDocument()
64     {
65         return m_document;
66     }
67 
68     /* ------------------------------------------------------------------ */
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     */
81     public Object get( 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     */
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)get( 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     */
119     public XDispatch getDispatcher( String sURL ) throws java.lang.Exception
120     {
121         URL[] aURL = new URL[] { new URL() };
122         aURL[0].Complete = sURL;
123         return getDispatcher( aURL );
124     }
125 
126     /* ------------------------------------------------------------------ */
127     /* retrieves the form controller belonging to a given logical form
128      */
129     public XFormController getFormController( Object _form )
130     {
131         XFormLayerAccess formLayer = (XFormLayerAccess)get( XFormLayerAccess.class );
132         return formLayer.getFormController( (XForm)UnoRuntime.queryInterface( XForm.class, _form ) );
133     }
134 
135     /* ------------------------------------------------------------------ */
136     /** retrieves a control within the current view of a document
137         @param xModel
138             specifies the control model whose control should be located
139         @return
140             the control tied to the model
141     */
142     public XControl getFormControl( XControlModel xModel ) throws com.sun.star.uno.Exception
143     {
144         // the current view of the document
145         XControlAccess xCtrlAcc = (XControlAccess)get( XControlAccess.class );
146         // delegate the task of looking for the control
147         return xCtrlAcc.getControl( xModel );
148     }
149 
150     /* ------------------------------------------------------------------ */
151     public XControl getFormControl( Object aModel ) throws com.sun.star.uno.Exception
152     {
153         XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel );
154         return getFormControl( xModel );
155     }
156 
157     /* ------------------------------------------------------------------ */
158     public Object getFormControl( Object aModel, Class aInterfaceClass ) throws com.sun.star.uno.Exception
159     {
160         XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class, aModel );
161         return UnoRuntime.queryInterface( aInterfaceClass, getFormControl( xModel ) );
162     }
163 
164     /* ------------------------------------------------------------------ */
165     /** toggles the design mode of the form layer of active view of our sample document
166     */
167     protected void toggleFormDesignMode( ) throws java.lang.Exception
168     {
169         // get a dispatcher for the toggle URL
170         URL[] aToggleURL = new URL[] { new URL() };
171         aToggleURL[0].Complete = new String( ".uno:SwitchControlDesignMode" );
172         XDispatch xDispatcher = getDispatcher( aToggleURL );
173 
174         // dispatch the URL - this will result in toggling the mode
175         PropertyValue[] aDummyArgs = new PropertyValue[] { };
176         xDispatcher.dispatch( aToggleURL[0], aDummyArgs );
177     }
178 
179     /* ------------------------------------------------------------------ */
180     /** sets the focus to a specific control
181         @param xModel
182             a control model. The focus is set to that control which is part of our view
183             and associated with the given model.
184     */
185     public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception
186     {
187         // look for the control from the current view which belongs to the model
188         XControl xControl = getFormControl( xModel );
189 
190         // the focus can be set to an XWindow only
191         XWindow xControlWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class,
192             xControl );
193 
194         // grab the focus
195         xControlWindow.setFocus();
196     }
197 
198     /* ------------------------------------------------------------------ */
199     /** sets the focus to the first control
200     */
201     protected void grabControlFocus( ) throws java.lang.Exception
202     {
203         // the forms container of our document
204         XIndexContainer xForms = UNO.queryIndexContainer( m_document.getFormComponentTreeRoot( ) );
205         // the first form
206         XIndexContainer xForm = UNO.queryIndexContainer( xForms.getByIndex( 0 ) );
207 
208         // the first control model which is no FixedText (FixedText's can't have the focus)
209         for ( int i = 0; i<xForm.getCount(); ++i )
210         {
211             XPropertySet xControlProps = UNO.queryPropertySet( xForm.getByIndex( i ) );
212             if ( FormComponentType.FIXEDTEXT != ((Short)xControlProps.getPropertyValue( "ClassId" )).shortValue() )
213             {
214                 XControlModel xControlModel = (XControlModel)UnoRuntime.queryInterface(
215                     XControlModel.class, xControlProps );
216                 // set the focus to this control
217                 grabControlFocus( xControlModel );
218                 // outta here
219                 break;
220             }
221         }
222 
223         // Note that we simply took the first control model from the hierarchy. This does state nothing
224         // about the location of the respective control in the view. A control model is tied to a control
225         // shape, and the shapes are where the geometry information such as position and size is hung up.
226         // So you could easily have a document where the first control model is bound to a shape which
227         // has a greater ordinate than any other control model.
228     }
229 };
230 
231