JGridView (Part 2)

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Wed, 22 Jun 2011 02:20:45 -0700 Indexed on 2011/06/22 16:26 UTC
Read the original article Hit count: 297

Filed under:

The second sample in the JGrid download is a picture viewer that needs to be seen to be believed. Here it is, integrated into a NetBeans Platform application (click to enlarge it):

When you mouse over the images, they change, showing several different images instantaneously.

Here's the explorer view above, mainly making use of code from the sample:

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 PicViewerObject[] pics = new PicViewerObject[nodes.length];
            for (int i = 0; i < nodes.length; i++) {
                Node node = nodes[i];
                pics[i] = node.getLookup().lookup(PicViewerObject.class);
            }
            grid.getCellRendererManager().setDefaultRenderer(new PicViewerRenderer());
            grid.setModel(new DefaultListModel() {
                @Override
                public int getSize() {
                    return pics.length;
                }
                @Override
                public Object getElementAt(int i) {
                    return pics[i];
                }
            });
            grid.setFixedCellDimension(160);
            grid.addMouseMotionListener(new MouseAdapter() {
                int lastIndex = -1;
                @Override
                public void mouseMoved(MouseEvent e) {
                    if (lastIndex >= 0) {
                        Object o = grid.getModel().getElementAt(lastIndex);
                        if (o instanceof PicViewerObject) {
                            Rectangle r = grid.getCellBounds(lastIndex);
                            if (r != null && !r.contains(e.getPoint())) {
                                ((PicViewerObject) o).setMarker(false);
                                grid.repaint(r);
                            }
                        }
                    }
                    int index = grid.getCellAt(e.getPoint());
                    if (index >= 0) {
                        Object o = grid.getModel().getElementAt(index);
                        if (o instanceof PicViewerObject) {
                            Rectangle r = grid.getCellBounds(index);
                            if (r != null) {
                                ((PicViewerObject) o).setFraction(((float) e.getPoint().x - (float) r.x)
                                        / (float) r.width);
                                ((PicViewerObject) o).setMarker(true);
                                lastIndex = index;
                                grid.repaint(r);
                            }
                        }
                    }

                }
            });
            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++) {
                        int picId = pics[i].getId();
                        if (selectedIndex == picId) {
                            try {
                                em.setSelectedNodes(new Node[]{nodes[i]});
                            } catch (PropertyVetoException ex) {
                                Exceptions.printStackTrace(ex);
                            }
                        } 
                    }
                }
            });
            setViewportView(grid);
        }
    }

}

The next step is to create a generic JGridView that will handle any kind of object automatically.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE