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.script.framework.browse; 25 26 import com.sun.star.uno.UnoRuntime; 27 import com.sun.star.uno.XComponentContext; 28 29 import com.sun.star.lang.XMultiComponentFactory; 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.lang.EventObject; 32 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.container.XNameContainer; 35 36 import com.sun.star.awt.*; 37 38 39 public class DialogFactory 40 { 41 private static DialogFactory factory; 42 private XComponentContext xComponentContext; 43 44 // singleton DialogFactory(XComponentContext xComponentContext)45 private DialogFactory(XComponentContext xComponentContext) 46 { 47 this.xComponentContext = xComponentContext; 48 factory = this; 49 } 50 createDialogFactory(XComponentContext xComponentContext)51 public static void createDialogFactory(XComponentContext xComponentContext) 52 { 53 if (factory == null) 54 { 55 synchronized(DialogFactory.class) 56 { 57 if (factory == null) 58 { 59 factory = new DialogFactory(xComponentContext); 60 } 61 } 62 } 63 } 64 getDialogFactory()65 public static DialogFactory getDialogFactory() 66 throws java.lang.Exception 67 { 68 if (factory == null) 69 { 70 throw new java.lang.Exception("DialogFactory not initialized"); 71 } 72 return factory; 73 } 74 showConfirmDialog(String title, String prompt)75 public boolean showConfirmDialog(String title, String prompt) 76 { 77 final XDialog xDialog; 78 try 79 { 80 xDialog = createConfirmDialog(title, prompt); 81 } 82 catch (com.sun.star.uno.Exception e) 83 { 84 return false; 85 } 86 87 // add an action listener to the button controls 88 XControlContainer controls = (XControlContainer) 89 UnoRuntime.queryInterface(XControlContainer.class, xDialog); 90 91 XButton okButton = (XButton) UnoRuntime.queryInterface( 92 XButton.class, controls.getControl("Ok")); 93 okButton.setActionCommand("Ok"); 94 95 XButton cancelButton = (XButton) UnoRuntime.queryInterface( 96 XButton.class, controls.getControl("Cancel")); 97 cancelButton.setActionCommand("Cancel"); 98 99 final ResultHolder resultHolder = new ResultHolder(); 100 101 com.sun.star.awt.XActionListener listener = 102 new com.sun.star.awt.XActionListener() 103 { 104 public void actionPerformed(com.sun.star.awt.ActionEvent e) { 105 if (e.ActionCommand.equals("Cancel")) 106 { 107 resultHolder.setResult(Boolean.FALSE); 108 xDialog.endExecute(); 109 } 110 else 111 { 112 resultHolder.setResult(Boolean.TRUE); 113 xDialog.endExecute(); 114 } 115 } 116 117 public void disposing(EventObject o) { 118 // does nothing 119 } 120 }; 121 122 okButton.addActionListener(listener); 123 cancelButton.addActionListener(listener); 124 125 xDialog.execute(); 126 127 Boolean result = (Boolean)resultHolder.getResult(); 128 129 return result == null ? false : result.booleanValue(); 130 } 131 showInputDialog(String title, String prompt)132 public String showInputDialog(String title, String prompt) 133 { 134 final XDialog xDialog; 135 try 136 { 137 xDialog = createInputDialog(title, prompt); 138 } 139 catch (com.sun.star.uno.Exception e) 140 { 141 return null; 142 } 143 144 // add an action listener to the button controls 145 XControlContainer controls = (XControlContainer) 146 UnoRuntime.queryInterface(XControlContainer.class, xDialog); 147 148 XButton okButton = (XButton) UnoRuntime.queryInterface( 149 XButton.class, controls.getControl("Ok")); 150 okButton.setActionCommand("Ok"); 151 152 XButton cancelButton = (XButton) UnoRuntime.queryInterface( 153 XButton.class, controls.getControl("Cancel")); 154 cancelButton.setActionCommand("Cancel"); 155 156 final XTextComponent textField = (XTextComponent) 157 UnoRuntime.queryInterface( 158 XTextComponent.class, controls.getControl("NameField")); 159 160 final ResultHolder resultHolder = new ResultHolder(); 161 162 com.sun.star.awt.XActionListener listener = 163 new com.sun.star.awt.XActionListener() 164 { 165 public void actionPerformed(com.sun.star.awt.ActionEvent e) { 166 if (e.ActionCommand.equals("Cancel")) 167 { 168 resultHolder.setResult(null); 169 xDialog.endExecute(); 170 } 171 else 172 { 173 resultHolder.setResult(textField.getText()); 174 xDialog.endExecute(); 175 } 176 } 177 178 public void disposing(EventObject o) { 179 // does nothing 180 } 181 }; 182 183 okButton.addActionListener(listener); 184 cancelButton.addActionListener(listener); 185 186 xDialog.execute(); 187 188 return (String)resultHolder.getResult(); 189 } 190 createConfirmDialog(String title, String prompt)191 private XDialog createConfirmDialog(String title, String prompt) 192 throws com.sun.star.uno.Exception 193 { 194 if (title == null || title.equals("")) 195 { 196 title = "Scripting Framework"; 197 } 198 199 if (prompt == null || prompt.equals("")) 200 { 201 prompt = "Enter name"; 202 } 203 204 // get the service manager from the component context 205 XMultiComponentFactory xMultiComponentFactory = 206 xComponentContext.getServiceManager(); 207 208 // create the dialog model and set the properties 209 Object dialogModel = xMultiComponentFactory.createInstanceWithContext( 210 "com.sun.star.awt.UnoControlDialogModel", xComponentContext); 211 212 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface( 213 XPropertySet.class, dialogModel); 214 215 props.setPropertyValue("Title", title); 216 setDimensions(dialogModel, 100, 100, 157, 37); 217 218 // get the service manager from the dialog model 219 XMultiServiceFactory xMultiServiceFactory = 220 (XMultiServiceFactory) UnoRuntime.queryInterface( 221 XMultiServiceFactory.class, dialogModel); 222 223 // create the label model and set the properties 224 Object label = xMultiServiceFactory.createInstance( 225 "com.sun.star.awt.UnoControlFixedTextModel"); 226 227 setDimensions(label, 15, 5, 134, 12); 228 229 XPropertySet labelProps = (XPropertySet) UnoRuntime.queryInterface( 230 XPropertySet.class, label); 231 labelProps.setPropertyValue("Name", "PromptLabel"); 232 labelProps.setPropertyValue("Label", prompt); 233 234 // create the Run Macro button model and set the properties 235 Object okButtonModel = xMultiServiceFactory.createInstance( 236 "com.sun.star.awt.UnoControlButtonModel"); 237 238 setDimensions(okButtonModel, 40, 18, 38, 15); 239 240 XPropertySet buttonProps = (XPropertySet) UnoRuntime.queryInterface( 241 XPropertySet.class, okButtonModel); 242 buttonProps.setPropertyValue("Name", "Ok"); 243 buttonProps.setPropertyValue("Label", "Ok"); 244 245 // create the Dont Run Macro button model and set the properties 246 Object cancelButtonModel = xMultiServiceFactory.createInstance( 247 "com.sun.star.awt.UnoControlButtonModel"); 248 249 setDimensions(cancelButtonModel, 83, 18, 38, 15); 250 251 buttonProps = (XPropertySet) UnoRuntime.queryInterface( 252 XPropertySet.class, cancelButtonModel); 253 buttonProps.setPropertyValue("Name", "Cancel"); 254 buttonProps.setPropertyValue("Label", "Cancel"); 255 256 // insert the control models into the dialog model 257 XNameContainer xNameCont = (XNameContainer) UnoRuntime.queryInterface( 258 XNameContainer.class, dialogModel); 259 260 xNameCont.insertByName("PromptLabel", label); 261 xNameCont.insertByName("Ok", okButtonModel); 262 xNameCont.insertByName("Cancel", cancelButtonModel); 263 264 // create the dialog control and set the model 265 Object dialog = xMultiComponentFactory.createInstanceWithContext( 266 "com.sun.star.awt.UnoControlDialog", xComponentContext); 267 XControl xControl = (XControl) UnoRuntime.queryInterface( 268 XControl.class, dialog); 269 270 XControlModel xControlModel = (XControlModel) 271 UnoRuntime.queryInterface(XControlModel.class, dialogModel); 272 xControl.setModel(xControlModel); 273 274 // create a peer 275 Object toolkit = xMultiComponentFactory.createInstanceWithContext( 276 "com.sun.star.awt.ExtToolkit", xComponentContext); 277 XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface( 278 XToolkit.class, toolkit); 279 XWindow xWindow = (XWindow) UnoRuntime.queryInterface( 280 XWindow.class, xControl); 281 xWindow.setVisible(false); 282 xControl.createPeer(xToolkit, null); 283 284 return (XDialog) UnoRuntime.queryInterface(XDialog.class, dialog); 285 } 286 setDimensions(Object o, int x, int y, int width, int height)287 private void setDimensions(Object o, int x, int y, int width, int height) 288 throws com.sun.star.uno.Exception 289 { 290 XPropertySet props = (XPropertySet) 291 UnoRuntime.queryInterface(XPropertySet.class, o); 292 293 props.setPropertyValue("PositionX", new Integer(x)); 294 props.setPropertyValue("PositionY", new Integer(y)); 295 props.setPropertyValue("Height", new Integer(height)); 296 props.setPropertyValue("Width", new Integer(width)); 297 } 298 createInputDialog(String title, String prompt)299 private XDialog createInputDialog(String title, String prompt) 300 throws com.sun.star.uno.Exception 301 { 302 if (title == null || title.equals("")) 303 { 304 title = "Scripting Framework"; 305 } 306 307 if (prompt == null || prompt.equals("")) 308 { 309 prompt = "Enter name"; 310 } 311 312 // get the service manager from the component context 313 XMultiComponentFactory xMultiComponentFactory = 314 xComponentContext.getServiceManager(); 315 316 // create the dialog model and set the properties 317 Object dialogModel = xMultiComponentFactory.createInstanceWithContext( 318 "com.sun.star.awt.UnoControlDialogModel", xComponentContext); 319 320 setDimensions(dialogModel, 100, 100, 157, 58); 321 322 XPropertySet props = (XPropertySet) UnoRuntime.queryInterface( 323 XPropertySet.class, dialogModel); 324 props.setPropertyValue("Title", title); 325 326 // get the service manager from the dialog model 327 XMultiServiceFactory xMultiServiceFactory = 328 (XMultiServiceFactory) UnoRuntime.queryInterface( 329 XMultiServiceFactory.class, dialogModel); 330 331 // create the label model and set the properties 332 Object label = xMultiServiceFactory.createInstance( 333 "com.sun.star.awt.UnoControlFixedTextModel"); 334 335 setDimensions(label, 15, 5, 134, 12); 336 337 XPropertySet labelProps = (XPropertySet) UnoRuntime.queryInterface( 338 XPropertySet.class, label); 339 labelProps.setPropertyValue("Name", "PromptLabel"); 340 labelProps.setPropertyValue("Label", prompt); 341 342 // create the text field 343 Object edit = xMultiServiceFactory.createInstance( 344 "com.sun.star.awt.UnoControlEditModel"); 345 346 setDimensions(edit, 15, 18, 134, 12); 347 348 XPropertySet editProps = (XPropertySet) UnoRuntime.queryInterface( 349 XPropertySet.class, edit); 350 editProps.setPropertyValue("Name", "NameField"); 351 352 // create the OK button 353 Object okButtonModel = xMultiServiceFactory.createInstance( 354 "com.sun.star.awt.UnoControlButtonModel"); 355 356 setDimensions(okButtonModel, 40, 39, 38, 15); 357 358 XPropertySet buttonProps = (XPropertySet) UnoRuntime.queryInterface( 359 XPropertySet.class, okButtonModel); 360 buttonProps.setPropertyValue("Name", "Ok"); 361 buttonProps.setPropertyValue("Label", "Ok"); 362 363 // create the Cancel button 364 Object cancelButtonModel = xMultiServiceFactory.createInstance( 365 "com.sun.star.awt.UnoControlButtonModel"); 366 367 setDimensions(cancelButtonModel, 83, 39, 38, 15); 368 369 buttonProps = (XPropertySet) UnoRuntime.queryInterface( 370 XPropertySet.class, cancelButtonModel); 371 buttonProps.setPropertyValue("Name", "Cancel"); 372 buttonProps.setPropertyValue("Label", "Cancel"); 373 374 // insert the control models into the dialog model 375 XNameContainer xNameCont = (XNameContainer) 376 UnoRuntime.queryInterface(XNameContainer.class, dialogModel); 377 378 xNameCont.insertByName("PromptLabel", label); 379 xNameCont.insertByName("NameField", edit); 380 xNameCont.insertByName("Ok", okButtonModel); 381 xNameCont.insertByName("Cancel", cancelButtonModel); 382 383 // create the dialog control and set the model 384 Object dialog = xMultiComponentFactory.createInstanceWithContext( 385 "com.sun.star.awt.UnoControlDialog", xComponentContext); 386 XControl xControl = (XControl) UnoRuntime.queryInterface( 387 XControl.class, dialog); 388 389 XControlModel xControlModel = (XControlModel) 390 UnoRuntime.queryInterface(XControlModel.class, dialogModel); 391 xControl.setModel(xControlModel); 392 393 // create a peer 394 Object toolkit = xMultiComponentFactory.createInstanceWithContext( 395 "com.sun.star.awt.ExtToolkit", xComponentContext); 396 XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface( 397 XToolkit.class, toolkit); 398 XWindow xWindow = (XWindow) UnoRuntime.queryInterface( 399 XWindow.class, xControl); 400 xWindow.setVisible(false); 401 xControl.createPeer(xToolkit, null); 402 403 return (XDialog) UnoRuntime.queryInterface(XDialog.class, dialog); 404 } 405 406 private static class ResultHolder { 407 private Object result = null; 408 getResult()409 public Object getResult() 410 { 411 return result; 412 } 413 setResult(Object result)414 public void setResult(Object result) 415 { 416 this.result = result; 417 } 418 } 419 } 420