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