1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult
36 {
37     private String aName;
38     private int nValue;
39     private java.util.Vector aListeners = new java.util.Vector();
40 
41     public ExampleAddInResult( String aNewName )
42     {
43         aName = aNewName;
44     }
45 
46     private com.sun.star.sheet.ResultEvent getResult()
47     {
48         com.sun.star.sheet.ResultEvent aEvent =
49             new com.sun.star.sheet.ResultEvent();
50         aEvent.Value = aName + " " + String.valueOf( nValue );
51         aEvent.Source = this;
52         return aEvent;
53     }
54 
55     public void addResultListener(com.sun.star.sheet.XResultListener aListener)
56     {
57         aListeners.addElement( aListener );
58 
59         // immediately notify of initial value
60         aListener.modified( getResult() );
61     }
62 
63     public void removeResultListener(com.sun.star.sheet.XResultListener aListener)
64     {
65         aListeners.removeElement( aListener );
66     }
67 
68     public void incrementValue()
69     {
70         ++nValue;
71         com.sun.star.sheet.ResultEvent aEvent = getResult();
72 
73         java.util.Enumeration aEnum = aListeners.elements();
74         while (aEnum.hasMoreElements())
75             ((com.sun.star.sheet.XResultListener)aEnum.nextElement()).modified(
76                 aEvent);
77     }
78 }
79 
80 class ExampleAddInThread extends Thread
81 {
82     private java.util.Hashtable aCounters;
83 
84     public ExampleAddInThread( java.util.Hashtable aResults )
85     {
86         aCounters = aResults;
87     }
88 
89     public void run()
90     {
91         while ( true )
92         {
93             try
94             {
95                 sleep(1000);
96             }
97             catch( InterruptedException exception )
98             {
99             }
100 
101             // increment all counters
102             java.util.Enumeration aEnum = aCounters.elements();
103             while (aEnum.hasMoreElements())
104                 ((ExampleAddInResult)aEnum.nextElement()).incrementValue();
105         }
106     }
107 }
108 
109 public class ExampleAddIn
110 {
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
116     {
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();
120 
121         private static final short FUNCTION_INVALID   = -1;
122         private static final short FUNCTION_INCREMENT = 0;
123         private static final short FUNCTION_COUNTER   = 1;
124 
125         private static final String[] aFunctionNames =
126         {
127             "getIncremented",
128             "getCounter"
129         };
130         private static final String[] aDisplayFunctionNames =
131         {
132             "Increment",
133             "Counter"
134         };
135         private static final String[] aDescriptions =
136         {
137             "Increments a value",
138             "Returns a counter"
139         };
140         private static final String[] aFirstArgumentNames =
141         {
142             "Value",
143             "Name"
144         };
145         private static final String[] aFirstArgumentDescriptions =
146         {
147             "The value that is incremented",
148             "The name of the counter"
149         };
150 
151         private com.sun.star.lang.Locale aFuncLocale;
152         private java.util.Hashtable aResults;
153 
154         public _ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )
155         {
156         }
157 
158         private int getFunctionID( String aProgrammaticFunctionName )
159         {
160             for ( int i = 0; i < aFunctionNames.length; i++ )
161                 if ( aProgrammaticFunctionName.equals(aFunctionNames[i]) )
162                     return i;
163             return FUNCTION_INVALID;
164         }
165 
166         //  XExampleAddIn
167 
168         public int getIncremented( int nValue )
169         {
170             return nValue + 1;
171         }
172 
173         public com.sun.star.sheet.XVolatileResult getCounter(String aName)
174         {
175             if ( aResults == null )
176             {
177                 // create the table of results, and start a thread to increment
178                 // all counters
179                 aResults = new java.util.Hashtable();
180                 ExampleAddInThread aThread = new ExampleAddInThread( aResults );
181                 aThread.start();
182             }
183 
184             ExampleAddInResult aResult = (ExampleAddInResult) aResults.get(aName);
185             if ( aResult == null )
186             {
187                 aResult = new ExampleAddInResult(aName);
188                 aResults.put( aName, aResult );
189             }
190             return aResult;
191         }
192 
193         //  XAddIn
194 
195         public String getProgrammaticFuntionName(String aDisplayName)
196         {
197             for ( int i = 0; i < aFunctionNames.length; i++ )
198                 if ( aDisplayName.equals(aDisplayFunctionNames[i]) )
199                     return aFunctionNames[i];
200             return "";
201         }
202 
203         public String getDisplayFunctionName(String aProgrammaticName)
204         {
205             int nFunction = getFunctionID( aProgrammaticName );
206             return ( nFunction == FUNCTION_INVALID ) ? "" :
207                 aDisplayFunctionNames[nFunction];
208         }
209 
210         public String getFunctionDescription(String aProgrammaticName)
211         {
212             int nFunction = getFunctionID( aProgrammaticName );
213             return ( nFunction == FUNCTION_INVALID ) ? "" :
214                 aDescriptions[nFunction];
215         }
216 
217         public String getDisplayArgumentName(String aProgrammaticFunctionName,
218                                              int nArgument)
219         {
220             //  both functions in this example only have a first argument
221             int nFunction = getFunctionID( aProgrammaticFunctionName );
222             return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
223                 aFirstArgumentNames[nFunction];
224         }
225 
226         public String getArgumentDescription(String aProgrammaticFunctionName,
227                                              int nArgument )
228         {
229             //  both functions in this example only have a first argument
230             int nFunction = getFunctionID( aProgrammaticFunctionName );
231             return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
232                 aFirstArgumentDescriptions[nFunction];
233         }
234 
235         public String getProgrammaticCategoryName(String aProgrammaticFunctionName)
236         {
237             return( "Add-In" );
238         }
239 
240         public String getDisplayCategoryName(String aProgrammaticFunctionName)
241         {
242             return( "Add-In" );
243         }
244 
245         //  XLocalizable
246 
247         public void setLocale( com.sun.star.lang.Locale aLocale )
248         {
249             // the locale is stored and used for getLocale, but otherwise
250             // ignored in this example
251             aFuncLocale = aLocale;
252         }
253 
254         public com.sun.star.lang.Locale getLocale()
255         {
256             return aFuncLocale;
257         }
258 
259         //  XServiceName
260 
261         public String getServiceName()
262         {
263             return aExampleService;
264         }
265 
266         //  XServiceInfo
267 
268         public String getImplementationName()
269         {
270             return aImplName;
271         }
272 
273         public String[] getSupportedServiceNames()
274         {
275             String [] aSupportedServices = new String[ 2 ];
276             aSupportedServices[ 0 ] = aExampleService;
277             aSupportedServices[ 1 ] = aAddInService;
278             return aSupportedServices;
279         }
280 
281         public boolean supportsService( String aService )
282         {
283             return (aService.equals( aExampleService ) ||
284                     aService.equals( aAddInService ) );
285         }
286 
287     }
288 
289 
290     public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
291         String implName,
292         com.sun.star.lang.XMultiServiceFactory multiFactory,
293         com.sun.star.registry.XRegistryKey regKey)
294     {
295         com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
296         if ( implName.equals(_ExampleAddIn.aImplName) )
297             xSingleServiceFactory =
298                 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
299                     _ExampleAddIn.class, _ExampleAddIn.aExampleService,
300                     multiFactory, regKey);
301         return xSingleServiceFactory;
302     }
303 
304     // This method not longer necessary since OOo 3.4 where the component registration
305     // was changed to passive component registration. For more details see
306     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
307 
308 //     public static boolean __writeRegistryServiceInfo(
309 //         com.sun.star.registry.XRegistryKey regKey)
310 //     {
311 //         //  register for both the base AddIn and the own service
312 //         return com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
313 //                     _ExampleAddIn.aImplName, _ExampleAddIn.aExampleService, regKey)
314 //             && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
315 //                     _ExampleAddIn.aImplName, _ExampleAddIn.aAddInService, regKey);
316 //     }
317 }
318 
319