0

Parent of Jframe needs to be spreadsheet, I need to add Jframe to dialog created in open office during run time.So that spreadsheet is disabled for processing, clicking on buttons and only work can be done in dialog consisting of JFrame. I am using java, UNO. I tried to follow Creating dialog at runtime in open office

    package com.example;

import com.sun.star.awt.ActionEvent;
   import com.sun.star.awt.XButton;
   import com.sun.star.awt.XControl;
   import com.sun.star.awt.XControlContainer;
   import com.sun.star.awt.XControlModel;
   import com.sun.star.awt.XDialog;
   import com.sun.star.awt.XFixedText;
   import com.sun.star.awt.XListBox;
   import com.sun.star.awt.XToolkit;
   import com.sun.star.awt.XWindow;
   import com.sun.star.beans.XMultiPropertySet;
   import com.sun.star.beans.XPropertySet;
   import com.sun.star.container.XNameContainer;
   import com.sun.star.lang.EventObject;
   import com.sun.star.lang.XComponent;
   import com.sun.star.lang.XMultiComponentFactory;
   import com.sun.star.lang.XMultiServiceFactory;
   import com.sun.star.lang.XSingleComponentFactory;
   import com.sun.star.lib.uno.helper.Factory;
   import com.sun.star.lib.uno.helper.WeakBase;
   import com.sun.star.registry.XRegistryKey;
   import com.sun.star.uno.UnoRuntime;
   import com.sun.star.uno.XComponentContext;
      public final class AddOn extends WeakBase
        implements com.sun.star.frame.XDispatchProvider,
        com.sun.star.frame.XDispatch,
        com.sun.star.lang.XServiceInfo,
        com.sun.star.lang.XInitialization {

    private final XComponentContext m_xContext;
    private com.sun.star.frame.XFrame m_xFrame;
    private static final String m_implementationName = AddOn.class
            .getName();
    private static final String[] m_serviceNames = {
        "com.sun.star.frame.ProtocolHandler"};
    private static final String _buttonName = "Button1";
    private static final String _cancelButtonName = "CancelButton";
    private static final String _labelName = "Label1";
    private static final String _labelPrefix = "Number of button clicks: ";
    protected XNameContainer m_xDlgModelNameContainer;

    protected XMultiServiceFactory m_xMSFDialogModel;
      public AddOn(XComponentContext context) {
        m_xContext = context;
    }

    ;

    public static XSingleComponentFactory __getComponentFactory(String sImplementationName) {
        XSingleComponentFactory xFactory = null;

        if (sImplementationName.equals(m_implementationName)) {
            xFactory = Factory.createComponentFactory(AddOn.class, m_serviceNames);
        }
        return xFactory;
    }

    public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey) {
        return Factory.writeRegistryServiceInfo(m_implementationName,
                m_serviceNames,
                xRegistryKey);
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch queryDispatch(com.sun.star.util.URL aURL,
            String sTargetFrameName,
            int iSearchFlags) {
        if (aURL.Protocol.compareTo("com.example.addon:") == 0) {
            if (aURL.Path.compareTo("Command0") == 0) {
                return this;
            }
            if (aURL.Path.compareTo("Command1") == 0) {
                return this;
            }
            if (aURL.Path.compareTo("Command2") == 0) {
                return this;
            }
        }
        return null;
    }

    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch[] queryDispatches(
            com.sun.star.frame.DispatchDescriptor[] seqDescriptors) {
        int nCount = seqDescriptors.length;
        com.sun.star.frame.XDispatch[] seqDispatcher
                = new com.sun.star.frame.XDispatch[seqDescriptors.length];

        for (int i = 0; i < nCount; ++i) {
            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
                    seqDescriptors[i].FrameName,
                    seqDescriptors[i].SearchFlags);
        }
        return seqDispatcher;
    }
    // com.sun.star.frame.XDispatch:
    public void dispatch(com.sun.star.util.URL aURL,
            com.sun.star.beans.PropertyValue[] aArguments) {
        if (aURL.Protocol.compareTo("com.example.addon:") == 0) {
            if (aURL.Path.compareTo("Command0") == 0) {
                // add your own code here
                trigger("execute");
                return;
            }
            if (aURL.Path.compareTo("Command1") == 0) {
                // add your own code here
                return;
            }
            if (aURL.Path.compareTo("Command2") == 0) {
                // add your own code here
                return;
            }
        }
    }

    public void addStatusListener(com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL) {
        // add your own code here
    }

    public void removeStatusListener(com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL) {
        // add your own code here
    }

    // com.sun.star.lang.XServiceInfo:
    public String getImplementationName() {
        return m_implementationName;
    }

    public boolean supportsService(String sService) {
        int len = m_serviceNames.length;

        for (int i = 0; i < len; i++) {
            if (sService.equals(m_serviceNames[i])) {
                return true;
            }
        }
        return false;
    }

    public String[] getSupportedServiceNames() {
        return m_serviceNames;
    }

    // com.sun.star.lang.XInitialization:
    public void initialize(Object[] object)
            throws com.sun.star.uno.Exception {
        if (object.length > 0) {
            m_xFrame = (com.sun.star.frame.XFrame) UnoRuntime.queryInterface(
                    com.sun.star.frame.XFrame.class, object[0]);
        }
    }

    // XJobExecutor
    public void trigger(String sEvent) {
        if (sEvent.compareTo("execute") == 0) {
            try {
                createDialog();
            } catch (Exception e) {
                throw new com.sun.star.lang.WrappedTargetRuntimeException(e.getMessage(), this, e);
            }
        }
    }

    /**
     * method for creating a dialog at runtime
     */
    private void createDialog() throws com.sun.star.uno.Exception {

        // get the service manager from the component context
        XMultiComponentFactory xMultiComponentFactory = m_xContext.getServiceManager();

        // create the dialog model and set the properties
        Object dialogModel = xMultiComponentFactory.createInstanceWithContext(
                "com.sun.star.awt.UnoControlDialogModel", m_xContext);
        XPropertySet xPSetDialog = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, dialogModel);
        xPSetDialog.setPropertyValue("PositionX", 100);
        xPSetDialog.setPropertyValue("PositionY", 100);
        xPSetDialog.setPropertyValue("Width", 150);
        xPSetDialog.setPropertyValue("Height", 200);
        xPSetDialog.setPropertyValue("Title", "Runtime Dialog Button 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(
                "com.sun.star.awt.UnoControlButtonModel");
        XPropertySet xPSetButton = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, buttonModel);
        xPSetButton.setPropertyValue("PositionX", 20);
        xPSetButton.setPropertyValue("PositionY", 70);
        xPSetButton.setPropertyValue("Width", 50);
        xPSetButton.setPropertyValue("Height", 14);
        xPSetButton.setPropertyValue("Name", _buttonName);
        xPSetButton.setPropertyValue("TabIndex", (short) 0);
        xPSetButton.setPropertyValue("Label", "Click Me");

        // create the label model and set the properties
        Object labelModel = xMultiServiceFactory.createInstance(
                "com.sun.star.awt.UnoControlFixedTextModel");
        XPropertySet xPSetLabel = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, labelModel);
        xPSetLabel.setPropertyValue("PositionX", new Integer(40));
        xPSetLabel.setPropertyValue("PositionY", new Integer(30));
        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);

        // create a Cancel button model and set the properties
        Object cancelButtonModel = xMultiServiceFactory.createInstance(
                "com.sun.star.awt.UnoControlButtonModel");
        XPropertySet xPSetCancelButton = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, cancelButtonModel);
        xPSetCancelButton.setPropertyValue("PositionX", new Integer(80));
        xPSetCancelButton.setPropertyValue("PositionY", new Integer(70));
        xPSetCancelButton.setPropertyValue("Width", new Integer(50));
        xPSetCancelButton.setPropertyValue("Height", new Integer(14));
        xPSetCancelButton.setPropertyValue("Name", _cancelButtonName);
        xPSetCancelButton.setPropertyValue("TabIndex", new Short((short) 2));
        xPSetCancelButton.setPropertyValue("PushButtonType", new Short((short) 2));
        xPSetCancelButton.setPropertyValue("Label", new String("Cancel"));

// insert the control models into the dialog model
        XNameContainer xNameCont = (XNameContainer) UnoRuntime.queryInterface(
                XNameContainer.class, dialogModel);
        xNameCont.insertByName(_buttonName, buttonModel);
        xNameCont.insertByName(_labelName, labelModel);
        xNameCont.insertByName(_cancelButtonName, cancelButtonModel);

        // create the dialog control and set the model
        Object dialog = xMultiComponentFactory.createInstanceWithContext(
                "com.sun.star.awt.UnoControlDialog", m_xContext);
        XControl xControl = (XControl) UnoRuntime.queryInterface(
                XControl.class, dialog);
        XControlModel xControlModel = (XControlModel) UnoRuntime.queryInterface(
                XControlModel.class, dialogModel);
        xControl.setModel(xControlModel);

        // 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(
                "com.sun.star.awt.ExtToolkit", m_xContext);
        XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(
                XToolkit.class, toolkit);
        XWindow xWindow = (XWindow) UnoRuntime.queryInterface(
                XWindow.class, xControl);
        xWindow.setVisible(false);
        xControl.createPeer(xToolkit, null);

        // execute the dialog
        XDialog xDialog = (XDialog) UnoRuntime.queryInterface(
                XDialog.class, dialog);
        xDialog.execute();

        // dispose the dialog
        XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
                XComponent.class, dialog);
        xComponent.dispose();

    }

    /**
     * action listener
     */
    public class ActionListenerImpl implements com.sun.star.awt.XActionListener {

        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
            _nCounts++;

            // set label text
            Object label = _xControlCont.getControl("Label1");
            XFixedText xLabel = (XFixedText) UnoRuntime.queryInterface(
                    XFixedText.class, label);
            xLabel.setText(_labelPrefix + _nCounts);
        }
    }
}

This is a sample project , I am able to create a dialog box on open office, but my next task, issue is how should I add JFrame to the dialog.

I have tried this as well

    public void addConfigToDialog() throws Exception { // to be tested
        setIdsconfig(IDSConfig.getIDSConfigObject(getM_xContext(), getXcomponent()));//calls singleton method for IDSConfig
        JDialog dialog = new JDialog(getIdsconfig());
        showModal(dialog);
    }

    public void showModal(javax.swing.JDialog dialog) {
        try {
            XMultiComponentFactory multiComponentFactory = m_xContext.getServiceManager();

            // create the dialog model and set the properties
            Object tempDialogModel
                    = multiComponentFactory.createInstanceWithContext("com.sun.star.awt.UnoControlDialogModel",
                            m_xContext);
            XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, tempDialogModel);
            properties.setPropertyValue("PositionX", new Integer(0));
            properties.setPropertyValue("PositionY", new Integer(0));
            properties.setPropertyValue("Width", new Integer(150));
            properties.setPropertyValue("Height", new Integer(100));
//            properties.setPropertyValue("Enabled", false);
            final Object tempDialog
                    = multiComponentFactory.createInstanceWithContext("com.sun.star.awt.UnoControlDialog",
                            m_xContext);
            XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, tempDialog);
            XControlModel xControlModel = (XControlModel) UnoRuntime.queryInterface(XControlModel.class, tempDialogModel);
            xControl.setModel(xControlModel);

            Object toolkit
                    = multiComponentFactory.createInstanceWithContext("com.sun.star.awt.Toolkit",
                            m_xContext);
            XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class, toolkit);
            XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xControl);
            xWindow.setVisible(false);//changed
            xControl.createPeer(xToolkit, null);

            final XDialog xTempDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, tempDialog);
            dialog.addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent e) {
                    xTempDialog.endExecute();
                    XComponent component = (XComponent) UnoRuntime.queryInterface(XComponent.class, tempDialog);
                    component.dispose();
                }
            });

            dialog.setVisible(true);
            xTempDialog.execute();

        } catch (com.sun.star.uno.Exception ex) {
            ex.printStackTrace();
        }
    }

I used this piece of code, but having issues in getting data of Jframe into dialog of openoffice. I am calling addConfigToDialog() on a button in open office spreadsheet.

Rubal
  • 11
  • 6
  • Were you able to get the example to run? If not, then please describe the error or failed behavior. If it worked, then in what ways did it not do what you need? OpenOffice dialogs use [their own API](http://www.openoffice.org/api/), not swing components such as [JFrame](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html). – Jim K Jun 17 '16 at 16:58
  • I am now able to create sample dialog, with buttons and labels now @JimK, but I need to add JFrame somehow to dialog of open office, because I have already made a lot of functionality into that JFrame – Rubal Jun 18 '16 at 06:18

2 Answers2

0

There are several choices, but I do not think what you are asking is possible.

  1. What I recommend is to simply use OpenOffice dialogs, and rewrite the JFrame functionality to use the UNO API instead. This is how your example works.

  2. Or instead of creating an OpenOffice dialog, you could create a Java swing window. An example is the ScriptSelector. Then the window will function independently of OpenOffice, and could easily appear behind the OpenOffice window instead of in front. It should be possible to lock the OpenOffice window controller to prevent editing.

  3. Or create a windowed AWT application in Java, and embed an OpenOffice window using an OOBean.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • 1st one doesn't seems to be feasible option, 2nd one requires scripting as in BeanShell (requires XScriptContext). I am not able to get, how should I get the value of XScriptContext . 3rd one with OOoBeanViewer.java could be found on same link as of ScriptSelector, runs with exceptions, I am not able to solve it. – Rubal Jun 20 '16 at 10:32
  • I tried, but I do not want to use any scripting framework, neither applets ie option 2 and 3. – Rubal Jun 20 '16 at 12:30
  • For a "Hello World" example that uses `XScriptContext`, follow the instructions for Java at https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Writing_Macros. – Jim K Jun 20 '16 at 13:47
  • yes, I saw about its working, installation, checked out the code for XScriptContext too. I have tried it but I don't want to create any macro or use any scripting for my project. – Rubal Jun 21 '16 at 05:42
0

I didn't found any solution of how to add Jframe to dialog in open office, instead I found another way was to lock the container window, pass XFrame to parameter of JFrame. And I can use window events of open office and that of java.awt.event and use

XDesktop desktop;//get the current desktop object being used
desktop.getCurrentFrame().getContainerWindow().setEnable(false);// uses uno component window methods and uno objects methods

then enable it in

JFrame jFrame;//get the JFrame being used
jFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
getxDesktop().getCurrentFrame().getContainerWindow().setEnable(true);//get Xdesktop being in used and enable the containerWindow to true
}
});

Enable the container window, where so ever is required to unlock the spreadsheet. Remember, it is necessary to unlock the spreadsheet, by enabling the containerWindow to true, else your document is locked, and can only be closed by cancelling the uno-run project or by ending the related process in task manager or via command in terminal.

Rubal
  • 11
  • 6