KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > LinkHandler


1 /*
2  * $Id: LinkHandler.java,v 1.4 2005/01/19 12:39:10 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Cursor JavaDoc;
11
12 import java.awt.event.MouseAdapter JavaDoc;
13 import java.awt.event.MouseEvent JavaDoc;
14 import java.awt.event.MouseMotionListener JavaDoc;
15
16 import javax.swing.JComponent JavaDoc;
17 import javax.swing.table.TableModel JavaDoc;
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 /**
26  * Handles mouse actions on components with embedded Link objects. This
27  * includes with table cells, labels and other controls.
28  * When clicked, the Link contents will be displayed
29  * in a browser if the applciation is run in an Applet or WebStart
30  * application context.
31  * <p>
32  * TODO: Should make this general for Trees, TreeTables and lists.
33  *
34  * @see org.jdesktop.swing.data.Link
35  * @author Mark Davidson
36  */

37 public class LinkHandler extends MouseAdapter JavaDoc implements MouseMotionListener JavaDoc {
38
39     private Cursor JavaDoc oldCursor;
40
41     public void mouseClicked(MouseEvent JavaDoc evt) {
42     Link link = null;
43     JComponent JavaDoc component = (JComponent JavaDoc)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         // Value of the link is stored as a client property
58
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 JavaDoc evt) {
69     setCursor(evt);
70     }
71
72     public void mouseDragged(MouseEvent JavaDoc evt) { }
73
74     private boolean isLinkColumn(JXTable table, int column) {
75         // JW: Quickfix - the index might be -1 if
76
// hitting outside of the columns
77
if (column < 0) return false;
78         TableModel JavaDoc model = table.getModel();
79         return (model.getColumnClass(table.convertColumnIndexToModel(column)) == Link.class);
80     }
81
82     private void setCursor(MouseEvent JavaDoc 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