1 /*************************************************************************
3 * $RCSfile: SampleDialog.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:08:18 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 package com
.sun
.star
.comp
.sdk
.examples
;
43 import com
.sun
.star
.awt
.ActionEvent
;
44 import com
.sun
.star
.awt
.XActionListener
;
45 import com
.sun
.star
.awt
.XButton
;
46 import com
.sun
.star
.lang
.XComponent
;
47 import com
.sun
.star
.awt
.XControl
;
48 import com
.sun
.star
.awt
.XControlModel
;
49 import com
.sun
.star
.awt
.XControlContainer
;
50 import com
.sun
.star
.awt
.XDialog
;
51 import com
.sun
.star
.awt
.XFixedText
;
52 import com
.sun
.star
.awt
.XToolkit
;
53 import com
.sun
.star
.awt
.XWindow
;
54 import com
.sun
.star
.beans
.XPropertySet
;
55 import com
.sun
.star
.comp
.loader
.FactoryHelper
;
56 import com
.sun
.star
.container
.XNameContainer
;
57 import com
.sun
.star
.lang
.EventObject
;
58 import com
.sun
.star
.lang
.XMultiComponentFactory
;
59 import com
.sun
.star
.lang
.XMultiServiceFactory
;
60 import com
.sun
.star
.lang
.XSingleServiceFactory
;
61 import com
.sun
.star
.lang
.XTypeProvider
;
62 import com
.sun
.star
.lang
.XServiceInfo
;
63 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
64 import com
.sun
.star
.registry
.XRegistryKey
;
65 import com
.sun
.star
.task
.XJobExecutor
;
66 import com
.sun
.star
.uno
.Type
;
67 import com
.sun
.star
.uno
.UnoRuntime
;
68 import com
.sun
.star
.uno
.XComponentContext
;
71 /** example of a Java component which creates a dialog at runtime
73 This component can be tested by the following StarOffice Basic code:
76 oJobExecutor = CreateUnoService( "com.sun.star.examples.SampleDialog" )
77 oJobExecutor.trigger( "execute" )
81 public class SampleDialog
extends WeakBase
implements XServiceInfo
, XJobExecutor
{
83 static final String __serviceName
= "com.sun.star.examples.SampleDialog";
85 private static final String _buttonName
= "Button1";
86 private static final String _cancelButtonName
= "CancelButton";
87 private static final String _labelName
= "Label1";
88 private static final String _labelPrefix
= "Number of button clicks: ";
90 private XComponentContext _xComponentContext
;
92 public SampleDialog( XComponentContext xComponentContext
) {
93 _xComponentContext
= xComponentContext
;
96 // static component operations
97 public static XSingleServiceFactory
__getServiceFactory( String implName
,
98 XMultiServiceFactory multiFactory
,
99 XRegistryKey regKey
) {
100 XSingleServiceFactory xSingleServiceFactory
= null;
101 if ( implName
.equals( SampleDialog
.class.getName() ) ) {
102 xSingleServiceFactory
= FactoryHelper
.getServiceFactory(
103 SampleDialog
.class, SampleDialog
.__serviceName
, multiFactory
, regKey
);
105 return xSingleServiceFactory
;
108 public static boolean __writeRegistryServiceInfo( XRegistryKey regKey
) {
109 return FactoryHelper
.writeRegistryServiceInfo(
110 SampleDialog
.class.getName(), SampleDialog
.__serviceName
, regKey
);
114 public String
getImplementationName( ) {
115 return getClass().getName();
119 public boolean supportsService( /*IN*/String serviceName
) {
120 if ( serviceName
.equals( __serviceName
))
126 public String
[] getSupportedServiceNames( ) {
127 String
[] retValue
= new String
[0];
128 retValue
[0] = __serviceName
;
133 public void trigger(String sEvent
) {
134 if ( sEvent
.compareTo( "execute" ) == 0 ) {
138 catch ( Exception e
) {
139 throw new com
.sun
.star
.lang
.WrappedTargetRuntimeException( e
.getMessage(), this, e
);
144 /** method for creating a dialog at runtime
146 private void createDialog() throws com
.sun
.star
.uno
.Exception
{
148 // get the service manager from the component context
149 XMultiComponentFactory xMultiComponentFactory
= _xComponentContext
.getServiceManager();
151 // create the dialog model and set the properties
152 Object dialogModel
= xMultiComponentFactory
.createInstanceWithContext(
153 "com.sun.star.awt.UnoControlDialogModel", _xComponentContext
);
154 XPropertySet xPSetDialog
= ( XPropertySet
)UnoRuntime
.queryInterface(
155 XPropertySet
.class, dialogModel
);
156 xPSetDialog
.setPropertyValue( "PositionX", new Integer( 100 ) );
157 xPSetDialog
.setPropertyValue( "PositionY", new Integer( 100 ) );
158 xPSetDialog
.setPropertyValue( "Width", new Integer( 150 ) );
159 xPSetDialog
.setPropertyValue( "Height", new Integer( 100 ) );
160 xPSetDialog
.setPropertyValue( "Title", new String( "Runtime Dialog Demo" ) );
162 // get the service manager from the dialog model
163 XMultiServiceFactory xMultiServiceFactory
= ( XMultiServiceFactory
)UnoRuntime
.queryInterface(
164 XMultiServiceFactory
.class, dialogModel
);
166 // create the button model and set the properties
167 Object buttonModel
= xMultiServiceFactory
.createInstance(
168 "com.sun.star.awt.UnoControlButtonModel" );
169 XPropertySet xPSetButton
= ( XPropertySet
)UnoRuntime
.queryInterface(
170 XPropertySet
.class, buttonModel
);
171 xPSetButton
.setPropertyValue( "PositionX", new Integer( 20 ) );
172 xPSetButton
.setPropertyValue( "PositionY", new Integer( 70 ) );
173 xPSetButton
.setPropertyValue( "Width", new Integer( 50 ) );
174 xPSetButton
.setPropertyValue( "Height", new Integer( 14 ) );
175 xPSetButton
.setPropertyValue( "Name", _buttonName
);
176 xPSetButton
.setPropertyValue( "TabIndex", new Short( (short)0 ) );
177 xPSetButton
.setPropertyValue( "Label", new String( "Click Me" ) );
179 // create the label model and set the properties
180 Object labelModel
= xMultiServiceFactory
.createInstance(
181 "com.sun.star.awt.UnoControlFixedTextModel" );
182 XPropertySet xPSetLabel
= ( XPropertySet
)UnoRuntime
.queryInterface(
183 XPropertySet
.class, labelModel
);
184 xPSetLabel
.setPropertyValue( "PositionX", new Integer( 40 ) );
185 xPSetLabel
.setPropertyValue( "PositionY", new Integer( 30 ) );
186 xPSetLabel
.setPropertyValue( "Width", new Integer( 100 ) );
187 xPSetLabel
.setPropertyValue( "Height", new Integer( 14 ) );
188 xPSetLabel
.setPropertyValue( "Name", _labelName
);
189 xPSetLabel
.setPropertyValue( "TabIndex", new Short( (short)1 ) );
190 xPSetLabel
.setPropertyValue( "Label", _labelPrefix
);
192 // create a Cancel button model and set the properties
193 Object cancelButtonModel
= xMultiServiceFactory
.createInstance(
194 "com.sun.star.awt.UnoControlButtonModel" );
195 XPropertySet xPSetCancelButton
= ( XPropertySet
)UnoRuntime
.queryInterface(
196 XPropertySet
.class, cancelButtonModel
);
197 xPSetCancelButton
.setPropertyValue( "PositionX", new Integer( 80 ) );
198 xPSetCancelButton
.setPropertyValue( "PositionY", new Integer( 70 ) );
199 xPSetCancelButton
.setPropertyValue( "Width", new Integer( 50 ) );
200 xPSetCancelButton
.setPropertyValue( "Height", new Integer( 14 ) );
201 xPSetCancelButton
.setPropertyValue( "Name", _cancelButtonName
);
202 xPSetCancelButton
.setPropertyValue( "TabIndex", new Short( (short)2 ) );
203 xPSetCancelButton
.setPropertyValue( "PushButtonType", new Short( (short)2 ) );
204 xPSetCancelButton
.setPropertyValue( "Label", new String( "Cancel" ) );
206 // insert the control models into the dialog model
207 XNameContainer xNameCont
= ( XNameContainer
)UnoRuntime
.queryInterface(
208 XNameContainer
.class, dialogModel
);
209 xNameCont
.insertByName( _buttonName
, buttonModel
);
210 xNameCont
.insertByName( _labelName
, labelModel
);
211 xNameCont
.insertByName( _cancelButtonName
, cancelButtonModel
);
213 // create the dialog control and set the model
214 Object dialog
= xMultiComponentFactory
.createInstanceWithContext(
215 "com.sun.star.awt.UnoControlDialog", _xComponentContext
);
216 XControl xControl
= ( XControl
)UnoRuntime
.queryInterface(
217 XControl
.class, dialog
);
218 XControlModel xControlModel
= ( XControlModel
)UnoRuntime
.queryInterface(
219 XControlModel
.class, dialogModel
);
220 xControl
.setModel( xControlModel
);
222 // add an action listener to the button control
223 XControlContainer xControlCont
= ( XControlContainer
)UnoRuntime
.queryInterface(
224 XControlContainer
.class, dialog
);
225 Object objectButton
= xControlCont
.getControl( "Button1" );
226 XButton xButton
= ( XButton
)UnoRuntime
.queryInterface(
227 XButton
.class, objectButton
);
228 xButton
.addActionListener( new ActionListenerImpl( xControlCont
) );
231 Object toolkit
= xMultiComponentFactory
.createInstanceWithContext(
232 "com.sun.star.awt.ExtToolkit", _xComponentContext
);
233 XToolkit xToolkit
= ( XToolkit
)UnoRuntime
.queryInterface(
234 XToolkit
.class, toolkit
);
235 XWindow xWindow
= ( XWindow
)UnoRuntime
.queryInterface(
236 XWindow
.class, xControl
);
237 xWindow
.setVisible( false );
238 xControl
.createPeer( xToolkit
, null );
240 // execute the dialog
241 XDialog xDialog
= ( XDialog
)UnoRuntime
.queryInterface(
242 XDialog
.class, dialog
);
245 // dispose the dialog
246 XComponent xComponent
= ( XComponent
)UnoRuntime
.queryInterface(
247 XComponent
.class, dialog
);
248 xComponent
.dispose();
253 public class ActionListenerImpl
implements com
.sun
.star
.awt
.XActionListener
{
255 private int _nCounts
= 0;
257 private XControlContainer _xControlCont
;
259 public ActionListenerImpl( XControlContainer xControlCont
) {
260 _xControlCont
= xControlCont
;
264 public void disposing( EventObject eventObject
) {
265 _xControlCont
= null;
269 public void actionPerformed( ActionEvent actionEvent
) {
271 // increase click counter
275 Object label
= _xControlCont
.getControl( "Label1" );
276 XFixedText xLabel
= ( XFixedText
)UnoRuntime
.queryInterface(
277 XFixedText
.class, label
);
278 xLabel
.setText( _labelPrefix
+ _nCounts
);