merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Forms / FLTools.java
bloba275ca15caf7fdcfedede1329e82017dba19eb25
1 /*************************************************************************
3 * $RCSfile: FLTools.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:30:10 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.uno.*;
42 import com.sun.star.lang.*;
43 import com.sun.star.util.*;
44 import com.sun.star.beans.*;
45 import com.sun.star.container.*;
46 import com.sun.star.awt.*;
47 import com.sun.star.form.*;
50 /** provides global helpers
52 public class FLTools
54 /* ------------------------------------------------------------------ */
55 static void dump_Object( Object aObject )
57 XServiceInfo xSI = UNO.queryServiceInfo( aObject );
58 if ( null != xSI )
59 System.out.println( "dumping object with name \"" + xSI.getImplementationName() + "\"" );
60 else
61 System.out.println( "object has no service info!" );
64 /* ------------------------------------------------------------------ */
65 /** translates a string containing an URL into a complete
66 <type scope="com.sun.star.util">URL</type> object.
68 static public URL parseURL( String sURL, XComponentContext xCtx ) throws java.lang.Exception
70 URL[] aURL = new URL[] { new URL() };
71 aURL[0].Complete = sURL;
72 // need an URLTransformer
73 XURLTransformer xTransformer = (XURLTransformer)UnoRuntime.queryInterface(
74 XURLTransformer.class,
75 xCtx.getServiceManager().createInstanceWithContext(
76 "com.sun.star.util.URLTransformer", xCtx ) );
77 xTransformer.parseStrict( aURL );
79 return aURL[0];
82 /* ------------------------------------------------------------------ */
83 /** returns the name of the given form component
85 public static String getName( Object aFormComponent ) throws com.sun.star.uno.Exception
87 XNamed xNamed = (XNamed)UnoRuntime.queryInterface( XNamed.class,
88 aFormComponent );
89 String sName = "";
90 if ( null != xNamed )
91 sName = xNamed.getName();
92 return sName;
95 /* ------------------------------------------------------------------ */
96 /** returns the label of the given form component
98 public static String getLabel( Object aFormComponent ) throws com.sun.star.uno.Exception
100 String sLabel = "";
102 XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
103 XPropertySetInfo xPSI = ( null != xProps ) ? xProps.getPropertySetInfo() : null;
104 if ( null == xPSI )
105 { // no property set or no property set info
106 // can't do anything except falling back to the name
107 return getName( aFormComponent );
110 // first check if the component has a LabelControl
111 if ( xPSI.hasPropertyByName( "LabelControl" ) )
112 sLabel = getLabel( xProps.getPropertyValue( "LabelControl" ) );
114 // no LabelControl or no label at the LabelControl
115 if ( 0 == sLabel.length() )
117 // a "Label" property?
118 if ( xPSI.hasPropertyByName( "Label" ) )
119 sLabel = (String)xProps.getPropertyValue( "Label" );
121 if ( 0 == sLabel.length() )
122 { // no Label property or no label set
123 // -> fallback to the component name
124 sLabel = getName( aFormComponent );
128 return sLabel;
131 /* ------------------------------------------------------------------ */
132 /** retrieves the index of a form component within it's parent
134 static public int getIndexInParent( Object aContainer, Object aElement ) throws com.sun.star.uno.Exception
136 int nIndex = -1;
138 // norm the element
139 XInterface xElement = (XInterface)UnoRuntime.queryInterface(
140 XInterface.class, aElement );
142 // get the container
143 XIndexContainer xIndexCont = UNO.queryIndexContainer( aContainer );
144 if ( null != xIndexCont )
146 // loop through all children
147 int nCount = xIndexCont.getCount();
148 for ( int i = 0; i < nCount; ++i )
150 // compare with the element
151 XInterface xCurrent = (XInterface)UnoRuntime.queryInterface(
152 XInterface.class, xIndexCont.getByIndex( 0 ) );
153 if ( xCurrent.equals( xElement ) )
154 { // found
155 nIndex = i;
156 break;
161 // outta here
162 return nIndex;
165 /* ------------------------------------------------------------------ */
166 /** retrieves the parent of the given object
168 static Object getParent( Object aComponent, Class aInterfaceClass )
170 XChild xAsChild = (XChild)UnoRuntime.queryInterface( XChild.class, aComponent );
172 return UnoRuntime.queryInterface( aInterfaceClass, xAsChild.getParent() );
175 /* ------------------------------------------------------------------ */
176 /** retrieves the parent of the given object
178 static XPropertySet getParent( Object aComponent )
180 return (XPropertySet)getParent( aComponent, XPropertySet.class );
183 /* ------------------------------------------------------------------ */
184 /** disposes the component given
186 static public void disposeComponent( Object xComp ) throws java.lang.RuntimeException
188 XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class,
189 xComp );
190 if ( null != xComponent )
191 xComponent.dispose();
194 /* ------------------------------------------------------------------ */
195 /** get's the XControlModel for a control
197 static public Object getModel( Object aControl, Class aInterfaceClass )
199 XControl xControl = (XControl)UnoRuntime.queryInterface(
200 XControl.class, aControl );
201 XControlModel xModel = null;
202 if ( null != xControl )
203 xModel = xControl.getModel();
205 return UnoRuntime.queryInterface( aInterfaceClass, xModel );
208 /* ------------------------------------------------------------------ */
209 /** retrieves the type of a form component.
210 <p>Speaking strictly, the function recognizes more than form components. Especially,
211 it survives a null argument. which means it can be safely applied to the a top-level
212 forms container; and it is able to classify grid columns (which are no form components)
213 as well.</p>
215 static public String classifyFormComponentType( XPropertySet xComponent ) throws com.sun.star.uno.Exception
217 String sType = "<unknown component>";
219 XServiceInfo xSI = UNO.queryServiceInfo( xComponent );
221 XPropertySetInfo xPSI = null;
222 if ( null != xComponent )
223 xPSI = xComponent.getPropertySetInfo();
225 if ( ( null != xPSI ) && xPSI.hasPropertyByName( "ClassId" ) )
227 // get the ClassId property
228 XPropertySet xCompProps = UNO.queryPropertySet( xComponent );
230 Short nClassId = (Short)xCompProps.getPropertyValue( "ClassId" );
231 switch ( nClassId.intValue() )
233 case FormComponentType.COMMANDBUTTON: sType = "Command button"; break;
234 case FormComponentType.RADIOBUTTON : sType = "Radio button"; break;
235 case FormComponentType.IMAGEBUTTON : sType = "Image button"; break;
236 case FormComponentType.CHECKBOX : sType = "Check Box"; break;
237 case FormComponentType.LISTBOX : sType = "List Box"; break;
238 case FormComponentType.COMBOBOX : sType = "Combo Box"; break;
239 case FormComponentType.GROUPBOX : sType = "Group Box"; break;
240 case FormComponentType.FIXEDTEXT : sType = "Fixed Text"; break;
241 case FormComponentType.GRIDCONTROL : sType = "Grid Control"; break;
242 case FormComponentType.FILECONTROL : sType = "File Control"; break;
243 case FormComponentType.HIDDENCONTROL: sType = "Hidden Control"; break;
244 case FormComponentType.IMAGECONTROL : sType = "Image Control"; break;
245 case FormComponentType.DATEFIELD : sType = "Date Field"; break;
246 case FormComponentType.TIMEFIELD : sType = "Time Field"; break;
247 case FormComponentType.NUMERICFIELD : sType = "Numeric Field"; break;
248 case FormComponentType.CURRENCYFIELD: sType = "Currency Field"; break;
249 case FormComponentType.PATTERNFIELD : sType = "Pattern Field"; break;
251 case FormComponentType.TEXTFIELD :
252 // there are two known services with this class id: the usual text field,
253 // and the formatted field
254 sType = "Text Field";
255 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.FormattedField" ) )
257 sType = "Formatted Field";
259 break;
261 default:
262 break;
265 else
267 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.DataForm" ) )
269 sType = "Form";
273 return sType;