 | OO-Snippets: Adding a Combo-box to a ToolbarCommons| Keywords | ToolbarController, Combobox, Combo-Box, Toolbar |
|---|
| Language | Java |
|---|
| Application | Office |
|---|
| Authors | Aidan Butler (initial)
|
|---|
| Supported Versions | 2.0.x |
|---|
| Supported OS | All |
|---|
| Question | How Do I add a Combo-box to an OpenOffice Toolbar
<node oorname="c3" oorop="replace">
<prop oorname="Command">
<value>.uno:CommandLineToolbar</value>
</prop>
<prop oorname="Module">
<value></value>
</prop>
<prop oorname="Controller">
<value>com.sun.star.test.testtoolbar</value>
</prop>
</node>
xml version="1.0" encoding="UTF-8"
<!DOCTYPE toolbar:toolbar PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "toolbar.dtd">
<toolbartoolbar xmlnstoolbar="http://openoffice.org/2001/toolbar" xmlnsxlink="http://www.w3.org/1999/xlink" toolbaruiname="custom_toolbar_1">
<toolbartoolbaritem xlinkhref=".uno:CommandLineToolbar"/>
</toolbartoolbar>
|
|
|---|
| 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. |
|---|
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 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
public String getName()
{
synchronized(this)
{
return msInternalName;
}
}
Sets the internal name of this component
@param sName
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.
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
@return
@see XWindow
public XWindow createItemWindow(XWindow xWindow) {
try {
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);
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";
XWindowPeer cBox_xWinPeer = xToolkit.createWindow(wd);
cBox_xComboBox = (XComboBox) UnoRuntime.queryInterface(XComboBox.class, cBox_xWinPeer);
fixedText = (XTextComponent)UnoRuntime.queryInterface(XTextComponent.class,cBox_xComboBox);
fixedText.setText("Enter Command Here");
XWindow cBox_xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, cBox_xWinPeer);
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
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.
@param event
@see KeyEvent
public void keyPressed(KeyEvent event)
{
}
@param event
@see KeyEvent
public void keyReleased(KeyEvent event)
{
}
Returns the Service names supported by this component
@return
public String[] getSupportedServiceNames()
{
return msServiceNames;
}
public boolean supportsService( String sService )
{
return ( sService.equals( msServiceNames[0] ));
}
Provides the implementation name of the service implementation
@return
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 );
}
}
|
|