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