xref: /AOO41X/main/odk/examples/DevelopersGuide/Forms/FLTools.java (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.*;
25cdf0e10cSrcweir import com.sun.star.lang.*;
26cdf0e10cSrcweir import com.sun.star.util.*;
27cdf0e10cSrcweir import com.sun.star.beans.*;
28cdf0e10cSrcweir import com.sun.star.container.*;
29cdf0e10cSrcweir import com.sun.star.awt.*;
30cdf0e10cSrcweir import com.sun.star.form.*;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /** provides global helpers
34cdf0e10cSrcweir */
35cdf0e10cSrcweir public class FLTools
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
dump_Object( Object aObject )38cdf0e10cSrcweir     static void dump_Object( Object aObject )
39cdf0e10cSrcweir     {
40cdf0e10cSrcweir         XServiceInfo xSI = UNO.queryServiceInfo( aObject );
41cdf0e10cSrcweir         if ( null != xSI )
42cdf0e10cSrcweir             System.out.println( "dumping object with name \"" + xSI.getImplementationName() + "\"" );
43cdf0e10cSrcweir         else
44cdf0e10cSrcweir             System.out.println( "object has no service info!" );
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
48cdf0e10cSrcweir     /** translates a string containing an URL into a complete
49cdf0e10cSrcweir         <type scope="com.sun.star.util">URL</type> object.
50cdf0e10cSrcweir     */
parseURL( String sURL, XComponentContext xCtx )51cdf0e10cSrcweir     static public URL parseURL( String sURL, XComponentContext xCtx ) throws java.lang.Exception
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         URL[] aURL = new URL[] { new URL() };
54cdf0e10cSrcweir         aURL[0].Complete = sURL;
55cdf0e10cSrcweir         // need an URLTransformer
56cdf0e10cSrcweir         XURLTransformer xTransformer = (XURLTransformer)UnoRuntime.queryInterface(
57cdf0e10cSrcweir             XURLTransformer.class,
58cdf0e10cSrcweir             xCtx.getServiceManager().createInstanceWithContext(
59cdf0e10cSrcweir                 "com.sun.star.util.URLTransformer", xCtx ) );
60cdf0e10cSrcweir         xTransformer.parseStrict( aURL );
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         return aURL[0];
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
66cdf0e10cSrcweir     /** returns the name of the given form component
67cdf0e10cSrcweir     */
getName( Object aFormComponent )68cdf0e10cSrcweir     public static String getName( Object aFormComponent ) throws com.sun.star.uno.Exception
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         XNamed xNamed = (XNamed)UnoRuntime.queryInterface( XNamed.class,
71cdf0e10cSrcweir             aFormComponent );
72cdf0e10cSrcweir         String sName = "";
73cdf0e10cSrcweir         if ( null != xNamed )
74cdf0e10cSrcweir             sName = xNamed.getName();
75cdf0e10cSrcweir         return sName;
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
79cdf0e10cSrcweir     /** returns the label of the given form component
80cdf0e10cSrcweir     */
getLabel( Object aFormComponent )81cdf0e10cSrcweir     public static String getLabel( Object aFormComponent ) throws com.sun.star.uno.Exception
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         String sLabel = "";
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
86cdf0e10cSrcweir         XPropertySetInfo xPSI = ( null != xProps ) ? xProps.getPropertySetInfo() : null;
87cdf0e10cSrcweir         if ( null == xPSI )
88cdf0e10cSrcweir         {   // no property set or no property set info
89cdf0e10cSrcweir             // can't do anything except falling back to the name
90cdf0e10cSrcweir             return getName( aFormComponent );
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         // first check if the component has a LabelControl
94cdf0e10cSrcweir         if ( xPSI.hasPropertyByName( "LabelControl" ) )
95cdf0e10cSrcweir             sLabel = getLabel( xProps.getPropertyValue( "LabelControl" ) );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         // no LabelControl or no label at the LabelControl
98cdf0e10cSrcweir         if ( 0 == sLabel.length() )
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             // a "Label" property?
101cdf0e10cSrcweir             if ( xPSI.hasPropertyByName( "Label" ) )
102cdf0e10cSrcweir                 sLabel = (String)xProps.getPropertyValue( "Label" );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             if ( 0 == sLabel.length() )
105cdf0e10cSrcweir             {   // no Label property or no label set
106cdf0e10cSrcweir                 // -> fallback to the component name
107cdf0e10cSrcweir                 sLabel = getName( aFormComponent );
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         return sLabel;
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
115cdf0e10cSrcweir     /** retrieves the index of a form component within it's parent
116cdf0e10cSrcweir     */
getIndexInParent( Object aContainer, Object aElement )117cdf0e10cSrcweir     static public int getIndexInParent( Object aContainer, Object aElement ) throws com.sun.star.uno.Exception
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         int nIndex = -1;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         // norm the element
122cdf0e10cSrcweir         XInterface xElement = (XInterface)UnoRuntime.queryInterface(
123cdf0e10cSrcweir             XInterface.class, aElement );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // get the container
126cdf0e10cSrcweir         XIndexContainer xIndexCont = UNO.queryIndexContainer( aContainer );
127cdf0e10cSrcweir         if ( null != xIndexCont )
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             // loop through all children
130cdf0e10cSrcweir             int nCount = xIndexCont.getCount();
131cdf0e10cSrcweir             for ( int i = 0; i < nCount; ++i )
132cdf0e10cSrcweir             {
133cdf0e10cSrcweir                 // compare with the element
134cdf0e10cSrcweir                 XInterface xCurrent = (XInterface)UnoRuntime.queryInterface(
135cdf0e10cSrcweir                     XInterface.class, xIndexCont.getByIndex( 0 ) );
136cdf0e10cSrcweir                 if ( xCurrent.equals( xElement ) )
137cdf0e10cSrcweir                 {   // found
138cdf0e10cSrcweir                     nIndex = i;
139cdf0e10cSrcweir                     break;
140cdf0e10cSrcweir                 }
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         // outta here
145cdf0e10cSrcweir         return nIndex;
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
149cdf0e10cSrcweir     /** retrieves the parent of the given object
150cdf0e10cSrcweir     */
getParent( Object aComponent, Class aInterfaceClass )151cdf0e10cSrcweir     static Object getParent( Object aComponent, Class aInterfaceClass )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         XChild xAsChild = (XChild)UnoRuntime.queryInterface( XChild.class, aComponent );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         return UnoRuntime.queryInterface( aInterfaceClass, xAsChild.getParent() );
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
159cdf0e10cSrcweir     /** retrieves the parent of the given object
160cdf0e10cSrcweir     */
getParent( Object aComponent )161cdf0e10cSrcweir     static XPropertySet getParent( Object aComponent )
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         return (XPropertySet)getParent( aComponent, XPropertySet.class );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
167cdf0e10cSrcweir     /** disposes the component given
168cdf0e10cSrcweir     */
disposeComponent( Object xComp )169cdf0e10cSrcweir     static public void disposeComponent( Object xComp ) throws java.lang.RuntimeException
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         XComponent xComponent = (XComponent)UnoRuntime.queryInterface( XComponent.class,
172cdf0e10cSrcweir             xComp );
173cdf0e10cSrcweir         if ( null != xComponent )
174cdf0e10cSrcweir             xComponent.dispose();
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
178cdf0e10cSrcweir     /** get's the XControlModel for a control
179cdf0e10cSrcweir     */
getModel( Object aControl, Class aInterfaceClass )180cdf0e10cSrcweir     static public Object getModel( Object aControl, Class aInterfaceClass )
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         XControl xControl = (XControl)UnoRuntime.queryInterface(
183cdf0e10cSrcweir             XControl.class, aControl );
184cdf0e10cSrcweir         XControlModel xModel = null;
185cdf0e10cSrcweir         if ( null != xControl )
186cdf0e10cSrcweir             xModel = xControl.getModel();
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         return UnoRuntime.queryInterface( aInterfaceClass, xModel );
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
192cdf0e10cSrcweir     /** retrieves the type of a form component.
193cdf0e10cSrcweir         <p>Speaking strictly, the function recognizes more than form components. Especially,
194cdf0e10cSrcweir         it survives a null argument. which means it can be safely applied to the a top-level
195cdf0e10cSrcweir         forms container; and it is able to classify grid columns (which are no form components)
196cdf0e10cSrcweir         as well.</p>
197cdf0e10cSrcweir     */
classifyFormComponentType( XPropertySet xComponent )198cdf0e10cSrcweir     static public String classifyFormComponentType( XPropertySet xComponent ) throws com.sun.star.uno.Exception
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir         String sType = "<unknown component>";
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         XServiceInfo xSI = UNO.queryServiceInfo( xComponent );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         XPropertySetInfo xPSI = null;
205cdf0e10cSrcweir         if ( null != xComponent )
206cdf0e10cSrcweir             xPSI = xComponent.getPropertySetInfo();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         if ( ( null != xPSI ) && xPSI.hasPropertyByName( "ClassId" ) )
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             // get the ClassId property
211cdf0e10cSrcweir             XPropertySet xCompProps = UNO.queryPropertySet( xComponent );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             Short nClassId = (Short)xCompProps.getPropertyValue( "ClassId" );
214cdf0e10cSrcweir             switch ( nClassId.intValue() )
215cdf0e10cSrcweir             {
216cdf0e10cSrcweir                 case FormComponentType.COMMANDBUTTON: sType = "Command button"; break;
217cdf0e10cSrcweir                 case FormComponentType.RADIOBUTTON  : sType = "Radio button"; break;
218cdf0e10cSrcweir                 case FormComponentType.IMAGEBUTTON  : sType = "Image button"; break;
219cdf0e10cSrcweir                 case FormComponentType.CHECKBOX     : sType = "Check Box"; break;
220cdf0e10cSrcweir                 case FormComponentType.LISTBOX      : sType = "List Box"; break;
221cdf0e10cSrcweir                 case FormComponentType.COMBOBOX     : sType = "Combo Box"; break;
222cdf0e10cSrcweir                 case FormComponentType.GROUPBOX     : sType = "Group Box"; break;
223cdf0e10cSrcweir                 case FormComponentType.FIXEDTEXT    : sType = "Fixed Text"; break;
224cdf0e10cSrcweir                 case FormComponentType.GRIDCONTROL  : sType = "Grid Control"; break;
225cdf0e10cSrcweir                 case FormComponentType.FILECONTROL  : sType = "File Control"; break;
226cdf0e10cSrcweir                 case FormComponentType.HIDDENCONTROL: sType = "Hidden Control"; break;
227cdf0e10cSrcweir                 case FormComponentType.IMAGECONTROL : sType = "Image Control"; break;
228cdf0e10cSrcweir                 case FormComponentType.DATEFIELD    : sType = "Date Field"; break;
229cdf0e10cSrcweir                 case FormComponentType.TIMEFIELD    : sType = "Time Field"; break;
230cdf0e10cSrcweir                 case FormComponentType.NUMERICFIELD : sType = "Numeric Field"; break;
231cdf0e10cSrcweir                 case FormComponentType.CURRENCYFIELD: sType = "Currency Field"; break;
232cdf0e10cSrcweir                 case FormComponentType.PATTERNFIELD : sType = "Pattern Field"; break;
233cdf0e10cSrcweir 
234cdf0e10cSrcweir                 case FormComponentType.TEXTFIELD    :
235cdf0e10cSrcweir                     // there are two known services with this class id: the usual text field,
236cdf0e10cSrcweir                     // and the formatted field
237cdf0e10cSrcweir                     sType = "Text Field";
238cdf0e10cSrcweir                     if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.FormattedField" ) )
239cdf0e10cSrcweir                     {
240cdf0e10cSrcweir                         sType = "Formatted Field";
241cdf0e10cSrcweir                     }
242cdf0e10cSrcweir                     break;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir                 default:
245cdf0e10cSrcweir                     break;
246cdf0e10cSrcweir             }
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir         else
249cdf0e10cSrcweir         {
250cdf0e10cSrcweir             if ( ( null != xSI ) && xSI.supportsService( "com.sun.star.form.component.DataForm" ) )
251cdf0e10cSrcweir             {
252cdf0e10cSrcweir                 sType = "Form";
253cdf0e10cSrcweir             }
254cdf0e10cSrcweir         }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir         return sType;
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir };
260