001    package nl.cwi.sen1.visplugin.table.model;
002    
003    import java.awt.Cursor;
004    import java.awt.event.MouseEvent;
005    import java.awt.event.MouseListener;
006    import java.awt.event.MouseMotionListener;
007    
008    import javax.swing.JTable;
009    import javax.swing.table.TableColumnModel;
010    
011    import nl.cwi.sen1.relationstores.types.Location;
012    import nl.cwi.sen1.visplugin.table.TableVisualizationWindow;
013    
014    /**
015     * This class listens for mouse events in the JTable.
016     * Preferably we would like to listen for mouse events at the cell level.
017     * There was however not a simple solution for this so this isn't a very
018     * nice solution, but it works as it should be.
019     *
020     * @author Anton Lycklama a Nijeholt
021     * @author Bas Basten
022     * @date 13-03-2007
023     */
024    public class TableLocationButtonListener implements MouseListener, MouseMotionListener {
025        private JTable m_table;
026        private Cursor mouseOverCursor = new Cursor(Cursor.HAND_CURSOR);
027        private TableVisualizationWindow m_window;
028    
029        public TableLocationButtonListener(TableVisualizationWindow window, JTable table) {
030            m_window = window;
031            m_table = table;
032        }
033    
034        /**
035         * Get the cell's location ATerm at the X and Y coordinates of the mouse.
036         *
037         * @param event
038         * @return Returns a location ATerm if found, otherwise return null.
039         *
040         * @author Bas Basten
041         * @author Anton Lycklama a Nijeholt
042         * @date 13-03-2007
043         */
044        private Location getLocation(MouseEvent event) {
045            TableColumnModel columnModel = m_table.getColumnModel();
046            int column = columnModel.getColumnIndexAtX(event.getX());
047            int row = event.getY() / m_table.getRowHeight();
048    
049            Object value;
050    
051            if (row >= m_table.getRowCount() ||
052                row < 0 ||
053                column >= m_table.getColumnCount() ||
054                column < 0) {
055                return null;
056            }
057    
058            value = m_table.getValueAt(row, column);
059    
060            if (!(value instanceof Location)) {
061                return null;
062            }
063    
064            return (Location) value;
065        }
066    
067        /**
068         * Handle the mouse click only if is clicked on a cell
069         * with a location link.
070         *
071         * @author Bas Basten
072         * @author Anton Lycklama a Nijeholt
073         * @date 13-03-2007
074         */
075        public void mouseClicked(MouseEvent e) {
076            Location location = getLocation(e);
077            if (location != null) {
078                m_window.openLocationInEditor(location);
079            }
080        }
081    
082        /**
083         * Change the mousecursor if it moves over a location link.
084         *
085         * @author Bas Basten
086         * @author Anton Lycklama a Nijeholt
087         * @date 13-03-2007
088         */
089        public void mouseMoved(MouseEvent e) {
090            if (getLocation(e) != null) {
091                m_table.setCursor(mouseOverCursor);
092            }
093            else {
094                m_table.setCursor(Cursor.getDefaultCursor());
095            }
096        }
097    
098        public void mouseEntered(MouseEvent e) {
099            // No implementation
100        }
101    
102        public void mouseExited(MouseEvent e) {
103            // No implementation
104        }
105    
106        public void mousePressed(MouseEvent e) {
107            // No implementation
108        }
109    
110        public void mouseReleased(MouseEvent e) {
111            // No implementation
112        }
113    
114        public void mouseDragged(MouseEvent e) {
115            // TODO Auto-generated method stub
116        }
117    }