001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.swing.treetable;
043:
044: import javax.swing.JTree;
045: import javax.swing.SwingUtilities;
046: import javax.swing.table.AbstractTableModel;
047: import javax.swing.tree.TreePath;
048: import javax.swing.event.TreeExpansionEvent;
049: import javax.swing.event.TreeExpansionListener;
050: import javax.swing.event.TreeModelEvent;
051: import javax.swing.event.TreeModelListener;
052:
053: /**
054: * This is a wrapper class takes a TreeTableModel and implements
055: * the table model interface. The implementation is trivial, with
056: * all of the event dispatching support provided by the superclass:
057: * the AbstractTableModel.
058: *
059: * @version 1.2 10/27/98
060: *
061: * @author Philip Milne
062: * @author Scott Violet
063: */
064: public class TreeTableModelAdapter extends AbstractTableModel {
065: JTree tree;
066: TreeTableModel treeTableModel;
067:
068: public TreeTableModelAdapter(TreeTableModel treeTableModel,
069: JTree tree) {
070: this .tree = tree;
071: this .treeTableModel = treeTableModel;
072:
073: tree.addTreeExpansionListener(new TreeExpansionListener() {
074: // Don't use fireTableRowsInserted() here; the selection model
075: // would get updated twice.
076: public void treeExpanded(TreeExpansionEvent event) {
077: fireTableDataChanged();
078: }
079:
080: public void treeCollapsed(TreeExpansionEvent event) {
081: fireTableDataChanged();
082: }
083: });
084:
085: // Install a TreeModelListener that can update the table when
086: // tree changes. We use delayedFireTableDataChanged as we can
087: // not be guaranteed the tree will have finished processing
088: // the event before us.
089: treeTableModel.addTreeModelListener(new TreeModelListener() {
090: public void treeNodesChanged(TreeModelEvent e) {
091: delayedFireTableDataChanged();
092: }
093:
094: public void treeNodesInserted(TreeModelEvent e) {
095: delayedFireTableDataChanged();
096: }
097:
098: public void treeNodesRemoved(TreeModelEvent e) {
099: delayedFireTableDataChanged();
100: }
101:
102: public void treeStructureChanged(TreeModelEvent e) {
103: delayedFireTableDataChanged();
104: }
105: });
106: }
107:
108: // Wrappers, implementing TableModel interface.
109:
110: public int getColumnCount() {
111: return treeTableModel.getColumnCount();
112: }
113:
114: public String getColumnName(int column) {
115: return treeTableModel.getColumnName(column);
116: }
117:
118: public Class getColumnClass(int column) {
119: return treeTableModel.getColumnClass(column);
120: }
121:
122: public int getRowCount() {
123: return tree.getRowCount();
124: }
125:
126: protected Object nodeForRow(int row) {
127: TreePath treePath = tree.getPathForRow(row);
128: return treePath == null ? null : treePath
129: .getLastPathComponent();
130: }
131:
132: public Object getValueAt(int row, int column) {
133: return treeTableModel.getValueAt(nodeForRow(row), column);
134: }
135:
136: public boolean isCellEditable(int row, int column) {
137: return treeTableModel.isCellEditable(nodeForRow(row), column);
138: }
139:
140: public void setValueAt(Object value, int row, int column) {
141: treeTableModel.setValueAt(value, nodeForRow(row), column);
142: }
143:
144: /**
145: * Invokes fireTableDataChanged after all the pending events have been
146: * processed. SwingUtilities.invokeLater is used to handle this.
147: */
148: protected void delayedFireTableDataChanged() {
149: SwingUtilities.invokeLater(new Runnable() {
150: public void run() {
151: fireTableDataChanged();
152: }
153: });
154: }
155: }
|