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 package com.sun.star.comp.sdk.examples;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.awt.ActionEvent;
38*cdf0e10cSrcweir import com.sun.star.awt.XActionListener;
39*cdf0e10cSrcweir import com.sun.star.awt.XButton;
40*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
41*cdf0e10cSrcweir import com.sun.star.awt.XControl;
42*cdf0e10cSrcweir import com.sun.star.awt.XControlModel;
43*cdf0e10cSrcweir import com.sun.star.awt.XControlContainer;
44*cdf0e10cSrcweir import com.sun.star.awt.XDialog;
45*cdf0e10cSrcweir import com.sun.star.awt.XFixedText;
46*cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
47*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
48*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
49*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
50*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
51*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
52*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
53*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
54*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
55*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
56*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
57*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
58*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
59*cdf0e10cSrcweir import com.sun.star.task.XJobExecutor;
60*cdf0e10cSrcweir import com.sun.star.uno.Type;
61*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
62*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir /** example of a Java component which creates a dialog at runtime
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     This component can be tested by the following StarOffice Basic code:
68*cdf0e10cSrcweir         Sub Main
69*cdf0e10cSrcweir 	        Dim oJobExecutor
70*cdf0e10cSrcweir 	        oJobExecutor = CreateUnoService( "com.sun.star.examples.SampleDialog" )
71*cdf0e10cSrcweir 	        oJobExecutor.trigger( "execute" )
72*cdf0e10cSrcweir         End Sub
73*cdf0e10cSrcweir */
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir public class SampleDialog extends WeakBase implements XServiceInfo, XJobExecutor {
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     static final String __serviceName = "com.sun.star.examples.SampleDialog";
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     private static final String _buttonName = "Button1";
80*cdf0e10cSrcweir     private static final String _cancelButtonName = "CancelButton";
81*cdf0e10cSrcweir     private static final String _labelName = "Label1";
82*cdf0e10cSrcweir     private static final String _labelPrefix = "Number of button clicks: ";
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     private XComponentContext _xComponentContext;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	public SampleDialog( XComponentContext xComponentContext ) {
87*cdf0e10cSrcweir         _xComponentContext = xComponentContext;
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     // static component operations
91*cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory( String implName,
92*cdf0e10cSrcweir                                                              XMultiServiceFactory multiFactory,
93*cdf0e10cSrcweir                                                              XRegistryKey regKey ) {
94*cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
95*cdf0e10cSrcweir         if ( implName.equals( SampleDialog.class.getName() ) ) {
96*cdf0e10cSrcweir             xSingleServiceFactory = FactoryHelper.getServiceFactory(
97*cdf0e10cSrcweir                 SampleDialog.class, SampleDialog.__serviceName, multiFactory, regKey );
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir         return xSingleServiceFactory;
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo( XRegistryKey regKey ) {
103*cdf0e10cSrcweir         return FactoryHelper.writeRegistryServiceInfo(
104*cdf0e10cSrcweir             SampleDialog.class.getName(), SampleDialog.__serviceName, regKey );
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     // XServiceInfo
108*cdf0e10cSrcweir 	public String getImplementationName(  ) {
109*cdf0e10cSrcweir 		return getClass().getName();
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	// XServiceInfo
113*cdf0e10cSrcweir 	public boolean supportsService( /*IN*/String serviceName ) {
114*cdf0e10cSrcweir 		if ( serviceName.equals( __serviceName))
115*cdf0e10cSrcweir 			return true;
116*cdf0e10cSrcweir 		return false;
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	// XServiceInfo
120*cdf0e10cSrcweir 	public String[] getSupportedServiceNames(  ) {
121*cdf0e10cSrcweir 		String[] retValue= new String[0];
122*cdf0e10cSrcweir 		retValue[0] = __serviceName;
123*cdf0e10cSrcweir 		return retValue;
124*cdf0e10cSrcweir 	}
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     // XJobExecutor
127*cdf0e10cSrcweir     public void trigger(String sEvent) {
128*cdf0e10cSrcweir 		if ( sEvent.compareTo( "execute" ) == 0 ) {
129*cdf0e10cSrcweir             try {
130*cdf0e10cSrcweir                 createDialog();
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir             catch ( Exception e ) {
133*cdf0e10cSrcweir                 throw new com.sun.star.lang.WrappedTargetRuntimeException( e.getMessage(), this, e );
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     /** method for creating a dialog at runtime
139*cdf0e10cSrcweir      */
140*cdf0e10cSrcweir     private void createDialog() throws com.sun.star.uno.Exception {
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         // get the service manager from the component context
143*cdf0e10cSrcweir         XMultiComponentFactory xMultiComponentFactory = _xComponentContext.getServiceManager();
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         // create the dialog model and set the properties
146*cdf0e10cSrcweir         Object dialogModel = xMultiComponentFactory.createInstanceWithContext(
147*cdf0e10cSrcweir             "com.sun.star.awt.UnoControlDialogModel", _xComponentContext );
148*cdf0e10cSrcweir         XPropertySet xPSetDialog = ( XPropertySet )UnoRuntime.queryInterface(
149*cdf0e10cSrcweir             XPropertySet.class, dialogModel );
150*cdf0e10cSrcweir         xPSetDialog.setPropertyValue( "PositionX", new Integer( 100 ) );
151*cdf0e10cSrcweir         xPSetDialog.setPropertyValue( "PositionY", new Integer( 100 ) );
152*cdf0e10cSrcweir         xPSetDialog.setPropertyValue( "Width", new Integer( 150 ) );
153*cdf0e10cSrcweir         xPSetDialog.setPropertyValue( "Height", new Integer( 100 ) );
154*cdf0e10cSrcweir         xPSetDialog.setPropertyValue( "Title", new String( "Runtime Dialog Demo" ) );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         // get the service manager from the dialog model
157*cdf0e10cSrcweir         XMultiServiceFactory xMultiServiceFactory = ( XMultiServiceFactory )UnoRuntime.queryInterface(
158*cdf0e10cSrcweir             XMultiServiceFactory.class, dialogModel );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         // create the button model and set the properties
161*cdf0e10cSrcweir         Object buttonModel = xMultiServiceFactory.createInstance(
162*cdf0e10cSrcweir             "com.sun.star.awt.UnoControlButtonModel" );
163*cdf0e10cSrcweir         XPropertySet xPSetButton = ( XPropertySet )UnoRuntime.queryInterface(
164*cdf0e10cSrcweir             XPropertySet.class, buttonModel );
165*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "PositionX", new Integer( 20 ) );
166*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "PositionY", new Integer( 70 ) );
167*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "Width", new Integer( 50 ) );
168*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "Height", new Integer( 14 ) );
169*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "Name", _buttonName );
170*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "TabIndex", new Short( (short)0 ) );
171*cdf0e10cSrcweir         xPSetButton.setPropertyValue( "Label", new String( "Click Me" ) );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         // create the label model and set the properties
174*cdf0e10cSrcweir         Object labelModel = xMultiServiceFactory.createInstance(
175*cdf0e10cSrcweir             "com.sun.star.awt.UnoControlFixedTextModel" );
176*cdf0e10cSrcweir         XPropertySet xPSetLabel = ( XPropertySet )UnoRuntime.queryInterface(
177*cdf0e10cSrcweir             XPropertySet.class, labelModel );
178*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "PositionX", new Integer( 40 ) );
179*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "PositionY", new Integer( 30 ) );
180*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "Width", new Integer( 100 ) );
181*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "Height", new Integer( 14 ) );
182*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "Name", _labelName );
183*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "TabIndex", new Short( (short)1 ) );
184*cdf0e10cSrcweir         xPSetLabel.setPropertyValue( "Label", _labelPrefix );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         // create a Cancel button model and set the properties
187*cdf0e10cSrcweir         Object cancelButtonModel = xMultiServiceFactory.createInstance(
188*cdf0e10cSrcweir             "com.sun.star.awt.UnoControlButtonModel" );
189*cdf0e10cSrcweir         XPropertySet xPSetCancelButton = ( XPropertySet )UnoRuntime.queryInterface(
190*cdf0e10cSrcweir             XPropertySet.class, cancelButtonModel );
191*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "PositionX", new Integer( 80 ) );
192*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "PositionY", new Integer( 70 ) );
193*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "Width", new Integer( 50 ) );
194*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "Height", new Integer( 14 ) );
195*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "Name", _cancelButtonName );
196*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "TabIndex", new Short( (short)2 ) );
197*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "PushButtonType", new Short( (short)2 ) );
198*cdf0e10cSrcweir         xPSetCancelButton.setPropertyValue( "Label", new String( "Cancel" ) );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         // insert the control models into the dialog model
201*cdf0e10cSrcweir         XNameContainer xNameCont = ( XNameContainer )UnoRuntime.queryInterface(
202*cdf0e10cSrcweir             XNameContainer.class, dialogModel );
203*cdf0e10cSrcweir         xNameCont.insertByName( _buttonName, buttonModel );
204*cdf0e10cSrcweir         xNameCont.insertByName( _labelName, labelModel );
205*cdf0e10cSrcweir         xNameCont.insertByName( _cancelButtonName, cancelButtonModel );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         // create the dialog control and set the model
208*cdf0e10cSrcweir         Object dialog = xMultiComponentFactory.createInstanceWithContext(
209*cdf0e10cSrcweir             "com.sun.star.awt.UnoControlDialog", _xComponentContext );
210*cdf0e10cSrcweir         XControl xControl = ( XControl )UnoRuntime.queryInterface(
211*cdf0e10cSrcweir             XControl.class, dialog );
212*cdf0e10cSrcweir         XControlModel xControlModel = ( XControlModel )UnoRuntime.queryInterface(
213*cdf0e10cSrcweir             XControlModel.class, dialogModel );
214*cdf0e10cSrcweir         xControl.setModel( xControlModel );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         // add an action listener to the button control
217*cdf0e10cSrcweir         XControlContainer xControlCont = ( XControlContainer )UnoRuntime.queryInterface(
218*cdf0e10cSrcweir             XControlContainer.class, dialog );
219*cdf0e10cSrcweir         Object objectButton = xControlCont.getControl( "Button1" );
220*cdf0e10cSrcweir         XButton xButton = ( XButton )UnoRuntime.queryInterface(
221*cdf0e10cSrcweir             XButton.class, objectButton );
222*cdf0e10cSrcweir         xButton.addActionListener( new ActionListenerImpl( xControlCont ) );
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir         // create a peer
225*cdf0e10cSrcweir         Object toolkit = xMultiComponentFactory.createInstanceWithContext(
226*cdf0e10cSrcweir             "com.sun.star.awt.ExtToolkit", _xComponentContext );
227*cdf0e10cSrcweir         XToolkit xToolkit = ( XToolkit )UnoRuntime.queryInterface(
228*cdf0e10cSrcweir             XToolkit.class, toolkit );
229*cdf0e10cSrcweir         XWindow xWindow = ( XWindow )UnoRuntime.queryInterface(
230*cdf0e10cSrcweir             XWindow.class, xControl );
231*cdf0e10cSrcweir         xWindow.setVisible( false );
232*cdf0e10cSrcweir         xControl.createPeer( xToolkit, null );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir         // execute the dialog
235*cdf0e10cSrcweir         XDialog xDialog = ( XDialog )UnoRuntime.queryInterface(
236*cdf0e10cSrcweir             XDialog.class, dialog );
237*cdf0e10cSrcweir         xDialog.execute();
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         // dispose the dialog
240*cdf0e10cSrcweir         XComponent xComponent = ( XComponent )UnoRuntime.queryInterface(
241*cdf0e10cSrcweir             XComponent.class, dialog );
242*cdf0e10cSrcweir         xComponent.dispose();
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     /** action listener
246*cdf0e10cSrcweir      */
247*cdf0e10cSrcweir     public class ActionListenerImpl implements com.sun.star.awt.XActionListener {
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         private int _nCounts = 0;
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         private XControlContainer _xControlCont;
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir         public ActionListenerImpl( XControlContainer xControlCont ) {
254*cdf0e10cSrcweir             _xControlCont = xControlCont;
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         // XEventListener
258*cdf0e10cSrcweir         public void disposing( EventObject eventObject ) {
259*cdf0e10cSrcweir             _xControlCont = null;
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         // XActionListener
263*cdf0e10cSrcweir         public void actionPerformed( ActionEvent actionEvent ) {
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir             // increase click counter
266*cdf0e10cSrcweir             _nCounts++;
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir             // set label text
269*cdf0e10cSrcweir             Object label = _xControlCont.getControl( "Label1" );
270*cdf0e10cSrcweir             XFixedText xLabel = ( XFixedText )UnoRuntime.queryInterface(
271*cdf0e10cSrcweir                 XFixedText.class, label );
272*cdf0e10cSrcweir             xLabel.setText( _labelPrefix + _nCounts );
273*cdf0e10cSrcweir         }
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir }
276