1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  The Contents of this file are made available subject to the terms of
5  *  the BSD license.
6  *
7  *  Copyright 2000, 2010 Oracle and/or its affiliates.
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *  1. Redistributions of source code must retain the above copyright
14  *     notice, this list of conditions and the following disclaimer.
15  *  2. Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in the
17  *     documentation and/or other materials provided with the distribution.
18  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
19  *     contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *************************************************************************/
35 
36 // Template for an Office Calc add-in Java implementation file.
37 
38 /** You can find more
39  * information on the following web page:
40  * https://api.libreoffice.org/
41  */
42 import com.sun.star.comp.loader.FactoryHelper;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.lang.XSingleServiceFactory;
45 import com.sun.star.registry.XRegistryKey;
46 import com.sun.star.lib.uno.helper.WeakBase;
47 import com.sun.star.lang.XServiceInfo;
48 import com.sun.star.lang.XServiceName;
49 import com.sun.star.sheet.XAddIn;
50 import com.sun.star.lang.Locale;
51 import org.openoffice.sheet.addin.XCalcAddins;
52 
53 /** This outer class provides an inner class to implement the service
54  * description, a method to instantiate the
55  * component on demand (__getServiceFactory()), and a method to give
56  * information about the component (__writeRegistryServiceInfo()).
57  */
58 public class CalcAddins {
59 
60 /** This inner class provides the component as a concrete implementation
61  * of the service description. It implements the needed interfaces.
62  * @implements XCalcAddins, XAddIn, XServiceName, XServiceInfo, XTypeProvider
63  */
64     public static class _CalcAddins extends WeakBase implements
65                                             XCalcAddins,
66                                             XAddIn,
67                                             XServiceName,
68                                             XServiceInfo
69     {
70 
71 /** The component will be registered under this name.
72  */
73         private static final String __serviceName = "org.openoffice.sheet.addin.CalcAddins";
74 
75         private static final String ADDIN_SERVICE = "com.sun.star.sheet.AddIn";
76 
77         private Locale aFuncLoc;
78 
79         private static final String[] stringFunctionName = {
80 /** TO DO:
81  * You should replace these method names by the method names of your interface.
82  */
83             "getMyFirstValue",
84             "getMySecondValue"
85         };
86 
87 /** TO DO:
88  * For each of your methods you should make up a new constant with a different value.
89  */
90         private static final short shortGETMYFIRSTVALUE = 0;
91         private static final short shortGETMYSECONDVALUE = 1;
92 
93 /** TO DO:
94  * This is where you implement all methods of your interface. The parameters have to
95  * be the same as in your IDL file and their types have to be the correct
96  * IDL-to-Java mappings of their types in the IDL file.
97  */
getMyFirstValue( com.sun.star.beans.XPropertySet xOptions )98         public int getMyFirstValue(
99             com.sun.star.beans.XPropertySet xOptions
100         ) {
101             return 1;
102         }
103 
getMySecondValue( com.sun.star.beans.XPropertySet xOptions, int intDummy )104         public int getMySecondValue(
105             com.sun.star.beans.XPropertySet xOptions,
106             int intDummy
107         ) {
108             return 2 + intDummy;
109         }
110 
111         // Implement method from interface XServiceName
getServiceName()112         public String getServiceName() {
113             return __serviceName;
114         }
115 
116         // Implement methods from interface XServiceInfo
supportsService(String stringServiceName)117         public boolean supportsService(String stringServiceName) {
118             return( stringServiceName.equals( ADDIN_SERVICE ) ||
119                     stringServiceName.equals( __serviceName ) );
120         }
121 
getImplementationName()122         public String getImplementationName() {
123             return _CalcAddins.class.getName();
124         }
125 
getSupportedServiceNames()126         public String[] getSupportedServiceNames() {
127             String[] stringSupportedServiceNames = { ADDIN_SERVICE, __serviceName };
128             return stringSupportedServiceNames;
129         }
130 
131         // Implement methods from interface XAddIn
getDisplayArgumentName(String stringProgrammaticFunctionName,int intArgument)132         public String getDisplayArgumentName(String stringProgrammaticFunctionName,int intArgument) {
133             String stringReturn = "";
134 
135             switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
136 /** TO DO:
137  * You should list all argument names for each of your methods, here.
138  */
139                 case shortGETMYFIRSTVALUE:
140                     switch( intArgument ) {
141                         case 0:
142                             stringReturn = "(internal)";
143                             break;
144                     }
145                     break;
146                 case shortGETMYSECONDVALUE:
147                     switch( intArgument ) {
148                         case 0:
149                             stringReturn = "(internal)";
150                             break;
151                         case 1:
152                             stringReturn = "intDummy";
153                             break;
154                     }
155                     break;
156             }
157             return stringReturn;
158         }
159 
getDisplayFunctionName(String stringProgrammaticName)160         public String getDisplayFunctionName(String stringProgrammaticName) {
161             String stringReturn = "";
162 
163             switch( this.getFunctionID( stringProgrammaticName ) ) {
164 /** TO DO:
165  * Assign the name of each of your methods.
166  */
167                 case shortGETMYFIRSTVALUE:
168                     stringReturn = "getMyFirstValue";
169                     break;
170                 case shortGETMYSECONDVALUE:
171                     stringReturn = "getMySecondValue";
172                     break;
173             }
174 
175             return stringReturn;
176         }
177 
getProgrammaticCategoryName(String p1)178         public String getProgrammaticCategoryName(String p1) {
179             return "Add-In";
180         }
181 
getDisplayCategoryName(String p1)182         public String getDisplayCategoryName(String p1) {
183             return "Add-In";
184         }
185 
getFunctionDescription(String stringProgrammaticName)186         public String getFunctionDescription(String stringProgrammaticName) {
187             String stringReturn = "";
188 
189             switch( this.getFunctionID( stringProgrammaticName ) ) {
190 /** TO DO:
191  * Enter a description for each of your methods that office users will understand.
192  */
193                 case shortGETMYFIRSTVALUE:
194                     stringReturn = "This is your first method.";
195                     break;
196                 case shortGETMYSECONDVALUE:
197                     stringReturn = "This is your second method.";
198                     break;
199             }
200 
201             return stringReturn;
202         }
203 
getArgumentDescription(String stringProgrammaticFunctionName,int intArgument)204         public String getArgumentDescription(String stringProgrammaticFunctionName,int intArgument) {
205             String stringReturn = "";
206 
207             switch( this.getFunctionID( stringProgrammaticFunctionName ) ) {
208 /** TO DO:
209  * Enter a description for every argument of every method. Make them so that office users will understand.
210  */
211                 case shortGETMYFIRSTVALUE:
212                     switch( intArgument ) {
213                         case 0:
214                             stringReturn = "(internal)";
215                             break;
216                     }
217                     break;
218                 case shortGETMYSECONDVALUE:
219                     switch( intArgument ) {
220                         case 0:
221                             stringReturn = "(internal)";
222                             break;
223                         case 1:
224                             stringReturn = "You can add this value.";
225                             break;
226                     }
227                     break;
228             }
229             return stringReturn;
230         }
231 
getProgrammaticFuntionName(String p1)232         public String getProgrammaticFuntionName(String p1) {
233             return "";
234         }
235 
236         // Implement methods from interface XLocalizable
getLocale()237         public Locale getLocale() {
238             return aFuncLoc;
239         }
240 
setLocale(Locale p1)241         public void setLocale(Locale p1) {
242             aFuncLoc = p1;
243         }
244 
245         // Auxiliary functions
getFunctionID( String stringProgrammaticName )246         private short getFunctionID( String stringProgrammaticName ) {
247             for ( int i = 0; i < stringFunctionName.length; i++ ) {
248                 if ( stringProgrammaticName.equals( stringFunctionName[ i ] ) ) {
249                     return ( short ) i;
250                 }
251             }
252 
253             return -1;
254         }
255     }
256 
257     /**
258      * Returns a factory for creating the service.
259      * This method is called by the <code>JavaLoader</code>
260      * <p>
261      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
262      * @param   implName     the name of the implementation for which a service is desired
263      * @param   multiFactory the service manager to be used if needed
264      * @param   regKey       the registryKey
265      * @see                  com.sun.star.comp.loader.JavaLoader
266      */
__getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)267     public static XSingleServiceFactory __getServiceFactory(String implName,
268     XMultiServiceFactory multiFactory,
269     XRegistryKey regKey) {
270         XSingleServiceFactory xSingleServiceFactory = null;
271 
272         if (implName.equals(_CalcAddins.class.getName()) )
273             xSingleServiceFactory = FactoryHelper.getServiceFactory(_CalcAddins.class,
274             _CalcAddins.__serviceName,
275             multiFactory,
276             regKey);
277 
278         return xSingleServiceFactory;
279     }
280 
281 }
282 
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
284