Hi Tabish,

>> Create a JAR package and make a desktop icon with a commandline like
>> this: java -jar myjar.jar
> 
> The ant script file will be inside the JAR ? Right now I type "ant run" to
> run my java code.
> You are saying that by making a jar which will have the ant file inside it
> the ant script will be automatically executed ?

My JAR archive has no ant script inside. Best way is to use the JAR
Packager of your Java IDE. The JAR File will have all the compiled
classes inside and also a META File. That file is generated by your IDE.
Please see also the comment to this snippet:
http://codesnippets.services.openoffice.org/Office/Office.BootstrapOpenOffice.snip

> Also I need the procedure for making an icon and how to attach that icon
> with whatever jar I make.

I can't help you with this. You should ask this in a Java forum, not in
an OOo specific forum.

>> If you like this idea, I can post you java code, how you add a menu /
>> menu item to your menu.
> 
> Yes thats exactly what I want to do.

I attach a java file that will be interesting for you and may be others.
It is free in use and you can copy it.

Greetings, Tobias

package de.twc.oocom.oo;

import java.io.File;
import java.util.Vector;

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.XController;
import com.sun.star.frame.XDispatchHelper;
import com.sun.star.frame.XDispatchProvider;
import com.sun.star.frame.XLayoutManager;
import com.sun.star.frame.XModel;
import com.sun.star.lang.EventObject;
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.XMultiServiceFactory;
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.util.CloseVetoException;
import com.sun.star.util.XCloseListener;
import com.sun.star.util.XCloseable;


/**
 * @author tobias
 *
 */
public class OOGUILayout {

    /**
     * OpenOffice.org representative of the opened file.
     * @see #openFilename
     */
    private XComponent xComponent = null; 
    
    /**
     * This variable contains the folder and name of the handled file
     * e.g "/tmp/myfile.odt" for Linux/Unix or "C:\folder\myfile.odt" for
     * Windows.
     */
    private String openFilename = null;
	
    /**
     * Setting to true turns on the debug mode.
     */
    private static int debug = 0;

    /**
     * Constructor that inherits from OOComNG and sets some parameters.
     * 
     * @param myOpenFilename See [EMAIL PROTECTED] de.twc.oocom.OOComNG#openFilename}. The
     * file that should be opened.
     */
    public OOGUILayout(XComponent myXComponent, String filename) {
    	this.xComponent = myXComponent;
    	this.openFilename = filename;
    }
    
    /**
     * This method adds a CloseListener to the opened document.
     */
    public void addCloseListener()
    {
        // initial close listener
        XCloseable close = (XCloseable)UnoRuntime.queryInterface(
                com.sun.star.util.XCloseable.class, this.xComponent);
        final File openFile = new File(this.openFilename);
        final long last_modified_old = openFile.lastModified();

        close.addCloseListener(new XCloseListener() {
            public void queryClosing(EventObject arg0, boolean arg1)
                    throws CloseVetoException {
                long last_modified = openFile.lastModified();
                if (last_modified != last_modified_old) {
                    System.exit(0);
                } else {
                    // XModifiable modi = xComponent.interfaces.getXModifiable();
                    // System.out.println(modi.isModified());
                    System.exit(1);
                }
            }

            public void notifyClosing(EventObject arg0) {}

            public void disposing(EventObject arg0) {}
        });

        // wait for closing document
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                if (debug > 1)
                    System.out.println(e);
            }
        }
    }
    
    /**
     * This method retrieves the Layout Manager that is used by a XFrame.
     * 
     * @return Returns the current used LayoutManager. 
     */
    public XLayoutManager getLayoutManager()
    {
    	// Getting the frame the loaded document resides in
        XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class,
    	        this.xComponent);
    	XController xController = xModel.getCurrentController();
    	
    	// Getting the properties of the frame
        XPropertySet xps = (XPropertySet)
                UnoRuntime.queryInterface(XPropertySet.class,
                        xController.getFrame());
        
        XLayoutManager xLayoutManager = null;
        try 
        {
            xLayoutManager = (XLayoutManager)
                    UnoRuntime.queryInterface(XLayoutManager.class,
                    xps.getPropertyValue("LayoutManager"));
        }
        catch (com.sun.star.uno.Exception e)
        {
            if (debug > 0)
                System.out.println("Cannot get Layout Manager.");
            if (debug > 1)
                System.out.println(e);
        }
        return xLayoutManager;
    }
    
    /**
     * Get the XUIConfigurationManager.
     */
    private XUIConfigurationManager getXUIConfigurationManager(
    		XComponent myXComponent)
    {
        Object oModuleCfgMgrSupplier = null;
        try
        {
            // Retrieve the module configuration manager from central module
            // configuration manager supplier
            oModuleCfgMgrSupplier = 
            	OOWorkbench.getXMCF().createInstanceWithContext(
                    "com.sun.star.ui.ModuleUIConfigurationManagerSupplier",
                    OOWorkbench.getXComponentContext());
        }
        catch(com.sun.star.uno.Exception e)
        {
            if (debug > 0)
                System.out.println("Can't retrieve the " +
                        "ModuleUIConfigurationManagerSupplier.");
            if (debug > 1)
                System.out.println(e);
        }
        
        // Retrieve the module configuration manager with module identifier
        XModuleUIConfigurationManagerSupplier confMngrS =
                (XModuleUIConfigurationManagerSupplier)
                    UnoRuntime.queryInterface(
                        XModuleUIConfigurationManagerSupplier.class,
                        oModuleCfgMgrSupplier);

        XUIConfigurationManager xConfMngr = null;
        try
        {
            xConfMngr = confMngrS.getUIConfigurationManager(
                    OODocument.getApplicationName(myXComponent));
        }
        catch (com.sun.star.container.NoSuchElementException e)
        {
            if (debug > 1)
                System.out.println(e);
        }
        return xConfMngr;
    }

    /**
     * This function shows the SaveAs dialog to the user but continues after
     * saving with the old file URL representative.
     */
    private void saveTo()
    {
        // Setting the SaveTo properties
        PropertyValue[] saveProperties = new PropertyValue[1];
        saveProperties[0] = new PropertyValue();
        saveProperties[0].Name = "SaveTo";
        saveProperties[0].Value = new Boolean(true);

        // Getting the frame the loaded document resides in
        XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class,
    	        this.xComponent);
    	XController xController = xModel.getCurrentController();

        // Getting the Dispatch Provider
        XDispatchProvider xDispatchProvider = (XDispatchProvider)
                UnoRuntime.queryInterface (XDispatchProvider.class,
                		xController.getFrame());


        // Getting the Dispatch Helper
        XMultiServiceFactory xMultiServiceManager = (XMultiServiceFactory)
                UnoRuntime.queryInterface(XMultiServiceFactory.class,
                        OOWorkbench.getXComponentContext().getServiceManager());
        Object oDispatchHelper = null;
        try{
            oDispatchHelper = xMultiServiceManager.createInstance(
                    "com.sun.star.frame.DispatchHelper");
        }
        catch(com.sun.star.uno.Exception e)
        {
            if (debug > 0)
                System.out.println("Can't get the Dispatch Handler Object...");
            if (debug > 1)
                System.out.println(e);
        }
        XDispatchHelper xDispatchHelper = (XDispatchHelper)
                UnoRuntime.queryInterface(XDispatchHelper.class, oDispatchHelper);
        // Executing the commandURL
        xDispatchHelper.executeDispatch(xDispatchProvider,
                ".uno:SaveAs",
                "_self",
                0,
                saveProperties);
    }
    
    /**
     * Removes the menu item with the CommandURL from all menus.
     * @param itemCommandURL
     */
    public void removeMenuItemTransient (String itemCommandURL) {
    	this.changeMenusTransient(".uno:SaveAs", null);
    }
    
    /**
     * Creates and adds a menu item to a menu. The menu item is inserted after
     * all found items with CommandURL myCommandURL
     * @param myCommandURL CommandURL after which the menu item should be
     * inserted.
     * @param addItemCommandURL CommandURL of the new item.
     * @param addItemLabel Name of the new item that is shown to the user.
     */
    public void addMenuItemTransient (String myCommandURL,
            String addItemCommandURL,
            String addItemLabel) {
    	this.changeMenusTransient(myCommandURL,
    			createMenuItemProperties(addItemCommandURL, addItemLabel));
    }
    
    /**
     * This method either removes the item specified in the CommandURL from
     * OpenOffice menubar or adds the menu item to the menu. Changes are
     * transient.
     * 
     * @param myCommandURL OpenOffice CommandURL that should be removed or after
     * which a new menu item is insterted.
     * @param newItemProps PropertyValue[] of a new menuItem (null if a item
     * should be removed.
     */
    private void changeMenusTransient(String myCommandURL, PropertyValue[] newItemProps) {

    	if (debug > 0)
    	    System.out.print("Starting to change menus... ");

        // Init menu elements
        // 1. Getting the menubar
        com.sun.star.ui.XUIElement myMenubar 
            	= getLayoutManager().getElement("private:resource/menubar/menubar");

        // 2. Getting the menubar settings
        XUIElementSettings myMenuBarSettings = (XUIElementSettings)
                UnoRuntime.queryInterface(XUIElementSettings.class, myMenubar);

        // 3. Casting the settings into a container to be able to get the
        // properties
        XIndexContainer myMenuBarContainer = (XIndexContainer)
                UnoRuntime.queryInterface(XIndexContainer.class,
                        myMenuBarSettings.getSettings(true));
        try
        {
            // Creating a Vector containing all menus with a "Save" item
            Vector foundMenuItems = this.searchXIndexContainerForItem(
                    myCommandURL,
                    myMenuBarContainer,
                    new Vector());
            if(newItemProps != null)
            {
                if (debug > 0)
                    System.out.print("(adding item " + newItemProps[1].Value
                    		+ ")... ");

                this.addItemToMenu(newItemProps, foundMenuItems);
            }
            else
            {
                if (debug > 0)
                    System.out.print("(removing an item)... ");
                
                // Remove the menu items
                this.removeXIndexContainerItems(foundMenuItems);
            }
            
            // Make changes only transient (temporary).
            com.sun.star.beans.XPropertySet xPropSet = (XPropertySet)
            UnoRuntime.queryInterface(XPropertySet.class, myMenubar);
            xPropSet.setPropertyValue("Persistent", new Boolean(false));
            
            // Apply settings to the root container (includes all subcontainers)
            myMenuBarSettings.setSettings(myMenuBarContainer);
        }
        catch (Exception e)
        {
            if (debug > 0)
                System.out.println("failed.");
            if (debug > 1)
                System.out.println(e);
            return;
        }
        
        if (debug > 0)
            System.out.println("done.");
    }

    /**
     * Removes a Toolbar item from a menu transient.
     * @param myCommandURL CommandURL of the toolbar item.
     */
    public void removeToolbarItemTransient(String myCommandURL) {
    	this.changeToolbarsTransient(myCommandURL, "remove");
    }
    
    /**
     * This method removes the in the CommandURL specified items from OpenOffice
     * toolbars. Changes are transient.
     * 
     * @param myCommandURL OpenOffice CommandURL that should be removed or after
     * which a new CommandURL is insterted.
     * @param action tells what to do with myCommandURL, e.g. "remove".
     */
    private void changeToolbarsTransient(String myCommandURL, String action)
    {
        if (debug > 0)
            System.out.print("Starting to change toolbars... ");

        final String[] toolbars = { "private:resource/toolbar/alignmentbar",
                "private:resource/toolbar/arrowshapes",
                "private:resource/toolbar/basicshapes",
                "private:resource/toolbar/calloutshapes",
                "private:resource/toolbar/colorbar",
                "private:resource/toolbar/drawbar",
                "private:resource/toolbar/drawobjectbar",
                "private:resource/toolbar/extrusionobjectbar",
                "private:resource/toolbar/fontworkobjectbar",
                "private:resource/toolbar/fontworkshapetypes",
                "private:resource/toolbar/formatobjectbar",
                "private:resource/toolbar/formcontrols",
                "private:resource/toolbar/formdesign",
                "private:resource/toolbar/formsfilterbar",
                "private:resource/toolbar/formsnavigationbar",
                "private:resource/toolbar/formsobjectbar",
                "private:resource/toolbar/formtextobjectbar",
                "private:resource/toolbar/fullscreenbar",
                "private:resource/toolbar/graphicobjectbar",
                "private:resource/toolbar/insertbar",
                "private:resource/toolbar/insertcellsbar",
                "private:resource/toolbar/insertobjectbar",
                "private:resource/toolbar/mediaobjectbar",
                "private:resource/toolbar/moreformcontrols",
                "private:resource/toolbar/previewbar",
                "private:resource/toolbar/standardbar",
                "private:resource/toolbar/starshapes",
                "private:resource/toolbar/symbolshapes",
                "private:resource/toolbar/textobjectbar",
                "private:resource/toolbar/toolbar",
                "private:resource/toolbar/viewerbar"};

        for (int i = 0; i < toolbars.length; i++)
        {
            if (getLayoutManager().getElement(toolbars[i]) != null)
            {
                // Getting the toolbar
                XUIElement myToolbar = getLayoutManager().getElement(toolbars[i]);

                // Getting the toolbar settings
                XUIElementSettings myToolBarSettings = (XUIElementSettings)
                        UnoRuntime.queryInterface(XUIElementSettings.class,
                        myToolbar);
                // Casting the settings into a container to be able to get the
                // properties
                XIndexContainer myToolBarSettingsContainer = (XIndexContainer)
                        UnoRuntime.queryInterface(
                        XIndexContainer.class,
                        myToolBarSettings.getSettings(true));

                try
                {
                    // Creating a Vector containing all toolbars containing
                    // the CommanURL
                    Vector foundSaveToolbarItems = this.searchXIndexContainerForItem(
                            myCommandURL,
                            myToolBarSettingsContainer,
                            new Vector());
                    if(action.equals("remove"))
                    {
                        // Remove the toolbar items
                        this.removeXIndexContainerItems(foundSaveToolbarItems);
                    }
                    else
                    {
                        if (debug > 0)
                            System.out.println("Unknown action: '" + action + "'");
                    }
                    
                    // Make changes only transient (temporary).
                    com.sun.star.beans.XPropertySet xPropSet = (XPropertySet)
                            UnoRuntime.queryInterface(XPropertySet.class,
                                    myToolbar);
                    xPropSet.setPropertyValue("Persistent", new Boolean(false));
                    
                    // Apply changes
                    myToolBarSettings.setSettings(myToolBarSettingsContainer);
                }
                catch (Exception e)
                {
                    if (debug > 0)
                        System.out.println("failed.");
                    if (debug > 1)
                        System.out.println(e);
                    return;
                }
            }
        }
        if (debug > 0)
            System.out.println("done.");
    }
    
    /**
     * 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 Vector containing all menu / toolbar items to be
     * removed from menu / toolbar
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    private void removeXIndexContainerItems(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 OpenOffice CommandURL which is searched for.
     * @param myMenuContainer XIndexContainer (menu bar or toolbar) which is
     * searched.
     * @param Vector with all found menu / toolbar items.
     * @return foundMenuItems Vector with all found menu / toolbar items.
     * @throws IllegalArgumentException
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    private Vector searchXIndexContainerForItem(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(new Integer(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)
                        {
                            searchXIndexContainerForItem(myCommandURL,
                                    subMenuContainer,
                                    foundMenuItems);
                        }
                    }
                }
            }
        }
        return foundMenuItems;
    }

    /**
     * This method adds the specified myCommandURL with the itemLabel to the
     * menu / toolbar after the specified menu item in foundMenuItems. 
     * 
     * @param newItemProps PropertyValue[] of the new menu item.
     * @param foundMenuItems Vector with all menu items after which the new item
     * should be placed.
     */
    private void addItemToMenu(PropertyValue[] newItemProps,
    		Vector foundMenuItems) {
        try
        {
            // Starting with the last Element, because removing changes item index
            for (int i = foundMenuItems.size() -1; i >= 0; i--)
            {
                Vector thisVectorItem = (Vector)foundMenuItems.elementAt(i);
                XIndexContainer thisMenuContainer = (XIndexContainer)
                    thisVectorItem.elementAt(0);
                Integer menuPosition = (Integer)thisVectorItem.elementAt(1);
    
                // add the menu item
                thisMenuContainer.insertByIndex(menuPosition.intValue() + 1,
                        newItemProps);
            }
        }
        catch(Exception e)
        {
            if (debug > 0)
                System.out.println("Can't add this item.");
            if (debug > 1)
                System.out.println(e);
        }
    }
    
    /**
     * This method adds a new menu to the menubar.
     * 
     * @param Name of the new menu, e.g. "Testmenu
     * @param int Value of the menu position (counting starts at 0).
     * @param String array with menu items. Format example: 
     * { {"Item01Name", "Item01CommandURL"}, {"Item02Name", "Item02CommandURL"}, ...}
     */
    public void createTransientMenu(String menuName,
    		int menuPosition,
    		String[][] menuItems) {
        try
        {
            // Init menu elements
            // 1. Getting the menubar
            com.sun.star.ui.XUIElement myMenubar 
                	= getLayoutManager().getElement("private:resource/menubar/menubar");
            // 2. Getting the menubar settings
            XUIElementSettings myMenuBarSettings = (XUIElementSettings)
                    UnoRuntime.queryInterface(XUIElementSettings.class, myMenubar);
            // 3. Casting the settings into a container to be able to get the
            // properties
            XIndexContainer myMenuBarSettingsContainer = (XIndexContainer)
                    UnoRuntime.queryInterface(
                    XIndexContainer.class, myMenuBarSettings.getSettings(true));
                    
            // Create properties for a new menu
            PropertyValue[] oMenuItem = createMenuProperties(menuName);
            // Create container for the new menu has to be added to properties
            XIndexContainer oContainer = getXUIConfigurationManager(
            		this.xComponent).createSettings();
            
            // Create menu item, which will be placed into container from
            // previouse step.
            for (int i = 0; i < menuItems.length; i++) {
                PropertyValue[] itemProps = createMenuItemProperties(
                		menuItems[i][1], menuItems[i][0]);
            	oContainer.insertByIndex(i, itemProps);
            }

            // Add container to menu
            oMenuItem[2].Value = oContainer;
            
            // Add menu to menubar
            myMenuBarSettingsContainer.insertByIndex(menuPosition, oMenuItem);

            // Make changes only transient (temporary).
            com.sun.star.beans.XPropertySet xPropSet = (XPropertySet)
                    UnoRuntime.queryInterface(XPropertySet.class, myMenubar);
            xPropSet.setPropertyValue("Persistent", new Boolean(false));

            // Make changes visible.
            myMenuBarSettings.setSettings(myMenuBarSettingsContainer);
        }
        catch (Exception e)
        {
            if (debug > 1) {
                System.out.println(e);
            }
        }
    }
    
    
    /**
     * The method create menu entry with a place for the submenu.
     *
     * @param label Label for the new submenu.
     * @return PropertyValue[] for the new menu.
     * @throws IllegalArgumentException
     * @throws IndexOutOfBoundsException
     * @throws WrappedTargetException
     */
    private static PropertyValue[] createMenuProperties(String label)
            throws IllegalArgumentException,
            IndexOutOfBoundsException,
            WrappedTargetException {

        PropertyValue[] menuProperties = new PropertyValue[3];
        
        menuProperties[0] = new PropertyValue();
        menuProperties[0].Name = "CommandURL";
        menuProperties[0].Value = ".uno:PickList";

        menuProperties[1] = new PropertyValue();
        menuProperties[1].Name = "Label";
        menuProperties[1].Value = label;

        menuProperties[2] = new PropertyValue();
        menuProperties[2].Name = "ItemDescriptorContainer";
        // Value has to be added later
        
        return menuProperties;
    }
    
    
    /**
     * Creating the properties for a new menu item.
     * 
     * @param sMyCommand OpenOffice CommandURL for the new item.
     * @param label Label for the new item.
     * @return PropertyValue[] for the new menu item.
     */
    private static PropertyValue[] createMenuItemProperties(String sMyCommand,
    		String label) {
        PropertyValue[] menuItemProperties = new PropertyValue[3];
        
        menuItemProperties[0] = new PropertyValue();
        menuItemProperties[0].Name = "CommandURL";
        menuItemProperties[0].Value = sMyCommand;

        menuItemProperties[1] = new PropertyValue();
        menuItemProperties[1].Name = "Label";
        menuItemProperties[1].Value = label;

        menuItemProperties[2] = new PropertyValue();
        menuItemProperties[2].Name = "Type";
        menuItemProperties[2].Value = new Short((short)0);
        
        return menuItemProperties;
    }
    

    /**
     * Set debug value.
     * @param debug 0 is quiet, 1 is normal, 2 is verbose
     */
	public void setDebug(int mydebug) {
		debug = mydebug;
	}
}

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

Reply via email to