JGridView (Part 1)

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.

Comments:

wow cool, i`ve always wanted to do that, awesome!!!

Posted by Deni Setiawan on June 26, 2011 at 04:16 PM PDT #

Cool! My jGrid in Netbeans :)

Posted by Hendrik Ebbers on June 26, 2011 at 05:28 PM PDT #

Hi Geertjan
Can you please change the link to guigarage.com

Posted by Hendrik Ebbers on September 18, 2011 at 07:57 AM PDT #

Hi Hendrik, done!

Posted by Geertjan on September 20, 2011 at 02:04 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today