1 7 8 package org.jdesktop.swing; 9 10 import java.awt.Cursor ; 11 12 import java.awt.event.MouseAdapter ; 13 import java.awt.event.MouseEvent ; 14 import java.awt.event.MouseMotionListener ; 15 16 import javax.swing.JComponent ; 17 import javax.swing.table.TableModel ; 18 19 import org.jdesktop.swing.JXTable; 20 21 import org.jdesktop.swing.data.Link; 22 23 import org.jdesktop.swing.table.TableColumnExt; 24 25 37 public class LinkHandler extends MouseAdapter implements MouseMotionListener { 38 39 private Cursor oldCursor; 40 41 public void mouseClicked(MouseEvent evt) { 42 Link link = null; 43 JComponent component = (JComponent )evt.getSource(); 44 45 if (component instanceof JXTable) { 46 JXTable table = (JXTable)evt.getSource(); 47 int col = table.columnAtPoint(evt.getPoint()); 48 49 if (isLinkColumn(table,col)) { 50 int row = table.rowAtPoint(evt.getPoint()); 51 if (row != -1) { 52 link = (Link)table.getValueAt(row, col); 53 } 54 } 55 } 56 else { 57 link = (Link)component.getClientProperty("jdnc.link.value"); 59 } 60 61 if (link != null) { 62 Application app = Application.getApp(component); 63 app.showDocument(link.getURL(), link.getTarget()); 64 link.setVisited(true); 65 } 66 } 67 68 public void mouseMoved(MouseEvent evt) { 69 setCursor(evt); 70 } 71 72 public void mouseDragged(MouseEvent evt) { } 73 74 private boolean isLinkColumn(JXTable table, int column) { 75 if (column < 0) return false; 78 TableModel model = table.getModel(); 79 return (model.getColumnClass(table.convertColumnIndexToModel(column)) == Link.class); 80 } 81 82 private void setCursor(MouseEvent evt) { 83 if (evt.getSource() instanceof JXTable) { 84 JXTable table = (JXTable)evt.getSource(); 85 int col = table.columnAtPoint(evt.getPoint()); 86 87 if (isLinkColumn(table,col)) { 88 if (oldCursor == null) { 89 oldCursor = table.getCursor(); 90 table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 91 } 92 } else { 93 if (oldCursor != null) { 94 table.setCursor(oldCursor); 95 oldCursor = null; 96 } 97 } 98 } 99 } 100 } 101 | Popular Tags |