1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 class ExampleAddInResult implements com.sun.star.sheet.XVolatileResult
25 {
26     private String aName;
27     private int nValue;
28     private java.util.Vector aListeners = new java.util.Vector();
29 
ExampleAddInResult( String aNewName )30     public ExampleAddInResult( String aNewName )
31     {
32         aName = aNewName;
33     }
34 
getResult()35     private com.sun.star.sheet.ResultEvent getResult()
36     {
37         com.sun.star.sheet.ResultEvent aEvent =
38             new com.sun.star.sheet.ResultEvent();
39         aEvent.Value = aName + " " + String.valueOf( nValue );
40         aEvent.Source = this;
41         return aEvent;
42     }
43 
addResultListener(com.sun.star.sheet.XResultListener aListener)44     public void addResultListener(com.sun.star.sheet.XResultListener aListener)
45     {
46         aListeners.addElement( aListener );
47 
48         // immediately notify of initial value
49         aListener.modified( getResult() );
50     }
51 
removeResultListener(com.sun.star.sheet.XResultListener aListener)52     public void removeResultListener(com.sun.star.sheet.XResultListener aListener)
53     {
54         aListeners.removeElement( aListener );
55     }
56 
incrementValue()57     public void incrementValue()
58     {
59         ++nValue;
60         com.sun.star.sheet.ResultEvent aEvent = getResult();
61 
62         java.util.Enumeration aEnum = aListeners.elements();
63         while (aEnum.hasMoreElements())
64             ((com.sun.star.sheet.XResultListener)aEnum.nextElement()).modified(
65                 aEvent);
66     }
67 }
68 
69 class ExampleAddInThread extends Thread
70 {
71     private java.util.Hashtable aCounters;
72 
ExampleAddInThread( java.util.Hashtable aResults )73     public ExampleAddInThread( java.util.Hashtable aResults )
74     {
75         aCounters = aResults;
76     }
77 
run()78     public void run()
79     {
80         while ( true )
81         {
82             try
83             {
84                 sleep(1000);
85             }
86             catch( InterruptedException exception )
87             {
88             }
89 
90             // increment all counters
91             java.util.Enumeration aEnum = aCounters.elements();
92             while (aEnum.hasMoreElements())
93                 ((ExampleAddInResult)aEnum.nextElement()).incrementValue();
94         }
95     }
96 }
97 
98 public class ExampleAddIn
99 {
100     static public class _ExampleAddIn extends com.sun.star.lib.uno.helper.WeakBase
101            implements org.openoffice.sheet.addin.XExampleAddIn,
102                       com.sun.star.sheet.XAddIn,
103                       com.sun.star.lang.XServiceName,
104                       com.sun.star.lang.XServiceInfo
105     {
106         static private final String aExampleService = "org.openoffice.sheet.addin.ExampleAddIn";
107         static private final String aAddInService = "com.sun.star.sheet.AddIn";
108         static private final String aImplName = _ExampleAddIn.class.getName();
109 
110         private static final short FUNCTION_INVALID   = -1;
111         private static final short FUNCTION_INCREMENT = 0;
112         private static final short FUNCTION_COUNTER   = 1;
113 
114         private static final String[] aFunctionNames =
115         {
116             "getIncremented",
117             "getCounter"
118         };
119         private static final String[] aDisplayFunctionNames =
120         {
121             "Increment",
122             "Counter"
123         };
124         private static final String[] aDescriptions =
125         {
126             "Increments a value",
127             "Returns a counter"
128         };
129         private static final String[] aFirstArgumentNames =
130         {
131             "Value",
132             "Name"
133         };
134         private static final String[] aFirstArgumentDescriptions =
135         {
136             "The value that is incremented",
137             "The name of the counter"
138         };
139 
140         private com.sun.star.lang.Locale aFuncLocale;
141         private java.util.Hashtable aResults;
142 
_ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )143         public _ExampleAddIn( com.sun.star.lang.XMultiServiceFactory xFactory )
144         {
145         }
146 
getFunctionID( String aProgrammaticFunctionName )147         private int getFunctionID( String aProgrammaticFunctionName )
148         {
149             for ( int i = 0; i < aFunctionNames.length; i++ )
150                 if ( aProgrammaticFunctionName.equals(aFunctionNames[i]) )
151                     return i;
152             return FUNCTION_INVALID;
153         }
154 
155         //  XExampleAddIn
156 
getIncremented( int nValue )157         public int getIncremented( int nValue )
158         {
159             return nValue + 1;
160         }
161 
getCounter(String aName)162         public com.sun.star.sheet.XVolatileResult getCounter(String aName)
163         {
164             if ( aResults == null )
165             {
166                 // create the table of results, and start a thread to increment
167                 // all counters
168                 aResults = new java.util.Hashtable();
169                 ExampleAddInThread aThread = new ExampleAddInThread( aResults );
170                 aThread.start();
171             }
172 
173             ExampleAddInResult aResult = (ExampleAddInResult) aResults.get(aName);
174             if ( aResult == null )
175             {
176                 aResult = new ExampleAddInResult(aName);
177                 aResults.put( aName, aResult );
178             }
179             return aResult;
180         }
181 
182         //  XAddIn
183 
getProgrammaticFuntionName(String aDisplayName)184         public String getProgrammaticFuntionName(String aDisplayName)
185         {
186             for ( int i = 0; i < aFunctionNames.length; i++ )
187                 if ( aDisplayName.equals(aDisplayFunctionNames[i]) )
188                     return aFunctionNames[i];
189             return "";
190         }
191 
getDisplayFunctionName(String aProgrammaticName)192         public String getDisplayFunctionName(String aProgrammaticName)
193         {
194             int nFunction = getFunctionID( aProgrammaticName );
195             return ( nFunction == FUNCTION_INVALID ) ? "" :
196                 aDisplayFunctionNames[nFunction];
197         }
198 
getFunctionDescription(String aProgrammaticName)199         public String getFunctionDescription(String aProgrammaticName)
200         {
201             int nFunction = getFunctionID( aProgrammaticName );
202             return ( nFunction == FUNCTION_INVALID ) ? "" :
203                 aDescriptions[nFunction];
204         }
205 
getDisplayArgumentName(String aProgrammaticFunctionName, int nArgument)206         public String getDisplayArgumentName(String aProgrammaticFunctionName,
207                                              int nArgument)
208         {
209             //  both functions in this example only have a first argument
210             int nFunction = getFunctionID( aProgrammaticFunctionName );
211             return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
212                 aFirstArgumentNames[nFunction];
213         }
214 
getArgumentDescription(String aProgrammaticFunctionName, int nArgument )215         public String getArgumentDescription(String aProgrammaticFunctionName,
216                                              int nArgument )
217         {
218             //  both functions in this example only have a first argument
219             int nFunction = getFunctionID( aProgrammaticFunctionName );
220             return ( nFunction == FUNCTION_INVALID || nArgument != 0) ? "" :
221                 aFirstArgumentDescriptions[nFunction];
222         }
223 
getProgrammaticCategoryName(String aProgrammaticFunctionName)224         public String getProgrammaticCategoryName(String aProgrammaticFunctionName)
225         {
226             return( "Add-In" );
227         }
228 
getDisplayCategoryName(String aProgrammaticFunctionName)229         public String getDisplayCategoryName(String aProgrammaticFunctionName)
230         {
231             return( "Add-In" );
232         }
233 
234         //  XLocalizable
235 
setLocale( com.sun.star.lang.Locale aLocale )236         public void setLocale( com.sun.star.lang.Locale aLocale )
237         {
238             // the locale is stored and used for getLocale, but otherwise
239             // ignored in this example
240             aFuncLocale = aLocale;
241         }
242 
getLocale()243         public com.sun.star.lang.Locale getLocale()
244         {
245             return aFuncLocale;
246         }
247 
248         //  XServiceName
249 
getServiceName()250         public String getServiceName()
251         {
252             return aExampleService;
253         }
254 
255         //  XServiceInfo
256 
getImplementationName()257         public String getImplementationName()
258         {
259             return aImplName;
260         }
261 
getSupportedServiceNames()262         public String[] getSupportedServiceNames()
263         {
264             String [] aSupportedServices = new String[ 2 ];
265             aSupportedServices[ 0 ] = aExampleService;
266             aSupportedServices[ 1 ] = aAddInService;
267             return aSupportedServices;
268         }
269 
supportsService( String aService )270         public boolean supportsService( String aService )
271         {
272             return (aService.equals( aExampleService ) ||
273                     aService.equals( aAddInService ) );
274         }
275 
276     }
277 
278 
__getServiceFactory( String implName, com.sun.star.lang.XMultiServiceFactory multiFactory, com.sun.star.registry.XRegistryKey regKey)279     public static com.sun.star.lang.XSingleServiceFactory __getServiceFactory(
280         String implName,
281         com.sun.star.lang.XMultiServiceFactory multiFactory,
282         com.sun.star.registry.XRegistryKey regKey)
283     {
284         com.sun.star.lang.XSingleServiceFactory xSingleServiceFactory = null;
285         if ( implName.equals(_ExampleAddIn.aImplName) )
286             xSingleServiceFactory =
287                 com.sun.star.comp.loader.FactoryHelper.getServiceFactory(
288                     _ExampleAddIn.class, _ExampleAddIn.aExampleService,
289                     multiFactory, regKey);
290         return xSingleServiceFactory;
291     }
292 
293     // This method not longer necessary since OOo 3.4 where the component registration
294     // was changed to passive component registration. For more details see
295     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
296 
297 //     public static boolean __writeRegistryServiceInfo(
298 //         com.sun.star.registry.XRegistryKey regKey)
299 //     {
300 //         //  register for both the base AddIn and the own service
301 //         return com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
302 //                     _ExampleAddIn.aImplName, _ExampleAddIn.aExampleService, regKey)
303 //             && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
304 //                     _ExampleAddIn.aImplName, _ExampleAddIn.aAddInService, regKey);
305 //     }
306 }
307 
308