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