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