001: package net.sf.memoranda.ui.treetable;
002:
003: /*
004: * @(#)TreeTableModelAdapter.java 1.2 98/10/27
005: *
006: * Copyright 1997, 1998 by Sun Microsystems, Inc.,
007: * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
008: * All rights reserved.
009: *
010: * This software is the confidential and proprietary information
011: * of Sun Microsystems, Inc. ("Confidential Information"). You
012: * shall not disclose such Confidential Information and shall use
013: * it only in accordance with the terms of the license agreement
014: * you entered into with Sun.
015: */
016:
017: import javax.swing.JTree;
018: import javax.swing.SwingUtilities;
019: import javax.swing.table.AbstractTableModel;
020: import javax.swing.tree.TreePath;
021: import javax.swing.event.TreeExpansionEvent;
022: import javax.swing.event.TreeExpansionListener;
023: import javax.swing.event.TreeModelEvent;
024: import javax.swing.event.TreeModelListener;
025:
026: /**
027: * This is a wrapper class takes a TreeTableModel and implements
028: * the table model interface. The implementation is trivial, with
029: * all of the event dispatching support provided by the superclass:
030: * the AbstractTableModel.
031: *
032: * @version 1.2 10/27/98
033: *
034: * @author Philip Milne
035: * @author Scott Violet
036: */
037: public class TreeTableModelAdapter extends AbstractTableModel {
038: JTree tree;
039: TreeTableModel treeTableModel;
040:
041: public TreeTableModelAdapter(TreeTableModel treeTableModel,
042: JTree tree) {
043: this .tree = tree;
044: this .treeTableModel = treeTableModel;
045:
046: tree.addTreeExpansionListener(new TreeExpansionListener() {
047: // Don't use fireTableRowsInserted() here; the selection model
048: // would get updated twice.
049: public void treeExpanded(TreeExpansionEvent event) {
050: fireTableDataChanged();
051: }
052:
053: public void treeCollapsed(TreeExpansionEvent event) {
054: fireTableDataChanged();
055: }
056: });
057:
058: // Install a TreeModelListener that can update the table when
059: // tree changes. We use delayedFireTableDataChanged as we can
060: // not be guaranteed the tree will have finished processing
061: // the event before us.
062: treeTableModel.addTreeModelListener(new TreeModelListener() {
063: public void treeNodesChanged(TreeModelEvent e) {
064: delayedFireTableDataChanged();
065: }
066:
067: public void treeNodesInserted(TreeModelEvent e) {
068: delayedFireTableDataChanged();
069: }
070:
071: public void treeNodesRemoved(TreeModelEvent e) {
072: delayedFireTableDataChanged();
073: }
074:
075: public void treeStructureChanged(TreeModelEvent e) {
076: delayedFireTableDataChanged();
077: }
078: });
079: }
080:
081: // Wrappers, implementing TableModel interface.
082:
083: public int getColumnCount() {
084: return treeTableModel.getColumnCount();
085: }
086:
087: public String getColumnName(int column) {
088: return treeTableModel.getColumnName(column);
089: }
090:
091: public Class getColumnClass(int column) {
092: return treeTableModel.getColumnClass(column);
093: }
094:
095: public int getRowCount() {
096: return tree.getRowCount();
097: }
098:
099: protected Object nodeForRow(int row) {
100: TreePath treePath = tree.getPathForRow(row);
101: return treePath.getLastPathComponent();
102: }
103:
104: public Object getValueAt(int row, int column) {
105: return treeTableModel.getValueAt(nodeForRow(row), column);
106: }
107:
108: public boolean isCellEditable(int row, int column) {
109: return treeTableModel.isCellEditable(nodeForRow(row), column);
110: }
111:
112: public void setValueAt(Object value, int row, int column) {
113: treeTableModel.setValueAt(value, nodeForRow(row), column);
114: }
115:
116: /**
117: * Invokes fireTableDataChanged after all the pending events have been
118: * processed. SwingUtilities.invokeLater is used to handle this.
119: */
120: protected void delayedFireTableDataChanged() {
121: SwingUtilities.invokeLater(new Runnable() {
122: public void run() {
123: fireTableDataChanged();
124: }
125: });
126: }
127: }
|