Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / FormLayer.java
blob6b049a2e5324d3fa30f7c0176f20b9802b85c637
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.uno.UnoRuntime;
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.beans.XPropertySetInfo;
22 import com.sun.star.container.XIndexContainer;
23 import com.sun.star.container.XIndexAccess;
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.drawing.XControlShape;
26 import com.sun.star.drawing.XShapes;
27 import com.sun.star.awt.Size;
28 import com.sun.star.awt.Point;
29 import com.sun.star.awt.XControlModel;
30 import com.sun.star.text.TextContentAnchorType;
31 import com.sun.star.drawing.XDrawPage;
33 public class FormLayer
35 private DocumentHelper m_document;
36 private int m_insertPage;
38 /* ------------------------------------------------------------------ */
39 /** Creates a new instance of FormLayer */
40 public FormLayer( DocumentHelper _document )
42 m_document = _document;
43 m_insertPage = -1;
50 /* ------------------------------------------------------------------ */
51 /** creates a control in the document
53 <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
54 it creates a control shape, together with a control model, and inserts them into the document model.
55 This will result in every view to this document creating a control described by the model-shape pair.
56 </p>
58 @param sFormComponentService
59 the service name of the form component to create, e.g. "TextField"
60 @param nXPos
61 the abscissa of the position of the newly inserted shape
62 @param nXPos
63 the ordinate of the position of the newly inserted shape
64 @param nWidth
65 the width of the newly inserted shape
66 @param nHeight
67 the height of the newly inserted shape
68 @param xParentForm
69 the form to use as parent for the newly create form component. May be null, in this case
70 a default parent is chosen by the implementation
71 @return
72 the property access to the control's model
74 protected XPropertySet createControlAndShape( String sFormComponentService, int nXPos,
75 int nYPos, int nWidth, int nHeight, XIndexContainer xParentForm ) throws java.lang.Exception
77 // let the document create a shape
78 XMultiServiceFactory xDocAsFactory = UnoRuntime.queryInterface(
79 XMultiServiceFactory.class, m_document.getDocument() );
80 XControlShape xShape = UnoRuntime.queryInterface( XControlShape.class,
81 xDocAsFactory.createInstance( "com.sun.star.drawing.ControlShape" ) );
83 // position and size of the shape
84 xShape.setSize( new Size( nWidth * 100, nHeight * 100 ) );
85 xShape.setPosition( new Point( nXPos * 100, nYPos * 100 ) );
87 // adjust the anchor so that the control is tied to the page
88 XPropertySet xShapeProps = UNO.queryPropertySet( xShape );
89 TextContentAnchorType eAnchorType = TextContentAnchorType.AT_PARAGRAPH;
90 xShapeProps.setPropertyValue( "AnchorType", eAnchorType );
92 // create the form component (the model of a form control)
93 String sQualifiedComponentName = "com.sun.star.form.component." + sFormComponentService;
94 XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class,
95 m_document.getOrb().createInstance( sQualifiedComponentName ) );
97 // insert the model into the form component hierarchy, if the caller gave us a location
98 if ( null != xParentForm )
100 xParentForm.insertByIndex( xParentForm.getCount(), xModel );
103 // knitt them
104 xShape.setControl( xModel );
106 // add the shape to the shapes collection of the document
107 XDrawPage pageWhereToInsert = ( m_insertPage != -1 ) ? m_document.getDrawPage( m_insertPage ) : m_document.getMainDrawPage();
109 XShapes xDocShapes = UnoRuntime.queryInterface( XShapes.class, pageWhereToInsert );
110 xDocShapes.add( xShape );
112 // some initializations which are the same for all controls
113 XPropertySet xModelProps = UNO.queryPropertySet( xModel );
116 XPropertySetInfo xPSI = xModelProps.getPropertySetInfo();
117 if ( xPSI.hasPropertyByName( "Border" ) )
119 if ( ((Short)xModelProps.getPropertyValue( "Border" )).shortValue() == com.sun.star.awt.VisualEffect.LOOK3D )
120 xModelProps.setPropertyValue( "Border", Short.valueOf( com.sun.star.awt.VisualEffect.FLAT ) );
122 if ( xPSI.hasPropertyByName( "VisualEffect" ) )
123 xModelProps.setPropertyValue( "VisualEffect", Short.valueOf( com.sun.star.awt.VisualEffect.FLAT ) );
124 if ( m_document.classify() != DocumentType.CALC )
125 if ( xPSI.hasPropertyByName( "BorderColor" ) )
126 xModelProps.setPropertyValue( "BorderColor", Integer.valueOf( 0x00C0C0C0 ) );
128 catch( com.sun.star.uno.Exception e )
130 System.err.println(e);
131 e.printStackTrace( System.err );
133 return xModelProps;
136 /* ------------------------------------------------------------------ */
137 /** creates a control in the document
139 <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
140 it creates a control shape, together with a control model, and inserts them into the document model.
141 This will result in every view to this document creating a control described by the model-shape pair.
142 </p>
144 @param sFormComponentService
145 the service name of the form component to create, e.g. "TextField"
146 @param nXPos
147 the abscissa of the position of the newly inserted shape
148 @param nXPos
149 the ordinate of the position of the newly inserted shape
150 @param nWidth
151 the width of the newly inserted shape
152 @param nHeight
153 the height of the newly inserted shape
154 @return
155 the property access to the control's model
157 protected XPropertySet createControlAndShape( String sFormComponentService, int nXPos,
158 int nYPos, int nWidth, int nHeight ) throws java.lang.Exception
160 return createControlAndShape( sFormComponentService, nXPos, nYPos, nWidth, nHeight, null );
163 /* ------------------------------------------------------------------ */
164 /** creates a line of controls, consisting of a label and a field for data input.
166 <p>In opposite to the second form of this method, here the height of the field,
167 as well as the abscissa of the label, are under the control of the caller.</p>
169 @param sControlType
170 specifies the type of the data input control
171 @param sFieldName
172 specifies the field name the text field should be bound to
173 @param sControlNamePostfix
174 specifies a postfix to append to the logical control names
175 @param nYPos
176 specifies the Y position of the line to start at
177 @param nHeight
178 the height of the field
179 @return
180 the control model of the created data input field
182 protected XPropertySet insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nXPos, int nYPos, int nHeight )
183 throws java.lang.Exception
185 // insert the label control
186 XPropertySet xLabelModel = createControlAndShape( "FixedText", nXPos, nYPos, 25, 6 );
187 xLabelModel.setPropertyValue( "Label", sFieldName );
189 // insert the text field control
190 XPropertySet xFieldModel = createControlAndShape( sControlType, nXPos + 26, nYPos, 40, nHeight );
191 xFieldModel.setPropertyValue( "DataField", sFieldName );
192 // knit it to it's label component
193 xFieldModel.setPropertyValue( "LabelControl", xLabelModel );
195 // some names, so later on we can find them
196 xLabelModel.setPropertyValue( "Name", sFieldName + sControlNamePostfix + "_Label" );
197 xFieldModel.setPropertyValue( "Name", sFieldName + sControlNamePostfix );
199 return xFieldModel;
202 /* ------------------------------------------------------------------ */
203 /** creates a line of controls, consisting of a label and a field for data input.
205 @param sControlType
206 specifies the type of the data input control
207 @param sFieldName
208 specifies the field name the text field should be bound to
209 @param nYPos
210 specifies the Y position of the line to start at
211 @return
212 the control model of the created data input field
214 protected XPropertySet insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nYPos )
215 throws java.lang.Exception
217 return insertControlLine( sControlType, sFieldName, sControlNamePostfix, 2, nYPos, 6 );
220 /* ------------------------------------------------------------------ */
221 /** retrieves the radio button model with the given name and the given ref value
222 * @param form
223 * the parent form of the radio button model to find
224 * @param name
225 * the name of the radio button
226 * @param refValue
227 * the reference value of the radio button
229 public XPropertySet getRadioModelByRefValue( XPropertySet form, String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
231 XIndexAccess indexAccess = UnoRuntime.queryInterface( XIndexAccess.class,
232 form );
234 for ( int i=0; i<indexAccess.getCount(); ++i )
236 XPropertySet control = UNO.queryPropertySet( indexAccess.getByIndex( i ) );
238 if ( ((String)control.getPropertyValue( "Name" )).equals( name ) )
239 if ( ((String)control.getPropertyValue( "RefValue" )).equals( refValue ) )
240 return control;
242 return null;
245 /* ------------------------------------------------------------------ */
246 /** retrieves the radio button model with the given name and the given tag
247 * @param form
248 * the parent form of the radio button model to find
249 * @param name
250 * the name of the radio button
251 * @param tag
252 * the tag of the radio button
254 public XPropertySet getRadioModelByTag( XPropertySet form, String name, String tag ) throws com.sun.star.uno.Exception, java.lang.Exception
256 XIndexAccess indexAccess = UnoRuntime.queryInterface( XIndexAccess.class,
257 form );
259 for ( int i=0; i<indexAccess.getCount(); ++i )
261 XPropertySet control = UNO.queryPropertySet( indexAccess.getByIndex( i ) );
263 if ( ((String)control.getPropertyValue( "Name" )).equals( name ) )
264 if ( ((String)control.getPropertyValue( "Tag" )).equals( tag ) )
265 return control;
267 return null;