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