01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.mail.gui.table;
17:
18: import java.awt.event.MouseEvent;
19: import java.awt.event.MouseMotionAdapter;
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: import javax.swing.JTable;
24: import javax.swing.table.JTableHeader;
25: import javax.swing.table.TableColumn;
26: import javax.swing.table.TableColumnModel;
27:
28: /**
29: * Shows tooltips using a mouse motion listener.
30: *
31: * @author fdietz
32: */
33: public class ColumnHeaderTooltips extends MouseMotionAdapter {
34: // Current column whose tooltip is being displayed.
35: // This variable is used to minimize the calls to setToolTipText().
36: private TableColumn curCol;
37:
38: // Maps TableColumn objects to tooltips
39: private Map tips = new HashMap();
40:
41: // If tooltip is null, removes any tooltip text.
42: public void setToolTip(TableColumn col, String tooltip) {
43: if (tooltip == null) {
44: tips.remove(col);
45: } else {
46: tips.put(col, tooltip);
47: }
48: }
49:
50: public void mouseMoved(MouseEvent evt) {
51: TableColumn col = null;
52: JTableHeader header = (JTableHeader) evt.getSource();
53: JTable table = header.getTable();
54: TableColumnModel colModel = table.getColumnModel();
55: int vColIndex = colModel.getColumnIndexAtX(evt.getX());
56:
57: // Return if not clicked on any column header
58: if (vColIndex >= 0) {
59: col = colModel.getColumn(vColIndex);
60: }
61:
62: if (col != curCol) {
63: header.setToolTipText((String) tips.get(col));
64: curCol = col;
65: }
66: }
67:
68: public void clear() {
69: tips.clear();
70: }
71: }
|