Hi Carsten,

sorry for the late answer, I was very busy during the last weeks...


2005/11/17, Carsten Driesner <[EMAIL PROTECTED]>:
>
> Please look at the following example in Basic. It uses the function
> CreatePopupMenu to create a new popup menu. The line of interest uses
> the factory (the container) to create a sub container with
> createInstanceWithContext().


thank you for the example code. It works very well.

I would like to mention a behaviour I just noticed: the new created
popupmenu will not be restored after an restart of OOo, so there is no
persistence like described in your code comment... But I asume it was your
intention not to call the oModuleCfgMgr.store() method after the line "
oModuleCfgMgr.replaceSettings( sMenuBar, oMenuBarSettings )"? For my tests,
I added the respective line to your example-code.

But now to my question: I translated your example to java (I tried to do an
exaxt one-to-one-translation of your basic-example) and the java-code
bahaves slightly differt from the basic code: In basic, the popupmenu and
all elements are visible directly after calling the code. Using my
java-code, the popupmenu is created, but the element(s) in the popupmenu are
not visible. The strange thing is, that after the call of
oModuleCfgMgr.store() and a restart of OOo, the popupmenu and all its
previously unvisible item(s) get visible. Do you have got an idea, why the
items are not visible directly after creation, while the same code in basic
creates visible items? I think this might be a bug...

If you would like to follow, here's my translated java-code:

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XIndexContainer;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.ui.ItemType;
import com.sun.star.ui.XModuleUIConfigurationManagerSupplier;
import com.sun.star.ui.XUIConfigurationManager;
import com.sun.star.ui.XUIConfigurationPersistence;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class MenubarTest {

    public static void main(String[] args) throws java.lang.Exception {
        addItemToMenubar(Bootstrap.bootstrap());
        System.exit(0);
    }

    /**
     * Creates a top-level popup menu on the Writer menu bar persistently.
It
     * checks if its popup menu has already been added to the menu bar and
does
     * nothing. The popup menu contains one menu item which call the Basic
macro
     * test.
     *
     * @throws Exception
     */
    public static void addItemToMenubar(XComponentContext ctx)
            throws java.lang.Exception {

        // Initialize strings
        String sMenuBar = "private:resource/menubar/menubar";
        String sMyPopupMenuCmdId = "vnd.openoffice.org:MyMenu";

        // Retrieve the module configuration manager from central module
        // configuration manager supplier
        Object oModuleCfgMgrSupplier = ctx
            .getServiceManager().createInstanceWithContext(
                "com.sun.star.ui.ModuleUIConfigurationManagerSupplier",
ctx);
        XModuleUIConfigurationManagerSupplier xModuleUICfgMgrSupplier =
(XModuleUIConfigurationManagerSupplier) UnoRuntime
            .queryInterface(
                XModuleUIConfigurationManagerSupplier.class,
                oModuleCfgMgrSupplier);

        // Retrieve the module configuration manager with module
        // identifier
        // See com.sun.star.frame.ModuleManager for more information
        XUIConfigurationManager xModuleUICfgMgr = xModuleUICfgMgrSupplier
            .getUIConfigurationManager("com.sun.star.text.TextDocument");
        XIndexAccess oMenuBarSettings = xModuleUICfgMgr.getSettings(
            sMenuBar, true);
        XIndexContainer settingsContainer = (XIndexContainer) UnoRuntime
            .queryInterface(XIndexContainer.class, oMenuBarSettings);
        XSingleComponentFactory factory = (XSingleComponentFactory)
UnoRuntime
            .queryInterface(XSingleComponentFactory.class,
oMenuBarSettings);

        // Look for our top-level popup menu. Can be identified by the
        // CommandURL property.
        boolean bHasAlreadyPopupMenu = false;
        int nCount = oMenuBarSettings.getCount();
        for (int i = 0; i < nCount; ++i) {
            PropertyValue[] oPopupMenu = (PropertyValue[]) oMenuBarSettings
                .getByIndex(i);
            for (int j = 0; j < oPopupMenu.length; ++j) {
                if (oPopupMenu[j].Name.equals("CommandURL")) {
                    if (oPopupMenu[j].Value.equals(sMyPopupMenuCmdId)) {
                        bHasAlreadyPopupMenu = true;
                    }
                }
            }
        }

        System.out.println(bHasAlreadyPopupMenu);

        if (!bHasAlreadyPopupMenu) {
            String sString = "My Macro's";

            PropertyValue[] oPopupMenu = CreatePopupMenu(
                sMyPopupMenuCmdId, sString, factory, ctx);
            XIndexContainer oPopupMenuContainer = (XIndexContainer)
UnoRuntime
                .queryInterface(XIndexContainer.class, oPopupMenu[3].Value);

            PropertyValue[] oMenuItem = CreateMenuItem(
                "macro:///Standard.Module1.Test()", "Standard.Module1.Test
");
            oPopupMenuContainer.insertByIndex(0, oMenuItem);

            settingsContainer.insertByIndex(nCount, oPopupMenu);

            xModuleUICfgMgr.replaceSettings(sMenuBar, oMenuBarSettings);

            // make ist persistent (additional lines by clutz):
            XUIConfigurationPersistence xModuleUICfgMgrPers =
(XUIConfigurationPersistence) UnoRuntime
                .queryInterface(
                    XUIConfigurationPersistence.class, xModuleUICfgMgr);
            xModuleUICfgMgrPers.store();
        }

    }

    public static PropertyValue[] CreatePopupMenu(String commandID,
            String label, XSingleComponentFactory factory, XComponentContext
ctx)
            throws Exception {
        PropertyValue[] aPopupMenu = new PropertyValue[] { new
PropertyValue(),
                new PropertyValue(), new PropertyValue(), new
PropertyValue() };

        aPopupMenu[0].Name = "CommandURL";
        aPopupMenu[0].Value = commandID;
        aPopupMenu[1].Name = "Label";
        aPopupMenu[1].Value = label;
        aPopupMenu[2].Name = "Type";
        aPopupMenu[2].Value = new Integer(ItemType.DEFAULT);
        aPopupMenu[3].Name = "ItemDescriptorContainer";
        aPopupMenu[3].Value = factory.createInstanceWithContext(ctx);

        return aPopupMenu;
    }

    public static PropertyValue[] CreateMenuItem(String command, String
label) {
        PropertyValue[] aMenuItem = new PropertyValue[] { new
PropertyValue(),
                new PropertyValue(), new PropertyValue() };

        aMenuItem[0].Name = "CommandURL";
        aMenuItem[0].Value = command;
        aMenuItem[1].Name = "Label";
        aMenuItem[1].Value = label;
        aMenuItem[2].Name = "Type";
        aMenuItem[2].Value = new Integer(ItemType.DEFAULT);

        return aMenuItem;
    }
}


best regards,
Christoph

Reply via email to