KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > table > ColumnHeaderTooltips


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
19 import java.awt.event.MouseMotionAdapter JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.swing.JTable JavaDoc;
24 import javax.swing.table.JTableHeader JavaDoc;
25 import javax.swing.table.TableColumn JavaDoc;
26 import javax.swing.table.TableColumnModel JavaDoc;
27
28
29 /**
30  * Shows tooltips using a mouse motion listener.
31  *
32  * @author fdietz
33  */

34 public class ColumnHeaderTooltips extends MouseMotionAdapter JavaDoc {
35     // Current column whose tooltip is being displayed.
36
// This variable is used to minimize the calls to setToolTipText().
37
private TableColumn JavaDoc curCol;
38
39     // Maps TableColumn objects to tooltips
40
private Map JavaDoc tips = new HashMap JavaDoc();
41
42     // If tooltip is null, removes any tooltip text.
43
public void setToolTip(TableColumn JavaDoc col, String JavaDoc tooltip) {
44         if (tooltip == null) {
45             tips.remove(col);
46         } else {
47             tips.put(col, tooltip);
48         }
49     }
50
51     public void mouseMoved(MouseEvent JavaDoc evt) {
52         TableColumn JavaDoc col = null;
53         JTableHeader JavaDoc header = (JTableHeader JavaDoc) evt.getSource();
54         JTable JavaDoc table = header.getTable();
55         TableColumnModel JavaDoc colModel = table.getColumnModel();
56         int vColIndex = colModel.getColumnIndexAtX(evt.getX());
57
58         // Return if not clicked on any column header
59
if (vColIndex >= 0) {
60             col = colModel.getColumn(vColIndex);
61         }
62
63         if (col != curCol) {
64             header.setToolTipText((String JavaDoc) tips.get(col));
65             curCol = col;
66         }
67     }
68
69     public void clear() {
70         tips.clear();
71     }
72 }
73
Popular Tags