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