Attach JFrame to XDialog for Calc

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Attach JFrame to XDialog for Calc

Postby rubal » Wed Jun 15, 2016 10:34 am

AddOn.zip
To start with a sample of dialog, I have tried this example, consists of sample project. I think it is creating dialog as swriter, if I am wrong somewhere, do correct me.
(42.71 KiB) Downloaded 94 times
I need to create a dialog specific to spreadhseet( multiple spreadsheets may be opened containing their specific dialog boxes). I need to attach JFrame to it, I thought of JDialog of swings, but the constructor having parent cannot be spreadsheet, which can be Window, Dialog, Frame. I need to use Dialog ModalityType = DOCUMENT_MODAL.
I have tried searching on google and on this open office forum, So I got, I need to use open office XDialog(found in UNO), but I need to use JFrame and parent has to be spreadsheet. I read through DevelopersGuide_OOo3.1.0_19GUI.odt, but didn't got suitable examples for help to attach swings, like JFrame.
I would be using this for all OS, MAC, Linux, Windows. But currently developing in Ubuntu, using java in netbeans.

My first step would be to create a XDialog, but I am not able to achieve that either. I have UnoDialogSample2.java and UnoDialogSample.java are the standard examples being used in DevelopersGuide_OOo3.1.0_19GUI.odt. I have Addon (Apache open office project). I am not able to make it, how to call / create dialog on click of a button.

Thanks for reading the post and trying to help in advance. :)
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am

Re: Attach JFrame to XDialog for Calc

Postby rubal » Fri Jun 17, 2016 9:26 am

I am not able to crate any dialog, really confused with it. Can someone help?
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am

Re: Attach JFrame to XDialog for Calc

Postby rubal » Sat Jun 18, 2016 9:04 am

I am able to create a dialog in open office now . I have used the following code to add dialog in open office.
Code: Select all   Expand viewCollapse view
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);
        }
    }

}


I need to add jframe to dialog now.
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am

Re: Attach JFrame to XDialog for Calc

Postby Villeroy » Sat Jun 18, 2016 10:58 am

It is possible to load office dialogs built in the graphical dialog designer of the Basic IDE. IMHO you should do your entire GUI with whatever Java toolset you prefer. Stay away from all that com.sun.star.awt. It's not worthy if you have better toolsets at hand.
This is a user forum. Hardly anybody does any Java coding.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 25708
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Attach JFrame to XDialog for Calc

Postby rubal » Sat Jun 18, 2016 12:19 pm

Villeroy wrote:It is possible to load office dialogs built in the graphical dialog designer of the Basic IDE. IMHO you should do your entire GUI with whatever Java toolset you prefer. Stay away from all that com.sun.star.awt. It's not worthy if you have better toolsets at hand.
This is a user forum. Hardly anybody does any Java coding.


I am not able to understand what you mean by "It is possible to load office dialogs built in the graphical dialog designer of the Basic IDE.". To make it clear, I want to ask you, are you talking about Basic IDE in Open Office, and creating dialogs in it using GUI buttons in it.

The toolset that I am using is of Swings in Java. But what could be the possible options, in which a spreadsheet showing some frame, in which spreadsheet specific controls are selected, and in similar way, we open different spreadsheet simultaneously, and corresponding to that particular spreadsheet, we have the frame, having the buttons selected corresponding to 2nd selected spreadsheet.

Also there is functionality of, when frame is opened, we cant click on any button on that spreadsheet, and neither can't edit the spreadsheet.

The possible solution I thought of was putting frame into dialog.

Another solution was using XActionLockable and com.sun.star.sheet.XViewFreezable, that is freezing or locking the spreadsheet. but I am not able to achieve it either.
Code: Select all   Expand viewCollapse view
        XActionLockable xActionInterface = null;
        xActionInterface = (XActionLockable) UnoRuntime.queryInterface(
                XActionLockable.class, xSpreadsheet);

        // lock all actions
        xActionInterface.addActionLock();
        com.sun.star.sheet.XViewFreezable xFreeze = UnoRuntime.queryInterface(com.sun.star.sheet.XViewFreezable.class, xSpreadsheetController);
        xFreeze.freezeAtPosition(1, 2);


Last solution was if possible, using MediaDescriptor and to somehow make it read only the document, it didn't seemed to work either.
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am

Re: Attach JFrame to XDialog for Calc

Postby RoryOF » Sat Jun 18, 2016 12:41 pm

I have very limited (read: almost no) Java experience, but I can recollect using swing in Java to build a dialog. You will find details of this in Chapter 4 of 'NetBeans IDE 8 Cookbook' by Salter and Dantas, which can be found online for download (sorry, I don't have a URL)
Apache OpenOffice 4.1.5 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
14 October 2016 was Pooh's 90th birthday
User avatar
RoryOF
Moderator
 
Posts: 27190
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Attach JFrame to XDialog for Calc

Postby rubal » Sat Jun 18, 2016 1:20 pm

RoryOF wrote:I have very limited (read: almost no) Java experience, but I can recollect using swing in Java to build a dialog. You will find details of this in Chapter 4 of 'NetBeans IDE 8 Cookbook' by Salter and Dantas, which can be found online for download (sorry, I don't have a URL)



I have already tried, how to create dialog in Swings , but in argument I need to pass a parent which can be a dialog, frame and window. But in my case the parent is the open office spreadsheet. So creating dialog in swings, does not seem to be feasible.
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am

Re: Attach JFrame to XDialog for Calc

Postby Villeroy » Sat Jun 18, 2016 3:31 pm

Ask the developers on their mailing list. I'm confident, there is some solution.
http://blog.gmane.org/gmane.comp.apache ... fice.devel
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 25708
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Attach JFrame to XDialog for Calc

Postby rubal » Tue Jun 21, 2016 3:01 pm

I have mailed to the developers already, I saw this post http://openoffice.2283327.n4.nabble.com/Modal-Swing-JDialog-td2770139.html might be useful somehow

Code: Select all   Expand viewCollapse view
public void addConfigToDialog() throws Exception { // to be tested
        getIdsconfig().setVisible(true);// getIdsconfig() returns JFrame containing controls in swings
        JDialog dialog = new JDialog(getIdsconfig());
        dialog.setLocation(100, 100);
        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(1));
            properties.setPropertyValue("Height", new Integer(1));
            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);
            xControl.createPeer(xToolkit, null);

            final XDialog xTempDialog = (XDialog) UnoRuntime.queryInterface(XDialog.class, tempDialog);
//            dialog.add(getIdsconfig());
//            getIdsconfig().setVisible(true);
            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. None data of JFrame is visible. I am calling addConfigToDialog() on a button in open office spreadsheet.
open office 4.1.2
Ubuntu
rubal
 
Posts: 22
Joined: Fri May 27, 2016 8:41 am


Return to External Programs

Who is online

Users browsing this forum: No registered users and 1 guest