Creating a new top level menu tab at runtime using Java

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

Creating a new top level menu tab at runtime using Java

Postby gurujuly » Thu Sep 07, 2017 2:03 pm

Hi All,

I'm looking at creating a new top level Menu next to Help menu in my application. The below link shows how to do it using an Addon.

https://wiki.openoffice.org/wiki/OpenOffice_Add-On_Project_Type

However, I want to do it programmatically in Java like how we Disable and Enable the commands at runtime in the below link.

https://wiki.openoffice.org/wiki/Documentation/DevGuide/WritingUNO/Disable_Commands

Any idea how to create a new menu tab or to customize the menus. Thanks.
Open Office 4.3 and Windows 10.
gurujuly
 
Posts: 3
Joined: Thu Sep 07, 2017 1:44 pm

Re: Creating a new top level menu tab at runtime using Java

Postby gurujuly » Mon Sep 25, 2017 12:11 pm

I was able to get the top level menu runtime as below. Any idea or suggestions on how to handle the menu events? Thanks.

Code: Select all   Expand viewCollapse view
package com.example;

import com.sun.star.awt.MenuEvent;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexContainer;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrame;
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.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.ui.XModuleUIConfigurationManagerSupplier;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import java.util.Random;

/**
*
* @author GS027523
*/
public class Sample {

   
    protected PropertyValue[] SetTopLevelMenuProperties(String CommandId, String _label, XSingleComponentFactory xCfgMgrFactory, XComponentContext xContext)
        {

            PropertyValue[] MenuProperty = new PropertyValue[4];
           
            try
            {
                MenuProperty[0] = new PropertyValue();
                MenuProperty[0].Name = "CommandURL";
                MenuProperty[0].Value = "curl";
                MenuProperty[1] = new PropertyValue();
                MenuProperty[1].Name = "Label";
               
                MenuProperty[1].Value = "Cerner";
                MenuProperty[2] = new PropertyValue();
                MenuProperty[2].Name = "Type";
               
                MenuProperty[2].Value = "typ";
                MenuProperty[3] = new PropertyValue();
                MenuProperty[3].Name = "ItemDescriptorContainer";
               
                MenuProperty[3].Value = xCfgMgrFactory.createInstanceWithContext( xContext );;
            }
            catch (com.sun.star.uno.Exception ex)
            {
                ex.toString();
            }
            return MenuProperty;

        }
        static protected PropertyValue[] SetItemProperty(String CommandId, String _label)
        {

            PropertyValue[] itemProperty = new PropertyValue[3];
           
            itemProperty[0] = new PropertyValue();
            itemProperty[0].Name = "CommandURL";
            itemProperty[0].Value = "cmd";
            itemProperty[1] = new PropertyValue();
            itemProperty[1].Name = "Label";
           
            itemProperty[1].Value = "Cerner";
            itemProperty[2] = new PropertyValue();
            itemProperty[2].Name = "Type";
           
            itemProperty[2].Value = "typ";

            return itemProperty;
        }
   
   
    private XComponentContext mxRemoteContext = null;
    private XMultiComponentFactory mxRemoteServiceManager = null;   
    private XTextDocument mxDoc = null;
    private static XMultiServiceFactory multiServiceFactory = null;
    private XMultiServiceFactory mxFactory = null;
    private XPropertySet  mxDocProps = null;
    private XText mxDocText = null;
    private XTextCursor mxDocCursor = null;
    private XTextContent mxFishSection = null;
    private Random maRandom = null;
    private String menuBarURL = "private:resource/menubar/menubar";
    private String topLevelMenuCmd = ".uno:dct_test";
        private String topLevelMenuLabel = "Sun Developer Connection";
        private String itemCmd = "macro:///Standard.Module1.myMacro()";
        private String itemLabel = "myMacro";

        public String res = "1";
   
    /** Creates a new instance of Sample */
    public Sample() {
    }
   

    public static void main(String[] args) {
        try {
           
            Sample Sample1 = new Sample();
           
            Sample1.runDemo();
           
        }
        catch (java.lang.Exception e){
            e.toString();
            e.printStackTrace();
        }
        finally {
            System.exit( 0 );
        }
    }
   
    protected void runDemo() throws java.lang.Exception {
           
            menuExample();
    }
   
   
    private void menuExample () throws java.lang.Exception {
       
        XComponentContext localContext = Bootstrap.bootstrap();
        XComponent xEmptyWriterComponent = newDocComponent("swriter");
//            XComponentContext localContext = Bootstrap.bootstrap();
//           
            //multiServiceFactory = (XMultiServiceFactory) localContext.getServiceManager();
            //XMultiComponentFactory xMCF = localContext.getServiceManager();
           
            mxDoc = (XTextDocument)UnoRuntime.queryInterface(
            XTextDocument.class, xEmptyWriterComponent);
             
        // Access the text document's multi service factory, which we will need
        // for most of the following examples
        multiServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
            XMultiServiceFactory.class, mxRemoteServiceManager);
           
            Object o = (XInterface)multiServiceFactory.createInstance(
                    "com.sun.star.ui.ModuleUIConfigurationManagerSupplier");
           
            XModuleUIConfigurationManagerSupplier xmucm =
                    UnoRuntime.queryInterface(XModuleUIConfigurationManagerSupplier.class, o);
           
        //com.sun.star.ui.XModuleUIConfigurationManagerSupplier xmucm = (com.sun.star.ui.XModuleUIConfigurationManagerSupplier)multiServiceFactory.createInstance("com.sun.star.ui.ModuleUIConfigurationManagerSupplier");
        com.sun.star.ui.XUIConfigurationManager xui_conf_manager = xmucm.getUIConfigurationManager("com.sun.star.text.TextDocument");
       
            com.sun.star.container.XIndexAccess _xindexaccess = xui_conf_manager.getSettings("private:resource/menubar/menubar", true);
           
            XIndexContainer settingsContainer = (XIndexContainer) UnoRuntime
            .queryInterface(XIndexContainer.class, _xindexaccess);
           
            XSingleComponentFactory xCfgMgrFactory = (XSingleComponentFactory) UnoRuntime
            .queryInterface(XSingleComponentFactory.class, _xindexaccess);
           
            //XSingleComponentFactory xCfgMgrFactory = (XSingleComponentFactory)_xindexaccess;
           
            PropertyValue[] topLevelMenuProperty = SetTopLevelMenuProperties(topLevelMenuCmd, topLevelMenuLabel, xCfgMgrFactory, mxRemoteContext);
            PropertyValue[] menuItemProperty = SetItemProperty(itemCmd, itemLabel);
           
            com.sun.star.uno.Any a = new com.sun.star.uno.Any(topLevelMenuProperty.getClass(), topLevelMenuProperty);
            //XIndexContainer _xindexcontainer = (XIndexContainer)xCfgMgrFactory;
            settingsContainer.insertByIndex(4, a);

            xui_conf_manager.replaceSettings(menuBarURL, _xindexaccess);
           
    }
   
    private XMultiComponentFactory getRemoteServiceManager()
        throws java.lang.Exception
    {
        if (mxRemoteContext == null && mxRemoteServiceManager == null) {
            // get the remote office context. If necessary a new office
            // process is started
            mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
            System.out.println("Connected to a running office ...");
            mxRemoteServiceManager = mxRemoteContext.getServiceManager();
        }
        return mxRemoteServiceManager;
    }
       
    protected XComponent newDocComponent(String docType)
        throws java.lang.Exception
    {
        String loadUrl = "private:factory/" + docType;
        mxRemoteServiceManager = this.getRemoteServiceManager();
        Object desktop = mxRemoteServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", mxRemoteContext);
        XComponentLoader xComponentLoader = (XComponentLoader)
            UnoRuntime.queryInterface(XComponentLoader.class, desktop);
        PropertyValue[] loadProps = new PropertyValue[0];
        return xComponentLoader.loadComponentFromURL(loadUrl, "_blank",
                                                     0, loadProps);   
    }

}

Open Office 4.3 and Windows 10.
gurujuly
 
Posts: 3
Joined: Thu Sep 07, 2017 1:44 pm


Return to External Programs

Who is online

Users browsing this forum: No registered users and 3 guests