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
;
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.
58 @param sFormComponentService
59 the service name of the form component to create, e.g. "TextField"
61 the abscissa of the position of the newly inserted shape
63 the ordinate of the position of the newly inserted shape
65 the width of the newly inserted shape
67 the height of the newly inserted shape
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
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
);
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
);
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.
144 @param sFormComponentService
145 the service name of the form component to create, e.g. "TextField"
147 the abscissa of the position of the newly inserted shape
149 the ordinate of the position of the newly inserted shape
151 the width of the newly inserted shape
153 the height of the newly inserted shape
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>
170 specifies the type of the data input control
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
176 specifies the Y position of the line to start at
178 the height of the field
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
);
202 /* ------------------------------------------------------------------ */
203 /** creates a line of controls, consisting of a label and a field for data input.
206 specifies the type of the data input control
208 specifies the field name the text field should be bound to
210 specifies the Y position of the line to start at
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
223 * the parent form of the radio button model to find
225 * the name of the radio button
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,
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
) )
245 /* ------------------------------------------------------------------ */
246 /** retrieves the radio button model with the given name and the given tag
248 * the parent form of the radio button model to find
250 * the name of the radio button
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,
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
) )