OO-Snippets: Adding a Combo-box to a Toolbar

Commons

KeywordsToolbarController, Combobox, Combo-Box, Toolbar
LanguageJava
ApplicationOffice
AuthorsAidan Butler (initial)
Supported Versions2.0.x  
Supported OSAll  
QuestionHow Do I add a Combo-box to an OpenOffice Toolbar
<!-- Controller.xcu Entry -->

<node oor:name="c3" oor:op="replace">
      <prop oor:name="Command">
            <value>.uno:CommandLineToolbar</value>
      </prop>
      <prop oor:name="Module">
             <value></value>
      </prop>
      <prop oor:name="Controller">
              <value>com.sun.star.test.testtoolbar</value>
      </prop>
</node>



<!-- Toolbar xml definition -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE toolbar:toolbar PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "toolbar.dtd">
<toolbar:toolbar xmlns:toolbar="http://openoffice.org/2001/toolbar" xmlns:xlink="http://www.w3.org/1999/xlink" toolbar:uiname="custom_toolbar_1">
      <toolbar:toolbaritem xlink:href=".uno:CommandLineToolbar"/>
</toolbar:toolbar>
Answer

You need to create a component which implements the interfaces of the com.sun.star.frame.ToolbarController Service. The createItemWindow() method then need to return a combox instance which will be displayed to the user.

This implementation then needs to be registered with OpenOffice using unopkg, and linked to a toolbar controller Command in the Controller.xcu. Once this is completed, it can be reference within a toolbar xml definition.

Code-Snippet-Listing (snippet-source)

// Toolbar Controller implementation

package Toolbar;

import com.sun.star.beans.PropertyValue;
import com.sun.star.document.XImporter;
import com.sun.star.document.XExporter;
import com.sun.star.document.XFilter;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XInitialization;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;
import com.sun.star.uno.Type;
import com.sun.star.xml.sax.*;
import com.sun.star.frame.XToolbarController;
import com.sun.star.frame.XStatusListener;
import com.sun.star.util.XUpdatable;
import com.sun.star.frame.FeatureStateEvent;
import com.sun.star.awt.XWindow;
import com.sun.star.awt.XWindowPeer;
import com.sun.star.awt.WindowDescriptor;
import com.sun.star.awt.WindowClass;
import com.sun.star.awt.Rectangle;
import com.sun.star.awt.WindowAttribute;
import com.sun.star.awt.VclWindowPeerAttribute;
import com.sun.star.awt.XComboBox;
import com.sun.star.awt.XTextComponent;
import com.sun.star.awt.XFixedText;
import com.sun.star.awt.XToolkit;
import com.sun.star.lang.EventObject;
import com.sun.star.awt.XKeyListener;
import com.sun.star.awt.KeyEvent;
import com.sun.star.frame.XDesktop;
import com.sun.star.text.XTextDocument;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.util.XOfficeInstallationDirectories;
import com.sun.star.frame.XModel;

import com.sun.star.beans.XPropertySet;
import com.sun.star.text.XTextCursor;
import com.sun.star.frame.XFrame;


import com.sun.star.beans.Property;

/**
* ToolBar controller component registration wrapper
* <p>
* This wrapper is used by the unopkg registration application,
* to register the component name with the OpenOffice component Database.
*/
public class Toolbar
{
   /**
    * An com.sun.star.frame.XToolbarController implementation, for extending
    * the default operation and view of a Openoffice Toolbar.
    */
   public static class _Toolbar extends     WeakBase
               implements  XInitialization,
                   XToolbarController,
                   XStatusListener,
                   XUpdatable,
                   XKeyListener
   {
       // the supported service names, the first one being the service name of the component itself
       /**
        * The Services supported by this component
        */
       public static final String[] msServiceNames =
       {
           "com.sun.star.test.testtoolbar"
       };

 

       private XComponentContext mxContext;
       private XMultiComponentFactory mxMCF;
       private XMultiServiceFactory mxMSF;
       private String msInternalName;
       private XComponent mxDocument;
       private XTextComponent fixedText;
       private XComboBox cBox_xComboBox;

       public _Toolbar(XComponentContext xContext )
       {
           try
           {
               mxContext = xContext;
               mxMCF = mxContext.getServiceManager();
               mxMSF = (XMultiServiceFactory) UnoRuntime.queryInterface
                           (XMultiServiceFactory.class, mxMCF);
           }
           catch( Exception e )
           {
           }

           msInternalName    = new String();
           mxDocument        = null;
             }
       /**
        * Component Initialization.
        */
       public void initialize( Object[] lArguments ) throws com.sun.star.uno.Exception
       {
       }

       /**
        * Retunrns the internal name of this component
        *
        * @return String Name
        */
       public String getName()
       {
           synchronized(this)
           {
               return msInternalName;
           }
       }

       /**
        * Sets the internal name of this component
        *
        * @param sName The name to be set.
        */
       public void setName( String sName )
       {
       }

       public void execute ( short nKeyModifier )
       {
       }

       /**
        * Single Click Mouse event handler
        *
        */
       public void click ()
       {
       }

       /**
        * Double Click Mouse event handler
        *
        */
       public void doubleClick()
       {
       }

       /**
        * Pop-up window creator. Not Implemented
        *
        */
       public XWindow createPopupWindow ()
       {
           return null;
       }

       /**
        * Creates the command line combo box which will be embedded in the supplied com.sun.star.awt.XWindow.
        *
        * @param xWindow The XWindow instance which will contain the combo box
        * @return  XWindow
        * @see     XWindow       
        */
       public XWindow createItemWindow(XWindow xWindow) {

          // xMSF is set by initialize(Object[])
          try {
            // get XWindowPeer
            XWindowPeer xWinPeer = (XWindowPeer) UnoRuntime.queryInterface(
                XWindowPeer.class, xWindow);
            Object o = mxMSF.createInstance("com.sun.star.awt.Toolkit");
            XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(
                XToolkit.class, o);
                // create WindowDescriptor
            WindowDescriptor wd = new WindowDescriptor();
            wd.Type = WindowClass.SIMPLE;
            wd.Parent = xWinPeer;
            wd.Bounds = new Rectangle(0, 0, 230, 20);
            wd.ParentIndex = -1;
            wd.WindowAttributes = WindowAttribute.SHOW |VclWindowPeerAttribute .DROPDOWN;
            wd.WindowServiceName = "combobox";
            // create ComboBox
            XWindowPeer cBox_xWinPeer = xToolkit.createWindow(wd);
            cBox_xComboBox = (XComboBox) UnoRuntime.queryInterface(XComboBox.class, cBox_xWinPeer);
            // Get Interface for manipulating the text in the combobox
            fixedText = (XTextComponent)UnoRuntime.queryInterface(XTextComponent.class,cBox_xComboBox);
            fixedText.setText("Enter Command Here");
            XWindow cBox_xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, cBox_xWinPeer);
            // add some elements
            /* cBox_xComboBox.addItems(new String[] { "test", "foo", "bar",
             *   "test2", "foo2", "bar2" }, (short) 0);
             */
            cBox_xComboBox.addItems(new String[] {""}, (short) 4);
            cBox_xWindow.addKeyListener(this);
            return cBox_xWindow;
          } catch (com.sun.star.uno.Exception e) {
            return null;
          }
      }

      /**
       * Status Changed Event Listener
       * @param aState the com.sun.star.frame.FeatureStateEvent status
       */
      public void statusChanged ( FeatureStateEvent aState )
      {
      }

     /**
      * Disposing event handler
      */
     public void disposing ( EventObject aSource )
     {
     }

     /**
      * Toolbarcontroller com.sun.star.util.XUpdatable implementation update controller.
      *
      */
     public void update ( )
     {
     }

     /**
      * KeyPress event handler. This method is used to intercept the
      * Key press events
      *
      * @param event The com.sun.star.awt.KeyEvent that has occured
      * @see  KeyEvent
      */
     public void keyPressed(KeyEvent event)
     {
     }

     /**
      * @param event The com.sun.star.awt.KeyEvent that has occured
      * @see KeyEvent
      */
     public void keyReleased(KeyEvent event)
     {
         //KeyRelease Handler for combobox events
     }
 
     /**
      * Returns the Service names supported by this component
      *
      * @return String[] ServiceNames
      */
     public String[] getSupportedServiceNames()
     {
         return msServiceNames;
     }

     public boolean supportsService( String sService )
     {
         return ( sService.equals( msServiceNames[0] ));
     }

     /**
      * Provides the implementation name of the service implementation
      *
      * @return The Implementation Name
      */
     public String getImplementationName()
     {
         return _Toolbar.class.getName();
     }
   }

   public static XSingleComponentFactory __getComponentFactory(String sImplName)
   {
       XSingleComponentFactory xFactory = null;

       if ( sImplName.equals( _Toolbar.class.getName() ) )
           xFactory = com.sun.star.lib.uno.helper.Factory.createComponentFactory(_Toolbar.class, _Toolbar.msServiceNames);

       return xFactory;
   }


   public static boolean __writeRegistryServiceInfo( com.sun.star.registry.XRegistryKey xRegistryKey )
   {
       return Factory.writeRegistryServiceInfo( _Toolbar.class.getName(), _Toolbar.msServiceNames, xRegistryKey );
   }
}

Changelog

DateUserModification

and