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