JGridView (Part 2)

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.

Comments:

Hi Geertjan,

Is it possible to post the project, I'm having difficulty in rendering my images in the grid, also is it possible to copy/drag&drog one item in the grid in one module, to another grid in another module.

Thanks,
Love your blog keep it up!

Rui

Posted by Rui Brito on November 11, 2012 at 12:29 PM PST #

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