View Javadoc

1   package org.wcb.util;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   * 
19   * Abstract: Allows a Jtable to have tool tips added to its cells.
20   */
21  
22  import java.awt.Point;
23  import java.awt.Component;
24  import java.awt.event.MouseEvent;
25  import javax.swing.JTable;
26  import javax.swing.ListSelectionModel;
27  import javax.swing.table.TableModel;
28  import javax.swing.table.TableColumn;
29  import javax.swing.table.TableColumnModel;
30  import javax.swing.table.TableCellRenderer;
31  import javax.swing.table.DefaultTableModel;
32  import java.util.Vector;
33  
34  public class TooltippedTable extends JTable {
35      private boolean toolTipOff = false;
36  
37      public TooltippedTable() {
38  	this(null, null, null);
39      }
40  
41      public TooltippedTable(TableModel aModel) {
42  	this(aModel, null, null);
43      }
44  
45      // For AccountAdminView
46      public TooltippedTable(TableModel aModel, boolean turnOffToolTip) {
47  	this(aModel);
48  	toolTipOff = turnOffToolTip;
49      }
50  
51      public TooltippedTable(TableModel aModel, TableColumnModel colModel) {
52  	this(aModel, colModel, null);
53      }
54  
55      public TooltippedTable(TableModel aModel, TableColumnModel colModel,
56  			   ListSelectionModel selModel) {
57  	super(aModel, colModel, selModel);
58      }
59  
60      public TooltippedTable(int numRow, int numCol) {
61  	this(new DefaultTableModel(numRow, numCol));
62      }
63      
64      public TooltippedTable(final Vector data, final Vector colName) {
65  	super(data, colName);
66      }
67  
68      public TooltippedTable(final Object[][] data, final Object[] colName) {
69  	super(data, colName);
70      }
71  
72      public void sizeColumns(Object[] colHeaders) {
73  	int numCol = this.getColumnCount();
74  	if (numCol > 0) {
75  	    for (int i = 0; i < colHeaders.length; i++) {
76  		TableColumn col = this.getColumn(colHeaders[i]);
77  		int colWidth = getPreferredWidthForColumn(col);
78  		col.setMinWidth(colWidth);
79  		col.setMaxWidth(colWidth);
80  		sizeColumnsToFit(i);
81  	    }
82  	}
83      }
84  
85  
86      public void setColHeaderRender(SortButtonRenderer headerRenderer) {
87  	int numCol = this.getColumnCount();
88  	String colName;
89  
90  	headerRenderer.setToolTipText("Click here to sort");
91  	for (int i = 0; i < numCol; i++) {
92  	    colName = getColumnName(i);
93  	    TableColumn col = this.getColumn(colName);
94  	    col.setHeaderRenderer(headerRenderer);
95  	}
96      }
97  
98  
99      private int getPreferredWidthForColumn(TableColumn col) {
100 	int headerWidth = columnHeaderWidth(col),
101 	    colWidth = widestCellInColumn(col);
102 
103 	return headerWidth > colWidth ? headerWidth : colWidth;
104     }
105 
106     private int columnHeaderWidth(TableColumn col) {
107 	TableCellRenderer renderer = col.getHeaderRenderer();
108 
109 	Component c = renderer.getTableCellRendererComponent(this,
110 							     col.getHeaderValue(),
111 							     false, false, -1,0);
112 
113 	return c.getPreferredSize().width;
114     }
115 
116     private int widestCellInColumn(TableColumn col) {
117 	int c = col.getModelIndex(), width = 0, maxw = 0;
118 
119 	for (int r=0; r < this.getRowCount(); ++r) {
120 	    TableCellRenderer renderer = this.getCellRenderer(r,c);
121 
122 	    Component comp = renderer.getTableCellRendererComponent(this,
123 								    this.getValueAt(r, c),
124 								    false, 
125 								    false,
126 								    r, c);
127 
128 	    width = comp.getPreferredSize().width;
129 	    maxw = width > maxw ? width : maxw;
130 	}
131 
132 	return maxw;
133     }
134 
135 
136     public String getToolTipText(MouseEvent e) {
137 	if (!toolTipOff) {
138 	    int row = rowAtPoint(e.getPoint());
139 	    int col = columnAtPoint(e.getPoint());
140 	    Object obj = getValueAt(row, col);
141 	    
142 	    if (obj == null)
143 		return null;
144 	    
145 	    if (obj.toString().equals(""))
146 		return null;
147 	    
148 	    return obj.toString();
149 	}
150 	return null;
151     }
152 
153     public Point getToolTipLocation(MouseEvent e) {
154 	if (!toolTipOff) {
155 	    int row = rowAtPoint(e.getPoint());
156 	    int col = columnAtPoint(e.getPoint());
157 	    Object obj = getValueAt(row, col);
158 	    
159 	    if (obj == null)
160 		return null;
161 	    
162 	    if (obj.toString().equals(""))
163 		return null;
164 	    
165 	    Point p = getCellRect(row, col, true).getLocation();
166 	    p.translate(-1, -2);
167 	    return p;
168 	}
169 	return null;
170     }
171 }
172