merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Spreadsheet / ExampleAddIn.java
bloba98a615afb44c7a9e019a51ee6f05a708408b5f9
1 /*************************************************************************
3 * $RCSfile: ExampleAddIn.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:54:08 $
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 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult
43 private String aName;
44 private int nValue;
45 private java.util.Vector aListeners = new java.util.Vector();
47 public ExampleAddInResult( String aNewName )
49 aName = aNewName;
52 private com.sun.star.sheet.ResultEvent getResult()
54 com.sun.star.sheet.ResultEvent aEvent =
55 new com.sun.star.sheet.ResultEvent();
56 aEvent.Value = aName + " " + String.valueOf( nValue );
57 aEvent.Source = this;
58 return aEvent;
61 public void addResultListener(com.sun.star.sheet.XResultListener aListener)
63 aListeners.addElement( aListener );
65 // immediately notify of initial value
66 aListener.modified( getResult() );
69 public void removeResultListener(com.sun.star.sheet.XResultListener aListener)
71 aListeners.removeElement( aListener );
74 public void incrementValue()
76 ++nValue;
77 com.sun.star.sheet.ResultEvent aEvent = getResult();
79 java.util.Enumeration aEnum = aListeners.elements();
80 while (aEnum.hasMoreElements())
81 ((com.sun.star.sheet.XResultListener)aEnum.nextElement()).modified(
82 aEvent);
86 class ExampleAddInThread extends Thread
88 private java.util.Hashtable aCounters;
90 public ExampleAddInThread( java.util.Hashtable aResults )
92 aCounters = aResults;
95 public void run()
97 while ( true )
99 try
101 sleep(1000);
103 catch( InterruptedException exception )
107 // increment all counters
108 java.util.Enumeration aEnum = aCounters.elements();
109 while (aEnum.hasMoreElements())
110 ((ExampleAddInResult)aEnum.nextElement()).incrementValue();
115 public class ExampleAddIn
117 static public class _ExampleAddIn extends com.sun.star.lib.uno.helper.WeakBase
118 implements org.openoffice.sheet.addin.XExampleAddIn,
119 com.sun.star.sheet.XAddIn,
120 com.sun.star.lang.XServiceName,
121 com.sun.star.lang.XServiceInfo
123 static private final String aExampleService = "org.openoffice.sheet.addin.ExampleAddIn";
124 static private final String aAddInService = "com.sun.star.sheet.AddIn";
125 static private final String aImplName = "ExampleAddIn";
127 private static final short FUNCTION_INVALID = -1;
128 private static final short FUNCTION_INCREMENT = 0;
129 private static final short FUNCTION_COUNTER = 1;
131 private static final String[] aFunctionNames =
133 "getIncremented",
134 "getCounter"
136 private static final String[] aDisplayFunctionNames =
138 "Increment",
139 "Counter"
141 private static final String[] aDescriptions =
143 "Increments a value",
144 "Returns a counter"
146 private static final String[] aFirstArgumentNames =
148 "Value",
149 "Name"
151 private static final String[] aFirstArgumentDescriptions =
153 "The value that is incremented",
154 "The name of the counter"
157 private com.sun.star.lang.Locale aFuncLocale;
158 private java.util.Hashtable aResults;
160 public _ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )
164 private int getFunctionID( String aProgrammaticFunctionName )
166 for ( int i = 0; i < aFunctionNames.length; i++ )
167 if ( aProgrammaticFunctionName.equals(aFunctionNames[i]) )
168 return i;
169 return FUNCTION_INVALID;
172 // XExampleAddIn
174 public int getIncremented( int nValue )
176 return nValue + 1;
179 public com.sun.star.sheet.XVolatileResult getCounter(String aName)
181 if ( aResults == null )
183 // create the table of results, and start a thread to increment
184 // all counters
185 aResults = new java.util.Hashtable();
186 ExampleAddInThread aThread = new ExampleAddInThread( aResults );
187 aThread.start();
190 ExampleAddInResult aResult = (ExampleAddInResult) aResults.get(aName);
191 if ( aResult == null )
193 aResult = new ExampleAddInResult(aName);
194 aResults.put( aName, aResult );
196 return aResult;
199 // XAddIn
201 public String getProgrammaticFuntionName(String aDisplayName)
203 for ( int i = 0; i < aFunctionNames.length; i++ )
204 if ( aDisplayName.equals(aDisplayFunctionNames[i]) )
205 return aFunctionNames[i];
206 return "";
209 public String getDisplayFunctionName(String aProgrammaticName)
211 int nFunction = getFunctionID( aProgrammaticName );
212 return ( nFunction == FUNCTION_INVALID ) ? "" :
213 aDisplayFunctionNames[nFunction];
216 public String getFunctionDescription(String aProgrammaticName)
218 int nFunction = getFunctionID( aProgrammaticName );
219 return ( nFunction == FUNCTION_INVALID ) ? "" :
220 aDescriptions[nFunction];
223 public String getDisplayArgumentName(String aProgrammaticFunctionName,
224 int nArgument)
226 // both functions in this example only have a first argument
227 int nFunction = getFunctionID( aProgrammaticFunctionName );
228 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
229 aFirstArgumentNames[nFunction];
232 public String getArgumentDescription(String aProgrammaticFunctionName,
233 int nArgument )
235 // both functions in this example only have a first argument
236 int nFunction = getFunctionID( aProgrammaticFunctionName );
237 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
238 aFirstArgumentDescriptions[nFunction];
241 public String getProgrammaticCategoryName(String aProgrammaticFunctionName)
243 return( "Add-In" );
246 public String getDisplayCategoryName(String aProgrammaticFunctionName)
248 return( "Add-In" );
251 // XLocalizable
253 public void setLocale( com.sun.star.lang.Locale aLocale )
255 // the locale is stored and used for getLocale, but otherwise
256 // ignored in this example
257 aFuncLocale = aLocale;
260 public com.sun.star.lang.Locale getLocale()
262 return aFuncLocale;
265 // XServiceName
267 public String getServiceName()
269 return aExampleService;
272 // XServiceInfo
274 public String getImplementationName()
276 return aImplName;
279 public String[] getSupportedServiceNames()
281 String [] aSupportedServices = new String[ 2 ];
282 aSupportedServices[ 0 ] = aExampleService;
283 aSupportedServices[ 1 ] = aAddInService;
284 return aSupportedServices;
287 public boolean supportsService( String aService )
289 return (aService.equals( aExampleService ) ||
290 aService.equals( aAddInService ) );
296 public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
297 String implName,
298 com.sun.star.lang.XMultiServiceFactory multiFactory,
299 com.sun.star.registry.XRegistryKey regKey)
301 com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
302 if ( implName.equals(_ExampleAddIn.aImplName) )
303 xSingleServiceFactory =
304 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
305 _ExampleAddIn.class, _ExampleAddIn.aExampleService,
306 multiFactory, regKey);
307 return xSingleServiceFactory;
310 public static boolean __writeRegistryServiceInfo(
311 com.sun.star.registry.XRegistryKey regKey)
313 // register for both the base AddIn and the own service
314 return com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
315 _ExampleAddIn.aImplName, _ExampleAddIn.aExampleService, regKey)
316 && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
317 _ExampleAddIn.aImplName, _ExampleAddIn.aAddInService, regKey);