Difference between revisions of "NetBeans Tips and Tricks"

From Apache OpenOffice Wiki
Jump to: navigation, search
(New page: = 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. I would be very glad if others a...)
 
(Tips and Tricks)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Overview =
 
= 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.
 
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.
I would be very glad if others also would like to contribute.
+
Since I am working on the '''[[OpenOffice_NetBeans_Integration|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.
Since I am working on the '''[[OpenOffice_NetBeans_Integration|OpenOffice.org API plug-in for NetBeans]]''', I hope to add some of the stuff I present here to the plug-in at one stage.
+
 
 +
If not otherwise stated, everything works with the current NetBeans (major) version. At the time of writing, this is NetBeans 6.
  
 
== Tips and Tricks ==
 
== Tips and Tricks ==
  
 
=== UnoRuntime.queryInterface ===
 
=== 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.
+
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?
 
Why not make things easier using NetBeans on-bord features?
  
Open ~~green:Tools->Options->Editor->Code Templates and add a new template (click on "new"). I gave my template the abbreviation "ur" (as in '''U'''no'''R'''untime). Then I inserted the following code as "Expanded Text":
+
Open <span style="color:green">Tools->Options->Editor->Code Templates</span> and add a new template (click on &quot;New&quot;). I gave my template the abbreviation &quot;ur&quot; (as in '''U'''no'''R'''untime), but feel free to choose your own name. Then insert the following code as &quot;Expanded Text&quot;:
X${Interface} x${Interface} = (X${Interface})UnoRuntime.queryInterface(X${Interface}.class, ${EXP leftSideType instanceof="java.lang.Object" default="oObject"});
+
 
 +
X${Interface} x${Interface} = (X${Interface})UnoRuntime.queryInterface(X${Interface}.<span style="color:blue">class</span>, ${EXP leftSideType <span style="color:blue">instanceof</span>=<span style="color:red">"java.lang.Object"</span> <span style="color:blue">default</span>=<span style="color:red">"oObject"</span>});
 +
 
 +
Now, when you type &quot;ur&quot; and expand this (by pressing &lt;TAB&gt;) in the Java Editor, it becomes
 +
 
 +
X<span style="background-color:#6666CC;">Interface</span> x<span style="background-color:#0099FF;">Interface</span> = (X<span style="background-color:#0099FF;">Interface</span>)UnoRUntime.queryInterface(X<span style="background-color:#0099FF;">Interface</span>.<span style="color:blue">class</span>, oObject);
 +
 
 +
You can type the UNO type of your choice, e.g. &quot;Control&quot; and every &quot;Interface&quot; changes accordingly.
 +
After pressing return, you change to &quot;oObject&quot;. 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();<br>
 +
X<span style="background-color:#6666CC;">Interface</span> x<span style="background-color:#0099FF;">Interface</span> = (X<span style="background-color:#0099FF;">Interface</span>)UnoRUntime.queryInterface(X<span style="background-color:#0099FF;">Interface</span>.<span style="color:blue">class</span>, myName);
 +
 
 +
You can then just enter the UNO type and keep &quot;myName&quot; by pressing return.
 +
 
 +
 
 +
=== Assertions ===
 +
 
 +
To enable assertions in NetBeans, create a new configuration for executing your Java application. In the new configuration, assertions are enabled.
 +
 
 +
 
 +
Click right on your project and select &quot;Properties&quot; then go to the &quot;Run&quot; category:
 +
 
 +
 
 +
[[Image:new_configuration.png]]
 +
 
 +
 
 +
Click on &quot;New&quot; to create a new configuration. Enter the name of the configuration:
 +
 
 +
 
 +
[[Image:Configuration_name.png]]
 +
 
 +
 
 +
Click &quot;OK&quot; and enter the additional Java VM option to enable assertions:
 +
 
 +
 
 +
[[Image:Enable_assertions.png]]
 +
 
 +
 
 +
Click &quot;OK&quot; again to enable your new configuration.
 +
 
  
Now, when you type &quot;ur&quot; and expand this in the Java Editor, it becomes
+
Now you just have to switch between configurations to enable/disable the assertions in your project.
X''Interface'' x''Interface'' = (X''Interface'')UnoRUntime.queryInterface(X''Interface''.class, oObject);
+
  
You can type the UNO type of your choice, e.g. &quot;Control&quot; and every &quot;Interface&quot; above becomes &quot;Control&quot;.
 
After pressing return, you change to &quot;oObject&quot;. This is also a placeholder that gets filled with some intelligence. If the line above yours contains something like &quot;Object o = ...&quot;, &quot;oObject&quot; is replaced by o.
 
  
== Annoyances ==
+
[[Category:NetBeans]]
Not that there are none, but I cannot think of anything right now.
+
[[Category:API]]

Latest revision as of 11:02, 17 June 2008

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.


Assertions

To enable assertions in NetBeans, create a new configuration for executing your Java application. In the new configuration, assertions are enabled.


Click right on your project and select "Properties" then go to the "Run" category:


New configuration.png


Click on "New" to create a new configuration. Enter the name of the configuration:


Configuration name.png


Click "OK" and enter the additional Java VM option to enable assertions:


Enable assertions.png


Click "OK" again to enable your new configuration.


Now you just have to switch between configurations to enable/disable the assertions in your project.

Personal tools