Hi.

I have a problem when trying to add custom menus to
the OO 2.0 with Java.

I've translated the Basic-based example from
http://www.oooforum.org/forum/viewtopic.phtml?t=18491&highlight=
into Java. It works only to some extent. The place
with the problem is marked with the "PROBLEM" keyword
in the code. Short: a custom submenu is not shown,
something like the separator is shown insteed. The
submenu is show under certain condition, this is
mentioned in the code.

Thank you in advance, Andrej.

Code:

package de.muenchen.allg.itd51.wollmux.oooui;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XIndexContainer;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XLayoutManager;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import
com.sun.star.ui.XModuleUIConfigurationManagerSupplier;
import com.sun.star.ui.XUIConfigurationManager;
import com.sun.star.ui.XUIElement;
import com.sun.star.ui.XUIElementSettings;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

import de.muenchen.allg.afid.UnoService;

/*
 * TestMisc2.java
 *
 * Created on 11. April 2002, 08:47
 */

/**
 *
 * @author Martin Gallwey, Dietrich Schulten
 */
public class TestMisc2 {
    // adjust these constant to your local printer!
    private XComponentContext mxRemoteContext = null;
    private XMultiComponentFactory
mxRemoteServiceManager = null;
    private XDesktop xDesktop;
    private XModel xModel;
    private XController xController;
    private XFrame xFrame;
    private XLayoutManager xLayoutManager;
    private XUIConfigurationManager uiConfigManager;
    private XModuleUIConfigurationManagerSupplier
xCmSupplier;

    // 0. misc
    String sMenuBar =
"private:resource/menubar/menubar";
    String sMyPopupMenuCmdId = "TestMenu";
    private XIndexAccess settings;

    /** Creates a new instance of TestMisc2 */
    public TestMisc2() {
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        TestMisc2 textDocuments1 = new TestMisc2();
        try {
            textDocuments1.runDemo();
        } catch (java.lang.Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    protected void runDemo() throws
java.lang.Exception {
        // storePrintExample(); // depends on
printername
        // templateExample();
        menuExampe(); // makes changes to the current
document,
        // use with care
        // editingExample();
    }

    protected void menuExampe() throws Exception {
       
        // 1. standart steps, init variables like
XDesktop etc.
        mxRemoteServiceManager =
this.getRemoteServiceManager();
        xDesktop = (XDesktop)
UnoRuntime.queryInterface(XDesktop.class,
               
mxRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Desktop",
mxRemoteContext));
        xModel = (XModel)
UnoRuntime.queryInterface(XModel.class,
        xDesktop.getCurrentComponent());
        xController = xModel.getCurrentController();
        xFrame = xController.getFrame();
        XPropertySet xps = (XPropertySet)
UnoRuntime.queryInterface(
                XPropertySet.class, xFrame);
        xLayoutManager = (XLayoutManager)
UnoRuntime.queryInterface(
                XLayoutManager.class,
               
xps.getPropertyValue("LayoutManager"));
        xCmSupplier =
(XModuleUIConfigurationManagerSupplier) UnoRuntime.
             
queryInterface(XModuleUIConfigurationManagerSupplier.class,
             
mxRemoteServiceManager.createInstanceWithContext("com.sun.star.ui.ModuleUIConfigurationManagerSupplier",mxRemoteContext));
        uiConfigManager =
xCmSupplier.getUIConfigurationManager("com.sun.star.text.TextDocument");

        settings =
uiConfigManager.getSettings(sMenuBar, true);

        // 2. init menu elements
        XUIElement oMenuBar =
xLayoutManager.getElement(sMenuBar);
        XPropertySet props = (XPropertySet)
UnoRuntime.queryInterface(
                XPropertySet.class, oMenuBar);
        props.setPropertyValue("Persistent", new
Boolean(true));
        XUIElementSettings xoMenuBarSettings =
(XUIElementSettings) UnoRuntime
               
.queryInterface(XUIElementSettings.class, oMenuBar);
        XIndexContainer oMenuBarSettings =
(XIndexContainer) UnoRuntime
                .queryInterface(XIndexContainer.class,
xoMenuBarSettings
                        .getSettings(true));

        // 3. make "Test-Menu" top-menu at the level
of "File"
        PropertyValue[] oMenuItem =
createMenu(".uno:PickList", "TestMenu");
        // 3.1 create container in the "Test-Menu",
where submenu will be placed
        XIndexContainer oContainer =
uiConfigManager.createSettings();
        // 3.2 create submenu "Test", which will be
placed into container from previouse step
        PropertyValue[] subPv =
createMenuItem(".uno:PickList", "Test");
        // 3.3 get "File" menu
        PropertyValue[] fileMenu = (PropertyValue[])
UnoRuntime.queryInterface(PropertyValue[].class,
                oMenuBarSettings.getByIndex(0));
        // PROBLEM: the "Test" submenu is visible, but
only if the "File" is put as the submenu under
"TestMenu" first.
        //          This is the result of calls:
        //             
oContainer.insertByIndex(0,fileMenu);
        //             
oContainer.insertByIndex(1,subPv);
        //      The following call results in
something like the separator:
        oContainer.insertByIndex(0,subPv);
        oMenuItem[2].Name = "ItemDescriptorContainer";
        oMenuItem[2].Value = oContainer;

        oMenuBarSettings.insertByIndex(0, oMenuItem);
       
xoMenuBarSettings.setSettings(oMenuBarSettings);

    }
   
    /**
     * The method create menu entry with a place for
the submenu.
     *
     * @param sMyCommand
     * @param label
     * @return
     * @throws IllegalArgumentException
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    private PropertyValue[] createMenu(String
sMyCommand, String label)
            throws IllegalArgumentException,
IndexOutOfBoundsException,
            WrappedTargetException {
        PropertyValue[] loadProps = new
PropertyValue[3];

        loadProps[0] = new PropertyValue();
        loadProps[1] = new PropertyValue();
        loadProps[2] = new PropertyValue();

        loadProps[0].Name = "CommandURL";
        loadProps[0].Value = sMyCommand;
        loadProps[1].Name = "Label";
        loadProps[1].Value = label;

        return loadProps;
    }

    private PropertyValue[] createMenuItem(String
sMyCommand, String label) {
        PropertyValue[] loadProps = new
PropertyValue[2];
        loadProps[0] = new PropertyValue();
        loadProps[1] = new PropertyValue();

        loadProps[0].Name = "CommandURL";
        loadProps[0].Value = sMyCommand;
        loadProps[1].Name = "Label";
        loadProps[1].Value = label;

        return loadProps;
    }

    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);
    }

    /**
     * Load a document as template
     */
    protected XComponent
newDocComponentFromTemplate(String loadUrl)
            throws java.lang.Exception {
        // get the remote service manager
        mxRemoteServiceManager =
this.getRemoteServiceManager();
        // retrieve the Desktop object, we need its
XComponentLoader
        Object desktop =
mxRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Desktop",
mxRemoteContext);
        XComponentLoader xComponentLoader =
(XComponentLoader) UnoRuntime
               
.queryInterface(XComponentLoader.class, desktop);

        // define load properties according to
        // com.sun.star.document.MediaDescriptor
        // the boolean property AsTemplate tells the
office to create a new
        // document
        // from the given file
        PropertyValue[] loadProps = new
PropertyValue[1];
        loadProps[0] = new PropertyValue();
        loadProps[0].Name = "AsTemplate";
        loadProps[0].Value = new Boolean(true);
        // load
        return
xComponentLoader.loadComponentFromURL(loadUrl,
"_blank", 0,
                loadProps);
    }

}


        

        
                
___________________________________________________________ 
Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: 
http://mail.yahoo.de

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to