JGrid was announced last week so I wanted to integrate it into a NetBeans Platform app. I.e., I'd like to use Nodes instead of the DefaultListModel that is supported natively, so that I can integrate with the Properties Window, for example:
Here's how:
import de.jgrid.JGrid;
import java.beans.PropertyVetoException;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.book.domain.Book;
import org.openide.explorer.ExplorerManager;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
public class JGridView extends JScrollPane {
@Override
public void addNotify() {
super.addNotify();
final ExplorerManager em = ExplorerManager.find(this);
if (em != null) {
final JGrid grid = new JGrid();
Node root = em.getRootContext();
final Node[] nodes = root.getChildren().getNodes();
final Book[] books = new Book[nodes.length];
for (int i = 0; i < nodes.length; i++) {
Node node = nodes[i];
books[i] = node.getLookup().lookup(Book.class);
}
grid.getCellRendererManager().setDefaultRenderer(new OpenLibraryGridRenderer());
grid.setModel(new DefaultListModel() {
@Override
public int getSize() {
return books.length;
}
@Override
public Object getElementAt(int i) {
return books[i];
}
});
grid.setUI(new BookshelfUI());
grid.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//Somehow compare the selected item
//with the list of books and find a matching book:
int selectedIndex = grid.getSelectedIndex();
for (int i = 0; i < nodes.length; i++) {
String nodeName = books[i].getTitel();
if (String.valueOf(selectedIndex).equals(nodeName)) {
try {
em.setSelectedNodes(new Node[]{nodes[i]});
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
});
setViewportView(grid);
}
}
}
Above, you see references to OpenLibraryGridRenderer and BookshelfUI, both of which are part of the "JGrid-Bookshelf" sample in the JGrid download.
The above is specific for Book objects, i.e., that's one of the samples that comes with the JGrid download. I need to make the above more general, so that any kind of object can be handled without requiring changes to the JGridView.
Once you have the above, it's easy to integrate it into a TopComponent, just like any other NetBeans explorer view.