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