1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FormLayer.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 import com
.sun
.star
.uno
.UnoRuntime
;
32 import com
.sun
.star
.beans
.XPropertySet
;
33 import com
.sun
.star
.beans
.XPropertySetInfo
;
34 import com
.sun
.star
.container
.XIndexContainer
;
35 import com
.sun
.star
.container
.XIndexAccess
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.drawing
.XControlShape
;
38 import com
.sun
.star
.drawing
.XShapes
;
39 import com
.sun
.star
.awt
.Size
;
40 import com
.sun
.star
.awt
.Point
;
41 import com
.sun
.star
.awt
.XControlModel
;
42 import com
.sun
.star
.text
.TextContentAnchorType
;
43 import com
.sun
.star
.drawing
.XDrawPage
;
47 * @author fs@openoffice.org
49 public class FormLayer
51 private DocumentHelper m_document
;
52 private int m_insertPage
;
54 /* ------------------------------------------------------------------ */
55 /** Creates a new instance of FormLayer */
56 public FormLayer( DocumentHelper _document
)
58 m_document
= _document
;
62 /* ------------------------------------------------------------------ */
63 /** sets the page which is to be used for subsequent insertions of controls/shapes
65 void setInsertPage( int page
)
70 /* ------------------------------------------------------------------ */
71 /** retrieves the page which is to be used for subsequent insertions of controls/shapes
73 final int getInsertPage( )
78 /* ------------------------------------------------------------------ */
79 /** creates a control in the document
81 <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
82 it creates a control shape, together with a control model, and inserts them into the document model.
83 This will result in every view to this document creating a control described by the model-shape pair.
86 @param sFormComponentService
87 the service name of the form component to create, e.g. "TextField"
89 the abscissa of the position of the newly inserted shape
91 the ordinate of the position of the newly inserted shape
93 the width of the newly inserted shape
95 the height of the newly inserted shape
97 the form to use as parent for the newly create form component. May be null, in this case
98 a default parent is chosen by the implementation
100 the property access to the control's model
102 protected XPropertySet
createControlAndShape( String sFormComponentService
, int nXPos
,
103 int nYPos
, int nWidth
, int nHeight
, XIndexContainer xParentForm
) throws java
.lang
.Exception
105 // let the document create a shape
106 XMultiServiceFactory xDocAsFactory
= (XMultiServiceFactory
)UnoRuntime
.queryInterface(
107 XMultiServiceFactory
.class, m_document
.getDocument() );
108 XControlShape xShape
= (XControlShape
)UnoRuntime
.queryInterface( XControlShape
.class,
109 xDocAsFactory
.createInstance( "com.sun.star.drawing.ControlShape" ) );
111 // position and size of the shape
112 xShape
.setSize( new Size( nWidth
* 100, nHeight
* 100 ) );
113 xShape
.setPosition( new Point( nXPos
* 100, nYPos
* 100 ) );
115 // adjust the anchor so that the control is tied to the page
116 XPropertySet xShapeProps
= UNO
.queryPropertySet( xShape
);
117 TextContentAnchorType eAnchorType
= TextContentAnchorType
.AT_PARAGRAPH
;
118 xShapeProps
.setPropertyValue( "AnchorType", eAnchorType
);
120 // create the form component (the model of a form control)
121 String sQualifiedComponentName
= "com.sun.star.form.component." + sFormComponentService
;
122 XControlModel xModel
= (XControlModel
)UnoRuntime
.queryInterface( XControlModel
.class,
123 m_document
.getOrb().createInstance( sQualifiedComponentName
) );
125 // insert the model into the form component hierarchy, if the caller gave us a location
126 if ( null != xParentForm
)
128 xParentForm
.insertByIndex( xParentForm
.getCount(), xModel
);
132 xShape
.setControl( xModel
);
134 // add the shape to the shapes collection of the document
135 XDrawPage pageWhereToInsert
= ( m_insertPage
!= -1 ) ? m_document
.getDrawPage( m_insertPage
) : m_document
.getMainDrawPage();
137 XShapes xDocShapes
= (XShapes
)UnoRuntime
.queryInterface( XShapes
.class, pageWhereToInsert
);
138 xDocShapes
.add( xShape
);
140 // some initializations which are the same for all controls
141 XPropertySet xModelProps
= UNO
.queryPropertySet( xModel
);
144 XPropertySetInfo xPSI
= xModelProps
.getPropertySetInfo();
145 if ( xPSI
.hasPropertyByName( "Border" ) )
147 if ( ((Short
)xModelProps
.getPropertyValue( "Border" )).shortValue() == com
.sun
.star
.awt
.VisualEffect
.LOOK3D
)
148 xModelProps
.setPropertyValue( "Border", new Short( com
.sun
.star
.awt
.VisualEffect
.FLAT
) );
150 if ( xPSI
.hasPropertyByName( "VisualEffect" ) )
151 xModelProps
.setPropertyValue( "VisualEffect", new Short( com
.sun
.star
.awt
.VisualEffect
.FLAT
) );
152 if ( m_document
.classify() != DocumentType
.CALC
)
153 if ( xPSI
.hasPropertyByName( "BorderColor" ) )
154 xModelProps
.setPropertyValue( "BorderColor", new Integer( 0x00C0C0C0 ) );
156 catch( com
.sun
.star
.uno
.Exception e
)
158 System
.err
.println(e
);
159 e
.printStackTrace( System
.err
);
164 /* ------------------------------------------------------------------ */
165 /** creates a control in the document
167 <p>Note that <em>control<em> here is an incorrect terminology. What the method really does is
168 it creates a control shape, together with a control model, and inserts them into the document model.
169 This will result in every view to this document creating a control described by the model-shape pair.
172 @param sFormComponentService
173 the service name of the form component to create, e.g. "TextField"
175 the abscissa of the position of the newly inserted shape
177 the ordinate of the position of the newly inserted shape
179 the width of the newly inserted shape
181 the height of the newly inserted shape
183 the property access to the control's model
185 protected XPropertySet
createControlAndShape( String sFormComponentService
, int nXPos
,
186 int nYPos
, int nWidth
, int nHeight
) throws java
.lang
.Exception
188 return createControlAndShape( sFormComponentService
, nXPos
, nYPos
, nWidth
, nHeight
, null );
191 /* ------------------------------------------------------------------ */
192 /** creates a line of controls, consisting of a label and a field for data input.
194 <p>In opposite to the second form of this method, here the height of the field,
195 as well as the abscissa of the label, are under the control of the caller.</p>
198 specifies the type of the data input control
200 specifies the field name the text field should be bound to
201 @param sControlNamePostfix
202 specifies a postfix to append to the logical control names
204 specifies the Y position of the line to start at
206 the height of the field
208 the control model of the created data input field
210 protected XPropertySet
insertControlLine( String sControlType
, String sFieldName
, String sControlNamePostfix
, int nXPos
, int nYPos
, int nHeight
)
211 throws java
.lang
.Exception
213 // insert the label control
214 XPropertySet xLabelModel
= createControlAndShape( "FixedText", nXPos
, nYPos
, 25, 6 );
215 xLabelModel
.setPropertyValue( "Label", sFieldName
);
217 // insert the text field control
218 XPropertySet xFieldModel
= createControlAndShape( sControlType
, nXPos
+ 26, nYPos
, 40, nHeight
);
219 xFieldModel
.setPropertyValue( "DataField", sFieldName
);
220 // knit it to it's label component
221 xFieldModel
.setPropertyValue( "LabelControl", xLabelModel
);
223 // some names, so later on we can find them
224 xLabelModel
.setPropertyValue( "Name", sFieldName
+ sControlNamePostfix
+ "_Label" );
225 xFieldModel
.setPropertyValue( "Name", sFieldName
+ sControlNamePostfix
);
230 /* ------------------------------------------------------------------ */
231 /** creates a line of controls, consisting of a label and a field for data input.
234 specifies the type of the data input control
236 specifies the field name the text field should be bound to
238 specifies the Y position of the line to start at
240 the control model of the created data input field
242 protected XPropertySet
insertControlLine( String sControlType
, String sFieldName
, String sControlNamePostfix
, int nYPos
)
243 throws java
.lang
.Exception
245 return insertControlLine( sControlType
, sFieldName
, sControlNamePostfix
, 2, nYPos
, 6 );
248 /* ------------------------------------------------------------------ */
249 /** retrieves the radio button model with the given name and the given ref value
251 * the parent form of the radio button model to find
253 * the name of the radio button
255 * the reference value of the radio button
257 public XPropertySet
getRadioModelByRefValue( XPropertySet form
, String name
, String refValue
) throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
259 XIndexAccess indexAccess
= (XIndexAccess
)UnoRuntime
.queryInterface( XIndexAccess
.class,
262 for ( int i
=0; i
<indexAccess
.getCount(); ++i
)
264 XPropertySet control
= UNO
.queryPropertySet( indexAccess
.getByIndex( i
) );
266 if ( ((String
)control
.getPropertyValue( "Name" )).equals( name
) )
267 if ( ((String
)control
.getPropertyValue( "RefValue" )).equals( refValue
) )
273 /* ------------------------------------------------------------------ */
274 /** retrieves the radio button model with the given name and the given tag
276 * the parent form of the radio button model to find
278 * the name of the radio button
280 * the tag of the radio button
282 public XPropertySet
getRadioModelByTag( XPropertySet form
, String name
, String tag
) throws com
.sun
.star
.uno
.Exception
, java
.lang
.Exception
284 XIndexAccess indexAccess
= (XIndexAccess
)UnoRuntime
.queryInterface( XIndexAccess
.class,
287 for ( int i
=0; i
<indexAccess
.getCount(); ++i
)
289 XPropertySet control
= UNO
.queryPropertySet( indexAccess
.getByIndex( i
) );
291 if ( ((String
)control
.getPropertyValue( "Name" )).equals( name
) )
292 if ( ((String
)control
.getPropertyValue( "Tag" )).equals( tag
) )