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