Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Spreadsheet / ExampleAddIn.java
blobe6d6830a48edb81d56667914482f0c61275051e3
1 import com.sun.star.sheet.XResultListener;
3 /*************************************************************************
5 * The Contents of this file are made available subject to the terms of
6 * the BSD license.
8 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
33 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *************************************************************************/
37 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult
39 private final String aName;
40 private int nValue;
41 private final java.util.ArrayList<XResultListener> aListeners = new java.util.ArrayList<XResultListener>();
43 public ExampleAddInResult( String aNewName )
45 aName = aNewName;
48 private com.sun.star.sheet.ResultEvent getResult()
50 com.sun.star.sheet.ResultEvent aEvent =
51 new com.sun.star.sheet.ResultEvent();
52 aEvent.Value = aName + " " + nValue;
53 aEvent.Source = this;
54 return aEvent;
57 public void addResultListener(com.sun.star.sheet.XResultListener aListener)
59 aListeners.add( aListener );
61 // immediately notify of initial value
62 aListener.modified( getResult() );
65 public void removeResultListener(com.sun.star.sheet.XResultListener aListener)
67 aListeners.remove( aListener );
70 public void incrementValue()
72 ++nValue;
73 com.sun.star.sheet.ResultEvent aEvent = getResult();
75 for( XResultListener l : aListeners)
76 l.modified(aEvent);
80 class ExampleAddInThread extends Thread
82 private final java.util.HashMap<String, ExampleAddInResult> aCounters;
84 public ExampleAddInThread( java.util.HashMap<String, ExampleAddInResult> aResults )
86 aCounters = aResults;
89 @Override
90 public void run()
92 while ( true )
94 try
96 sleep(1000);
98 catch( InterruptedException exception )
102 // increment all counters
103 for (ExampleAddInResult r : aCounters.values())
104 r.incrementValue();
109 public class ExampleAddIn
111 static public class _ExampleAddIn extends com.sun.star.lib.uno.helper.WeakBase
112 implements org.openoffice.sheet.addin.XExampleAddIn,
113 com.sun.star.sheet.XAddIn,
114 com.sun.star.lang.XServiceName,
115 com.sun.star.lang.XServiceInfo
117 static private final String aExampleService = "org.openoffice.sheet.addin.ExampleAddIn";
118 static private final String aAddInService = "com.sun.star.sheet.AddIn";
119 static private final String aImplName = _ExampleAddIn.class.getName();
121 private static final short FUNCTION_INVALID = -1;
123 private static final String[] aFunctionNames =
125 "getIncremented",
126 "getCounter"
128 private static final String[] aDisplayFunctionNames =
130 "Increment",
131 "Counter"
133 private static final String[] aDescriptions =
135 "Increments a value",
136 "Returns a counter"
138 private static final String[] aFirstArgumentNames =
140 "Value",
141 "Name"
143 private static final String[] aFirstArgumentDescriptions =
145 "The value that is incremented",
146 "The name of the counter"
149 private com.sun.star.lang.Locale aFuncLocale;
150 private java.util.HashMap<String, ExampleAddInResult> aResults;
152 public _ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )
156 private int getFunctionID( String aProgrammaticFunctionName )
158 for ( int i = 0; i < aFunctionNames.length; i++ )
159 if ( aProgrammaticFunctionName.equals(aFunctionNames[i]) )
160 return i;
161 return FUNCTION_INVALID;
164 // XExampleAddIn
166 public int getIncremented( int nValue )
168 return nValue + 1;
171 public com.sun.star.sheet.XVolatileResult getCounter(String aName)
173 if ( aResults == null )
175 // create the table of results, and start a thread to increment
176 // all counters
177 aResults = new java.util.HashMap<String, ExampleAddInResult>();
178 ExampleAddInThread aThread = new ExampleAddInThread( aResults );
179 aThread.start();
182 ExampleAddInResult aResult = aResults.get(aName);
183 if ( aResult == null )
185 aResult = new ExampleAddInResult(aName);
186 aResults.put( aName, aResult );
188 return aResult;
191 // XAddIn
193 public String getProgrammaticFuntionName(String aDisplayName)
195 for ( int i = 0; i < aFunctionNames.length; i++ )
196 if ( aDisplayName.equals(aDisplayFunctionNames[i]) )
197 return aFunctionNames[i];
198 return "";
201 public String getDisplayFunctionName(String aProgrammaticName)
203 int nFunction = getFunctionID( aProgrammaticName );
204 return ( nFunction == FUNCTION_INVALID ) ? "" :
205 aDisplayFunctionNames[nFunction];
208 public String getFunctionDescription(String aProgrammaticName)
210 int nFunction = getFunctionID( aProgrammaticName );
211 return ( nFunction == FUNCTION_INVALID ) ? "" :
212 aDescriptions[nFunction];
215 public String getDisplayArgumentName(String aProgrammaticFunctionName,
216 int nArgument)
218 // both functions in this example only have a first argument
219 int nFunction = getFunctionID( aProgrammaticFunctionName );
220 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
221 aFirstArgumentNames[nFunction];
224 public String getArgumentDescription(String aProgrammaticFunctionName,
225 int nArgument )
227 // both functions in this example only have a first argument
228 int nFunction = getFunctionID( aProgrammaticFunctionName );
229 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
230 aFirstArgumentDescriptions[nFunction];
233 public String getProgrammaticCategoryName(String aProgrammaticFunctionName)
235 return( "Add-In" );
238 public String getDisplayCategoryName(String aProgrammaticFunctionName)
240 return( "Add-In" );
243 // XLocalizable
245 public void setLocale( com.sun.star.lang.Locale aLocale )
247 // the locale is stored and used for getLocale, but otherwise
248 // ignored in this example
249 aFuncLocale = aLocale;
252 public com.sun.star.lang.Locale getLocale()
254 return aFuncLocale;
257 // XServiceName
259 public String getServiceName()
261 return aExampleService;
264 // XServiceInfo
266 public String getImplementationName()
268 return aImplName;
271 public String[] getSupportedServiceNames()
273 String [] aSupportedServices = new String[ 2 ];
274 aSupportedServices[ 0 ] = aExampleService;
275 aSupportedServices[ 1 ] = aAddInService;
276 return aSupportedServices;
279 public boolean supportsService( String aService )
281 return (aService.equals( aExampleService ) ||
282 aService.equals( aAddInService ) );
288 public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
289 String implName,
290 com.sun.star.lang.XMultiServiceFactory multiFactory,
291 com.sun.star.registry.XRegistryKey regKey)
293 com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
294 if ( implName.equals(_ExampleAddIn.aImplName) )
295 xSingleServiceFactory =
296 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
297 _ExampleAddIn.class, _ExampleAddIn.aExampleService,
298 multiFactory, regKey);
299 return xSingleServiceFactory;