Friday October 05, 2007
NetBeans APIs Outside of the NetBeans Platform
Jarda and I are in Budapest visiting a team of developers. Somewhere along the line, I think in the taxi on the way from the hotel to the site, he knocked together an application that uses the NetBeans APIs, without using the NetBeans Platform. In other words, there's no module here. Just a plain Java application. The end result is a file browser that looks as follows:
And this is the source structure of the application. Of course, take special note of the NetBeans API JAR files. You can easily get them from your distribution of NetBeans IDE and then simply add them to your own application:
By the way, Jarda added the beans from the org.openide.explorer.ExplorerManager JAR to the Palette, in order to be able to drag and drop the two views on the JFrame! He says that ideally the apisupport project should do that by default. Here's how it looks:
Here's FileNode.java:
import java.io.File;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
public final class FileNode extends AbstractNode {
private File file;
private FileNode(File f) {
super(new FileKids(f));
file = f;
setName(f.getName());
}
public static Node files() {
AbstractNode n = new AbstractNode(new FileKids(null));
n.setName("Root");
return n;
}
private static final class FileKids extends Children.Keys<File> {
File file;
public FileKids(File file) {
this.file = file;
}
@Override
protected void addNotify() {
if (file == null) {
File[] arr = File.listRoots();
if (arr.length == 1) {
arr = arr[0].listFiles();
}
setKeys(arr);
} else {
File[] arr = file.listFiles();
if (arr != null) {
setKeys(arr);
}
}
}
@Override
protected Node[] createNodes(File f) {
FileNode n = new FileNode(f);
return new Node[] { n };
}
}
}
Here's Explorer.java:
import org.openide.explorer.ExplorerManager;
public class Explorer extends javax.swing.JFrame implements ExplorerManager.Provider {
private ExplorerManager em;
/** Creates new form Explorer */
public Explorer() {
em = new ExplorerManager();
em.setRootContext(FileNode.files());
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
contextTreeView1 = new org.openide.explorer.view.ContextTreeView();
listView1 = new org.openide.explorer.view.ListView();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(contextTreeView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(listView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, listView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, contextTreeView1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
.add(23, 23, 23))
);
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Explorer().setVisible(true);
}
});
}
public ExplorerManager getExplorerManager() {
return em;
}
// Variables declaration - do not modify
private org.openide.explorer.view.ContextTreeView contextTreeView1;
private org.openide.explorer.view.ListView listView1;
// End of variables declaration
}
Finally, here's Main.java:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Explorer.main(args);
}
}Oct 05 2007, 08:26:46 AM PDT Permalink


