merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / Linguistic / SampleThesaurus.java
blob9752d0c92e378c3ca0bb7a0c28d55c3e3ee8b73e
1 /*************************************************************************
3 * $RCSfile: SampleThesaurus.java,v $
5 * $Revision: 1.3 $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:41:22 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 // uno
42 import com.sun.star.lib.uno.helper.ComponentBase;
43 import com.sun.star.uno.UnoRuntime;
45 // factories
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.lang.XSingleServiceFactory;
49 // supported Interfaces
50 import com.sun.star.linguistic2.XThesaurus;
51 import com.sun.star.lang.XInitialization;
52 import com.sun.star.lang.XComponent;
53 import com.sun.star.lang.XServiceInfo;
54 import com.sun.star.lang.XServiceDisplayName;
56 // Exceptions
57 import com.sun.star.uno.Exception;
58 import com.sun.star.uno.RuntimeException;
59 import com.sun.star.lang.IllegalArgumentException;
61 //used Interfaces
62 import com.sun.star.linguistic2.XMeaning;
63 import com.sun.star.lang.Locale;
64 import com.sun.star.lang.XEventListener;
65 import com.sun.star.lang.EventObject;
66 import com.sun.star.beans.XPropertySet;
67 import com.sun.star.beans.PropertyValue;
68 import com.sun.star.uno.AnyConverter;
69 import com.sun.star.lang.XTypeProvider;
70 import com.sun.star.uno.Type;
72 import java.util.ArrayList;
74 public class SampleThesaurus extends ComponentBase implements
75 XThesaurus,
76 XInitialization,
77 XServiceDisplayName,
78 XServiceInfo
80 PropChgHelper aPropChgHelper;
81 ArrayList aEvtListeners;
82 boolean bDisposing;
84 public SampleThesaurus()
86 // names of relevant properties to be used
87 String[] aProps = new String[]
89 "IsIgnoreControlCharacters",
90 "IsUseDictionaryList",
91 "IsGermanPreReform",
94 // this service has no listeners thus we may use the base class,
95 // which is here basically used only to keep track of the
96 // property set (and it's lifetime) since it gets used in the
97 // 'GetValueToUse' function
98 aPropChgHelper = new PropChgHelper( (XThesaurus) this, aProps );
100 aEvtListeners = new ArrayList();
101 bDisposing = false;
104 private boolean IsEqual( Locale aLoc1, Locale aLoc2 )
106 return aLoc1.Language.equals( aLoc2.Language ) &&
107 aLoc1.Country .equals( aLoc2.Country ) &&
108 aLoc1.Variant .equals( aLoc2.Variant );
111 private boolean GetValueToUse(
112 String aPropName,
113 boolean bDefaultVal,
114 PropertyValue[] aProps )
116 boolean bRes = bDefaultVal;
120 // use temporary value if supplied
121 for (int i = 0; i < aProps.length; ++i)
123 if (aPropName.equals( aProps[i].Name ))
125 Object aObj = aProps[i].Value;
126 if (AnyConverter.isBoolean( aObj ))
128 bRes = AnyConverter.toBoolean( aObj );
129 return bRes;
134 // otherwise use value from property set (if available)
135 XPropertySet xPropSet = aPropChgHelper.GetPropSet();
136 if (xPropSet != null) // should always be the case
138 Object aObj = xPropSet.getPropertyValue( aPropName );
139 if (AnyConverter.isBoolean( aObj ))
140 bRes = AnyConverter.toBoolean( aObj );
143 catch (Exception e) {
144 bRes = bDefaultVal;
147 return bRes;
150 // __________ interface methods __________
153 //*****************
154 //XSupportedLocales
155 //*****************
156 public Locale[] getLocales()
157 throws com.sun.star.uno.RuntimeException
159 Locale aLocales[] =
161 new Locale( "en", "US", "" )
164 return aLocales;
167 public boolean hasLocale( Locale aLocale )
168 throws com.sun.star.uno.RuntimeException
170 boolean bRes = false;
171 if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
172 bRes = true;
173 return bRes;
176 //**********
177 //XThesaurus
178 //**********
179 public XMeaning[] queryMeanings(
180 String aTerm, Locale aLocale,
181 PropertyValue[] aProperties )
182 throws com.sun.star.lang.IllegalArgumentException,
183 com.sun.star.uno.RuntimeException
185 if (IsEqual( aLocale, new Locale() ) || aTerm.length() == 0)
186 return null;
188 // linguistic is currently not allowed to throw exceptions
189 // thus we return null fwhich means 'word cannot be looked up'
190 if (!hasLocale( aLocale ))
191 return null;
193 // get values of relevant properties that may be used.
194 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
195 //! are handled by the dispatcher! Thus there is no need to access
196 //! them here.
197 boolean bIsGermanPreReform = GetValueToUse( "IsGermanPreReform", false, aProperties );
199 XMeaning[] aRes = null;
201 //!! This code needs to be replaced by code calling the actual
202 //!! implementation of your thesaurus
203 if (aTerm.equals( "house" ) &&
204 IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
206 aRes = new XMeaning[]
208 new XMeaning_impl( "a building where one lives",
209 new String[]{ "home", "place", "dwelling" } ),
210 new XMeaning_impl( "a group of people sharing common ancestry",
211 new String[]{ "family", "clan", "kindred" } ),
212 new XMeaning_impl( "to provide with lodging",
213 new String[]{ "room", "board", "put up" } )
217 return aRes;
221 //********************
222 // XServiceDisplayName
223 //********************
224 public String getServiceDisplayName( Locale aLocale )
225 throws com.sun.star.uno.RuntimeException
227 return "Java Samples";
230 //****************
231 // XInitialization
232 //****************
233 public void initialize( Object[] aArguments )
234 throws com.sun.star.uno.Exception,
235 com.sun.star.uno.RuntimeException
237 int nLen = aArguments.length;
238 if (2 == nLen)
240 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
241 XPropertySet.class, aArguments[0]);
242 // start listening to property changes
243 aPropChgHelper.AddAsListenerTo( xPropSet );
247 //*************
248 // XServiceInfo
249 //*************
250 public boolean supportsService( String aServiceName )
251 throws com.sun.star.uno.RuntimeException
253 String[] aServices = getSupportedServiceNames_Static();
254 int i, nLength = aServices.length;
255 boolean bResult = false;
257 for( i = 0; !bResult && i < nLength; ++i )
258 bResult = aServiceName.equals( aServices[ i ] );
260 return bResult;
263 public String getImplementationName()
264 throws com.sun.star.uno.RuntimeException
266 return _aSvcImplName;
269 public String[] getSupportedServiceNames()
270 throws com.sun.star.uno.RuntimeException
272 return getSupportedServiceNames_Static();
275 // __________ static things __________
277 public static String _aSvcImplName = "com.sun.star.linguistic2.JavaSamples.SampleThesaurus";
279 public static String[] getSupportedServiceNames_Static()
281 String[] aResult = { "com.sun.star.linguistic2.Thesaurus" };
282 return aResult;
287 * Returns a factory for creating the service.
288 * This method is called by the <code>JavaLoader</code>
289 * <p>
290 * @return returns a <code>XSingleServiceFactory</code> for creating the component
291 * @param implName the name of the implementation for which a service is desired
292 * @param multiFactory the service manager to be used if needed
293 * @param regKey the registryKey
294 * @see com.sun.star.comp.loader.JavaLoader
296 public static XSingleServiceFactory __getServiceFactory(
297 String aImplName,
298 XMultiServiceFactory xMultiFactory,
299 com.sun.star.registry.XRegistryKey xRegKey )
301 XSingleServiceFactory xSingleServiceFactory = null;
302 if( aImplName.equals( _aSvcImplName ) )
304 xSingleServiceFactory = new OneInstanceFactory(
305 SampleThesaurus.class, _aSvcImplName,
306 getSupportedServiceNames_Static(),
307 xMultiFactory );
309 return xSingleServiceFactory;
313 * Writes the service information into the given registry key.
314 * This method is called by the <code>JavaLoader</code>
315 * <p>
316 * @return returns true if the operation succeeded
317 * @param xRegKey the registryKey
318 * @see com.sun.star.comp.loader.JavaLoader
320 public static boolean __writeRegistryServiceInfo(
321 com.sun.star.registry.XRegistryKey xRegKey )
323 boolean bResult = true;
324 String[] aServices = getSupportedServiceNames_Static();
325 int i, nLength = aServices.length;
326 for( i = 0; i < nLength; ++i )
328 bResult = bResult && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
329 _aSvcImplName, aServices[i], xRegKey );
331 return bResult;