Embed into my JAVA Swing App

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Embed into my JAVA Swing App

Post by wanglong »

I'm inspired byhttps://wiki.openoffice.org/wiki/Docume ... ntegration.

And coding JAVA like this:

Code: Select all

	// get Remote Service Manager
        mxRemoteServiceManager = this.getRemoteServiceManager();

        Object toolKit = mxRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.awt.Toolkit", mxRemoteContext);
        XSystemChildFactory xSystemChildFactory = UnoRuntime.queryInterface(XSystemChildFactory.class, toolKit);

	//get JAVA Swing proccess handler.
        WindowHandler windowHandler = new WindowHandler();
        long nHandle = windowHandler.getWindowHandle();
        byte[] lIgnoredProcessID = new byte[4];
        com.sun.star.awt.XWindowPeer xPeer =
                xSystemChildFactory.createSystemChild(
                        (Object) nHandle,
                        lIgnoredProcessID,
                        com.sun.star.lang.SystemDependent.SYSTEM_WIN32);

        com.sun.star.awt.XWindow xFrameContainerWindow  =
                (com.sun.star.awt.XWindow)UnoRuntime.queryInterface(
                        com.sun.star.awt.XWindow.class,
                        xPeer);

        Object object = mxRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Task", mxRemoteContext);
        if(object == null){
            object = mxRemoteServiceManager.createInstanceWithContext(
                    "com.sun.star.frame.Frame", mxRemoteContext);
        }
        XFrame xFrame = (XFrame) UnoRuntime
                .queryInterface(XFrame.class, object);

        xFrame.initialize(xFrameContainerWindow);
        xFrame.setName("myframe");      //Frame identity
        String debug = xFrame.getName();

        // get DeskTop and XComponentLoader
        desktop = mxRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Desktop", mxRemoteContext);
        XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, desktop);

        XFramesSupplier xFramesSupplier = (XFramesSupplier)UnoRuntime.queryInterface(
                XFramesSupplier.class, desktop);
        XFrames xChildContainer = xFramesSupplier.getFrames();
        xChildContainer.append(xFrame);

        XSystemDependentWindowPeer xSystemDependentWindowPeer = UnoRuntime.queryInterface(
                XSystemDependentWindowPeer.class, xFrameContainerWindow);
        xFrameContainerWindow.setVisible(true);
        xSystemDependentWindowPeer.getWindowHandle(lIgnoredProcessID,
                com.sun.star.lang.SystemDependent.SYSTEM_WIN32);

        // construct properties
        PropertyValue[] loadProps = new PropertyValue[2];
        loadProps[0] = new PropertyValue();
        loadProps[0].Name = "AsTemplate";
        loadProps[0].Value = new Boolean(true);

        // hidden or not
        loadProps[1] = new PropertyValue();
        loadProps[1].Name = "Hidden";
        loadProps[1].Value = Hidden;

        // get Doc Path
        java.io.File sourceFile = new java.io.File(filePath);
        StringBuffer sTemplateFileUrl = new StringBuffer("file:///");
        sTemplateFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
        String loadUrl = sTemplateFileUrl.toString();
        // load
        xComp = xComponentLoader.loadComponentFromURL(loadUrl, "myframe",
                4, loadProps);
But this doesn't work like what I thought.The document is still opened from a separate window.
Libre Office 7.6 on Windows 11.
Post Reply