Sunday, December 6, 2009

Sinhala Inline for Firefox ver 1.1

This is the latest version of the Firefox addon which I build earlier.

Many new features are added to the extension to improve the usability.

Few screen shots of the add-on is shown below.














You can download it using the following Link
Click Here to Download (WithOut EnterKey Conversion)
Click Here to Download (With EnterKey Conversion)

Thursday, November 26, 2009

How to retrieve Text from a Document (Open Office AddOn)

The following code will retrieve the Word before the cursor position

public String readPreviousWordFromDocument() {


          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();


            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");


            XPageCursor xPageCursor = (XPageCursor) UnoRuntime.queryInterface(
                    XPageCursor.class, xViewCursor);
            XText xDocumentText = xViewCursor.getText();
            XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());


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


            xWordCurser.gotoPreviousWord(true);


            String lastWord = xWordCurser.getString();
           return lastWord;
        } catch (Exception e) {
           return "";
        }
    }


In the above code by changing the Cursor (eg:- xWordCurser ,xParagraphCursor etc.. ) you can shift between word word or else paragraphs.


If you want to change which word ...simply change by changing the following line
" xWordCurser.gotoPreviousWord(true);"

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 !!!


Sunday, November 8, 2009

Sinhala Inline for Firefox

Today I am going to introduce you to a new Firefox extension which help you to type in Sinhala Language.
you can type in Sinhala pronunciation of the word in English letters and translate as required.

An image of the Sinhala Tool Bar is shown below.

When Inactive




When Active



You can type in English letters in any text area in the browser window and by pressing
Convert Button or appropriate shortcut keys (F2 or Ctrl+Space) you can convert it to Sinhala Unicode Characters.

Small discription of the tool bar is given below.

Enable/Disable - To activate deactivate the tool bar

Preview Box- This box displays the converted form of the English letters you input while you are entering.In     some special cases you have to explicitly select the word to be previewed in the Preview box.

Convert- This converts the selected English pronunciation of the words to Sinhala unicode letters (if you haven't select any word,the entire text area is going to be converted).you can use the shortcut keys (F2 or Ctrl+Space) to do the same conversion and the right click menu button(Which will be shown to you later).

Copy- This button copies the converted form of the English word/letters (entire text area when not selected any word).

Clear-Clears the preview box and the current text input area.

Scheme-You can see the conversion scheme (English letters to Sinhala) by clicking this.

Help & Tips- A help window appears to describe the use of various buttons and menus and it gives few tips to improve the usability of the application.

About - This will show information about the Author (MY SELF :D )

The right click menu button is shown below.


















Since this is the initial release of the extension more update version will come in future with more improvements.Please feel free to give any suggestions you prefer to have in the application.

How to install
Down load the file from the link given below and simply drag and drop it to firefox window.Then you will be asked to install it.