cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / odk / examples / DevelopersGuide / Spreadsheet / ExampleAddIn.java
blob59cac873f7ae1c9b7c26d4bc7cc473b303977fae
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 import com.sun.star.sheet.XResultListener;
4 /*************************************************************************
6 * The Contents of this file are made available subject to the terms of
7 * the BSD license.
9 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *************************************************************************/
38 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult
40 private final String aName;
41 private int nValue;
42 private final java.util.ArrayList<XResultListener> aListeners = new java.util.ArrayList<XResultListener>();
44 public ExampleAddInResult( String aNewName )
46 aName = aNewName;
49 private com.sun.star.sheet.ResultEvent getResult()
51 com.sun.star.sheet.ResultEvent aEvent =
52 new com.sun.star.sheet.ResultEvent();
53 aEvent.Value = aName + " " + nValue;
54 aEvent.Source = this;
55 return aEvent;
58 public void addResultListener(com.sun.star.sheet.XResultListener aListener)
60 aListeners.add( aListener );
62 // immediately notify of initial value
63 aListener.modified( getResult() );
66 public void removeResultListener(com.sun.star.sheet.XResultListener aListener)
68 aListeners.remove( aListener );
71 public void incrementValue()
73 ++nValue;
74 com.sun.star.sheet.ResultEvent aEvent = getResult();
76 for( XResultListener l : aListeners)
77 l.modified(aEvent);
81 class ExampleAddInThread extends Thread
83 private final java.util.HashMap<String, ExampleAddInResult> aCounters;
85 public ExampleAddInThread( java.util.HashMap<String, ExampleAddInResult> aResults )
87 aCounters = aResults;
90 @Override
91 public void run()
93 while ( true )
95 try
97 sleep(1000);
99 catch( InterruptedException exception )
103 // increment all counters
104 for (ExampleAddInResult r : aCounters.values())
105 r.incrementValue();
110 public class ExampleAddIn
112 public static class _ExampleAddIn extends com.sun.star.lib.uno.helper.WeakBase
113 implements org.openoffice.sheet.addin.XExampleAddIn,
114 com.sun.star.sheet.XAddIn,
115 com.sun.star.lang.XServiceName,
116 com.sun.star.lang.XServiceInfo
118 private static final String aExampleService = "org.openoffice.sheet.addin.ExampleAddIn";
119 private static final String aAddInService = "com.sun.star.sheet.AddIn";
120 private static final String aImplName = _ExampleAddIn.class.getName();
122 private static final short FUNCTION_INVALID = -1;
124 private static final String[] aFunctionNames =
126 "getIncremented",
127 "getCounter"
129 private static final String[] aDisplayFunctionNames =
131 "Increment",
132 "Counter"
134 private static final String[] aDescriptions =
136 "Increments a value",
137 "Returns a counter"
139 private static final String[] aFirstArgumentNames =
141 "Value",
142 "Name"
144 private static final String[] aFirstArgumentDescriptions =
146 "The value that is incremented",
147 "The name of the counter"
150 private com.sun.star.lang.Locale aFuncLocale;
151 private java.util.HashMap<String, ExampleAddInResult> aResults;
153 public _ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )
157 private int getFunctionID( String aProgrammaticFunctionName )
159 for ( int i = 0; i < aFunctionNames.length; i++ )
160 if ( aProgrammaticFunctionName.equals(aFunctionNames[i]) )
161 return i;
162 return FUNCTION_INVALID;
165 // XExampleAddIn
167 public int getIncremented( int nValue )
169 return nValue + 1;
172 public com.sun.star.sheet.XVolatileResult getCounter(String aName)
174 if ( aResults == null )
176 // create the table of results, and start a thread to increment
177 // all counters
178 aResults = new java.util.HashMap<String, ExampleAddInResult>();
179 ExampleAddInThread aThread = new ExampleAddInThread( aResults );
180 aThread.start();
183 ExampleAddInResult aResult = aResults.get(aName);
184 if ( aResult == null )
186 aResult = new ExampleAddInResult(aName);
187 aResults.put( aName, aResult );
189 return aResult;
192 // XAddIn
194 public String getProgrammaticFuntionName(String aDisplayName)
196 for ( int i = 0; i < aFunctionNames.length; i++ )
197 if ( aDisplayName.equals(aDisplayFunctionNames[i]) )
198 return aFunctionNames[i];
199 return "";
202 public String getDisplayFunctionName(String aProgrammaticName)
204 int nFunction = getFunctionID( aProgrammaticName );
205 return ( nFunction == FUNCTION_INVALID ) ? "" :
206 aDisplayFunctionNames[nFunction];
209 public String getFunctionDescription(String aProgrammaticName)
211 int nFunction = getFunctionID( aProgrammaticName );
212 return ( nFunction == FUNCTION_INVALID ) ? "" :
213 aDescriptions[nFunction];
216 public String getDisplayArgumentName(String aProgrammaticFunctionName,
217 int nArgument)
219 // both functions in this example only have a first argument
220 int nFunction = getFunctionID( aProgrammaticFunctionName );
221 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
222 aFirstArgumentNames[nFunction];
225 public String getArgumentDescription(String aProgrammaticFunctionName,
226 int nArgument )
228 // both functions in this example only have a first argument
229 int nFunction = getFunctionID( aProgrammaticFunctionName );
230 return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
231 aFirstArgumentDescriptions[nFunction];
234 public String getProgrammaticCategoryName(String aProgrammaticFunctionName)
236 return( "Add-In" );
239 public String getDisplayCategoryName(String aProgrammaticFunctionName)
241 return( "Add-In" );
244 // XLocalizable
246 public void setLocale( com.sun.star.lang.Locale aLocale )
248 // the locale is stored and used for getLocale, but otherwise
249 // ignored in this example
250 aFuncLocale = aLocale;
253 public com.sun.star.lang.Locale getLocale()
255 return aFuncLocale;
258 // XServiceName
260 public String getServiceName()
262 return aExampleService;
265 // XServiceInfo
267 public String getImplementationName()
269 return aImplName;
272 public String[] getSupportedServiceNames()
274 String [] aSupportedServices = new String[ 2 ];
275 aSupportedServices[ 0 ] = aExampleService;
276 aSupportedServices[ 1 ] = aAddInService;
277 return aSupportedServices;
280 public boolean supportsService( String aService )
282 return (aService.equals( aExampleService ) ||
283 aService.equals( aAddInService ) );
289 public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
290 String implName,
291 com.sun.star.lang.XMultiServiceFactory multiFactory,
292 com.sun.star.registry.XRegistryKey regKey)
294 com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
295 if ( implName.equals(_ExampleAddIn.aImplName) )
296 xSingleServiceFactory =
297 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
298 _ExampleAddIn.class, _ExampleAddIn.aExampleService,
299 multiFactory, regKey);
300 return xSingleServiceFactory;
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */