Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / FLTools.java
blobcef9e4e1f32d0ea2c3d056da1fc7789c766f85b1
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com.sun.star.uno.*;
36 import com.sun.star.lang.*;
37 import com.sun.star.beans.*;
38 import com.sun.star.container.*;
39 import com.sun.star.awt.*;
40 import com.sun.star.form.*;
43 /** provides global helpers
45 public class FLTools
51 /* ------------------------------------------------------------------ */
52 /** returns the name of the given form component
54 private static String getName( Object aFormComponent )
56 XNamed xNamed = UnoRuntime.queryInterface( XNamed.class,
57 aFormComponent );
58 String sName = "";
59 if ( null != xNamed )
60 sName = xNamed.getName();
61 return sName;
64 /* ------------------------------------------------------------------ */
65 /** returns the label of the given form component
67 public static String getLabel( Object aFormComponent ) throws com.sun.star.uno.Exception
69 String sLabel = "";
71 XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
72 XPropertySetInfo xPSI = ( null != xProps ) ? xProps.getPropertySetInfo() : null;
73 if ( null == xPSI )
74 { // no property set or no property set info
75 // can't do anything except falling back to the name
76 return getName( aFormComponent );
79 // first check if the component has a LabelControl
80 if ( xPSI.hasPropertyByName( "LabelControl" ) )
81 sLabel = getLabel( xProps.getPropertyValue( "LabelControl" ) );
83 // no LabelControl or no label at the LabelControl
84 if ( 0 == sLabel.length() )
86 // a "Label" property?
87 if ( xPSI.hasPropertyByName( "Label" ) )
88 sLabel = (String)xProps.getPropertyValue( "Label" );
90 if ( 0 == sLabel.length() )
91 { // no Label property or no label set
92 // -> fallback to the component name
93 sLabel = getName( aFormComponent );
97 return sLabel;
102 /* ------------------------------------------------------------------ */
103 /** retrieves the parent of the given object
105 private static <T> T getParent( Object aComponent, Class<T> aInterfaceClass )
107 XChild xAsChild = UnoRuntime.queryInterface( XChild.class, aComponent );
109 return UnoRuntime.queryInterface( aInterfaceClass, xAsChild.getParent() );
112 /* ------------------------------------------------------------------ */
113 /** retrieves the parent of the given object
115 static XPropertySet getParent( Object aComponent )
117 return getParent( aComponent, XPropertySet.class );
120 /* ------------------------------------------------------------------ */
121 /** disposes the component given
123 static public void disposeComponent( Object xComp ) throws java.lang.RuntimeException
125 XComponent xComponent = UnoRuntime.queryInterface( XComponent.class,
126 xComp );
127 if ( null != xComponent )
128 xComponent.dispose();
131 /* ------------------------------------------------------------------ */
132 /** get's the XControlModel for a control
134 static public <T> T getModel( Object aControl, Class<T> aInterfaceClass )
136 XControl xControl = UnoRuntime.queryInterface(
137 XControl.class, aControl );
138 XControlModel xModel = null;
139 if ( null != xControl )
140 xModel = xControl.getModel();
142 return UnoRuntime.queryInterface( aInterfaceClass, xModel );
145 /* ------------------------------------------------------------------ */
146 /** retrieves the type of a form component.
147 <p>Speaking strictly, the function recognizes more than form components. Especially,
148 it survives a null argument. which means it can be safely applied to the a top-level
149 forms container; and it is able to classify grid columns (which are no form components)
150 as well.</p>
152 static public String classifyFormComponentType( XPropertySet xComponent ) throws com.sun.star.uno.Exception
154 String sType = "<unknown component>";
156 XServiceInfo xSI = UNO.queryServiceInfo( xComponent );
158 XPropertySetInfo xPSI = null;
159 if ( null != xComponent )
160 xPSI = xComponent.getPropertySetInfo();
162 if ( ( null != xPSI ) && xPSI.hasPropertyByName( "ClassId" ) )
164 // get the ClassId property
165 XPropertySet xCompProps = UNO.queryPropertySet( xComponent );
167 Short nClassId = (Short)xCompProps.getPropertyValue( "ClassId" );
168 switch ( nClassId.intValue() )
170 case FormComponentType.COMMANDBUTTON: sType = "Command button"; break;
171 case FormComponentType.RADIOBUTTON : sType = "Radio button"; break;
172 case FormComponentType.IMAGEBUTTON : sType = "Image button"; break;
173 case FormComponentType.CHECKBOX : sType = "Check Box"; break;
174 case FormComponentType.LISTBOX : sType = "List Box"; break;
175 case FormComponentType.COMBOBOX : sType = "Combo Box"; break;
176 case FormComponentType.GROUPBOX : sType = "Group Box"; break;
177 case FormComponentType.FIXEDTEXT : sType = "Fixed Text"; break;
178 case FormComponentType.GRIDCONTROL : sType = "Grid Control"; break;
179 case FormComponentType.FILECONTROL : sType = "File Control"; break;
180 case FormComponentType.HIDDENCONTROL: sType = "Hidden Control"; break;
181 case FormComponentType.IMAGECONTROL : sType = "Image Control"; break;
182 case FormComponentType.DATEFIELD : sType = "Date Field"; break;
183 case FormComponentType.TIMEFIELD : sType = "Time Field"; break;
184 case FormComponentType.NUMERICFIELD : sType = "Numeric Field"; break;
185 case FormComponentType.CURRENCYFIELD: sType = "Currency Field"; break;
186 case FormComponentType.PATTERNFIELD : sType = "Pattern Field"; break;
188 case FormComponentType.TEXTFIELD :
189 // there are two known services with this class id: the usual text field,
190 // and the formatted field
191 sType = "Text Field";
192 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.FormattedField" ) )
194 sType = "Formatted Field";
196 break;
198 default:
199 break;
202 else
204 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.DataForm" ) )
206 sType = "Form";
210 return sType;