satishbejgum Power User


Joined: 17 Sep 2007 Posts: 74
|
Posted: Thu Dec 27, 2007 9:22 pm Post subject: plugin for openoffice |
|
|
Hello every one
I have code which creates a button in menu bar. This code works for only one document. Now I want this Code to be worked for all documents, if i am not wrong i should create a plug-in for open office. | Code: |
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
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.ComponentBase;
import com.sun.star.ui.XUIElement;
import com.sun.star.ui.XUIElementSettings;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.xml.sax.*;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XLayoutManager;
import com.sun.star.frame.XToolbarController;
import com.sun.star.frame.XStatusListener;
import com.sun.star.frame.XUIControllerRegistration;
import com.sun.star.util.XUpdatable;
import com.sun.star.frame.FeatureStateEvent;
import com.sun.star.awt.XWindow;
import com.sun.star.lang.EventObject;
import com.sun.star.awt.XKeyListener;
import com.sun.star.awt.KeyEvent;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XIndexContainer;
import com.sun.star.container.XSet;
import javax.swing.JOptionPane;
public class Toolm
{
public static class Toolsc extends ComponentBase implements XInitialization, XStatusListener, XUpdatable, XKeyListener, XServiceInfo, XToolbarController
{
public static final String[] msServiceNames = { "ToolBar.testtoolsc" };
private XComponentContext mxContext =null;
private XMultiComponentFactory mxMCF=null;
private XMultiServiceFactory mxMSF=null;
private String msInternalName=null;
private XComponent mxDocument=null;
public Toolsc(XComponentContext xContext) {
try
{
mxContext = xContext;
mxMCF = mxContext.getServiceManager();
mxMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, mxMCF);
}
catch (Exception e)
{
e.printStackTrace();
}
msInternalName = new String();
mxDocument = null;
}
public void initialize(Object[] args) throws com.sun.star.uno.Exception {
// Component Initialization.
}
public String getName() {
// Retunrns the internal name of this component
synchronized (this) {
return msInternalName;
}
}
public void setName(String sName) {
// Sets the internal name of this component
}
public void execute(short nKeyModifier) {
System.out.println("Protecting wait...");
}
public void click() {
// Single Click Mouse event handler
JOptionPane.showMessageDialog(null, "Please wait");
}
public void doubleClick() {
// Double Click Mouse event handler
}
public XWindow createPopupWindow()
{
// Pop-up window creator. Not Implemented
return null;
}
public void statusChanged(FeatureStateEvent aState) {
// Status Changed Event Listener com.sun.star.frame.FeatureStateEvent status
System.out.println("statusChanged: ");
}
public void disposing(EventObject aSource) {
// Disposing event handler
}
public void update() {
System.out.println("Toolbar created & updated...");
}
public void keyPressed(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
}
public String[] getSupportedServiceNames() {
// Returns the Service names supported by this component String[] ServiceNames
return msServiceNames;
}
public boolean supportsService(String sService) {
return (sService.equals(msServiceNames[0]));
}
public String getImplementationName() {
return this.getClass().getName();
}
public XWindow createItemWindow(XWindow xWindow) {
return null;
}
}
public XMultiServiceFactory getMultiServiceFactory ( XComponentContext xComponentContext )
throws java.lang.Exception {
// Retrieve its
// property DefaultContext and get the
// remote service manager
XMultiComponentFactory xRemoteServiceManager =
xComponentContext.getServiceManager();
// Instantiate the MultiServiceFactory service
Object oMSF =
xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.lang.MultiServiceFactory",
xComponentContext);
return (XMultiServiceFactory) UnoRuntime.queryInterface ( XMultiServiceFactory.class, oMSF);
}
public static void main(String[] args) {
try {
// create and register a factory for your class
XComponentContext xContext = Bootstrap.bootstrap();
XSingleComponentFactory xFactory = Factory.createComponentFactory(Toolsc.class,
Toolsc.msServiceNames);
Object desktop = xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class, desktop);
// Load a Writer document, which will be automaticly displayed
xCompLoader.loadComponentFromURL("private:factory/swriter",
"_blank", 0, new PropertyValue[0]);
Object xDum = xContext.getValueByName("/singletons/com.sun.star.lang.theServiceManager");
XSet _SM = (XSet) UnoRuntime.queryInterface(XSet.class, xDum);
_SM.insert(xFactory);
// use the toolbar
final String resourceURL = "private:resource/toolbar/ToolBar_1";
final String sUnoUrl = ".uno:CommandLineToolbar";
final String sService = "ToolBar.testtoolsc";
final String sToolbarUIName = "ToolBar_1";
//-------------------- create the service
//oo = createUnoService("com.sun.star.frame.ToolBarControllerFactory");
Toolm toolm = new Toolm();
XMultiServiceFactory xMSF = toolm.getMultiServiceFactory ( xContext );
Object ooToolbar = xMSF.createInstance("com.sun.star.frame.ToolBarControllerFactory");
XUIControllerRegistration oo = (XUIControllerRegistration) UnoRuntime.queryInterface ( XUIControllerRegistration.class, ooToolbar);
if (oo.hasController( sUnoUrl, "")) oo.deregisterController( sUnoUrl, "");
else oo.registerController( sUnoUrl, "", sService);
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
XFrame xFrame = xDesktop.getCurrentFrame();
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xFrame);
XLayoutManager oLM = (XLayoutManager) UnoRuntime.queryInterface(XLayoutManager.class, xPropSet.getPropertyValue("LayoutManager"));
//-------------------- get my toolbar
if (oLM.getElement(resourceURL) != null) oLM.destroyElement(resourceURL);
oLM.createElement(resourceURL);
XUIElement toolbarUIElement = oLM.getElement(resourceURL);
//oUIElement has this interface too... Not really clear with the documentation...
XUIElementSettings toolbarUIElementSettings = (XUIElementSettings) UnoRuntime.queryInterface(
XUIElementSettings.class, toolbarUIElement);
XIndexAccess toolbarIndexAccess = toolbarUIElementSettings.getSettings(true);
XPropertySet toolbarUIElementSettingsPropertySet = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, toolbarIndexAccess );
toolbarUIElementSettingsPropertySet.setPropertyValue("UIName", sToolbarUIName );
PropertyValue [] aToolbarItem = new PropertyValue[2];
aToolbarItem[0] = new PropertyValue();
aToolbarItem[0].Name = "CommandURL";
aToolbarItem[0].Value = sUnoUrl;
aToolbarItem[1] = new PropertyValue();
aToolbarItem[1].Name = "Label";
aToolbarItem[1].Value = "Protect";
XIndexContainer toolbarIndexContainer = (XIndexContainer)
UnoRuntime.queryInterface( XIndexContainer.class,toolbarIndexAccess );
toolbarIndexContainer.insertByIndex( 0, aToolbarItem );
toolbarUIElementSettings.setSettings(toolbarIndexAccess);
oLM.showElement(resourceURL);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
can any one tell me how to create a plug-in for open office |
|