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 import com.sun.star.uno.Type; 36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 38*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 39*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider; 40*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 41*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 42*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 43*cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 44*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 45*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory; 46*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper; 47*cdf0e10cSrcweir import com.sun.star.awt.XDialog; 48*cdf0e10cSrcweir import com.sun.star.awt.XDialogProvider2; 49*cdf0e10cSrcweir import com.sun.star.awt.XDialogEventHandler; 50*cdf0e10cSrcweir import com.sun.star.awt.XControl; 51*cdf0e10cSrcweir import com.sun.star.awt.XControlModel; 52*cdf0e10cSrcweir import com.sun.star.awt.XControlContainer; 53*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 54*cdf0e10cSrcweir import com.sun.star.frame.XModel; 55*cdf0e10cSrcweir import com.sun.star.frame.XFrame; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir import com.sun.star.awt.XToolkit; 58*cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer; 59*cdf0e10cSrcweir import com.sun.star.awt.XMessageBox; 60*cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute; 61*cdf0e10cSrcweir import com.sun.star.awt.WindowClass; 62*cdf0e10cSrcweir import com.sun.star.awt.WindowDescriptor; 63*cdf0e10cSrcweir import com.sun.star.awt.Rectangle; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir import com.sun.star.test.XTestDialogHandler; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir // DialogComponent implements all necessary interfaces self, this is only 68*cdf0e10cSrcweir // for demonstration. More convenient is to use the impelmentation WeakBase or 69*cdf0e10cSrcweir // ComponentBase, see implementation of TestComponentA. 70*cdf0e10cSrcweir public class DialogComponent { 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir // public static class _DialogComponent extends WeakBase 73*cdf0e10cSrcweir public static class _DialogComponent 74*cdf0e10cSrcweir implements XTypeProvider, XServiceInfo, XTestDialogHandler, XDialogEventHandler { 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir static final String __serviceName= "com.sun.star.test.TestDialogHandler"; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir static byte[] _implementationId; 79*cdf0e10cSrcweir private XComponentContext m_xCmpCtx; 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir private XFrame m_xFrame; 82*cdf0e10cSrcweir private XToolkit m_xToolkit; 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir public _DialogComponent(XComponentContext context) { 85*cdf0e10cSrcweir m_xCmpCtx= context; 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir try { 88*cdf0e10cSrcweir // Create the toolkit to have access to it later 89*cdf0e10cSrcweir m_xToolkit = (XToolkit) UnoRuntime.queryInterface( 90*cdf0e10cSrcweir XToolkit.class, 91*cdf0e10cSrcweir m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit", 92*cdf0e10cSrcweir m_xCmpCtx)); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir catch (Exception e) { 95*cdf0e10cSrcweir e.printStackTrace(); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // XTestDialogHandler 100*cdf0e10cSrcweir public String createDialog( String DialogURL, XModel xModel, XFrame xFrame ) { 101*cdf0e10cSrcweir m_xFrame = xFrame; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir try { 104*cdf0e10cSrcweir XMultiComponentFactory xMCF = m_xCmpCtx.getServiceManager(); 105*cdf0e10cSrcweir Object obj; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir // If valid we must pass the XModel when creating a DialogProvider object 108*cdf0e10cSrcweir if( xModel != null ) { 109*cdf0e10cSrcweir Object[] args = new Object[1]; 110*cdf0e10cSrcweir args[0] = xModel; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir obj = xMCF.createInstanceWithArgumentsAndContext( 113*cdf0e10cSrcweir "com.sun.star.awt.DialogProvider2", args, m_xCmpCtx ); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir else { 116*cdf0e10cSrcweir obj = xMCF.createInstanceWithContext( 117*cdf0e10cSrcweir "com.sun.star.awt.DialogProvider2", m_xCmpCtx ); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir XDialogProvider2 xDialogProvider = (XDialogProvider2) 121*cdf0e10cSrcweir UnoRuntime.queryInterface( XDialogProvider2.class, obj ); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir XDialog xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this ); 124*cdf0e10cSrcweir if( xDialog != null ) 125*cdf0e10cSrcweir xDialog.execute(); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir catch (Exception e) { 128*cdf0e10cSrcweir e.printStackTrace(); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir return "Created dialog \"" + DialogURL + "\""; 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir public void copyText( XDialog xDialog, Object aEventObject ) { 134*cdf0e10cSrcweir XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface( 135*cdf0e10cSrcweir XControlContainer.class, xDialog ); 136*cdf0e10cSrcweir String aTextPropertyStr = "Text"; 137*cdf0e10cSrcweir String aText = ""; 138*cdf0e10cSrcweir XControl xTextField1Control = xControlContainer.getControl( "TextField1" ); 139*cdf0e10cSrcweir XControlModel xControlModel1 = xTextField1Control.getModel(); 140*cdf0e10cSrcweir XPropertySet xPropertySet1 = (XPropertySet)UnoRuntime.queryInterface( 141*cdf0e10cSrcweir XPropertySet.class, xControlModel1 ); 142*cdf0e10cSrcweir try 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir aText = (String)xPropertySet1.getPropertyValue( aTextPropertyStr ); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir catch (Exception e) { 147*cdf0e10cSrcweir e.printStackTrace(); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir XControl xTextField2Control = xControlContainer.getControl( "TextField2" ); 151*cdf0e10cSrcweir XControlModel xControlModel2 = xTextField2Control.getModel(); 152*cdf0e10cSrcweir XPropertySet xPropertySet2 = (XPropertySet)UnoRuntime.queryInterface( 153*cdf0e10cSrcweir XPropertySet.class, xControlModel2 ); 154*cdf0e10cSrcweir try 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir xPropertySet2.setPropertyValue( aTextPropertyStr, aText ); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir catch (Exception e) { 159*cdf0e10cSrcweir e.printStackTrace(); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir showMessageBox( "DialogComponent", "copyText() called" ); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir public void handleEvent() { 166*cdf0e10cSrcweir showMessageBox( "DialogComponent", "handleEvent() called" ); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir public void handleEventWithArguments( XDialog xDialog, Object aEventObject ) { 170*cdf0e10cSrcweir showMessageBox( "DialogComponent", "handleEventWithArguments() called\n\n" + 171*cdf0e10cSrcweir "Event Object = " + aEventObject ); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir private final String aHandlerMethod1 = "doit1"; 175*cdf0e10cSrcweir private final String aHandlerMethod2 = "doit2"; 176*cdf0e10cSrcweir private final String aHandlerMethod3 = "doit3"; 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir //XDialogEventHandler 179*cdf0e10cSrcweir public boolean callHandlerMethod( /*IN*/XDialog xDialog, /*IN*/Object EventObject, /*IN*/String MethodName ) { 180*cdf0e10cSrcweir if ( MethodName.equals( aHandlerMethod1 ) ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod1 + "\"" ); 183*cdf0e10cSrcweir return true; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir else if ( MethodName.equals( aHandlerMethod2 ) ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod2 + "\"" ); 188*cdf0e10cSrcweir return true; 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir else if ( MethodName.equals( aHandlerMethod3 ) ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod3 + "\"" ); 193*cdf0e10cSrcweir return true; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir return false; 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir public String[] getSupportedMethodNames() { 199*cdf0e10cSrcweir String[] retValue= new String[3]; 200*cdf0e10cSrcweir retValue[0]= aHandlerMethod1; 201*cdf0e10cSrcweir retValue[1]= aHandlerMethod2; 202*cdf0e10cSrcweir retValue[2]= aHandlerMethod3; 203*cdf0e10cSrcweir return retValue; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir //XTypeProvider 208*cdf0e10cSrcweir public com.sun.star.uno.Type[] getTypes( ) { 209*cdf0e10cSrcweir Type[] retValue= new Type[4]; 210*cdf0e10cSrcweir retValue[0]= new Type( XServiceInfo.class); 211*cdf0e10cSrcweir retValue[1]= new Type( XTypeProvider.class); 212*cdf0e10cSrcweir retValue[2]= new Type( XTestDialogHandler.class); 213*cdf0e10cSrcweir retValue[3]= new Type( XDialogEventHandler.class); 214*cdf0e10cSrcweir return retValue; 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir //XTypeProvider 217*cdf0e10cSrcweir synchronized public byte[] getImplementationId( ) { 218*cdf0e10cSrcweir if (_implementationId == null) { 219*cdf0e10cSrcweir _implementationId= new byte[16]; 220*cdf0e10cSrcweir int hash = hashCode(); 221*cdf0e10cSrcweir _implementationId[0] = (byte)(hash & 0xff); 222*cdf0e10cSrcweir _implementationId[1] = (byte)((hash >>> 8) & 0xff); 223*cdf0e10cSrcweir _implementationId[2] = (byte)((hash >>> 16) & 0xff); 224*cdf0e10cSrcweir _implementationId[3] = (byte)((hash >>>24) & 0xff); 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir return _implementationId; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir /** This method is a simple helper function to used in the 232*cdf0e10cSrcweir * static component initialisation functions as well as in 233*cdf0e10cSrcweir * getSupportedServiceNames. 234*cdf0e10cSrcweir */ 235*cdf0e10cSrcweir public static String[] getServiceNames() { 236*cdf0e10cSrcweir String[] sSupportedServiceNames = { __serviceName }; 237*cdf0e10cSrcweir return sSupportedServiceNames; 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir //XServiceInfo 241*cdf0e10cSrcweir public String[] getSupportedServiceNames() { 242*cdf0e10cSrcweir return getServiceNames(); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir //XServiceInfo 246*cdf0e10cSrcweir public boolean supportsService( String sServiceName ) { 247*cdf0e10cSrcweir return sServiceName.equals( __serviceName ); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir //XServiceInfo 251*cdf0e10cSrcweir public String getImplementationName() { 252*cdf0e10cSrcweir // return DialogComponent.class.getName(); 253*cdf0e10cSrcweir return _DialogComponent.class.getName(); 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir public void showMessageBox(String sTitle, String sMessage) { 257*cdf0e10cSrcweir try { 258*cdf0e10cSrcweir if ( null != m_xFrame && null != m_xToolkit ) { 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir // describe window properties. 261*cdf0e10cSrcweir WindowDescriptor aDescriptor = new WindowDescriptor(); 262*cdf0e10cSrcweir aDescriptor.Type = WindowClass.MODALTOP; 263*cdf0e10cSrcweir aDescriptor.WindowServiceName = new String( "infobox" ); 264*cdf0e10cSrcweir aDescriptor.ParentIndex = -1; 265*cdf0e10cSrcweir aDescriptor.Parent = (XWindowPeer)UnoRuntime.queryInterface( 266*cdf0e10cSrcweir XWindowPeer.class, m_xFrame.getContainerWindow()); 267*cdf0e10cSrcweir aDescriptor.Bounds = new Rectangle(0,0,300,200); 268*cdf0e10cSrcweir aDescriptor.WindowAttributes = WindowAttribute.BORDER | 269*cdf0e10cSrcweir WindowAttribute.MOVEABLE | 270*cdf0e10cSrcweir WindowAttribute.CLOSEABLE; 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor ); 273*cdf0e10cSrcweir if ( null != xPeer ) { 274*cdf0e10cSrcweir XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface( 275*cdf0e10cSrcweir XMessageBox.class, xPeer); 276*cdf0e10cSrcweir if ( null != xMsgBox ) 277*cdf0e10cSrcweir { 278*cdf0e10cSrcweir xMsgBox.setCaptionText( sTitle ); 279*cdf0e10cSrcweir xMsgBox.setMessageText( sMessage ); 280*cdf0e10cSrcweir xMsgBox.execute(); 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e) { 285*cdf0e10cSrcweir // do your error handling 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir /** 291*cdf0e10cSrcweir * Gives a factory for creating the service. 292*cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 293*cdf0e10cSrcweir * <p> 294*cdf0e10cSrcweir * @return returns a <code>XSingleComponentFactory</code> for creating 295*cdf0e10cSrcweir * the component 296*cdf0e10cSrcweir * @param sImplName the name of the implementation for which a 297*cdf0e10cSrcweir * service is desired 298*cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 299*cdf0e10cSrcweir */ 300*cdf0e10cSrcweir public static XSingleComponentFactory __getComponentFactory(String sImplName) 301*cdf0e10cSrcweir { 302*cdf0e10cSrcweir XSingleComponentFactory xFactory = null; 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir if ( sImplName.equals( _DialogComponent.class.getName() ) ) 305*cdf0e10cSrcweir xFactory = Factory.createComponentFactory(_DialogComponent.class, 306*cdf0e10cSrcweir _DialogComponent.getServiceNames()); 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir return xFactory; 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir /** 312*cdf0e10cSrcweir * Writes the service information into the given registry key. 313*cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 314*cdf0e10cSrcweir * <p> 315*cdf0e10cSrcweir * @return returns true if the operation succeeded 316*cdf0e10cSrcweir * @param regKey the registryKey 317*cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 318*cdf0e10cSrcweir */ 319*cdf0e10cSrcweir // This method not longer necessary since OOo 3.4 where the component registration 320*cdf0e10cSrcweir // was changed to passive component registration. For more details see 321*cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir // public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { 324*cdf0e10cSrcweir // return Factory.writeRegistryServiceInfo(_DialogComponent.class.getName(), 325*cdf0e10cSrcweir // _DialogComponent.getServiceNames(), 326*cdf0e10cSrcweir // regKey); 327*cdf0e10cSrcweir // } 328*cdf0e10cSrcweir } 329