Creating Dialogs at Runtime

From Apache OpenOffice Wiki
Jump to: navigation, search

When using Basic, the dialog editor is a tool for designing dialogs. Refer to Basic IDE for additional information. Since 2.0, dialogs that have been built with the dialog editor can be loaded by a macro written in any of the supported scripting framework languages (BeanShell, JavaScript, Java, Basic) by using the API. See section Using the Scripting Framework for more details.

In addition, it is also possible to create dialogs at runtime in a similar way as Java Swing components are created. Also, the event listeners are registered at runtime at the appropriate controls.

An example dialog

In the Java example described in this section, a simple modal dialog is created at runtime containing a command button and label field. Each time the user clicks on the button, the label field is updated and the total number of button clicks is displayed.

The dialog is implemented as a UNO component in Java that is instantiated with the service name For details about writing a Java component and the implementation of the UNO core interfaces, refer to Storing the Service Manager for Further Use. The method that creates and executes the dialog is shown below.

  /** method for creating a dialog at runtime
  private void createDialog() throws {
      // get the service manager from the component context
      XMultiComponentFactory xMultiComponentFactory = _xComponentContext.getServiceManager();
      // create the dialog model and set the properties
      Object dialogModel = xMultiComponentFactory.createInstanceWithContext(
          "", _xComponentContext);
      XPropertySet xPSetDialog = (XPropertySet)UnoRuntime.queryInterface(
          XPropertySet.class, dialogModel); 
      xPSetDialog.setPropertyValue("PositionX", new Integer(100));
      xPSetDialog.setPropertyValue("PositionY", new Integer(100));
      xPSetDialog.setPropertyValue("Width", new Integer(150));
      xPSetDialog.setPropertyValue("Height", new Integer(100));
      xPSetDialog.setPropertyValue("Title", new String("Runtime Dialog Demo"));
      // get the service manager from the dialog model
      XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
          XMultiServiceFactory.class, dialogModel);
      // create the button model and set the properties
      Object buttonModel = xMultiServiceFactory.createInstance(
          "" );
      XPropertySet xPSetButton = (XPropertySet)UnoRuntime.queryInterface(
          XPropertySet.class, buttonModel);
      xPSetButton.setPropertyValue("PositionX", new Integer(50));
      xPSetButton.setPropertyValue("PositionY", new Integer(30));
      xPSetButton.setPropertyValue("Width", new Integer(50));
      xPSetButton.setPropertyValue("Height", new Integer(14));
      xPSetButton.setPropertyValue("Name", _buttonName);
      xPSetButton.setPropertyValue("TabIndex", new Short((short)0)); 
      xPSetButton.setPropertyValue("Label", new String("Click Me"));
      // create the label model and set the properties
      Object labelModel = xMultiServiceFactory.createInstance(
          "" );
      XPropertySet xPSetLabel = ( XPropertySet )UnoRuntime.queryInterface(
          XPropertySet.class, labelModel );
      xPSetLabel.setPropertyValue("PositionX", new Integer(40));
      xPSetLabel.setPropertyValue("PositionY", new Integer(60));
      xPSetLabel.setPropertyValue("Width", new Integer(100));
      xPSetLabel.setPropertyValue("Height", new Integer(14));
      xPSetLabel.setPropertyValue("Name", _labelName);
      xPSetLabel.setPropertyValue("TabIndex", new Short((short)1)); 
      xPSetLabel.setPropertyValue("Label", _labelPrefix);
      // insert the control models into the dialog model
      XNameContainer xNameCont = (XNameContainer)UnoRuntime.queryInterface(
          XNameContainer.class, dialogModel);
      xNameCont.insertByName(_buttonName, buttonModel);
      xNameCont.insertByName(_labelName, labelModel);
      // create the dialog control and set the model
      Object dialog = xMultiComponentFactory.createInstanceWithContext(
          "", _xComponentContext);
      XControl xControl = (XControl)UnoRuntime.queryInterface(
          XControl.class, dialog );
      XControlModel xControlModel = (XControlModel)UnoRuntime.queryInterface(
          XControlModel.class, dialogModel); 
      // add an action listener to the button control
      XControlContainer xControlCont = (XControlContainer)UnoRuntime.queryInterface(
          XControlContainer.class, dialog); 
      Object objectButton = xControlCont.getControl("Button1");
      XButton xButton = (XButton)UnoRuntime.queryInterface(XButton.class, objectButton);
      xButton.addActionListener(new ActionListenerImpl(xControlCont));
      // create a peer
      Object toolkit = xMultiComponentFactory.createInstanceWithContext(
          "", _xComponentContext); 
      XToolkit xToolkit = (XToolkit)UnoRuntime.queryInterface(XToolkit.class, toolkit);
      XWindow xWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, xControl);
      xControl.createPeer(xToolkit, null);
      // execute the dialog
      XDialog xDialog = (XDialog)UnoRuntime.queryInterface(XDialog.class, dialog);
      // dispose the dialog
      XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class, dialog);

First, a dialog model is created by prompting the ServiceManager for the service. Then, the position, size and title of the dialog are set using the interface. In performance critical applications, the use of the interface is recommended. At this point, the dialog model describes an empty dialog, which does not contain any control models.

All control models in a dialog container have the common properties "PositionX", "PositionY", "Width", "Height", "Name", "TabIndex", "Step" and "Tag". These properties are optional and only added if the control model is created by a special object factory, namely the dialog model. Therefore, a dialog model also supports the interface. If the control model is created by the ServiceManager, these common properties are missing.

Documentation note.png Note that control models have the common properties "PositionX", "PositionY", "Width", "Height", "Name", "TabIndex", "Step" and "Tag" only if they were created by the dialog model that they belong to.

After the control models for the command button and label field are created, their position, size, name, tab index and label are set. Then, the control models are inserted into the dialog model using the interface. The model of the dialog has been fully described.

To display the dialog on the screen, a dialog control is created and the corresponding model is set. An action listener is added to the button control, because the label field is updated whenever the user clicks on the command button. The listener is explained below. Before the dialog is shown, a window or a peer is created on the screen. Finally, the dialog is displayed on the screen using the execute method of the interface.

The implementation of the action listener is shown in the following example.

 /** action listener
 public class ActionListenerImpl implements {
     private int _nCounts = 0;
     private XControlContainer _xControlCont;
     public ActionListenerImpl(XControlContainer xControlCont) {
         _xControlCont = xControlCont;
     // XEventListener
     public void disposing(EventObject eventObject) {
         _xControlCont = null;
     // XActionListener
     public void actionPerformed(ActionEvent actionEvent) {
         // increase click counter
         // set label text
         Object label = _xControlCont.getControl("Label1");
         XFixedText xLabel = (XFixedText)UnoRuntime.queryInterface(XFixedText.class, label); 
         xLabel.setText(_labelPrefix + _nCounts);

The action listener is fired each time the user clicks on the command button. In the actionPerformed method of the interface, an internal counter for the number of button clicks is increased. Then, this number is updated in the label field. In addition, the disposing method of the parent interface is implemented.

Our sample component executes the dialog from within the office by implementing the trigger method of the interface:

 public void trigger(String sEvent) {
     if (sEvent.equals("execute")) {
         try {
         catch (Exception e) {
             throw new, this, e);

A simple Basic macro that instantiates the service of our sample component and executes the dialog is shown below.

 Sub Main
     Dim oJobExecutor
     oJobExecutor = CreateUnoService("")
 End Sub

In future versions of, a method for executing dialogs created at runtime will be provided.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages