1*ae15d43aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22*ae15d43aSAndrew Rist 
23cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
26cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
27cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
28cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
29cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
30cdf0e10cSrcweir import com.sun.star.drawing.XControlShape;
31cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
32cdf0e10cSrcweir import com.sun.star.awt.Size;
33cdf0e10cSrcweir import com.sun.star.awt.Point;
34cdf0e10cSrcweir import com.sun.star.awt.XControlModel;
35cdf0e10cSrcweir import com.sun.star.text.TextContentAnchorType;
36cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir  *
40cdf0e10cSrcweir  * @author  fs@openoffice.org
41cdf0e10cSrcweir  */
42cdf0e10cSrcweir public class FormLayer
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     private DocumentHelper  m_document;
45cdf0e10cSrcweir     private int             m_insertPage;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
48cdf0e10cSrcweir     /** Creates a new instance of FormLayer */
FormLayer( DocumentHelper _document )49cdf0e10cSrcweir     public FormLayer( DocumentHelper _document )
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         m_document = _document;
52cdf0e10cSrcweir         m_insertPage = -1;
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
56cdf0e10cSrcweir     /** sets the page which is to be used for subsequent insertions of controls/shapes
57cdf0e10cSrcweir      */
setInsertPage( int page )58cdf0e10cSrcweir     void setInsertPage( int page )
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         m_insertPage = page;
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
64cdf0e10cSrcweir     /** retrieves the page which is to be used for subsequent insertions of controls/shapes
65cdf0e10cSrcweir      */
getInsertPage( )66cdf0e10cSrcweir     final int getInsertPage( )
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         return m_insertPage;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
72cdf0e10cSrcweir     /** creates a control in the document
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
75cdf0e10cSrcweir         it creates a control shape, together with a control model, and inserts them into the document model.
76cdf0e10cSrcweir         This will result in every view to this document creating a control described by the model-shape pair.
77cdf0e10cSrcweir         </p>
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         @param sFormComponentService
80cdf0e10cSrcweir             the service name of the form component to create, e.g. "TextField"
81cdf0e10cSrcweir         @param nXPos
82cdf0e10cSrcweir             the abscissa of the position of the newly inserted shape
83cdf0e10cSrcweir         @param nXPos
84cdf0e10cSrcweir             the ordinate of the position of the newly inserted shape
85cdf0e10cSrcweir         @param nWidth
86cdf0e10cSrcweir             the width of the newly inserted shape
87cdf0e10cSrcweir         @param nHeight
88cdf0e10cSrcweir             the height of the newly inserted shape
89cdf0e10cSrcweir         @param xParentForm
90cdf0e10cSrcweir             the form to use as parent for the newly create form component. May be null, in this case
91cdf0e10cSrcweir             a default parent is chosen by the implementation
92cdf0e10cSrcweir         @return
93cdf0e10cSrcweir             the property access to the control's model
94cdf0e10cSrcweir     */
createControlAndShape( String sFormComponentService, int nXPos, int nYPos, int nWidth, int nHeight, XIndexContainer xParentForm )95cdf0e10cSrcweir     protected XPropertySet createControlAndShape( String sFormComponentService, int nXPos,
96cdf0e10cSrcweir         int nYPos, int nWidth, int nHeight, XIndexContainer xParentForm ) throws java.lang.Exception
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         // let the document create a shape
99cdf0e10cSrcweir         XMultiServiceFactory xDocAsFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
100cdf0e10cSrcweir             XMultiServiceFactory.class, m_document.getDocument() );
101cdf0e10cSrcweir         XControlShape xShape = (XControlShape)UnoRuntime.queryInterface( XControlShape.class,
102cdf0e10cSrcweir             xDocAsFactory.createInstance( "com.sun.star.drawing.ControlShape" ) );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         // position and size of the shape
105cdf0e10cSrcweir         xShape.setSize( new Size( nWidth * 100, nHeight * 100 ) );
106cdf0e10cSrcweir         xShape.setPosition( new Point( nXPos * 100, nYPos * 100 ) );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // adjust the anchor so that the control is tied to the page
109cdf0e10cSrcweir         XPropertySet xShapeProps = UNO.queryPropertySet( xShape );
110cdf0e10cSrcweir         TextContentAnchorType eAnchorType = TextContentAnchorType.AT_PARAGRAPH;
111cdf0e10cSrcweir         xShapeProps.setPropertyValue( "AnchorType", eAnchorType );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         // create the form component (the model of a form control)
114cdf0e10cSrcweir         String sQualifiedComponentName = "com.sun.star.form.component." + sFormComponentService;
115cdf0e10cSrcweir         XControlModel xModel = (XControlModel)UnoRuntime.queryInterface( XControlModel.class,
116cdf0e10cSrcweir             m_document.getOrb().createInstance( sQualifiedComponentName ) );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         // insert the model into the form component hierarchy, if the caller gave us a location
119cdf0e10cSrcweir         if ( null != xParentForm )
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             xParentForm.insertByIndex( xParentForm.getCount(), xModel );
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         // knitt them
125cdf0e10cSrcweir         xShape.setControl( xModel );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         // add the shape to the shapes collection of the document
128cdf0e10cSrcweir         XDrawPage pageWhereToInsert = ( m_insertPage != -1 ) ? m_document.getDrawPage( m_insertPage ) : m_document.getMainDrawPage();
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         XShapes xDocShapes = (XShapes)UnoRuntime.queryInterface( XShapes.class, pageWhereToInsert );
131cdf0e10cSrcweir         xDocShapes.add( xShape );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // some initializations which are the same for all controls
134cdf0e10cSrcweir         XPropertySet xModelProps = UNO.queryPropertySet( xModel );
135cdf0e10cSrcweir         try
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             XPropertySetInfo xPSI = xModelProps.getPropertySetInfo();
138cdf0e10cSrcweir             if ( xPSI.hasPropertyByName( "Border" ) )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 if ( ((Short)xModelProps.getPropertyValue( "Border" )).shortValue() == com.sun.star.awt.VisualEffect.LOOK3D )
141cdf0e10cSrcweir                     xModelProps.setPropertyValue( "Border", new Short( com.sun.star.awt.VisualEffect.FLAT ) );
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir             if ( xPSI.hasPropertyByName( "VisualEffect" ) )
144cdf0e10cSrcweir                 xModelProps.setPropertyValue( "VisualEffect", new Short( com.sun.star.awt.VisualEffect.FLAT ) );
145cdf0e10cSrcweir             if ( m_document.classify() != DocumentType.CALC )
146cdf0e10cSrcweir                 if ( xPSI.hasPropertyByName( "BorderColor" ) )
147cdf0e10cSrcweir                     xModelProps.setPropertyValue( "BorderColor", new Integer( 0x00C0C0C0 ) );
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e )
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             System.err.println(e);
152cdf0e10cSrcweir             e.printStackTrace( System.err );
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir         return xModelProps;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
158cdf0e10cSrcweir     /** creates a control in the document
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
161cdf0e10cSrcweir         it creates a control shape, together with a control model, and inserts them into the document model.
162cdf0e10cSrcweir         This will result in every view to this document creating a control described by the model-shape pair.
163cdf0e10cSrcweir         </p>
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         @param sFormComponentService
166cdf0e10cSrcweir             the service name of the form component to create, e.g. "TextField"
167cdf0e10cSrcweir         @param nXPos
168cdf0e10cSrcweir             the abscissa of the position of the newly inserted shape
169cdf0e10cSrcweir         @param nXPos
170cdf0e10cSrcweir             the ordinate of the position of the newly inserted shape
171cdf0e10cSrcweir         @param nWidth
172cdf0e10cSrcweir             the width of the newly inserted shape
173cdf0e10cSrcweir         @param nHeight
174cdf0e10cSrcweir             the height of the newly inserted shape
175cdf0e10cSrcweir         @return
176cdf0e10cSrcweir             the property access to the control's model
177cdf0e10cSrcweir     */
createControlAndShape( String sFormComponentService, int nXPos, int nYPos, int nWidth, int nHeight )178cdf0e10cSrcweir     protected XPropertySet createControlAndShape( String sFormComponentService, int nXPos,
179cdf0e10cSrcweir         int nYPos, int nWidth, int nHeight ) throws java.lang.Exception
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         return createControlAndShape( sFormComponentService, nXPos, nYPos, nWidth, nHeight, null );
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
185cdf0e10cSrcweir     /** creates a line of controls, consisting of a label and a field for data input.
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         <p>In opposite to the second form of this method, here the height of the field,
188cdf0e10cSrcweir         as well as the abscissa of the label, are under the control of the caller.</p>
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         @param sControlType
191cdf0e10cSrcweir             specifies the type of the data input control
192cdf0e10cSrcweir         @param sFieldName
193cdf0e10cSrcweir             specifies the field name the text field should be bound to
194cdf0e10cSrcweir         @param sControlNamePostfix
195cdf0e10cSrcweir             specifies a postfix to append to the logical control names
196cdf0e10cSrcweir         @param nYPos
197cdf0e10cSrcweir             specifies the Y position of the line to start at
198cdf0e10cSrcweir         @param nHeight
199cdf0e10cSrcweir             the height of the field
200cdf0e10cSrcweir         @return
201cdf0e10cSrcweir             the control model of the created data input field
202cdf0e10cSrcweir     */
insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nXPos, int nYPos, int nHeight )203cdf0e10cSrcweir     protected XPropertySet insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nXPos, int nYPos, int nHeight )
204cdf0e10cSrcweir         throws java.lang.Exception
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         // insert the label control
207cdf0e10cSrcweir         XPropertySet xLabelModel = createControlAndShape( "FixedText", nXPos, nYPos, 25, 6 );
208cdf0e10cSrcweir         xLabelModel.setPropertyValue( "Label", sFieldName );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         // insert the text field control
211cdf0e10cSrcweir         XPropertySet xFieldModel = createControlAndShape( sControlType, nXPos + 26, nYPos, 40, nHeight );
212cdf0e10cSrcweir         xFieldModel.setPropertyValue( "DataField", sFieldName );
213cdf0e10cSrcweir         // knit it to it's label component
214cdf0e10cSrcweir         xFieldModel.setPropertyValue( "LabelControl", xLabelModel );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         // some names, so later on we can find them
217cdf0e10cSrcweir         xLabelModel.setPropertyValue( "Name", sFieldName + sControlNamePostfix + "_Label" );
218cdf0e10cSrcweir         xFieldModel.setPropertyValue( "Name", sFieldName + sControlNamePostfix );
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         return xFieldModel;
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
224cdf0e10cSrcweir     /** creates a line of controls, consisting of a label and a field for data input.
225cdf0e10cSrcweir 
226cdf0e10cSrcweir         @param sControlType
227cdf0e10cSrcweir             specifies the type of the data input control
228cdf0e10cSrcweir         @param sFieldName
229cdf0e10cSrcweir             specifies the field name the text field should be bound to
230cdf0e10cSrcweir         @param nYPos
231cdf0e10cSrcweir             specifies the Y position of the line to start at
232cdf0e10cSrcweir         @return
233cdf0e10cSrcweir             the control model of the created data input field
234cdf0e10cSrcweir     */
insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nYPos )235cdf0e10cSrcweir     protected XPropertySet insertControlLine( String sControlType, String sFieldName, String sControlNamePostfix, int nYPos )
236cdf0e10cSrcweir         throws java.lang.Exception
237cdf0e10cSrcweir     {
238cdf0e10cSrcweir         return insertControlLine( sControlType, sFieldName, sControlNamePostfix, 2, nYPos, 6 );
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
242cdf0e10cSrcweir     /** retrieves the radio button model with the given name and the given ref value
243cdf0e10cSrcweir      *  @param form
244cdf0e10cSrcweir      *      the parent form of the radio button model to find
245cdf0e10cSrcweir      *  @param name
246cdf0e10cSrcweir      *      the name of the radio button
247cdf0e10cSrcweir      *  @param refValue
248cdf0e10cSrcweir      *      the reference value of the radio button
249cdf0e10cSrcweir     */
getRadioModelByRefValue( XPropertySet form, String name, String refValue )250cdf0e10cSrcweir     public XPropertySet getRadioModelByRefValue( XPropertySet form, String name, String refValue ) throws com.sun.star.uno.Exception, java.lang.Exception
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir         XIndexAccess indexAccess = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class,
253cdf0e10cSrcweir             form );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir         for ( int i=0; i<indexAccess.getCount(); ++i )
256cdf0e10cSrcweir         {
257cdf0e10cSrcweir             XPropertySet control = UNO.queryPropertySet( indexAccess.getByIndex( i ) );
258cdf0e10cSrcweir 
259cdf0e10cSrcweir             if ( ((String)control.getPropertyValue( "Name" )).equals( name ) )
260cdf0e10cSrcweir                 if ( ((String)control.getPropertyValue( "RefValue" )).equals( refValue ) )
261cdf0e10cSrcweir                     return control;
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         return null;
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
267cdf0e10cSrcweir     /** retrieves the radio button model with the given name and the given tag
268cdf0e10cSrcweir      *  @param form
269cdf0e10cSrcweir      *      the parent form of the radio button model to find
270cdf0e10cSrcweir      *  @param name
271cdf0e10cSrcweir      *      the name of the radio button
272cdf0e10cSrcweir      *  @param refValue
273cdf0e10cSrcweir      *      the tag of the radio button
274cdf0e10cSrcweir     */
getRadioModelByTag( XPropertySet form, String name, String tag )275cdf0e10cSrcweir     public XPropertySet getRadioModelByTag( XPropertySet form, String name, String tag ) throws com.sun.star.uno.Exception, java.lang.Exception
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         XIndexAccess indexAccess = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class,
278cdf0e10cSrcweir             form );
279cdf0e10cSrcweir 
280cdf0e10cSrcweir         for ( int i=0; i<indexAccess.getCount(); ++i )
281cdf0e10cSrcweir         {
282cdf0e10cSrcweir             XPropertySet control = UNO.queryPropertySet( indexAccess.getByIndex( i ) );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir             if ( ((String)control.getPropertyValue( "Name" )).equals( name ) )
285cdf0e10cSrcweir                 if ( ((String)control.getPropertyValue( "Tag" )).equals( tag ) )
286cdf0e10cSrcweir                     return control;
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir         return null;
289cdf0e10cSrcweir     }
290cdf0e10cSrcweir }
291