Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DocumentViewHelper.java
blobbc1898d618cefa99e18cdd9da672f3c2f65a78ed
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.awt.XControl;
20 import com.sun.star.awt.XControlModel;
21 import com.sun.star.awt.XWindow;
22 import com.sun.star.beans.PropertyValue;
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.container.XIndexContainer;
25 import com.sun.star.form.FormComponentType;
26 import com.sun.star.form.XForm;
27 import com.sun.star.form.runtime.XFormController;
28 import com.sun.star.frame.XController;
29 import com.sun.star.frame.XDispatch;
30 import com.sun.star.frame.XDispatchProvider;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.URL;
34 import com.sun.star.util.XURLTransformer;
35 import com.sun.star.view.XControlAccess;
36 import com.sun.star.view.XFormLayerAccess;
39 /**************************************************************************/
40 /** provides a small wrapper around a document view
42 class DocumentViewHelper
44 private XMultiServiceFactory m_orb;
45 private XController m_controller;
46 private DocumentHelper m_document;
48 /* ------------------------------------------------------------------ */
49 final protected XController getController()
51 return m_controller;
54 /* ------------------------------------------------------------------ */
55 final protected DocumentHelper getDocument()
57 return m_document;
60 /* ------------------------------------------------------------------ */
61 public DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller )
63 m_orb = orb;
64 m_document = document;
65 m_controller = controller;
68 /* ------------------------------------------------------------------ */
69 /** Quick access to a given interface of the view
70 @param aInterfaceClass
71 the class of the interface which shall be returned
73 private <T> T get( Class<T> aInterfaceClass )
75 return UnoRuntime.queryInterface( aInterfaceClass, m_controller );
78 /* ------------------------------------------------------------------ */
79 /** retrieves a dispatcher for the given URL, obtained at the current view of the document
80 @param aURL
81 a one-element array. The first element must contain a valid
82 <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly
83 parsed.
84 @return
85 the dispatcher for the URL in question
87 private XDispatch getDispatcher( URL[] aURL ) throws java.lang.Exception
89 XDispatch xReturn = null;
91 // go get the current view
92 XController xController = get( XController.class );
93 // go get the dispatch provider of its frame
94 XDispatchProvider xProvider = UnoRuntime.queryInterface(
95 XDispatchProvider.class, xController.getFrame() );
96 if ( null != xProvider )
98 // need an URLTransformer
99 XURLTransformer xTransformer = UnoRuntime.queryInterface(
100 XURLTransformer.class, m_orb.createInstance( "com.sun.star.util.URLTransformer" ) );
101 xTransformer.parseStrict( aURL );
103 xReturn = xProvider.queryDispatch( aURL[0], "", 0 );
105 return xReturn;
110 /* ------------------------------------------------------------------ */
111 /* retrieves the form controller belonging to a given logical form
113 public XFormController getFormController( Object _form )
115 XFormLayerAccess formLayer = get( XFormLayerAccess.class );
116 return formLayer.getFormController( UnoRuntime.queryInterface( XForm.class, _form ) );
119 /* ------------------------------------------------------------------ */
120 /** retrieves a control within the current view of a document
121 @param xModel
122 specifies the control model whose control should be located
123 @return
124 the control tied to the model
126 private XControl getFormControl( XControlModel xModel ) throws com.sun.star.uno.Exception
128 // the current view of the document
129 XControlAccess xCtrlAcc = get( XControlAccess.class );
130 // delegate the task of looking for the control
131 return xCtrlAcc.getControl( xModel );
134 /* ------------------------------------------------------------------ */
135 public XControl getFormControl( Object aModel ) throws com.sun.star.uno.Exception
137 XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
138 return getFormControl( xModel );
141 /* ------------------------------------------------------------------ */
142 public <T> T getFormControl( Object aModel, Class<T> aInterfaceClass ) throws com.sun.star.uno.Exception
144 XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
145 return UnoRuntime.queryInterface( aInterfaceClass, getFormControl( xModel ) );
148 /* ------------------------------------------------------------------ */
149 /** toggles the design mode of the form layer of active view of our sample document
151 protected void toggleFormDesignMode( ) throws java.lang.Exception
153 // get a dispatcher for the toggle URL
154 URL[] aToggleURL = new URL[] { new URL() };
155 aToggleURL[0].Complete = ".uno:SwitchControlDesignMode";
156 XDispatch xDispatcher = getDispatcher( aToggleURL );
158 // dispatch the URL - this will result in toggling the mode
159 PropertyValue[] aDummyArgs = new PropertyValue[] { };
160 xDispatcher.dispatch( aToggleURL[0], aDummyArgs );
163 /* ------------------------------------------------------------------ */
164 /** sets the focus to a specific control
165 @param xModel
166 a control model. The focus is set to that control which is part of our view
167 and associated with the given model.
169 public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception
171 // look for the control from the current view which belongs to the model
172 XControl xControl = getFormControl( xModel );
174 // the focus can be set to an XWindow only
175 XWindow xControlWindow = UnoRuntime.queryInterface( XWindow.class,
176 xControl );
178 // grab the focus
179 xControlWindow.setFocus();
182 /* ------------------------------------------------------------------ */
183 /** sets the focus to the first control
185 protected void grabControlFocus( ) throws java.lang.Exception
187 // the forms container of our document
188 XIndexContainer xForms = UNO.queryIndexContainer( m_document.getFormComponentTreeRoot( ) );
189 // the first form
190 XIndexContainer xForm = UNO.queryIndexContainer( xForms.getByIndex( 0 ) );
192 // the first control model which is no FixedText (FixedText's can't have the focus)
193 for ( int i = 0; i<xForm.getCount(); ++i )
195 XPropertySet xControlProps = UNO.queryPropertySet( xForm.getByIndex( i ) );
196 if ( FormComponentType.FIXEDTEXT != ((Short)xControlProps.getPropertyValue( "ClassId" )).shortValue() )
198 XControlModel xControlModel = UnoRuntime.queryInterface(
199 XControlModel.class, xControlProps );
200 // set the focus to this control
201 grabControlFocus( xControlModel );
202 // outta here
203 break;
207 // Note that we simply took the first control model from the hierarchy. This does state nothing
208 // about the location of the respective control in the view. A control model is tied to a control
209 // shape, and the shapes are where the geometry information such as position and size is hung up.
210 // So you could easily have a document where the first control model is bound to a shape which
211 // has a greater ordinate than any other control model.
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */