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