NetBeans Tips and Tricks

From Apache OpenOffice Wiki
Revision as of 07:56, 6 June 2008 by Sg (Talk | contribs)

Jump to: navigation, search

Overview

I created this wiki page to share some of my settings, tricks, and best practices around using NetBeans to program for OpenOffice.org in Java. Since I am working on the OpenOffice.org API plug-in for NetBeans, I hope to add some of the stuff here to the plug-in at one stage. Please feel free to add your own ideas.

If not otherwise stated, everything works with the current NetBeans (major) version. At the time of writing, this is NetBeans 6.

Tips and Tricks

UnoRuntime.queryInterface

Eeverybody who programs in Java for OOo stumbles upon UnoRuntime.queryInterface(). It really started annoying me how often I had to type that even in small programs.

Why not make things easier using NetBeans on-bord features?

Open Tools->Options->Editor->Code Templates and add a new template (click on "New"). I gave my template the abbreviation "ur" (as in UnoRuntime), but feel free to choose your own name. Then insert the following code as "Expanded Text":

X${Interface} x${Interface} = (X${Interface})UnoRuntime.queryInterface(X${Interface}.class, ${EXP leftSideType instanceof="java.lang.Object" default="oObject"});

Now, when you type "ur" and expand this (by pressing <TAB>) in the Java Editor, it becomes

XInterface xInterface = (XInterface)UnoRUntime.queryInterface(XInterface.class, oObject);

You can type the UNO type of your choice, e.g. "Control" and every "Interface" changes accordingly. After pressing return, you change to "oObject". This is also a placeholder that gets filled with some intelligence. If the line above yours contains something that NetBeans recognizes, you will get this:

Object myName = getSomeUnoObject();
XInterface xInterface = (XInterface)UnoRUntime.queryInterface(XInterface.class, myName);

You can then just enter the UNO type and keep "myName" by pressing return.

Personal tools