Symphony Thursday : Developer 101 - Introduction to UNO Development and Documents/Writer "Hello World"

February 5 2009

This is the first of the Symphony Thursday developer articles. In this post, we will cover the introduction to UNO development, a bit of history, and our first "Hello World" application.

Introduction to OpenOffice.org Development

  • Designed on an interface-based component model called Universal Network Objects (UNO)
  • UNO definition
  • UNO offers interoperability between different programming languages, different object models, different machine architectures, and different processes; either in a local network or even via the Internet
  • UNO components can be implemented in, and accessed from, any programming language for which a UNO language
  • binding exists

OpenOffice.org? StarOffice? Lotus Symphony?
  • StarDivision StarOffice written in the mid 80s
  • Bought by Sun Microsystems in 1999, StarOffice released in August 1999
  • Source code released October 2000
  • OpenOffice.org 1.0 released May 2002, 2.0 in October 2005
  • OpenDocument format becomes international standard ISO/IEC 26300 November 2006
  • IBM Lotus Workplace Productivity Editors released 2005
  • Based on OpenOffice.org 1.1
  • OpenOffice.org 2.2 released March 2007
  • Notes 8 to include IBM Productivity Tools, based on OpenOffice.org 1.1.X
  • Lotus Symphony 1.2 Released December 2008

What is the ServiceManager?


The ServiceManager is similar to the NotesSession class that we are familiar with when using LotusScript with Notes and Domino applications. For those used to the Java world, The Service Manager is a factory class. I like to describe it as the foundation of the house. Once we have the foundation put in place, we can build the walls, roof, and all the details. To call the Service Manager, we do the following:

    Set SM=CreateObject("com.sun.star.ServiceManager")

Think of the ServiceManager as a back-end class that we use in LoutsScript. Once we have a handle to the back-end class, we want the front end (like NotesUIWorkspace). The following line of code is used:

    Set Desktop=SM.createInstance("com.sun.star.frame.Desktop")

Now for those of you who are familiar with the Java world, it looks like we are calling Java classes here. In fact we are. To be specific, we are using OLE automation to call Java classes that are in the Registry as COM objects. Talk about a righteous hack!

What Did That Do?


Image:Symphony Thursday : Developer 101 - Introduction to UNO Development and Documents/Writer "Hello World"



Calling the ServiceManager and the Desktop objects created an OpenOffice.org window, but it is just a shell. We have to do more to get one of the applications to open. If you were doing this with Lotus Symphony, we would not see anything. Lotus Symphony does not display the ServiceManager or Desktop.

Let’s Start Lotus Symphony Documents


So far we have an Lotus Symphony window, but it doesn’t know what application it will be. We need to tell Lotus Symphony to open a specific application. We use the following code:

    Dim args()
    Set WriterApplication=Desktop.loadComponentFromURL ("private:factory/swriter","_blank",0,args)

First of all, Args is a variant array. Even though we don’t want to pass any parameters at this time, we must pass an array into the method.

You will also notice that we have to call a URL to load the correct application / component. The “s” in “swriter” stands for “Star” which comes from the Star Office Writer application naming. From Star Office to OpenOffice to Sun Office, the internal component names have never changed!

After calling those two lines of code, we have a word processor up and running.

The Picture So Far


Image:Symphony Thursday : Developer 101 - Introduction to UNO Development and Documents/Writer "Hello World"



Now we have an application open and it is time do something.

Let’s Add Some Text


First, we need to get a handle to the text part of the document. We do that with the following code:

    Set WriterText=WriterApplication.getText()

Next, we need a cursor position where we can insert the text:

    Set Cursor=WriterText.createTextCursor()

Finally, we can make the traditional greeting

    Call WriterText.insertString(Cursor,"Hello World!",False)

Hello World!


After executing all of the code we walked through, this is the result:



Image:Symphony Thursday : Developer 101 - Introduction to UNO Development and Documents/Writer "Hello World"


Conclusion


To try this yourself, create a new button in a Notes document in Notes 8.0.1, 8.0.2, or 8.5. 8.0.0 will not work as it does not properly register the components in the registry. With 8.0.0 or Notes 6, 6.5, or 7.X, you can use this code with OpenOffice.org 1.1.6 or higher, 2.x, or 3.x. Try it and see it work for yourself:

Dim SM As Variant
Dim Desktop As Variant
Dim WriterApplication As Variant
Dim WriterText As Variant
Dim Cursor As Variant
Dim args()
       
Set SM=CreateObject("com.sun.star.ServiceManager")
Set Desktop=SM.createInstance("com.sun.star.frame.Desktop")
Set WriterApplication=Desktop.loadComponentFromURL("private:factory/swriter","_blank",0,args)
Set WriterText=WriterApplication.getText()
Set Cursor=WriterText.createTextCursor()
Call WriterText.insertString(Cursor,"Hello World!",False)

Next time, we will look at the basics of Lotus Symphony Spreadsheets / OpenOffice.org Calc