Geertjan's Blog

  • March 29, 2008

Integrating Ubuntu into NetBeans IDE

Geertjan Wielenga
Product Manager
I like Ubuntu's text editor, especially for XML files it's great—gives you very nice syntax coloring. So I created a menu item on XML file nodes in NetBeans IDE, especially for opening an XML file into Ubuntu's text editor. That way, I simply have an extra window for editing XML. Not super useful functionality, but handy sometimes.

To create this action, use the New Action wizard to create a new conditional action (i.e., not "always enabled"), for DataObjects. Specify that it should be invoked from the text/xml-mime MIME type. Then set dependencies on Datasystems API, Execution API, File System API, Nodes API, and Utilities API.

Then fill out the CookieAction.performAction as follows, taking note of the line in bold below, because that's where all the action happens:

protected void performAction(Node[] activatedNodes) {
try {
DataObject dataObject = activatedNodes[0].getLookup().lookup(DataObject.class);
FileObject fo = dataObject.getPrimaryFile();
String file = fo.getURL().getFile();
try {
NbProcessDescriptor desc = new NbProcessDescriptor("gedit", file);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex);
} catch (FileStateInvalidException ex) {

You should be using the following import statements:

import java.io.IOException;
import javax.swing.JOptionPane;
import org.openide.execution.NbProcessDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;

And that's it. Install the module and notice the new menu item on XML files (mine is called "Open in Text Editor"). When the menu item is selected, the selected XML file opens in Ubuntu's (or any other Linux or whatever system) text editor (or some other application), displaying your file.

This is how you would open a file programmatically in OpenOffice.org, just replacing the important line above with this one:

NbProcessDescriptor desc = new NbProcessDescriptor("ooffice", " -writer " +file);

I tried to open a file in both Eclipse and IntelliJ, using the above line of code, but—even though both applications start up successfully—in neither case does the file open when the application has started up. Maybe there's a special command line for opening a file in Eclipse or IntelliJ when they start up? (Something like -file fileName.) But I suspect that, unlike NetBeans IDE, they don't have an option like that. It seems to me that the other two IDEs are not able to have a specific file be opened on start up, based on a command line setting specified at start up.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.