Hi Carsten,

your ideas made me think...

> You have to check the name of each PropertyValue entry to find your
> entry as the order is NOT defined.

> Stephan Bergmann already wrote the correct code to retrieve the
> PropertyValue array from the UNO any in Java.

> If you want to make transient changes, you should not use the
> configuration manager. You have to use the user interface element and
> use the XUIElementSettings interface only. There you also have a
> setSettings function. The user interface configuration managers always
> work persistently!

>>         xFileMenuSettings.removeByIndex(7);
> Please be aware that this code only works, if nobody customized the menu
> bar. It would fail, if someone just moved the menu item you want to
> remove. If you want to be fail safe, you should iterate through the
> container and check the command url of every menu item to find the
> correct index.

>>         xoMenuBarSettings.setSettings(xFileMenuSettings);
> Don't use this call.
>>         xoMenuBarSettings.setSettings(oMenuBarSettings);
> You only have to call setSettings once. You have to do it with the root
> index container you got from the user interface element using getSettings.

Applying all your ideas the code looks like this. I think it will be useful:
-----%<-----
package de.twc.oocom;

import java.util.Vector;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XIndexContainer;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XLayoutManager;
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.XUIElementSettings;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class TemporaryRemoveMenuEntry {

    public TemporaryRemoveMenuEntry(){
        //Empty Constructor for OOComNG
    }

    // The file that should be processed, e.g. "/tmp/hello_world.ods"
    private String source_File = "/home/tobias/test.odt";
    private String ooport = "9000";

    public static void main(String[] arg){
        TemporaryRemoveMenuEntry myOOComNG =
            new TemporaryRemoveMenuEntry();

        try {
            myOOComNG.openDocument();
        }
        catch(Exception e) {
                System.out.println(e);
        }
    }

    private void openDocument() throws java.lang.Exception {
        XComponentContext xRemoteContext = Bootstrap.bootstrap();
        System.out.println("Connected to a running OpenOffice ...");
        // get OO desktop
        XMultiComponentFactory xRemoteServiceManager
                = xRemoteContext.getServiceManager();
        Object desktop =
            xRemoteServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Desktop", xRemoteContext
        );
        // query the XComponentLoader interface from the desktop
        XComponentLoader xComponentLoader =
                (XComponentLoader)UnoRuntime.queryInterface(
                XComponentLoader.class, desktop);

        PropertyValue[] myProperties = new PropertyValue[1];
        PropertyValue xProperty = new PropertyValue();
            xProperty.Name = "ReadOnly";
            xProperty.Value = new Boolean(false);
        myProperties[0] = xProperty;

        // load document
        XComponent openedDocument =
            xComponentLoader.loadComponentFromURL(
                "file://" + source_File,
                "_blank",
                Integer.parseInt(ooport),
                myProperties);

        com.sun.star.frame.XDesktop xDesktop =
            (com.sun.star.frame.XDesktop)
            UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class,
            desktop);
        com.sun.star.frame.XFrame xFrame = xDesktop.getCurrentFrame();

        XPropertySet xps = (XPropertySet) UnoRuntime.queryInterface(
                XPropertySet.class, xFrame);
        XLayoutManager xLayoutManager = (XLayoutManager)
            UnoRuntime.queryInterface(
                XLayoutManager.class,
                xps.getPropertyValue("LayoutManager"));
        xLayoutManager.attachFrame(xFrame);

        // Getting the menubar
        com.sun.star.ui.XUIElement myMenubar =
          xLayoutManager.getElement("private:resource/menubar/menubar");
        // Getting the menubar settings
        XUIElementSettings myMenuBarSettings = (XUIElementSettings)
                UnoRuntime.queryInterface(XUIElementSettings.class,
                myMenubar);
        // Casting the settings into a container to be able to get the
        //properties
        XIndexContainer myMenuBarSettingsContainer = (XIndexContainer)
                UnoRuntime.queryInterface(
                XIndexContainer.class,
                myMenuBarSettings.getSettings(true));

        // Creating a Vector containing all menus with a "Save" item
        Vector foundSaveMenuItems = searchMenuForItem(".uno:Save",
                myMenuBarSettingsContainer,
                new Vector());
        // Remove the menu items
        removeMenuItems(foundSaveMenuItems);

        // Apply settings to the root container (includes all
        // subcontainers)
        myMenuBarSettings.setSettings(myMenuBarSettingsContainer);
    }

    /**
     * Method that removes the given menu items. The given Vector
     * contains a
     * XIndexContainer at first position (the menu containing the Item
     * to remove)
     * and an Integer at second position (the item number of the item to
     * remove.
     * The changes are transient (temporary).
     *
     * @param itemsToRemove
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    public void removeMenuItems(Vector itemsToRemove) throws
            IndexOutOfBoundsException,
            WrappedTargetException
    {
        // Starting with the last Element, because removing changes item
        // index
        for (int i = itemsToRemove.size() -1; i >= 0; i--)
        {
            Vector thisVectorItem = (Vector)itemsToRemove.elementAt(i);
            XIndexContainer thisMenuContainer = (XIndexContainer)
                thisVectorItem.elementAt(0);
            Integer menuPosition = (Integer)thisVectorItem.elementAt(1);

            // remove the item, but settings must be set to make it
            // visible
            thisMenuContainer.removeByIndex(menuPosition.intValue());
        }
    }

    /**
     * Method that searches menus and its submenus for the "CommandURL"
     * property. The returned Vector consists of Vectors. Each contained
     * Vector
     * has two values. The first gives the XIndexContainer where the
     * searched
     * menu item was found, the second is an Integer that tells the
     * position
     * where the menu item was found.
     *
     * @param myCommandURL
     * @param myMenuContainer
     * @param foundMenuItems
     * @return
     * @throws IllegalArgumentException
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    public Vector searchMenuForItem(String myCommandURL,
            XIndexContainer myMenuContainer, Vector foundMenuItems)
            throws IllegalArgumentException,
                IndexOutOfBoundsException,
                WrappedTargetException  {

        if(myMenuContainer != null) {
            for(int g = 0; g < myMenuContainer.getCount(); g++) {
                // Getting the properties of the given container
                PropertyValue[] gMenuItem = (PropertyValue[])
                        com.sun.star.uno.AnyConverter.toObject(
                        PropertyValue[].class,
                        myMenuContainer.getByIndex(g));
                for(int h = 0; h < gMenuItem.length; h++)
                {
                    if(gMenuItem[h].Name.equals("CommandURL"))
                    {
                        if(gMenuItem[h].Value.equals(myCommandURL))
                        {
                            Vector thisMenuItem = new Vector();
                               thisMenuItem.addElement(myMenuContainer);
                               thisMenuItem.addElement(g);
                            foundMenuItems.addElement(thisMenuItem);
                            break;
                        }
                    }
            else if(gMenuItem[h].Name.equals("ItemDescriptorContainer"))
                    {
                        XIndexAccess subMenuAccess = (XIndexAccess)
                                com.sun.star.uno.AnyConverter.toObject(
                                XIndexAccess.class,
                                gMenuItem[h].Value);
                        XIndexContainer subMenuContainer =
                                (XIndexContainer)
                                UnoRuntime.queryInterface(
                                XIndexContainer.class, subMenuAccess);
                        if(subMenuAccess != null)
                        {
                            searchMenuForItem(myCommandURL,
                                    subMenuContainer,
                                    foundMenuItems);
                        }
                    }
                }
            }
        }
        return foundMenuItems;
    }
}
-----%<-----

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

Reply via email to