ToolbarController Combobox Combo-Box Toolbar Aidan Butler How 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>

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.

// Toolbar Controller implementation package Toolbar; import {%see com.sun.star.beans.PropertyValue}; import {%see com.sun.star.document.XImporter}; import {%see com.sun.star.document.XExporter}; import {%see com.sun.star.document.XFilter}; import {%see com.sun.star.lang.XComponent}; import {%see com.sun.star.lang.XInitialization}; import {%see com.sun.star.lang.XMultiComponentFactory}; import {%see com.sun.star.lang.XMultiServiceFactory}; import {%see com.sun.star.lang.XSingleComponentFactory}; import {%see com.sun.star.lang.XServiceInfo}; import {%see com.sun.star.lib.uno.helper.Factory}; import {%see com.sun.star.lib.uno.helper.WeakBase}; import {%see com.sun.star.registry.XRegistryKey}; import {%see com.sun.star.uno.XComponentContext}; import {%see com.sun.star.uno.UnoRuntime}; import {%see com.sun.star.uno.XInterface}; import {%see com.sun.star.uno.Type}; import com.sun.star.xml.sax.*; import {%see com.sun.star.frame.XToolbarController}; import {%see com.sun.star.frame.XStatusListener}; import {%see com.sun.star.util.XUpdatable}; import {%see com.sun.star.frame.FeatureStateEvent}; import {%see com.sun.star.awt.XWindow}; import {%see com.sun.star.awt.XWindowPeer}; import {%see com.sun.star.awt.WindowDescriptor}; import {%see com.sun.star.awt.WindowClass}; import {%see com.sun.star.awt.Rectangle}; import {%see com.sun.star.awt.WindowAttribute}; import {%see com.sun.star.awt.VclWindowPeerAttribute}; import {%see com.sun.star.awt.XComboBox}; import {%see com.sun.star.awt.XTextComponent}; import {%see com.sun.star.awt.XFixedText}; import {%see com.sun.star.awt.XToolkit}; import {%see com.sun.star.lang.EventObject}; import {%see com.sun.star.awt.XKeyListener}; import {%see com.sun.star.awt.KeyEvent}; import {%see com.sun.star.frame.XDesktop}; import {%see com.sun.star.text.XTextDocument}; import {%see com.sun.star.frame.XComponentLoader}; import {%see com.sun.star.util.XOfficeInstallationDirectories}; import {%see com.sun.star.frame.XModel}; import {%see com.sun.star.beans.XPropertySet}; import {%see com.sun.star.text.XTextCursor}; import {%see com.sun.star.frame.XFrame}; import {%see 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 {%see 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 {%see 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 {%see com.sun.star.frame.FeatureStateEvent} status */ public void statusChanged ( FeatureStateEvent aState ) { } /** * Disposing event handler */ public void disposing ( EventObject aSource ) { } /** * Toolbarcontroller {%see 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 {%see com.sun.star.awt.KeyEvent} that has occured * @see KeyEvent */ public void keyPressed(KeyEvent event) { } /** * @param event The {%see 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 ); } }