annotate agent/src/share/classes/sun/jvm/hotspot/ui/treetable/JTreeTable.java @ 152:c70a245cad3a

6670684: 4/5 SA command universe did not print out CMS space information Summary: Forward port of Yumin's fix for 6670684 from HSX-11; Yumin verified the port was correct. Reviewed-by: dcubed
author dcubed
date Fri, 09 May 2008 08:55:13 -0700
parents a61af66fc99e
children d1605aabd0a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.ui.treetable;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.awt.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import javax.swing.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import javax.swing.border.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import javax.swing.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import javax.swing.tree.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import javax.swing.table.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import java.awt.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 import java.util.EventObject;
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
40 * This example shows how to create a simple JTreeTable component,
a61af66fc99e Initial load
duke
parents:
diff changeset
41 * by using a JTree as a renderer (and editor) for the cells in a
a61af66fc99e Initial load
duke
parents:
diff changeset
42 * particular column in the JTable.
a61af66fc99e Initial load
duke
parents:
diff changeset
43 *
a61af66fc99e Initial load
duke
parents:
diff changeset
44 *
a61af66fc99e Initial load
duke
parents:
diff changeset
45 * @author Philip Milne
a61af66fc99e Initial load
duke
parents:
diff changeset
46 * @author Scott Violet
a61af66fc99e Initial load
duke
parents:
diff changeset
47 */
a61af66fc99e Initial load
duke
parents:
diff changeset
48 public class JTreeTable extends JTable {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 /** A subclass of JTree. */
a61af66fc99e Initial load
duke
parents:
diff changeset
50 protected TreeTableCellRenderer tree;
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 //////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // Convenience routines //
a61af66fc99e Initial load
duke
parents:
diff changeset
54 //////////////////////////
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 private boolean treeEditable = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 private boolean showsIcons = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 public boolean getTreeEditable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 return treeEditable;
a61af66fc99e Initial load
duke
parents:
diff changeset
61 }
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 public void setTreeEditable(boolean editable) {
a61af66fc99e Initial load
duke
parents:
diff changeset
64 treeEditable = editable;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 }
a61af66fc99e Initial load
duke
parents:
diff changeset
66
a61af66fc99e Initial load
duke
parents:
diff changeset
67 public boolean getShowsIcons() {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 return showsIcons;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 }
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 public void setShowsIcons(boolean show) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 showsIcons = show;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 public void setRootVisible(boolean visible) {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 tree.setRootVisible(visible);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 public boolean getShowsRootHandles() {
a61af66fc99e Initial load
duke
parents:
diff changeset
80 return tree.getShowsRootHandles();
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 public void setShowsRootHandles(boolean newValue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 tree.setShowsRootHandles(newValue);
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 public JTreeTable(TreeTableModel treeTableModel) {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 super();
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // Create the tree. It will be used as a renderer and editor.
a61af66fc99e Initial load
duke
parents:
diff changeset
91 tree = new TreeTableCellRenderer(treeTableModel);
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // Install a tableModel representing the visible rows in the tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
94 super.setModel(new TreeTableModelAdapter(treeTableModel, tree));
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 // Force the JTable and JTree to share their row selection models.
a61af66fc99e Initial load
duke
parents:
diff changeset
97 ListToTreeSelectionModelWrapper selectionWrapper = new
a61af66fc99e Initial load
duke
parents:
diff changeset
98 ListToTreeSelectionModelWrapper();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 tree.setSelectionModel(selectionWrapper);
a61af66fc99e Initial load
duke
parents:
diff changeset
100 setSelectionModel(selectionWrapper.getListSelectionModel());
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 // Install the tree editor renderer and editor.
a61af66fc99e Initial load
duke
parents:
diff changeset
103 setDefaultRenderer(TreeTableModel.class, tree);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 // No grid.
a61af66fc99e Initial load
duke
parents:
diff changeset
107 setShowGrid(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 // No intercell spacing
a61af66fc99e Initial load
duke
parents:
diff changeset
110 setIntercellSpacing(new Dimension(0, 0));
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // And update the height of the trees row to match that of
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // the table.
a61af66fc99e Initial load
duke
parents:
diff changeset
114 if (tree.getRowHeight() < 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 // Metal looks better like this.
a61af66fc99e Initial load
duke
parents:
diff changeset
116 setRowHeight(20);
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118 }
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
121 * Overridden to message super and forward the method to the tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
122 * Since the tree is not actually in the component hieachy it will
a61af66fc99e Initial load
duke
parents:
diff changeset
123 * never receive this unless we forward it in this manner.
a61af66fc99e Initial load
duke
parents:
diff changeset
124 */
a61af66fc99e Initial load
duke
parents:
diff changeset
125 public void updateUI() {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 super.updateUI();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 if(tree != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 tree.updateUI();
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // Do this so that the editor is referencing the current renderer
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // from the tree. The renderer can potentially change each time
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // laf changes.
a61af66fc99e Initial load
duke
parents:
diff changeset
132 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // Use the tree's default foreground and background colors in the
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // table.
a61af66fc99e Initial load
duke
parents:
diff changeset
136 LookAndFeel.installColorsAndFont(this, "Tree.background",
a61af66fc99e Initial load
duke
parents:
diff changeset
137 "Tree.foreground", "Tree.font");
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
141 * Workaround for BasicTableUI anomaly. Make sure the UI never tries to
a61af66fc99e Initial load
duke
parents:
diff changeset
142 * resize the editor. The UI currently uses different techniques to
a61af66fc99e Initial load
duke
parents:
diff changeset
143 * paint the renderers and editors and overriding setBounds() below
a61af66fc99e Initial load
duke
parents:
diff changeset
144 * is not the right thing to do for an editor. Returning -1 for the
a61af66fc99e Initial load
duke
parents:
diff changeset
145 * editing row in this case, ensures the editor is never painted.
a61af66fc99e Initial load
duke
parents:
diff changeset
146 */
a61af66fc99e Initial load
duke
parents:
diff changeset
147 public int getEditingRow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 :
a61af66fc99e Initial load
duke
parents:
diff changeset
149 editingRow;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
153 * Returns the actual row that is editing as <code>getEditingRow</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
154 * will always return -1.
a61af66fc99e Initial load
duke
parents:
diff changeset
155 */
a61af66fc99e Initial load
duke
parents:
diff changeset
156 private int realEditingRow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 return editingRow;
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
161 * This is overriden to invoke supers implementation, and then,
a61af66fc99e Initial load
duke
parents:
diff changeset
162 * if the receiver is editing a Tree column, the editors bounds is
a61af66fc99e Initial load
duke
parents:
diff changeset
163 * reset. The reason we have to do this is because JTable doesn't
a61af66fc99e Initial load
duke
parents:
diff changeset
164 * think the table is being edited, as <code>getEditingRow</code> returns
a61af66fc99e Initial load
duke
parents:
diff changeset
165 * -1, and therefore doesn't automaticly resize the editor for us.
a61af66fc99e Initial load
duke
parents:
diff changeset
166 */
a61af66fc99e Initial load
duke
parents:
diff changeset
167 public void sizeColumnsToFit(int resizingColumn) {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 super.sizeColumnsToFit(resizingColumn);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 if (getEditingColumn() != -1 && getColumnClass(editingColumn) ==
a61af66fc99e Initial load
duke
parents:
diff changeset
170 TreeTableModel.class) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 Rectangle cellRect = getCellRect(realEditingRow(),
a61af66fc99e Initial load
duke
parents:
diff changeset
172 getEditingColumn(), false);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 Component component = getEditorComponent();
a61af66fc99e Initial load
duke
parents:
diff changeset
174 component.setBounds(cellRect);
a61af66fc99e Initial load
duke
parents:
diff changeset
175 component.validate();
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177 }
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
180 * Overridden to pass the new rowHeight to the tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
181 */
a61af66fc99e Initial load
duke
parents:
diff changeset
182 public void setRowHeight(int rowHeight) {
a61af66fc99e Initial load
duke
parents:
diff changeset
183 super.setRowHeight(rowHeight);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 if (tree != null && tree.getRowHeight() != rowHeight) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 tree.setRowHeight(getRowHeight());
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
190 * Returns the tree that is being shared between the model.
a61af66fc99e Initial load
duke
parents:
diff changeset
191 */
a61af66fc99e Initial load
duke
parents:
diff changeset
192 public JTree getTree() {
a61af66fc99e Initial load
duke
parents:
diff changeset
193 return tree;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 }
a61af66fc99e Initial load
duke
parents:
diff changeset
195
a61af66fc99e Initial load
duke
parents:
diff changeset
196 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
197 * Overriden to invoke repaint for the particular location if
a61af66fc99e Initial load
duke
parents:
diff changeset
198 * the column contains the tree. This is done as the tree editor does
a61af66fc99e Initial load
duke
parents:
diff changeset
199 * not fill the bounds of the cell, we need the renderer to paint
a61af66fc99e Initial load
duke
parents:
diff changeset
200 * the tree in the background, and then draw the editor over it.
a61af66fc99e Initial load
duke
parents:
diff changeset
201 */
a61af66fc99e Initial load
duke
parents:
diff changeset
202 public boolean editCellAt(int row, int column, EventObject e){
a61af66fc99e Initial load
duke
parents:
diff changeset
203 boolean retValue = super.editCellAt(row, column, e);
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (retValue && getColumnClass(column) == TreeTableModel.class) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 repaint(getCellRect(row, column, false));
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207 return retValue;
a61af66fc99e Initial load
duke
parents:
diff changeset
208 }
a61af66fc99e Initial load
duke
parents:
diff changeset
209
a61af66fc99e Initial load
duke
parents:
diff changeset
210 /** A DefaultTreeCellRenderer which can optionally skip drawing
a61af66fc99e Initial load
duke
parents:
diff changeset
211 all icons. */
a61af66fc99e Initial load
duke
parents:
diff changeset
212 class JTreeTableCellRenderer extends DefaultTreeCellRenderer {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 public Icon getClosedIcon() { return (showsIcons ? super.getClosedIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
214 public Icon getDefaultClosedIcon() { return (showsIcons ? super.getDefaultClosedIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 public Icon getDefaultLeafIcon() { return (showsIcons ? super.getDefaultLeafIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 public Icon getDefaultOpenIcon() { return (showsIcons ? super.getDefaultOpenIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
217 public Icon getLeafIcon() { return (showsIcons ? super.getLeafIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
218 public Icon getOpenIcon() { return (showsIcons ? super.getOpenIcon() : null); }
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220
a61af66fc99e Initial load
duke
parents:
diff changeset
221 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
222 * A TreeCellRenderer that displays a JTree.
a61af66fc99e Initial load
duke
parents:
diff changeset
223 */
a61af66fc99e Initial load
duke
parents:
diff changeset
224 public class TreeTableCellRenderer extends JTree implements
a61af66fc99e Initial load
duke
parents:
diff changeset
225 TableCellRenderer {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 /** Last table/tree row asked to renderer. */
a61af66fc99e Initial load
duke
parents:
diff changeset
227 protected int visibleRow;
a61af66fc99e Initial load
duke
parents:
diff changeset
228 /** Border to draw around the tree, if this is non-null, it will
a61af66fc99e Initial load
duke
parents:
diff changeset
229 * be painted. */
a61af66fc99e Initial load
duke
parents:
diff changeset
230 protected Border highlightBorder;
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 public TreeTableCellRenderer(TreeModel model) {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 super(model);
a61af66fc99e Initial load
duke
parents:
diff changeset
234 setCellRenderer(new JTreeTableCellRenderer());
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
238 * updateUI is overridden to set the colors of the Tree's renderer
a61af66fc99e Initial load
duke
parents:
diff changeset
239 * to match that of the table.
a61af66fc99e Initial load
duke
parents:
diff changeset
240 */
a61af66fc99e Initial load
duke
parents:
diff changeset
241 public void updateUI() {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 super.updateUI();
a61af66fc99e Initial load
duke
parents:
diff changeset
243 // Make the tree's cell renderer use the table's cell selection
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // colors.
a61af66fc99e Initial load
duke
parents:
diff changeset
245 TreeCellRenderer tcr = getCellRenderer();
a61af66fc99e Initial load
duke
parents:
diff changeset
246 if (tcr instanceof DefaultTreeCellRenderer) {
a61af66fc99e Initial load
duke
parents:
diff changeset
247 DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer)tcr);
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // For 1.1 uncomment this, 1.2 has a bug that will cause an
a61af66fc99e Initial load
duke
parents:
diff changeset
249 // exception to be thrown if the border selection color is
a61af66fc99e Initial load
duke
parents:
diff changeset
250 // null.
a61af66fc99e Initial load
duke
parents:
diff changeset
251 // dtcr.setBorderSelectionColor(null);
a61af66fc99e Initial load
duke
parents:
diff changeset
252 dtcr.setTextSelectionColor(UIManager.getColor
a61af66fc99e Initial load
duke
parents:
diff changeset
253 ("Table.selectionForeground"));
a61af66fc99e Initial load
duke
parents:
diff changeset
254 dtcr.setBackgroundSelectionColor(UIManager.getColor
a61af66fc99e Initial load
duke
parents:
diff changeset
255 ("Table.selectionBackground"));
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
260 * Sets the row height of the tree, and forwards the row height to
a61af66fc99e Initial load
duke
parents:
diff changeset
261 * the table.
a61af66fc99e Initial load
duke
parents:
diff changeset
262 */
a61af66fc99e Initial load
duke
parents:
diff changeset
263 public void setRowHeight(int rowHeight) {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 if (rowHeight > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 super.setRowHeight(rowHeight);
a61af66fc99e Initial load
duke
parents:
diff changeset
266 if (JTreeTable.this != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
267 JTreeTable.this.getRowHeight() != rowHeight) {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 JTreeTable.this.setRowHeight(getRowHeight());
a61af66fc99e Initial load
duke
parents:
diff changeset
269 }
a61af66fc99e Initial load
duke
parents:
diff changeset
270 }
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
274 * This is overridden to set the height to match that of the JTable.
a61af66fc99e Initial load
duke
parents:
diff changeset
275 */
a61af66fc99e Initial load
duke
parents:
diff changeset
276 public void setBounds(int x, int y, int w, int h) {
a61af66fc99e Initial load
duke
parents:
diff changeset
277 super.setBounds(x, 0, w, JTreeTable.this.getHeight());
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
281 * Sublcassed to translate the graphics such that the last visible
a61af66fc99e Initial load
duke
parents:
diff changeset
282 * row will be drawn at 0,0.
a61af66fc99e Initial load
duke
parents:
diff changeset
283 */
a61af66fc99e Initial load
duke
parents:
diff changeset
284 public void paint(Graphics g) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 g.translate(0, -visibleRow * getRowHeight());
a61af66fc99e Initial load
duke
parents:
diff changeset
286 super.paint(g);
a61af66fc99e Initial load
duke
parents:
diff changeset
287 // Draw the Table border if we have focus.
a61af66fc99e Initial load
duke
parents:
diff changeset
288 if (highlightBorder != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 highlightBorder.paintBorder(this, g, 0, visibleRow *
a61af66fc99e Initial load
duke
parents:
diff changeset
290 getRowHeight(), getWidth(),
a61af66fc99e Initial load
duke
parents:
diff changeset
291 getRowHeight());
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293 }
a61af66fc99e Initial load
duke
parents:
diff changeset
294
a61af66fc99e Initial load
duke
parents:
diff changeset
295 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
296 * TreeCellRenderer method. Overridden to update the visible row.
a61af66fc99e Initial load
duke
parents:
diff changeset
297 */
a61af66fc99e Initial load
duke
parents:
diff changeset
298 public Component getTableCellRendererComponent(JTable table,
a61af66fc99e Initial load
duke
parents:
diff changeset
299 Object value,
a61af66fc99e Initial load
duke
parents:
diff changeset
300 boolean isSelected,
a61af66fc99e Initial load
duke
parents:
diff changeset
301 boolean hasFocus,
a61af66fc99e Initial load
duke
parents:
diff changeset
302 int row, int column) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 Color background;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 Color foreground;
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 if(isSelected) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 background = table.getSelectionBackground();
a61af66fc99e Initial load
duke
parents:
diff changeset
308 foreground = table.getSelectionForeground();
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
311 background = table.getBackground();
a61af66fc99e Initial load
duke
parents:
diff changeset
312 foreground = table.getForeground();
a61af66fc99e Initial load
duke
parents:
diff changeset
313 }
a61af66fc99e Initial load
duke
parents:
diff changeset
314 highlightBorder = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
315 if (realEditingRow() == row && getEditingColumn() == column) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 background = UIManager.getColor("Table.focusCellBackground");
a61af66fc99e Initial load
duke
parents:
diff changeset
317 foreground = UIManager.getColor("Table.focusCellForeground");
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319 else if (hasFocus) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 highlightBorder = UIManager.getBorder
a61af66fc99e Initial load
duke
parents:
diff changeset
321 ("Table.focusCellHighlightBorder");
a61af66fc99e Initial load
duke
parents:
diff changeset
322 if (isCellEditable(row, column)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 background = UIManager.getColor
a61af66fc99e Initial load
duke
parents:
diff changeset
324 ("Table.focusCellBackground");
a61af66fc99e Initial load
duke
parents:
diff changeset
325 foreground = UIManager.getColor
a61af66fc99e Initial load
duke
parents:
diff changeset
326 ("Table.focusCellForeground");
a61af66fc99e Initial load
duke
parents:
diff changeset
327 }
a61af66fc99e Initial load
duke
parents:
diff changeset
328 }
a61af66fc99e Initial load
duke
parents:
diff changeset
329
a61af66fc99e Initial load
duke
parents:
diff changeset
330 visibleRow = row;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 setBackground(background);
a61af66fc99e Initial load
duke
parents:
diff changeset
332
a61af66fc99e Initial load
duke
parents:
diff changeset
333 TreeCellRenderer tcr = getCellRenderer();
a61af66fc99e Initial load
duke
parents:
diff changeset
334 if (tcr instanceof DefaultTreeCellRenderer) {
a61af66fc99e Initial load
duke
parents:
diff changeset
335 DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer)tcr);
a61af66fc99e Initial load
duke
parents:
diff changeset
336 if (isSelected) {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 dtcr.setTextSelectionColor(foreground);
a61af66fc99e Initial load
duke
parents:
diff changeset
338 dtcr.setBackgroundSelectionColor(background);
a61af66fc99e Initial load
duke
parents:
diff changeset
339 }
a61af66fc99e Initial load
duke
parents:
diff changeset
340 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 dtcr.setTextNonSelectionColor(foreground);
a61af66fc99e Initial load
duke
parents:
diff changeset
342 dtcr.setBackgroundNonSelectionColor(background);
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344 }
a61af66fc99e Initial load
duke
parents:
diff changeset
345 return this;
a61af66fc99e Initial load
duke
parents:
diff changeset
346 }
a61af66fc99e Initial load
duke
parents:
diff changeset
347 }
a61af66fc99e Initial load
duke
parents:
diff changeset
348
a61af66fc99e Initial load
duke
parents:
diff changeset
349
a61af66fc99e Initial load
duke
parents:
diff changeset
350 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
351 * An editor that can be used to edit the tree column. This extends
a61af66fc99e Initial load
duke
parents:
diff changeset
352 * DefaultCellEditor and uses a JTextField (actually, TreeTableTextField)
a61af66fc99e Initial load
duke
parents:
diff changeset
353 * to perform the actual editing.
a61af66fc99e Initial load
duke
parents:
diff changeset
354 * <p>To support editing of the tree column we can not make the tree
a61af66fc99e Initial load
duke
parents:
diff changeset
355 * editable. The reason this doesn't work is that you can not use
a61af66fc99e Initial load
duke
parents:
diff changeset
356 * the same component for editing and renderering. The table may have
a61af66fc99e Initial load
duke
parents:
diff changeset
357 * the need to paint cells, while a cell is being edited. If the same
a61af66fc99e Initial load
duke
parents:
diff changeset
358 * component were used for the rendering and editing the component would
a61af66fc99e Initial load
duke
parents:
diff changeset
359 * be moved around, and the contents would change. When editing, this
a61af66fc99e Initial load
duke
parents:
diff changeset
360 * is undesirable, the contents of the text field must stay the same,
a61af66fc99e Initial load
duke
parents:
diff changeset
361 * including the caret blinking, and selections persisting. For this
a61af66fc99e Initial load
duke
parents:
diff changeset
362 * reason the editing is done via a TableCellEditor.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 * <p>Another interesting thing to be aware of is how tree positions
a61af66fc99e Initial load
duke
parents:
diff changeset
364 * its render and editor. The render/editor is responsible for drawing the
a61af66fc99e Initial load
duke
parents:
diff changeset
365 * icon indicating the type of node (leaf, branch...). The tree is
a61af66fc99e Initial load
duke
parents:
diff changeset
366 * responsible for drawing any other indicators, perhaps an additional
a61af66fc99e Initial load
duke
parents:
diff changeset
367 * +/- sign, or lines connecting the various nodes. So, the renderer
a61af66fc99e Initial load
duke
parents:
diff changeset
368 * is positioned based on depth. On the other hand, table always makes
a61af66fc99e Initial load
duke
parents:
diff changeset
369 * its editor fill the contents of the cell. To get the allusion
a61af66fc99e Initial load
duke
parents:
diff changeset
370 * that the table cell editor is part of the tree, we don't want the
a61af66fc99e Initial load
duke
parents:
diff changeset
371 * table cell editor to fill the cell bounds. We want it to be placed
a61af66fc99e Initial load
duke
parents:
diff changeset
372 * in the same manner as tree places it editor, and have table message
a61af66fc99e Initial load
duke
parents:
diff changeset
373 * the tree to paint any decorations the tree wants. Then, we would
a61af66fc99e Initial load
duke
parents:
diff changeset
374 * only have to worry about the editing part. The approach taken
a61af66fc99e Initial load
duke
parents:
diff changeset
375 * here is to determine where tree would place the editor, and to override
a61af66fc99e Initial load
duke
parents:
diff changeset
376 * the <code>reshape</code> method in the JTextField component to
a61af66fc99e Initial load
duke
parents:
diff changeset
377 * nudge the textfield to the location tree would place it. Since
a61af66fc99e Initial load
duke
parents:
diff changeset
378 * JTreeTable will paint the tree behind the editor everything should
a61af66fc99e Initial load
duke
parents:
diff changeset
379 * just work. So, that is what we are doing here. Determining of
a61af66fc99e Initial load
duke
parents:
diff changeset
380 * the icon position will only work if the TreeCellRenderer is
a61af66fc99e Initial load
duke
parents:
diff changeset
381 * an instance of DefaultTreeCellRenderer. If you need custom
a61af66fc99e Initial load
duke
parents:
diff changeset
382 * TreeCellRenderers, that don't descend from DefaultTreeCellRenderer,
a61af66fc99e Initial load
duke
parents:
diff changeset
383 * and you want to support editing in JTreeTable, you will have
a61af66fc99e Initial load
duke
parents:
diff changeset
384 * to do something similiar.
a61af66fc99e Initial load
duke
parents:
diff changeset
385 */
a61af66fc99e Initial load
duke
parents:
diff changeset
386 public class TreeTableCellEditor extends DefaultCellEditor {
a61af66fc99e Initial load
duke
parents:
diff changeset
387 public TreeTableCellEditor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
388 super(new TreeTableTextField());
a61af66fc99e Initial load
duke
parents:
diff changeset
389 }
a61af66fc99e Initial load
duke
parents:
diff changeset
390
a61af66fc99e Initial load
duke
parents:
diff changeset
391 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
392 * Overriden to determine an offset that tree would place the
a61af66fc99e Initial load
duke
parents:
diff changeset
393 * editor at. The offset is determined from the
a61af66fc99e Initial load
duke
parents:
diff changeset
394 * <code>getRowBounds</code> JTree method, and additionaly
a61af66fc99e Initial load
duke
parents:
diff changeset
395 * from the icon DefaultTreeCellRenderer will use.
a61af66fc99e Initial load
duke
parents:
diff changeset
396 * <p>The offset is then set on the TreeTableTextField component
a61af66fc99e Initial load
duke
parents:
diff changeset
397 * created in the constructor, and returned.
a61af66fc99e Initial load
duke
parents:
diff changeset
398 */
a61af66fc99e Initial load
duke
parents:
diff changeset
399 public Component getTableCellEditorComponent(JTable table,
a61af66fc99e Initial load
duke
parents:
diff changeset
400 Object value,
a61af66fc99e Initial load
duke
parents:
diff changeset
401 boolean isSelected,
a61af66fc99e Initial load
duke
parents:
diff changeset
402 int r, int c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
403 Component component = super.getTableCellEditorComponent
a61af66fc99e Initial load
duke
parents:
diff changeset
404 (table, value, isSelected, r, c);
a61af66fc99e Initial load
duke
parents:
diff changeset
405 JTree t = getTree();
a61af66fc99e Initial load
duke
parents:
diff changeset
406 boolean rv = t.isRootVisible();
a61af66fc99e Initial load
duke
parents:
diff changeset
407 int offsetRow = rv ? r : r - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
408 Rectangle bounds = t.getRowBounds(offsetRow);
a61af66fc99e Initial load
duke
parents:
diff changeset
409 int offset = bounds.x;
a61af66fc99e Initial load
duke
parents:
diff changeset
410 TreeCellRenderer tcr = t.getCellRenderer();
a61af66fc99e Initial load
duke
parents:
diff changeset
411 if (tcr instanceof DefaultTreeCellRenderer) {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 Object node = t.getPathForRow(offsetRow).
a61af66fc99e Initial load
duke
parents:
diff changeset
413 getLastPathComponent();
a61af66fc99e Initial load
duke
parents:
diff changeset
414 Icon icon;
a61af66fc99e Initial load
duke
parents:
diff changeset
415 if (t.getModel().isLeaf(node))
a61af66fc99e Initial load
duke
parents:
diff changeset
416 icon = ((DefaultTreeCellRenderer)tcr).getLeafIcon();
a61af66fc99e Initial load
duke
parents:
diff changeset
417 else if (tree.isExpanded(offsetRow))
a61af66fc99e Initial load
duke
parents:
diff changeset
418 icon = ((DefaultTreeCellRenderer)tcr).getOpenIcon();
a61af66fc99e Initial load
duke
parents:
diff changeset
419 else
a61af66fc99e Initial load
duke
parents:
diff changeset
420 icon = ((DefaultTreeCellRenderer)tcr).getClosedIcon();
a61af66fc99e Initial load
duke
parents:
diff changeset
421 if (icon != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 offset += ((DefaultTreeCellRenderer)tcr).getIconTextGap() +
a61af66fc99e Initial load
duke
parents:
diff changeset
423 icon.getIconWidth();
a61af66fc99e Initial load
duke
parents:
diff changeset
424 }
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
426 ((TreeTableTextField)getComponent()).offset = offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
427 return component;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
429
a61af66fc99e Initial load
duke
parents:
diff changeset
430 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
431 * This is overriden to forward the event to the tree. This will
a61af66fc99e Initial load
duke
parents:
diff changeset
432 * return true if the click count >= 3, or the event is null.
a61af66fc99e Initial load
duke
parents:
diff changeset
433 */
a61af66fc99e Initial load
duke
parents:
diff changeset
434 public boolean isCellEditable(EventObject e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
435 if (e instanceof MouseEvent) {
a61af66fc99e Initial load
duke
parents:
diff changeset
436 MouseEvent me = (MouseEvent)e;
a61af66fc99e Initial load
duke
parents:
diff changeset
437 // If the modifiers are not 0 (or the left mouse button),
a61af66fc99e Initial load
duke
parents:
diff changeset
438 // tree may try and toggle the selection, and table
a61af66fc99e Initial load
duke
parents:
diff changeset
439 // will then try and toggle, resulting in the
a61af66fc99e Initial load
duke
parents:
diff changeset
440 // selection remaining the same. To avoid this, we
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // only dispatch when the modifiers are 0 (or the left mouse
a61af66fc99e Initial load
duke
parents:
diff changeset
442 // button).
a61af66fc99e Initial load
duke
parents:
diff changeset
443 if (me.getModifiers() == 0 ||
a61af66fc99e Initial load
duke
parents:
diff changeset
444 me.getModifiers() == InputEvent.BUTTON1_MASK) {
a61af66fc99e Initial load
duke
parents:
diff changeset
445 for (int counter = getColumnCount() - 1; counter >= 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 counter--) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 if (getColumnClass(counter) == TreeTableModel.class) {
a61af66fc99e Initial load
duke
parents:
diff changeset
448 MouseEvent newME = new MouseEvent
a61af66fc99e Initial load
duke
parents:
diff changeset
449 (JTreeTable.this.tree, me.getID(),
a61af66fc99e Initial load
duke
parents:
diff changeset
450 me.getWhen(), me.getModifiers(),
a61af66fc99e Initial load
duke
parents:
diff changeset
451 me.getX() - getCellRect(0, counter, true).x,
a61af66fc99e Initial load
duke
parents:
diff changeset
452 me.getY(), me.getClickCount(),
a61af66fc99e Initial load
duke
parents:
diff changeset
453 me.isPopupTrigger());
a61af66fc99e Initial load
duke
parents:
diff changeset
454 JTreeTable.this.tree.dispatchEvent(newME);
a61af66fc99e Initial load
duke
parents:
diff changeset
455 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
456 }
a61af66fc99e Initial load
duke
parents:
diff changeset
457 }
a61af66fc99e Initial load
duke
parents:
diff changeset
458 }
a61af66fc99e Initial load
duke
parents:
diff changeset
459 if (me.getClickCount() >= 3) {
a61af66fc99e Initial load
duke
parents:
diff changeset
460 return treeEditable;
a61af66fc99e Initial load
duke
parents:
diff changeset
461 }
a61af66fc99e Initial load
duke
parents:
diff changeset
462 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
463 }
a61af66fc99e Initial load
duke
parents:
diff changeset
464 if (e == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 return treeEditable;
a61af66fc99e Initial load
duke
parents:
diff changeset
466 }
a61af66fc99e Initial load
duke
parents:
diff changeset
467 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 }
a61af66fc99e Initial load
duke
parents:
diff changeset
469 }
a61af66fc99e Initial load
duke
parents:
diff changeset
470
a61af66fc99e Initial load
duke
parents:
diff changeset
471
a61af66fc99e Initial load
duke
parents:
diff changeset
472 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
473 * Component used by TreeTableCellEditor. The only thing this does
a61af66fc99e Initial load
duke
parents:
diff changeset
474 * is to override the <code>reshape</code> method, and to ALWAYS
a61af66fc99e Initial load
duke
parents:
diff changeset
475 * make the x location be <code>offset</code>.
a61af66fc99e Initial load
duke
parents:
diff changeset
476 */
a61af66fc99e Initial load
duke
parents:
diff changeset
477 static class TreeTableTextField extends JTextField {
a61af66fc99e Initial load
duke
parents:
diff changeset
478 public int offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
479
152
c70a245cad3a 6670684: 4/5 SA command universe did not print out CMS space information
dcubed
parents: 0
diff changeset
480 public void setBounds(int x, int y, int w, int h) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
481 int newX = Math.max(x, offset);
152
c70a245cad3a 6670684: 4/5 SA command universe did not print out CMS space information
dcubed
parents: 0
diff changeset
482 super.setBounds(newX, y, w - (newX - x), h);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
483 }
a61af66fc99e Initial load
duke
parents:
diff changeset
484 }
a61af66fc99e Initial load
duke
parents:
diff changeset
485
a61af66fc99e Initial load
duke
parents:
diff changeset
486
a61af66fc99e Initial load
duke
parents:
diff changeset
487 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
488 * ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel
a61af66fc99e Initial load
duke
parents:
diff changeset
489 * to listen for changes in the ListSelectionModel it maintains. Once
a61af66fc99e Initial load
duke
parents:
diff changeset
490 * a change in the ListSelectionModel happens, the paths are updated
a61af66fc99e Initial load
duke
parents:
diff changeset
491 * in the DefaultTreeSelectionModel.
a61af66fc99e Initial load
duke
parents:
diff changeset
492 */
a61af66fc99e Initial load
duke
parents:
diff changeset
493 class ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel {
a61af66fc99e Initial load
duke
parents:
diff changeset
494 /** Set to true when we are updating the ListSelectionModel. */
a61af66fc99e Initial load
duke
parents:
diff changeset
495 protected boolean updatingListSelectionModel;
a61af66fc99e Initial load
duke
parents:
diff changeset
496
a61af66fc99e Initial load
duke
parents:
diff changeset
497 public ListToTreeSelectionModelWrapper() {
a61af66fc99e Initial load
duke
parents:
diff changeset
498 super();
a61af66fc99e Initial load
duke
parents:
diff changeset
499 getListSelectionModel().addListSelectionListener
a61af66fc99e Initial load
duke
parents:
diff changeset
500 (createListSelectionListener());
a61af66fc99e Initial load
duke
parents:
diff changeset
501 }
a61af66fc99e Initial load
duke
parents:
diff changeset
502
a61af66fc99e Initial load
duke
parents:
diff changeset
503 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
504 * Returns the list selection model. ListToTreeSelectionModelWrapper
a61af66fc99e Initial load
duke
parents:
diff changeset
505 * listens for changes to this model and updates the selected paths
a61af66fc99e Initial load
duke
parents:
diff changeset
506 * accordingly.
a61af66fc99e Initial load
duke
parents:
diff changeset
507 */
a61af66fc99e Initial load
duke
parents:
diff changeset
508 ListSelectionModel getListSelectionModel() {
a61af66fc99e Initial load
duke
parents:
diff changeset
509 return listSelectionModel;
a61af66fc99e Initial load
duke
parents:
diff changeset
510 }
a61af66fc99e Initial load
duke
parents:
diff changeset
511
a61af66fc99e Initial load
duke
parents:
diff changeset
512 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
513 * This is overridden to set <code>updatingListSelectionModel</code>
a61af66fc99e Initial load
duke
parents:
diff changeset
514 * and message super. This is the only place DefaultTreeSelectionModel
a61af66fc99e Initial load
duke
parents:
diff changeset
515 * alters the ListSelectionModel.
a61af66fc99e Initial load
duke
parents:
diff changeset
516 */
a61af66fc99e Initial load
duke
parents:
diff changeset
517 public void resetRowSelection() {
a61af66fc99e Initial load
duke
parents:
diff changeset
518 if(!updatingListSelectionModel) {
a61af66fc99e Initial load
duke
parents:
diff changeset
519 updatingListSelectionModel = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
521 super.resetRowSelection();
a61af66fc99e Initial load
duke
parents:
diff changeset
522 }
a61af66fc99e Initial load
duke
parents:
diff changeset
523 finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
524 updatingListSelectionModel = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
525 }
a61af66fc99e Initial load
duke
parents:
diff changeset
526 }
a61af66fc99e Initial load
duke
parents:
diff changeset
527 // Notice how we don't message super if
a61af66fc99e Initial load
duke
parents:
diff changeset
528 // updatingListSelectionModel is true. If
a61af66fc99e Initial load
duke
parents:
diff changeset
529 // updatingListSelectionModel is true, it implies the
a61af66fc99e Initial load
duke
parents:
diff changeset
530 // ListSelectionModel has already been updated and the
a61af66fc99e Initial load
duke
parents:
diff changeset
531 // paths are the only thing that needs to be updated.
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533
a61af66fc99e Initial load
duke
parents:
diff changeset
534 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
535 * Creates and returns an instance of ListSelectionHandler.
a61af66fc99e Initial load
duke
parents:
diff changeset
536 */
a61af66fc99e Initial load
duke
parents:
diff changeset
537 protected ListSelectionListener createListSelectionListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
538 return new ListSelectionHandler();
a61af66fc99e Initial load
duke
parents:
diff changeset
539 }
a61af66fc99e Initial load
duke
parents:
diff changeset
540
a61af66fc99e Initial load
duke
parents:
diff changeset
541 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
542 * If <code>updatingListSelectionModel</code> is false, this will
a61af66fc99e Initial load
duke
parents:
diff changeset
543 * reset the selected paths from the selected rows in the list
a61af66fc99e Initial load
duke
parents:
diff changeset
544 * selection model.
a61af66fc99e Initial load
duke
parents:
diff changeset
545 */
a61af66fc99e Initial load
duke
parents:
diff changeset
546 protected void updateSelectedPathsFromSelectedRows() {
a61af66fc99e Initial load
duke
parents:
diff changeset
547 if(!updatingListSelectionModel) {
a61af66fc99e Initial load
duke
parents:
diff changeset
548 updatingListSelectionModel = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
549 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
550 // This is way expensive, ListSelectionModel needs an
a61af66fc99e Initial load
duke
parents:
diff changeset
551 // enumerator for iterating.
a61af66fc99e Initial load
duke
parents:
diff changeset
552 int min = listSelectionModel.getMinSelectionIndex();
a61af66fc99e Initial load
duke
parents:
diff changeset
553 int max = listSelectionModel.getMaxSelectionIndex();
a61af66fc99e Initial load
duke
parents:
diff changeset
554
a61af66fc99e Initial load
duke
parents:
diff changeset
555 clearSelection();
a61af66fc99e Initial load
duke
parents:
diff changeset
556 if(min != -1 && max != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
557 for(int counter = min; counter <= max; counter++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
558 if(listSelectionModel.isSelectedIndex(counter)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
559 TreePath selPath = tree.getPathForRow
a61af66fc99e Initial load
duke
parents:
diff changeset
560 (counter);
a61af66fc99e Initial load
duke
parents:
diff changeset
561
a61af66fc99e Initial load
duke
parents:
diff changeset
562 if(selPath != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 addSelectionPath(selPath);
a61af66fc99e Initial load
duke
parents:
diff changeset
564 }
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }
a61af66fc99e Initial load
duke
parents:
diff changeset
566 }
a61af66fc99e Initial load
duke
parents:
diff changeset
567 }
a61af66fc99e Initial load
duke
parents:
diff changeset
568 }
a61af66fc99e Initial load
duke
parents:
diff changeset
569 finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
570 updatingListSelectionModel = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572 }
a61af66fc99e Initial load
duke
parents:
diff changeset
573 }
a61af66fc99e Initial load
duke
parents:
diff changeset
574
a61af66fc99e Initial load
duke
parents:
diff changeset
575 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
576 * Class responsible for calling updateSelectedPathsFromSelectedRows
a61af66fc99e Initial load
duke
parents:
diff changeset
577 * when the selection of the list changse.
a61af66fc99e Initial load
duke
parents:
diff changeset
578 */
a61af66fc99e Initial load
duke
parents:
diff changeset
579 class ListSelectionHandler implements ListSelectionListener {
a61af66fc99e Initial load
duke
parents:
diff changeset
580 public void valueChanged(ListSelectionEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
581 updateSelectedPathsFromSelectedRows();
a61af66fc99e Initial load
duke
parents:
diff changeset
582 }
a61af66fc99e Initial load
duke
parents:
diff changeset
583 }
a61af66fc99e Initial load
duke
parents:
diff changeset
584 }
a61af66fc99e Initial load
duke
parents:
diff changeset
585 }