Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / FLTools.java
blobea1467b52adb77f7281d22cc36a351a58e7647d7
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 import com.sun.star.uno.*;
37 import com.sun.star.lang.*;
38 import com.sun.star.beans.*;
39 import com.sun.star.container.*;
40 import com.sun.star.awt.*;
41 import com.sun.star.form.*;
44 /** provides global helpers
46 public class FLTools
52 /* ------------------------------------------------------------------ */
53 /** returns the name of the given form component
55 private static String getName( Object aFormComponent )
57 XNamed xNamed = UnoRuntime.queryInterface( XNamed.class,
58 aFormComponent );
59 String sName = "";
60 if ( null != xNamed )
61 sName = xNamed.getName();
62 return sName;
65 /* ------------------------------------------------------------------ */
66 /** returns the label of the given form component
68 public static String getLabel( Object aFormComponent ) throws com.sun.star.uno.Exception
70 String sLabel = "";
72 XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
73 XPropertySetInfo xPSI = ( null != xProps ) ? xProps.getPropertySetInfo() : null;
74 if ( null == xPSI )
75 { // no property set or no property set info
76 // can't do anything except falling back to the name
77 return getName( aFormComponent );
80 // first check if the component has a LabelControl
81 if ( xPSI.hasPropertyByName( "LabelControl" ) )
82 sLabel = getLabel( xProps.getPropertyValue( "LabelControl" ) );
84 // no LabelControl or no label at the LabelControl
85 if ( 0 == sLabel.length() )
87 // a "Label" property?
88 if ( xPSI.hasPropertyByName( "Label" ) )
89 sLabel = (String)xProps.getPropertyValue( "Label" );
91 if ( 0 == sLabel.length() )
92 { // no Label property or no label set
93 // -> fallback to the component name
94 sLabel = getName( aFormComponent );
98 return sLabel;
103 /* ------------------------------------------------------------------ */
104 /** retrieves the parent of the given object
106 private static <T> T getParent( Object aComponent, Class<T> aInterfaceClass )
108 XChild xAsChild = UnoRuntime.queryInterface( XChild.class, aComponent );
110 return UnoRuntime.queryInterface( aInterfaceClass, xAsChild.getParent() );
113 /* ------------------------------------------------------------------ */
114 /** retrieves the parent of the given object
116 static XPropertySet getParent( Object aComponent )
118 return getParent( aComponent, XPropertySet.class );
121 /* ------------------------------------------------------------------ */
122 /** disposes the component given
124 public static void disposeComponent( Object xComp ) throws java.lang.RuntimeException
126 XComponent xComponent = UnoRuntime.queryInterface( XComponent.class,
127 xComp );
128 if ( null != xComponent )
129 xComponent.dispose();
132 /* ------------------------------------------------------------------ */
133 /** gets the XControlModel for a control
135 public static <T> T getModel( Object aControl, Class<T> aInterfaceClass )
137 XControl xControl = UnoRuntime.queryInterface(
138 XControl.class, aControl );
139 XControlModel xModel = null;
140 if ( null != xControl )
141 xModel = xControl.getModel();
143 return UnoRuntime.queryInterface( aInterfaceClass, xModel );
146 /* ------------------------------------------------------------------ */
147 /** retrieves the type of a form component.
148 <p>Speaking strictly, the function recognizes more than form components. Especially,
149 it survives a null argument. which means it can be safely applied to the a top-level
150 forms container; and it is able to classify grid columns (which are no form components)
151 as well.</p>
153 public static String classifyFormComponentType( XPropertySet xComponent ) throws com.sun.star.uno.Exception
155 String sType = "<unknown component>";
157 XServiceInfo xSI = UNO.queryServiceInfo( xComponent );
159 XPropertySetInfo xPSI = null;
160 if ( null != xComponent )
161 xPSI = xComponent.getPropertySetInfo();
163 if ( ( null != xPSI ) && xPSI.hasPropertyByName( "ClassId" ) )
165 // get the ClassId property
166 XPropertySet xCompProps = UNO.queryPropertySet( xComponent );
168 Short nClassId = (Short)xCompProps.getPropertyValue( "ClassId" );
169 switch ( nClassId.intValue() )
171 case FormComponentType.COMMANDBUTTON: sType = "Command button"; break;
172 case FormComponentType.RADIOBUTTON : sType = "Radio button"; break;
173 case FormComponentType.IMAGEBUTTON : sType = "Image button"; break;
174 case FormComponentType.CHECKBOX : sType = "Check Box"; break;
175 case FormComponentType.LISTBOX : sType = "List Box"; break;
176 case FormComponentType.COMBOBOX : sType = "Combo Box"; break;
177 case FormComponentType.GROUPBOX : sType = "Group Box"; break;
178 case FormComponentType.FIXEDTEXT : sType = "Fixed Text"; break;
179 case FormComponentType.GRIDCONTROL : sType = "Grid Control"; break;
180 case FormComponentType.FILECONTROL : sType = "File Control"; break;
181 case FormComponentType.HIDDENCONTROL: sType = "Hidden Control"; break;
182 case FormComponentType.IMAGECONTROL : sType = "Image Control"; break;
183 case FormComponentType.DATEFIELD : sType = "Date Field"; break;
184 case FormComponentType.TIMEFIELD : sType = "Time Field"; break;
185 case FormComponentType.NUMERICFIELD : sType = "Numeric Field"; break;
186 case FormComponentType.CURRENCYFIELD: sType = "Currency Field"; break;
187 case FormComponentType.PATTERNFIELD : sType = "Pattern Field"; break;
189 case FormComponentType.TEXTFIELD :
190 // there are two known services with this class id: the usual text field,
191 // and the formatted field
192 sType = "Text Field";
193 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.FormattedField" ) )
195 sType = "Formatted Field";
197 break;
199 default:
200 break;
203 else
205 if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.DataForm" ) )
207 sType = "Form";
211 return sType;
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */