Hi,
I also read through the "Programmatic control of Toolbar Items:
ComboBox possible?" thread, and while I found it useful, it missed out
some of the practical steps for creating the toolbar controller. I have
attached some code which may be of use below. Compiling this code and
registering it with unopkg will make it available for use as a
toolbarcontroller in openoffice. Entries must then be added to the
Controller.xcu to register the new controller and a toolbar definition
must be created.
The code that will interest you most is the createItemWindow() method
which creates teh comboBox for insertion into to Toolbar.
Hope this helps,
-Aidan
//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>
// 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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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 The [EMAIL PROTECTED] FeatureStateEvent} status
*/
public void statusChanged ( FeatureStateEvent aState )
{
}
/**
* Disposing event handler
*/
public void disposing ( EventObject aSource )
{
}
/**
* Toolbarcontroller [EMAIL PROTECTED] XUpdatable} implementation update
controller.
*
*/
public void update ( )
{
}
/**
* KeyPress event handler. This method is used to intercept the
* Key press events
*
* @param event The [EMAIL PROTECTED] KeyEvent} that has occured
* @see KeyEvent
*/
public void keyPressed(KeyEvent event){
}
/**
* @param event The [EMAIL PROTECTED] 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 );
}
}
//end
Carsten Driesner wrote:
Jérémy Bethmont wrote:
What are the possibilities of the toolbar? Can i add any kind of
star.awt widgets or only buttons?
Hi Jérémy,
You can add any window type which is a child window and can be created
by the com.sun.star.awt.Toolkit. There are some limitations how you
can use these widgets by the Toolkit API. Please see the IDL
documentation for com.sun.star.awt. If you want to use widgets for
your toolbar controls, you have to implement toolbar controllers.
There is a long going discussion on openoffice.dev called
"Programmatic control of Toolbar Items: ComboBox possible?" between
Matthias Benkmann and me about toolbar controllers. You can definitely
find some valuable information there.
Regards,
Carsten
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]