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