Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/debugger/treetable/JTreeTable.java

raw symbols SCIP
1 /*
2 * @(#)JTreeTable.java 1.2 98/10/27
3 *
4 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * - Neither the name of Sun Microsystems nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 package org.mozilla.javascript.tools.debugger.treetable;
35
36 import javax.swing.*;
37 import javax.swing.event.*;
38 import javax.swing.tree.*;
39 import javax.swing.table.*;
40
41 import java.awt.Dimension;
42 import java.awt.Component;
43 import java.awt.Graphics;
44
45 import java.awt.event.MouseEvent;
46
47 import java.util.EventObject;
48
49 /**
50 * This example shows how to create a simple JTreeTable component,
51 * by using a JTree as a renderer (and editor) for the cells in a
52 * particular column in the JTable.
53 *
54 * @version 1.2 10/27/98
55 *
56 * @author Philip Milne
57 * @author Scott Violet
58 */
59 public class JTreeTable extends JTable {
60 /**
61 *
62 */
63 private static final long serialVersionUID = -2103973006456695515L;
64 /** A subclass of JTree. */
65 protected TreeTableCellRenderer tree;
66
67 public JTreeTable(TreeTableModel treeTableModel) {
68 super();
69
70 // Create the tree. It will be used as a renderer and editor.
71 tree = new TreeTableCellRenderer(treeTableModel);
72
73 // Install a tableModel representing the visible rows in the tree.
74 super.setModel(new TreeTableModelAdapter(treeTableModel, tree));
75
76 // Force the JTable and JTree to share their row selection models.
77 ListToTreeSelectionModelWrapper selectionWrapper = new
78 ListToTreeSelectionModelWrapper();
79 tree.setSelectionModel(selectionWrapper);
80 setSelectionModel(selectionWrapper.getListSelectionModel());
81
82 // Install the tree editor renderer and editor.
83 setDefaultRenderer(TreeTableModel.class, tree);
84 setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor());
85
86 // No grid.
87 setShowGrid(false);
88
89 // No intercell spacing
90 setIntercellSpacing(new Dimension(0, 0));
91
92 // And update the height of the trees row to match that of
93 // the table.
94 if (tree.getRowHeight() < 1) {
95 // Metal looks better like this.
96 setRowHeight(18);
97 }
98 }
99
100 /**
101 * Overridden to message super and forward the method to the tree.
102 * Since the tree is not actually in the component hierarchy it will
103 * never receive this unless we forward it in this manner.
104 */
105 @Override
106 public void updateUI() {
107 super.updateUI();
108 if(tree != null) {
109 tree.updateUI();
110 }
111 // Use the tree's default foreground and background colors in the
112 // table.
113 LookAndFeel.installColorsAndFont(this, "Tree.background",
114 "Tree.foreground", "Tree.font");
115 }
116
117 /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to
118 * paint the editor. The UI currently uses different techniques to
119 * paint the renderers and editors and overriding setBounds() below
120 * is not the right thing to do for an editor. Returning -1 for the
121 * editing row in this case, ensures the editor is never painted.
122 */
123 @Override
124 public int getEditingRow() {
125 return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 :
126 editingRow;
127 }
128
129 /**
130 * Overridden to pass the new rowHeight to the tree.
131 */
132 @Override
133 public void setRowHeight(int rowHeight) {
134 super.setRowHeight(rowHeight);
135 if (tree != null && tree.getRowHeight() != rowHeight) {
136 tree.setRowHeight(getRowHeight());
137 }
138 }
139
140 /**
141 * Returns the tree that is being shared between the model.
142 */
143 public JTree getTree() {
144 return tree;
145 }
146
147 /**
148 * A TreeCellRenderer that displays a JTree.
149 */
150 public class TreeTableCellRenderer extends JTree implements TableCellRenderer {
151 private static final long serialVersionUID = -193867880014600717L;
152 /** Last table/tree row asked to renderer. */
153 protected int visibleRow;
154
155 public TreeTableCellRenderer(TreeModel model) {
156 super(model);
157 }
158
159 /**
160 * updateUI is overridden to set the colors of the Tree's renderer
161 * to match that of the table.
162 */
163 @Override
164 public void updateUI() {
165 super.updateUI();
166 // Make the tree's cell renderer use the table's cell selection
167 // colors.
168 TreeCellRenderer tcr = getCellRenderer();
169 if (tcr instanceof DefaultTreeCellRenderer) {
170 DefaultTreeCellRenderer dtcr = ((DefaultTreeCellRenderer)tcr);
171 // For 1.1 uncomment this, 1.2 has a bug that will cause an
172 // exception to be thrown if the border selection color is
173 // null.
174 // dtcr.setBorderSelectionColor(null);
175 dtcr.setTextSelectionColor(UIManager.getColor
176 ("Table.selectionForeground"));
177 dtcr.setBackgroundSelectionColor(UIManager.getColor
178 ("Table.selectionBackground"));
179 }
180 }
181
182 /**
183 * Sets the row height of the tree, and forwards the row height to
184 * the table.
185 */
186 @Override
187 public void setRowHeight(int rowHeight) {
188 if (rowHeight > 0) {
189 super.setRowHeight(rowHeight);
190 if (JTreeTable.this != null &&
191 JTreeTable.this.getRowHeight() != rowHeight) {
192 JTreeTable.this.setRowHeight(getRowHeight());
193 }
194 }
195 }
196
197 /**
198 * This is overridden to set the height to match that of the JTable.
199 */
200 @Override
201 public void setBounds(int x, int y, int w, int h) {
202 super.setBounds(x, 0, w, JTreeTable.this.getHeight());
203 }
204
205 /**
206 * Sublcassed to translate the graphics such that the last visible
207 * row will be drawn at 0,0.
208 */
209 @Override
210 public void paint(Graphics g) {
211 g.translate(0, -visibleRow * getRowHeight());
212 super.paint(g);
213 }
214
215 /**
216 * TreeCellRenderer method. Overridden to update the visible row.
217 */
218 public Component getTableCellRendererComponent(JTable table,
219 Object value,
220 boolean isSelected,
221 boolean hasFocus,
222 int row, int column) {
223 if(isSelected)
224 setBackground(table.getSelectionBackground());
225 else
226 setBackground(table.getBackground());
227
228 visibleRow = row;
229 return this;
230 }
231 }
232
233
234 /**
235 * TreeTableCellEditor implementation. Component returned is the
236 * JTree.
237 */
238 public class TreeTableCellEditor extends AbstractCellEditor implements
239 TableCellEditor {
240 public Component getTableCellEditorComponent(JTable table,
241 Object value,
242 boolean isSelected,
243 int r, int c) {
244 return tree;
245 }
246
247 /**
248 * Overridden to return false, and if the event is a mouse event
249 * it is forwarded to the tree.<p>
250 * The behavior for this is debatable, and should really be offered
251 * as a property. By returning false, all keyboard actions are
252 * implemented in terms of the table. By returning true, the
253 * tree would get a chance to do something with the keyboard
254 * events. For the most part this is ok. But for certain keys,
255 * such as left/right, the tree will expand/collapse where as
256 * the table focus should really move to a different column. Page
257 * up/down should also be implemented in terms of the table.
258 * By returning false this also has the added benefit that clicking
259 * outside of the bounds of the tree node, but still in the tree
260 * column will select the row, whereas if this returned true
261 * that wouldn't be the case.
262 * <p>By returning false we are also enforcing the policy that
263 * the tree will never be editable (at least by a key sequence).
264 */
265 @Override
266 public boolean isCellEditable(EventObject e) {
267 if (e instanceof MouseEvent) {
268 for (int counter = getColumnCount() - 1; counter >= 0;
269 counter--) {
270 if (getColumnClass(counter) == TreeTableModel.class) {
271 MouseEvent me = (MouseEvent)e;
272 MouseEvent newME = new MouseEvent(tree, me.getID(),
273 me.getWhen(), me.getModifiers(),
274 me.getX() - getCellRect(0, counter, true).x,
275 me.getY(), me.getClickCount(),
276 me.isPopupTrigger());
277 tree.dispatchEvent(newME);
278 break;
279 }
280 }
281 }
282 return false;
283 }
284 }
285
286
287 /**
288 * ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel
289 * to listen for changes in the ListSelectionModel it maintains. Once
290 * a change in the ListSelectionModel happens, the paths are updated
291 * in the DefaultTreeSelectionModel.
292 */
293 public class ListToTreeSelectionModelWrapper
294 extends DefaultTreeSelectionModel
295 {
296 private static final long serialVersionUID = 8168140829623071131L;
297
298 /** Set to true when we are updating the ListSelectionModel. */
299 protected boolean updatingListSelectionModel;
300
301 public ListToTreeSelectionModelWrapper() {
302 super();
303 getListSelectionModel().addListSelectionListener
304 (createListSelectionListener());
305 }
306
307 /**
308 * Returns the list selection model. ListToTreeSelectionModelWrapper
309 * listens for changes to this model and updates the selected paths
310 * accordingly.
311 */
312 public ListSelectionModel getListSelectionModel() {
313 return listSelectionModel;
314 }
315
316 /**
317 * This is overridden to set <code>updatingListSelectionModel</code>
318 * and message super. This is the only place DefaultTreeSelectionModel
319 * alters the ListSelectionModel.
320 */
321 @Override
322 public void resetRowSelection() {
323 if(!updatingListSelectionModel) {
324 updatingListSelectionModel = true;
325 try {
326 super.resetRowSelection();
327 }
328 finally {
329 updatingListSelectionModel = false;
330 }
331 }
332 // Notice how we don't message super if
333 // updatingListSelectionModel is true. If
334 // updatingListSelectionModel is true, it implies the
335 // ListSelectionModel has already been updated and the
336 // paths are the only thing that needs to be updated.
337 }
338
339 /**
340 * Creates and returns an instance of ListSelectionHandler.
341 */
342 protected ListSelectionListener createListSelectionListener() {
343 return new ListSelectionHandler();
344 }
345
346 /**
347 * If <code>updatingListSelectionModel</code> is false, this will
348 * reset the selected paths from the selected rows in the list
349 * selection model.
350 */
351 protected void updateSelectedPathsFromSelectedRows() {
352 if(!updatingListSelectionModel) {
353 updatingListSelectionModel = true;
354 try {
355 // This is way expensive, ListSelectionModel needs an
356 // enumerator for iterating.
357 int min = listSelectionModel.getMinSelectionIndex();
358 int max = listSelectionModel.getMaxSelectionIndex();
359
360 clearSelection();
361 if(min != -1 && max != -1) {
362 for(int counter = min; counter <= max; counter++) {
363 if(listSelectionModel.isSelectedIndex(counter)) {
364 TreePath selPath = tree.getPathForRow
365 (counter);
366
367 if(selPath != null) {
368 addSelectionPath(selPath);
369 }
370 }
371 }
372 }
373 }
374 finally {
375 updatingListSelectionModel = false;
376 }
377 }
378 }
379
380 /**
381 * Class responsible for calling updateSelectedPathsFromSelectedRows
382 * when the selection of the list changse.
383 */
384 class ListSelectionHandler implements ListSelectionListener {
385 public void valueChanged(ListSelectionEvent e) {
386 updateSelectedPathsFromSelectedRows();
387 }
388 }
389 }
390 }