DialogComponent.java (34dd1e25) DialogComponent.java (be8bac39)
1/**************************************************************
1/**************************************************************
2 *
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
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 *
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
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 *
19 *
20 *************************************************************/
21
22
23
24import com.sun.star.uno.Type;
20 *************************************************************/
21
22
23
24import com.sun.star.uno.Type;
25import com.sun.star.uno.UnoRuntime;
25import com.sun.star.uno.UnoRuntime;
26import com.sun.star.uno.XComponentContext;
27import com.sun.star.registry.XRegistryKey;
28import com.sun.star.lang.XTypeProvider;
29import com.sun.star.lang.XServiceInfo;
30import com.sun.star.lang.XSingleServiceFactory;
31import com.sun.star.lang.XMultiServiceFactory;
32import com.sun.star.lang.XSingleComponentFactory;
33import com.sun.star.lang.XMultiComponentFactory;

--- 11 unchanged lines hidden (view full) ---

45
46import com.sun.star.awt.XToolkit;
47import com.sun.star.awt.XWindowPeer;
48import com.sun.star.awt.XMessageBox;
49import com.sun.star.awt.WindowAttribute;
50import com.sun.star.awt.WindowClass;
51import com.sun.star.awt.WindowDescriptor;
52import com.sun.star.awt.Rectangle;
26import com.sun.star.uno.XComponentContext;
27import com.sun.star.registry.XRegistryKey;
28import com.sun.star.lang.XTypeProvider;
29import com.sun.star.lang.XServiceInfo;
30import com.sun.star.lang.XSingleServiceFactory;
31import com.sun.star.lang.XMultiServiceFactory;
32import com.sun.star.lang.XSingleComponentFactory;
33import com.sun.star.lang.XMultiComponentFactory;

--- 11 unchanged lines hidden (view full) ---

45
46import com.sun.star.awt.XToolkit;
47import com.sun.star.awt.XWindowPeer;
48import com.sun.star.awt.XMessageBox;
49import com.sun.star.awt.WindowAttribute;
50import com.sun.star.awt.WindowClass;
51import com.sun.star.awt.WindowDescriptor;
52import com.sun.star.awt.Rectangle;
53
53
54import com.sun.star.test.XTestDialogHandler;
55
56// DialogComponent implements all necessary interfaces self, this is only
54import com.sun.star.test.XTestDialogHandler;
55
56// DialogComponent implements all necessary interfaces self, this is only
57// for demonstration. More convenient is to use the impelmentation WeakBase or
57// for demonstration. More convenient is to use the implementation WeakBase or
58// ComponentBase, see implementation of TestComponentA.
59public class DialogComponent {
60
58// ComponentBase, see implementation of TestComponentA.
59public class DialogComponent {
60
61 // public static class _DialogComponent extends WeakBase
61 // public static class _DialogComponent extends WeakBase
62 public static class _DialogComponent
63 implements XTypeProvider, XServiceInfo, XTestDialogHandler, XDialogEventHandler {
64
65 static final String __serviceName= "com.sun.star.test.TestDialogHandler";
62 public static class _DialogComponent
63 implements XTypeProvider, XServiceInfo, XTestDialogHandler, XDialogEventHandler {
64
65 static final String __serviceName= "com.sun.star.test.TestDialogHandler";
66
67 static byte[] _implementationId;
66
67 static byte[] _implementationId;
68 private XComponentContext m_xCmpCtx;
68 private XComponentContext m_xCmpCtx;
69
69
70 private XFrame m_xFrame;
71 private XToolkit m_xToolkit;
72
73 public _DialogComponent(XComponentContext context) {
74 m_xCmpCtx= context;
75
76 try {
77 // Create the toolkit to have access to it later
78 m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
79 XToolkit.class,
80 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
70 private XFrame m_xFrame;
71 private XToolkit m_xToolkit;
72
73 public _DialogComponent(XComponentContext context) {
74 m_xCmpCtx= context;
75
76 try {
77 // Create the toolkit to have access to it later
78 m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
79 XToolkit.class,
80 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
81 m_xCmpCtx));
81 m_xCmpCtx));
82 }
83 catch (Exception e) {
84 e.printStackTrace();
85 }
86 }
82 }
83 catch (Exception e) {
84 e.printStackTrace();
85 }
86 }
87
87
88 // XTestDialogHandler
89 public String createDialog( String DialogURL, XModel xModel, XFrame xFrame ) {
90 m_xFrame = xFrame;
88 // XTestDialogHandler
89 public String createDialog( String DialogURL, XModel xModel, XFrame xFrame ) {
90 m_xFrame = xFrame;
91
91
92 try {
93 XMultiComponentFactory xMCF = m_xCmpCtx.getServiceManager();
94 Object obj;
95
96 // If valid we must pass the XModel when creating a DialogProvider object
97 if( xModel != null ) {
98 Object[] args = new Object[1];
99 args[0] = xModel;
92 try {
93 XMultiComponentFactory xMCF = m_xCmpCtx.getServiceManager();
94 Object obj;
95
96 // If valid we must pass the XModel when creating a DialogProvider object
97 if( xModel != null ) {
98 Object[] args = new Object[1];
99 args[0] = xModel;
100
100
101 obj = xMCF.createInstanceWithArgumentsAndContext(
102 "com.sun.star.awt.DialogProvider2", args, m_xCmpCtx );
103 }
104 else {
105 obj = xMCF.createInstanceWithContext(
106 "com.sun.star.awt.DialogProvider2", m_xCmpCtx );
107 }
108
109 XDialogProvider2 xDialogProvider = (XDialogProvider2)
110 UnoRuntime.queryInterface( XDialogProvider2.class, obj );
101 obj = xMCF.createInstanceWithArgumentsAndContext(
102 "com.sun.star.awt.DialogProvider2", args, m_xCmpCtx );
103 }
104 else {
105 obj = xMCF.createInstanceWithContext(
106 "com.sun.star.awt.DialogProvider2", m_xCmpCtx );
107 }
108
109 XDialogProvider2 xDialogProvider = (XDialogProvider2)
110 UnoRuntime.queryInterface( XDialogProvider2.class, obj );
111
111
112 XDialog xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
113 if( xDialog != null )
114 xDialog.execute();
115 }
116 catch (Exception e) {
117 e.printStackTrace();
118 }
119 return "Created dialog \"" + DialogURL + "\"";
120 }
112 XDialog xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
113 if( xDialog != null )
114 xDialog.execute();
115 }
116 catch (Exception e) {
117 e.printStackTrace();
118 }
119 return "Created dialog \"" + DialogURL + "\"";
120 }
121
121
122 public void copyText( XDialog xDialog, Object aEventObject ) {
123 XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface(
124 XControlContainer.class, xDialog );
125 String aTextPropertyStr = "Text";
126 String aText = "";
122 public void copyText( XDialog xDialog, Object aEventObject ) {
123 XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface(
124 XControlContainer.class, xDialog );
125 String aTextPropertyStr = "Text";
126 String aText = "";
127 XControl xTextField1Control = xControlContainer.getControl( "TextField1" );
127 XControl xTextField1Control = xControlContainer.getControl( "TextField1" );
128 XControlModel xControlModel1 = xTextField1Control.getModel();
129 XPropertySet xPropertySet1 = (XPropertySet)UnoRuntime.queryInterface(
130 XPropertySet.class, xControlModel1 );
131 try
132 {
133 aText = (String)xPropertySet1.getPropertyValue( aTextPropertyStr );
134 }
135 catch (Exception e) {
136 e.printStackTrace();
137 }
128 XControlModel xControlModel1 = xTextField1Control.getModel();
129 XPropertySet xPropertySet1 = (XPropertySet)UnoRuntime.queryInterface(
130 XPropertySet.class, xControlModel1 );
131 try
132 {
133 aText = (String)xPropertySet1.getPropertyValue( aTextPropertyStr );
134 }
135 catch (Exception e) {
136 e.printStackTrace();
137 }
138
139 XControl xTextField2Control = xControlContainer.getControl( "TextField2" );
138
139 XControl xTextField2Control = xControlContainer.getControl( "TextField2" );
140 XControlModel xControlModel2 = xTextField2Control.getModel();
141 XPropertySet xPropertySet2 = (XPropertySet)UnoRuntime.queryInterface(
142 XPropertySet.class, xControlModel2 );
143 try
144 {
145 xPropertySet2.setPropertyValue( aTextPropertyStr, aText );
146 }
147 catch (Exception e) {
148 e.printStackTrace();
149 }
150
151 showMessageBox( "DialogComponent", "copyText() called" );
152 }
153
154 public void handleEvent() {
155 showMessageBox( "DialogComponent", "handleEvent() called" );
156 }
140 XControlModel xControlModel2 = xTextField2Control.getModel();
141 XPropertySet xPropertySet2 = (XPropertySet)UnoRuntime.queryInterface(
142 XPropertySet.class, xControlModel2 );
143 try
144 {
145 xPropertySet2.setPropertyValue( aTextPropertyStr, aText );
146 }
147 catch (Exception e) {
148 e.printStackTrace();
149 }
150
151 showMessageBox( "DialogComponent", "copyText() called" );
152 }
153
154 public void handleEvent() {
155 showMessageBox( "DialogComponent", "handleEvent() called" );
156 }
157
157
158 public void handleEventWithArguments( XDialog xDialog, Object aEventObject ) {
159 showMessageBox( "DialogComponent", "handleEventWithArguments() called\n\n" +
160 "Event Object = " + aEventObject );
161 }
162
163 private final String aHandlerMethod1 = "doit1";
164 private final String aHandlerMethod2 = "doit2";
165 private final String aHandlerMethod3 = "doit3";

--- 46 unchanged lines hidden (view full) ---

212 _implementationId[2] = (byte)((hash >>> 16) & 0xff);
213 _implementationId[3] = (byte)((hash >>>24) & 0xff);
214 }
215 return _implementationId;
216 }
217
218
219
158 public void handleEventWithArguments( XDialog xDialog, Object aEventObject ) {
159 showMessageBox( "DialogComponent", "handleEventWithArguments() called\n\n" +
160 "Event Object = " + aEventObject );
161 }
162
163 private final String aHandlerMethod1 = "doit1";
164 private final String aHandlerMethod2 = "doit2";
165 private final String aHandlerMethod3 = "doit3";

--- 46 unchanged lines hidden (view full) ---

212 _implementationId[2] = (byte)((hash >>> 16) & 0xff);
213 _implementationId[3] = (byte)((hash >>>24) & 0xff);
214 }
215 return _implementationId;
216 }
217
218
219
220 /** This method is a simple helper function to used in the
221 * static component initialisation functions as well as in
222 * getSupportedServiceNames.
223 */
224 public static String[] getServiceNames() {
225 String[] sSupportedServiceNames = { __serviceName };
226 return sSupportedServiceNames;
227 }
228
220 /** This method is a simple helper function to used in the
221 * static component initialisation functions as well as in
222 * getSupportedServiceNames.
223 */
224 public static String[] getServiceNames() {
225 String[] sSupportedServiceNames = { __serviceName };
226 return sSupportedServiceNames;
227 }
228
229 //XServiceInfo
229 //XServiceInfo
230 public String[] getSupportedServiceNames() {
231 return getServiceNames();
232 }
230 public String[] getSupportedServiceNames() {
231 return getServiceNames();
232 }
233
234 //XServiceInfo
233
234 //XServiceInfo
235 public boolean supportsService( String sServiceName ) {
236 return sServiceName.equals( __serviceName );
237 }
238
235 public boolean supportsService( String sServiceName ) {
236 return sServiceName.equals( __serviceName );
237 }
238
239 //XServiceInfo
239 //XServiceInfo
240 public String getImplementationName() {
241 // return DialogComponent.class.getName();
242 return _DialogComponent.class.getName();
243 }
240 public String getImplementationName() {
241 // return DialogComponent.class.getName();
242 return _DialogComponent.class.getName();
243 }
244
244
245 public void showMessageBox(String sTitle, String sMessage) {
245 public void showMessageBox(String sTitle, String sMessage) {
246 try {
247 if ( null != m_xFrame && null != m_xToolkit ) {
248
249 // describe window properties.
250 WindowDescriptor aDescriptor = new WindowDescriptor();
251 aDescriptor.Type = WindowClass.MODALTOP;
252 aDescriptor.WindowServiceName = new String( "infobox" );
253 aDescriptor.ParentIndex = -1;
254 aDescriptor.Parent = (XWindowPeer)UnoRuntime.queryInterface(
255 XWindowPeer.class, m_xFrame.getContainerWindow());
256 aDescriptor.Bounds = new Rectangle(0,0,300,200);
257 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
258 WindowAttribute.MOVEABLE |
259 WindowAttribute.CLOSEABLE;
246 try {
247 if ( null != m_xFrame && null != m_xToolkit ) {
248
249 // describe window properties.
250 WindowDescriptor aDescriptor = new WindowDescriptor();
251 aDescriptor.Type = WindowClass.MODALTOP;
252 aDescriptor.WindowServiceName = new String( "infobox" );
253 aDescriptor.ParentIndex = -1;
254 aDescriptor.Parent = (XWindowPeer)UnoRuntime.queryInterface(
255 XWindowPeer.class, m_xFrame.getContainerWindow());
256 aDescriptor.Bounds = new Rectangle(0,0,300,200);
257 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
258 WindowAttribute.MOVEABLE |
259 WindowAttribute.CLOSEABLE;
260
260
261 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
262 if ( null != xPeer ) {
263 XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface(
264 XMessageBox.class, xPeer);
265 if ( null != xMsgBox )
266 {
267 xMsgBox.setCaptionText( sTitle );
268 xMsgBox.setMessageText( sMessage );
269 xMsgBox.execute();
270 }
271 }
272 }
273 } catch ( com.sun.star.uno.Exception e) {
261 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
262 if ( null != xPeer ) {
263 XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface(
264 XMessageBox.class, xPeer);
265 if ( null != xMsgBox )
266 {
267 xMsgBox.setCaptionText( sTitle );
268 xMsgBox.setMessageText( sMessage );
269 xMsgBox.execute();
270 }
271 }
272 }
273 } catch ( com.sun.star.uno.Exception e) {
274 // do your error handling
274 // do your error handling
275 }
275 }
276 }
276 }
277 }
278
279 /**
280 * Gives a factory for creating the service.
281 * This method is called by the <code>JavaLoader</code>
282 * <p>
283 * @return returns a <code>XSingleComponentFactory</code> for creating
284 * the component
285 * @param sImplName the name of the implementation for which a
286 * service is desired
287 * @see com.sun.star.comp.loader.JavaLoader
288 */
289 public static XSingleComponentFactory __getComponentFactory(String sImplName)
290 {
291 XSingleComponentFactory xFactory = null;
277 }
278
279 /**
280 * Gives a factory for creating the service.
281 * This method is called by the <code>JavaLoader</code>
282 * <p>
283 * @return returns a <code>XSingleComponentFactory</code> for creating
284 * the component
285 * @param sImplName the name of the implementation for which a
286 * service is desired
287 * @see com.sun.star.comp.loader.JavaLoader
288 */
289 public static XSingleComponentFactory __getComponentFactory(String sImplName)
290 {
291 XSingleComponentFactory xFactory = null;
292
292
293 if ( sImplName.equals( _DialogComponent.class.getName() ) )
294 xFactory = Factory.createComponentFactory(_DialogComponent.class,
295 _DialogComponent.getServiceNames());
293 if ( sImplName.equals( _DialogComponent.class.getName() ) )
294 xFactory = Factory.createComponentFactory(_DialogComponent.class,
295 _DialogComponent.getServiceNames());
296
296
297 return xFactory;
298 }
299
300 /**
301 * Writes the service information into the given registry key.
302 * This method is called by the <code>JavaLoader</code>
303 * <p>
304 * @return returns true if the operation succeeded
305 * @param regKey the registryKey
306 * @see com.sun.star.comp.loader.JavaLoader
307 */
308 // This method not longer necessary since OOo 3.4 where the component registration
309 // was changed to passive component registration. For more details see
297 return xFactory;
298 }
299
300 /**
301 * Writes the service information into the given registry key.
302 * This method is called by the <code>JavaLoader</code>
303 * <p>
304 * @return returns true if the operation succeeded
305 * @param regKey the registryKey
306 * @see com.sun.star.comp.loader.JavaLoader
307 */
308 // This method not longer necessary since OOo 3.4 where the component registration
309 // was changed to passive component registration. For more details see
310 // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
310 // https://wiki.openoffice.org/wiki/Passive_Component_Registration
311
312// public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
313// return Factory.writeRegistryServiceInfo(_DialogComponent.class.getName(),
314// _DialogComponent.getServiceNames(),
315// regKey);
316// }
317}
311
312// public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
313// return Factory.writeRegistryServiceInfo(_DialogComponent.class.getName(),
314// _DialogComponent.getServiceNames(),
315// regKey);
316// }
317}
318