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 // Template for an Office Calc add-in Java implementation file.
36 
37 /** You can find more
38  * information on the following web page:
39  * http://api.openoffice.org/common/ref/com/sun/star/index.html
40  */
41 import com.sun.star.comp.loader.FactoryHelper;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.lang.XSingleServiceFactory;
44 import com.sun.star.registry.XRegistryKey;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.lib.uno.helper.WeakBase;
47 import com.sun.star.lang.XInitialization;
48 import com.sun.star.beans.XPropertySet;
49 import com.sun.star.lang.XServiceInfo;
50 import com.sun.star.lang.XServiceName;
51 import com.sun.star.sheet.XAddIn;
52 import com.sun.star.lang.Locale;
53 import com.sun.star.lang.XTypeProvider;
54 import com.sun.star.uno.Type;
55 
56 import org.openoffice.sheet.addin.XCalcAddins;
57 
58 /** This outer class provides an inner class to implement the service
59  * description, a method to instantiate the
60  * component on demand (__getServiceFactory()), and a method to give
61  * information about the component (__writeRegistryServiceInfo()).
62  */
63 public class CalcAddins {
64 
65 /** This inner class provides the component as a concrete implementation
66  * of the service description. It implements the needed interfaces.
67  * @implements XCalcAddins, XAddIn, XServiceName, XServiceInfo, XTypeProvider
68  */
69     static public class _CalcAddins extends WeakBase implements
70                                             XCalcAddins,
71                                             XAddIn,
72                                             XServiceName,
73                                             XServiceInfo
74     {
75 
76 /** The component will be registered under this name.
77  */
78         static private final String __serviceName = "org.openoffice.sheet.addin.CalcAddins";
79 
80         static private final String ADDIN_SERVICE = "com.sun.star.sheet.AddIn";
81 
82         private Locale aFuncLoc;
83 
84         private static final String[] stringFunctionName = {
85 /** TO DO:
86  * You should replace these method names by the method names of your interface.
87  */
88             "getMyFirstValue",
89             "getMySecondValue"
90         };
91 
92         private static final short shortINVALID = -1;
93 
94 /** TO DO:
95  * For each of your methods you should make up a new constant with a different value.
96  */
97         private static final short shortGETMYFIRSTVALUE = 0;
98         private static final short shortGETMYSECONDVALUE = 1;
99 
100 
101 /** TO DO:
102  * This is where you implement all methods of your interface. The parameters have to
103  * be the same as in your IDL file and their types have to be the correct
104  * IDL-to-Java mappings of their types in the IDL file.
105  */
106         public int getMyFirstValue(
107             com.sun.star.beans.XPropertySet xOptions
108         ) {
109             return (int) 1;
110         }
111 
112         public int getMySecondValue(
113             com.sun.star.beans.XPropertySet xOptions,
114             int intDummy
115         ) {
116             return( (int) ( 2 + intDummy ) );
117         }
118 
119 
120         // Implement method from interface XServiceName
121         public String getServiceName() {
122             return( __serviceName );
123         }
124 
125         // Implement methods from interface XServiceInfo
126         public boolean supportsService(String stringServiceName) {
127             return( stringServiceName.equals( ADDIN_SERVICE ) ||
128                     stringServiceName.equals( __serviceName ) );
129         }
130 
131         public String getImplementationName() {
132             return( _CalcAddins.class.getName() );
133         }
134 
135         public String[] getSupportedServiceNames() {
136             String[] stringSupportedServiceNames = { ADDIN_SERVICE, __serviceName };
137             return( stringSupportedServiceNames );
138         }
139 
140         // Implement methods from interface XAddIn
141         public String getDisplayArgumentName(String stringProgrammaticFunctionName,int intArgument) {
142             String stringReturn = "";
143 
144             switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
145 /** TO DO:
146  * You should list all argument names for each of your methods, here.
147  */
148                 case shortGETMYFIRSTVALUE:
149                     switch( intArgument ) {
150                         case 0:
151                             stringReturn = "(internal)";
152                             break;
153                     }
154                     break;
155                 case shortGETMYSECONDVALUE:
156                     switch( intArgument ) {
157                         case 0:
158                             stringReturn = "(internal)";
159                             break;
160                         case 1:
161                             stringReturn = "intDummy";
162                             break;
163                     }
164                     break;
165             }
166             return( stringReturn );
167         }
168 
169         public String getDisplayFunctionName(String stringProgrammaticName) {
170             String stringReturn = "";
171 
172             switch( this.getFunctionID( stringProgrammaticName ) ) {
173 /** TO DO:
174  * Assign the name of each of your methods.
175  */
176                 case shortGETMYFIRSTVALUE:
177                     stringReturn = "getMyFirstValue";
178                     break;
179                 case shortGETMYSECONDVALUE:
180                     stringReturn = "getMySecondValue";
181                     break;
182             }
183 
184             return( stringReturn );
185         }
186 
187         public String getProgrammaticCategoryName(String p1) {
188             return( "Add-In" );
189         }
190 
191         public String getDisplayCategoryName(String p1) {
192             return( "Add-In" );
193         }
194 
195         public String getFunctionDescription(String stringProgrammaticName) {
196             String stringReturn = "";
197 
198             switch( this.getFunctionID( stringProgrammaticName ) ) {
199 /** TO DO:
200  * Enter a description for each of your methods that office users will understand.
201  */
202                 case shortGETMYFIRSTVALUE:
203                     stringReturn = "This is your first method.";
204                     break;
205                 case shortGETMYSECONDVALUE:
206                     stringReturn = "This is your second method.";
207                     break;
208             }
209 
210             return( stringReturn );
211         }
212 
213         public String getArgumentDescription(String stringProgrammaticFunctionName,int intArgument) {
214             String stringReturn = "";
215 
216             switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
217 /** TO DO:
218  * Enter a description for every argument of every method. Make them so that office users will understand.
219  */
220                 case shortGETMYFIRSTVALUE:
221                     switch( intArgument ) {
222                         case 0:
223                             stringReturn = "(internal)";
224                             break;
225                     }
226                     break;
227                 case shortGETMYSECONDVALUE:
228                     switch( intArgument ) {
229                         case 0:
230                             stringReturn = "(internal)";
231                             break;
232                         case 1:
233                             stringReturn = "You can add this value.";
234                             break;
235                     }
236                     break;
237             }
238             return( stringReturn );
239         }
240 
241         public String getProgrammaticFuntionName(String p1) {
242             return( "" );
243         }
244 
245         // Implement methods from interface XLocalizable
246         public Locale getLocale() {
247             return( aFuncLoc );
248         }
249 
250         public void setLocale(Locale p1) {
251             aFuncLoc = p1;
252         }
253 
254         // Auxiliary functions
255         private short getFunctionID( String stringProgrammaticName ) {
256             for ( int i = 0; i < stringFunctionName.length; i++ ) {
257                 if ( stringProgrammaticName.equals( stringFunctionName[ i ] ) ) {
258                     return( ( short ) i );
259                 }
260             }
261 
262             return( -1 );
263         }
264     }
265 
266     /**
267      * Returns a factory for creating the service.
268      * This method is called by the <code>JavaLoader</code>
269      * <p>
270      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
271      * @param   implName     the name of the implementation for which a service is desired
272      * @param   multiFactory the service manager to be used if needed
273      * @param   regKey       the registryKey
274      * @see                  com.sun.star.comp.loader.JavaLoader
275      */
276     public static XSingleServiceFactory __getServiceFactory(String implName,
277     XMultiServiceFactory multiFactory,
278     XRegistryKey regKey) {
279         XSingleServiceFactory xSingleServiceFactory = null;
280 
281         if (implName.equals(_CalcAddins.class.getName()) )
282             xSingleServiceFactory = FactoryHelper.getServiceFactory(_CalcAddins.class,
283             _CalcAddins.__serviceName,
284             multiFactory,
285             regKey);
286 
287         return xSingleServiceFactory;
288     }
289 
290     /**
291      * Writes the service information into the given registry key.
292      * This method is called by the <code>JavaLoader</code>
293      * <p>
294      * @return  returns true if the operation succeeded
295      * @param   regKey       the registryKey
296      * @see                  com.sun.star.comp.loader.JavaLoader
297      */
298     // This method not longer necessary since OOo 3.4 where the component registration
299     // was changed to passive component registration. For more details see
300     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
301 
302 //     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
303 //         return FactoryHelper.writeRegistryServiceInfo(_CalcAddins.class.getName(),
304 //         _CalcAddins.__serviceName, regKey)
305 //         && FactoryHelper.writeRegistryServiceInfo(_CalcAddins.class.getName(),
306 //         _CalcAddins.ADDIN_SERVICE, regKey);
307 //     }
308 }
309