Wednesday, November 25, 2009

Open Office AddOn Development

Introduction
Open office extension is the best way of adding extra features and functionalities which are customized to your work.Developing a OOExtension is not hard as you think.


Requirements:
Java programing knowledge is required with little amount of experience in dealing with API's.
NetbeansIDE with OpenOffice SDK installed...
and the additional requirements will be discussed later.


So lets build one ................ 


Establishing the required applications


Install Open Office application in you computer ( http://www.openoffice.org/ )
Install Netbeans IDE ( http://netbeans.org/ )
OpenOffice SDK ( http://download.openoffice.org/3.0.0/sdk.html 
Installing Required Plugins for Netbeans : - 
         Go to  Tools > Plugins 





install the plugin which is selected in the image
Plugin : - OpenOffice.org API Plugin


Now you are ready to start!!


Just go through the following link and you will get the basic understanding of the overall process of building to deployment.............. Just go through the images...... :D


http://wiki.services.openoffice.org/wiki/OpenOffice_Add-On_Project_Type


Hope now you have build the structure of the Addon...Where to start programing ???????? What are the classes?????? Whats that long codes.............???????


Isnt it the main problem you are facing ... :) No worries. Most of the code which are automatically generated is to make the Addon fit in to openoffice.


So lets check the main things you need to know


Mainly there are two classes....
1. CentralRegistrationClass
2.Other class


You only have to deal with the latter one (2 one)


you can add extraclasses if you want including any GUI items as well to you project.


Main thing you have to understand is how this starts.The following code shows you the place you can start with (Generally)



    public void dispatch(com.sun.star.util.URL aURL,
            com.sun.star.beans.PropertyValue[] aArguments) {
        
        if (aURL.Protocol.compareTo("com.example.sinhalaaddon:") == 0) {
           
            if (aURL.Path.compareTo("Command1") == 0) {
                // add your own code here
                return;
            }



you can add the code which you need to execute when a button is pressed in the place


" // add your own code here "


So if you need to open a new Dialog box..Jframe ...anything, add the code in a method and simple call it inside the place mentioned above.


Lets look at few specific things you need 
Total Reference:
http://api.openoffice.org/docs/DevelopersGuide/DevelopersGuide.xhtml
Accessing the Document ( xText )
The following code will do the things 



     public void setTextDocument(String text) {
         XMultiComponentFactory xmcf = m_xContext.getServiceManager();
        Object desktop;


        try {
            desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);


            // retrieve the current component and access the controller
            XComponent xCurrentComponent = xDesktop.getCurrentComponent();


            // get the XModel interface from the component
            XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xCurrentComponent);


            // the model knows its controller
            XController xController = xModel.getCurrentController();


            // the controller gives us the TextViewCursor
            // query the viewcursor supplier interface
            XTextViewCursorSupplier xViewCursorSupplier =
                    (XTextViewCursorSupplier) UnoRuntime.queryInterface(
                    XTextViewCursorSupplier.class, xController);


            // get the cursor
            XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();


            // query its XPropertySet interface, we want to set character and paragraph properties
            XPropertySet xCursorPropertySet = (XPropertySet) UnoRuntime.queryInterface(
                    XPropertySet.class, xViewCursor);


            // set the appropriate properties for character and paragraph style
            xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
            xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");


            // print the current page number – we need the XPageCursor interface for this
            XPageCursor xPageCursor = (XPageCursor) UnoRuntime.queryInterface(
                    XPageCursor.class, xViewCursor);




            // the model cursor is much more powerful, so
            // we create a model cursor at the current view cursor position with the following steps:
            // we get the Text service from the TextViewCursor, the cursor is an XTextRange and has
            // therefore a method getText()
            XText xDocumentText = xViewCursor.getText();


            // the text creates a model cursor from the viewcursor
            XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());


            XWordCursor xWordCurser = (XWordCursor) UnoRuntime.queryInterface(
                    XWordCursor.class, xModelCursor);


            xWordCurser.gotoPreviousWord(true);


            String lastWord = xWordCurser.getString();
            System.out.println(lastWord);
            xWordCurser.setString(text);
        } catch (Exception e) {
        }



}


 above code will set some string to the document to the left of the cursor posision.
Here are some links for resources to learn more about
Details about document Structure :-


Do not get confused with the codes and OpenOffice Architecture.Only focus on you section of interest.. :)




So Now Lets Look at how to addEventListeners
Add KeyListener:
First make you class implements the "XKeyHandler" interface and its abstract methods ( given below).Your code which needs to be run attached to an event goes inside the following methods.


public boolean keyPressed(com.sun.star.awt.KeyEvent arg0) { }
 public boolean keyReleased(com.sun.star.awt.KeyEvent arg0) { }
public void disposing(EventObject arg0) { }


The following code will add a KeyListener to the Document window.



  public  void AddKeyListener() {
       
        XMultiComponentFactory xmcf = m_xContext.getServiceManager();
        Object desktop;
       
        try {
            desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
            XWindow MyWindow     = xDesktop.getCurrentFrame().getComponentWindow(); //for testing
       
XWindowPeer MyWindowPeer = (XWindowPeer)
UnoRuntime.queryInterface (XWindowPeer.class, MyWindow);
XToolkit MyToolkit = MyWindowPeer.getToolkit();
XExtendedToolkit MyExtToolkit = (XExtendedToolkit)
UnoRuntime.queryInterface (XExtendedToolkit.class, MyToolkit);
MyExtToolkit.addKeyHandler (this);

        } catch (Exception e) {
            System.out.println("dispatch Exception : " + e.getMessage());
            e.printStackTrace();
        }


    }



The method in Bold is the place where you register the Listener.




If you implement the same class with KeyHandler then user "this".


Alternative
or else create a instance of a new class as follows


MyExtToolkit.addKeyHandler (new YourClass());


"YourClass()" has to implement the KeyHandler interface and its abstracts method




What about MouseListeners ???????????????
Well this process is also similar like adding a keyHandler.Go through the Openoffice API and mailing lists for more information.


Best way of referring the API is using your IDE as shown in the following image

you can register in a mailing list using the following link
http://www.oooforum.org/

Adding the AddOn to Openoffice :-


Its that simple :D

As a Developer of Open Office extensions you are not alone..Lot of people out there to help you.

Good Luck !!!


1 comment:

  1. Good day Ashan,

    First off, let me say a big THANK YOU for posting this. I have spent a little while trying to discern exactly how key/mouse events would be assigned in UNO.

    My concern is this, I've tried to use your example and it compiles and is added as an extension to UNO, but after the addKeyHandler is called, keys that are typed are no longer being displayed in Writer, only the code in keyPressed/Released methods are executed. Is this how the listeners are supposed to work? Is it that the programmer is to write the supporting code to allow Open Office to operate 'as per usual'?

    Thanks again,
    Darryl

    ReplyDelete