Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DocumentViewHelper.java
blob148eecd2f91e11b46692f720dbff585d5ca4cb4c
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 import com.sun.star.awt.XControl;
19 import com.sun.star.awt.XControlModel;
20 import com.sun.star.awt.XWindow;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.container.XIndexContainer;
24 import com.sun.star.form.FormComponentType;
25 import com.sun.star.form.XForm;
26 import com.sun.star.form.runtime.XFormController;
27 import com.sun.star.frame.XController;
28 import com.sun.star.frame.XDispatch;
29 import com.sun.star.frame.XDispatchProvider;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.util.URL;
33 import com.sun.star.util.XURLTransformer;
34 import com.sun.star.view.XControlAccess;
35 import com.sun.star.view.XFormLayerAccess;
38 /**************************************************************************/
39 /** provides a small wrapper around a document view
41 class DocumentViewHelper
43 private XMultiServiceFactory m_orb;
44 private XController m_controller;
45 private DocumentHelper m_document;
47 /* ------------------------------------------------------------------ */
48 final protected XController getController()
50 return m_controller;
53 /* ------------------------------------------------------------------ */
54 final protected DocumentHelper getDocument()
56 return m_document;
59 /* ------------------------------------------------------------------ */
60 public DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller )
62 m_orb = orb;
63 m_document = document;
64 m_controller = controller;
67 /* ------------------------------------------------------------------ */
68 /** Quick access to a given interface of the view
69 @param aInterfaceClass
70 the class of the interface which shall be returned
72 private <T> T get( Class<T> aInterfaceClass )
74 return UnoRuntime.queryInterface( aInterfaceClass, m_controller );
77 /* ------------------------------------------------------------------ */
78 /** retrieves a dispatcher for the given URL, obtained at the current view of the document
79 @param aURL
80 a one-element array. The first element must contain a valid
81 <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly
82 parsed.
83 @return
84 the dispatcher for the URL in question
86 private XDispatch getDispatcher( URL[] aURL ) throws java.lang.Exception
88 XDispatch xReturn = null;
90 // go get the current view
91 XController xController = get( XController.class );
92 // go get the dispatch provider of it's frame
93 XDispatchProvider xProvider = UnoRuntime.queryInterface(
94 XDispatchProvider.class, xController.getFrame() );
95 if ( null != xProvider )
97 // need an URLTransformer
98 XURLTransformer xTransformer = UnoRuntime.queryInterface(
99 XURLTransformer.class, m_orb.createInstance( "com.sun.star.util.URLTransformer" ) );
100 xTransformer.parseStrict( aURL );
102 xReturn = xProvider.queryDispatch( aURL[0], "", 0 );
104 return xReturn;
109 /* ------------------------------------------------------------------ */
110 /* retrieves the form controller belonging to a given logical form
112 public XFormController getFormController( Object _form )
114 XFormLayerAccess formLayer = get( XFormLayerAccess.class );
115 return formLayer.getFormController( UnoRuntime.queryInterface( XForm.class, _form ) );
118 /* ------------------------------------------------------------------ */
119 /** retrieves a control within the current view of a document
120 @param xModel
121 specifies the control model whose control should be located
122 @return
123 the control tied to the model
125 private XControl getFormControl( XControlModel xModel ) throws com.sun.star.uno.Exception
127 // the current view of the document
128 XControlAccess xCtrlAcc = get( XControlAccess.class );
129 // delegate the task of looking for the control
130 return xCtrlAcc.getControl( xModel );
133 /* ------------------------------------------------------------------ */
134 public XControl getFormControl( Object aModel ) throws com.sun.star.uno.Exception
136 XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
137 return getFormControl( xModel );
140 /* ------------------------------------------------------------------ */
141 public <T> T getFormControl( Object aModel, Class<T> aInterfaceClass ) throws com.sun.star.uno.Exception
143 XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
144 return UnoRuntime.queryInterface( aInterfaceClass, getFormControl( xModel ) );
147 /* ------------------------------------------------------------------ */
148 /** toggles the design mode of the form layer of active view of our sample document
150 protected void toggleFormDesignMode( ) throws java.lang.Exception
152 // get a dispatcher for the toggle URL
153 URL[] aToggleURL = new URL[] { new URL() };
154 aToggleURL[0].Complete = ".uno:SwitchControlDesignMode";
155 XDispatch xDispatcher = getDispatcher( aToggleURL );
157 // dispatch the URL - this will result in toggling the mode
158 PropertyValue[] aDummyArgs = new PropertyValue[] { };
159 xDispatcher.dispatch( aToggleURL[0], aDummyArgs );
162 /* ------------------------------------------------------------------ */
163 /** sets the focus to a specific control
164 @param xModel
165 a control model. The focus is set to that control which is part of our view
166 and associated with the given model.
168 public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception
170 // look for the control from the current view which belongs to the model
171 XControl xControl = getFormControl( xModel );
173 // the focus can be set to an XWindow only
174 XWindow xControlWindow = UnoRuntime.queryInterface( XWindow.class,
175 xControl );
177 // grab the focus
178 xControlWindow.setFocus();
181 /* ------------------------------------------------------------------ */
182 /** sets the focus to the first control
184 protected void grabControlFocus( ) throws java.lang.Exception
186 // the forms container of our document
187 XIndexContainer xForms = UNO.queryIndexContainer( m_document.getFormComponentTreeRoot( ) );
188 // the first form
189 XIndexContainer xForm = UNO.queryIndexContainer( xForms.getByIndex( 0 ) );
191 // the first control model which is no FixedText (FixedText's can't have the focus)
192 for ( int i = 0; i<xForm.getCount(); ++i )
194 XPropertySet xControlProps = UNO.queryPropertySet( xForm.getByIndex( i ) );
195 if ( FormComponentType.FIXEDTEXT != ((Short)xControlProps.getPropertyValue( "ClassId" )).shortValue() )
197 XControlModel xControlModel = UnoRuntime.queryInterface(
198 XControlModel.class, xControlProps );
199 // set the focus to this control
200 grabControlFocus( xControlModel );
201 // outta here
202 break;
206 // Note that we simply took the first control model from the hierarchy. This does state nothing
207 // about the location of the respective control in the view. A control model is tied to a control
208 // shape, and the shapes are where the geometry information such as position and size is hung up.
209 // So you could easily have a document where the first control model is bound to a shape which
210 // has a greater ordinate than any other control model.