1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2002 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.JViewport;
import javax.swing.table.TableCellRenderer;
/**
* A TableCellRenderer that paints a portion of a JTree. Extends JViewport to
* take advantage of buffering and fast blitting (avoids repeated clipping and
* repainting). Defaults opaque to false: to see selection background painted,
* call setOpaque( true ).
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 904 $
*/
public class TreeTableCellRenderer extends JViewport implements TableCellRenderer, MouseListener {
JTree tree;
Component emptyComponent;
JTable delegateTable;
int lastKnownColumn;
/**
* Constructor takes a JTree and modifies it by setting rootVisible to false,
* showsRootHandles to true, opaque to false, and border to null.
*/
public TreeTableCellRenderer(JTree aTree) {
setView(aTree);
setBorder(null);
tree = aTree;
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setBorder(null);
tree.setOpaque(false);
Object renderer = tree.getCellRenderer();
if (renderer instanceof JComponent) {
((JComponent) renderer).setOpaque(false);
}
Object editor = tree.getCellEditor();
if (editor instanceof JComponent) {
((JComponent) editor).setOpaque(false);
}
this.setOpaque(false);
emptyComponent = new JLabel();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
lastKnownColumn = column;
if (delegateTable != table) {
if (delegateTable != null) {
delegateTable.removeMouseListener(this);
}
table.addMouseListener(this);
delegateTable = table;
}
Rectangle rect = tree.getRowBounds(row);
if (rect != null) {
setViewPosition(new Point(0 /* rect.x */, rect.y));
// FIXME: this causes problems for some LAFs (like Metal):
// in particular, the table height seems to get stuck.
// if ( table.getRowHeight( row ) != rect.height )
// {
// table.setRowHeight( row, rect.height );
// }
return this;
} else {
return emptyComponent;
}
}
public void mouseClicked(MouseEvent e) {
delegateToTree(e);
}
public void mousePressed(MouseEvent e) {
delegateToTree(e);
}
public void mouseReleased(MouseEvent e) {
delegateToTree(e);
}
public void mouseEntered(MouseEvent e) {
delegateToTree(e);
}
public void mouseExited(MouseEvent e) {
delegateToTree(e);
}
protected void delegateToTree(MouseEvent e) {
int col = delegateTable.getColumnModel().getColumnIndexAtX(e.getX());
if (col == lastKnownColumn) {
Rectangle nodeRect = tree.getRowBounds(0);
Rectangle cellRect = delegateTable.getCellRect(-1, col, false);
if (nodeRect != null) {
e.translatePoint(-cellRect.x, nodeRect.y);
tree.dispatchEvent( // e );
new MouseEvent(tree, e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(),
e.getClickCount(), e.isPopupTrigger()));
}
}
}
public void repaint() {
// if ( delegateTable != null ) delegateTable.repaint();
// not calling super.repaint() does not seem to cause
// any problems so we're not doing it.
}
}
/*
* $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven
* dependencies.
*
* Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.11 2003/08/06 23:07:53 chochos general code cleanup (mostly,
* removing unused imports)
*
* Revision 1.10 2002/04/12 20:07:35 mpowers Fixed cool/annoying view position.
*
* Revision 1.9 2002/04/09 18:12:21 mpowers Fixes for 1.4.
*
* Revision 1.8 2002/03/22 22:39:24 mpowers Can now move column to any position
* in the table.
*
* Revision 1.7 2002/03/11 03:13:22 mpowers Adjusting for viewport position; no
* longer responding to repaint().
*
* Revision 1.6 2002/03/07 23:04:36 mpowers Refining TreeColumnAssociation.
*
* Revision 1.5 2002/03/05 23:18:28 mpowers Added documentation. Added
* isSelectionPaintedImmediate and isSelectionTracking attributes to
* TableAssociation. Added getTableAssociation to TableColumnAssociation.
*
* Revision 1.3 2002/02/27 23:19:17 mpowers Refactoring of TreeAssociation to
* create TreeModelAssociation parent.
*
* Revision 1.2 2002/02/18 23:13:55 mpowers Only setting row height when needed.
*
* Revision 1.1 2002/02/18 03:46:08 mpowers Implemented TreeTableCellRenderer.
*
*
*/
|