Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * @(#)TreeTableModelAdapter.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.JTree;
37 import javax.swing.SwingUtilities;
38 import javax.swing.table.AbstractTableModel;
39 import javax.swing.tree.TreePath;
40 import javax.swing.event.TreeExpansionEvent;
41 import javax.swing.event.TreeExpansionListener;
42 import javax.swing.event.TreeModelEvent;
43 import javax.swing.event.TreeModelListener;
44
45 /**
46 * This is a wrapper class takes a TreeTableModel and implements
47 * the table model interface. The implementation is trivial, with
48 * all of the event dispatching support provided by the superclass:
49 * the AbstractTableModel.
50 *
51 * @version 1.2 10/27/98
52 *
53 * @author Philip Milne
54 * @author Scott Violet
55 */
56 public class TreeTableModelAdapter extends AbstractTableModel {
57 private static final long serialVersionUID = 48741114609209052L;
58 JTree tree;
59 TreeTableModel treeTableModel;
60
61 public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
62 this.tree = tree;
63 this.treeTableModel = treeTableModel;
64
65 tree.addTreeExpansionListener(new TreeExpansionListener() {
66 // Don't use fireTableRowsInserted() here; the selection model
67 // would get updated twice.
68 public void treeExpanded(TreeExpansionEvent event) {
69 fireTableDataChanged();
70 }
71 public void treeCollapsed(TreeExpansionEvent event) {
72 fireTableDataChanged();
73 }
74 });
75
76 // Install a TreeModelListener that can update the table when
77 // tree changes. We use delayedFireTableDataChanged as we can
78 // not be guaranteed the tree will have finished processing
79 // the event before us.
80 treeTableModel.addTreeModelListener(new TreeModelListener() {
81 public void treeNodesChanged(TreeModelEvent e) {
82 delayedFireTableDataChanged();
83 }
84
85 public void treeNodesInserted(TreeModelEvent e) {
86 delayedFireTableDataChanged();
87 }
88
89 public void treeNodesRemoved(TreeModelEvent e) {
90 delayedFireTableDataChanged();
91 }
92
93 public void treeStructureChanged(TreeModelEvent e) {
94 delayedFireTableDataChanged();
95 }
96 });
97 }
98
99 // Wrappers, implementing TableModel interface.
100
101 public int getColumnCount() {
102 return treeTableModel.getColumnCount();
103 }
104
105 @Override
106 public String getColumnName(int column) {
107 return treeTableModel.getColumnName(column);
108 }
109
110 @Override
111 public Class<?> getColumnClass(int column) {
112 return treeTableModel.getColumnClass(column);
113 }
114
115 public int getRowCount() {
116 return tree.getRowCount();
117 }
118
119 protected Object nodeForRow(int row) {
120 TreePath treePath = tree.getPathForRow(row);
121 return treePath.getLastPathComponent();
122 }
123
124 public Object getValueAt(int row, int column) {
125 return treeTableModel.getValueAt(nodeForRow(row), column);
126 }
127
128 @Override
129 public boolean isCellEditable(int row, int column) {
130 return treeTableModel.isCellEditable(nodeForRow(row), column);
131 }
132
133 @Override
134 public void setValueAt(Object value, int row, int column) {
135 treeTableModel.setValueAt(value, nodeForRow(row), column);
136 }
137
138 /**
139 * Invokes fireTableDataChanged after all the pending events have been
140 * processed. SwingUtilities.invokeLater is used to handle this.
141 */
142 protected void delayedFireTableDataChanged() {
143 SwingUtilities.invokeLater(new Runnable() {
144 public void run() {
145 fireTableDataChanged();
146 }
147 });
148 }
149 }