001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing.outline;
024:
025: import javax.swing.JTree;
026: import javax.swing.SwingUtilities;
027: import javax.swing.event.TreeExpansionEvent;
028: import javax.swing.event.TreeExpansionListener;
029: import javax.swing.event.TreeModelEvent;
030: import javax.swing.event.TreeModelListener;
031: import javax.swing.table.AbstractTableModel;
032: import javax.swing.tree.TreePath;
033:
034: /**
035: * TreeTableModel is the model used by a JTreeTable.
036: * <p>
037: * It extends TreeModel to add methods for getting inforamtion about the set of columns each node in the TreeTableModel
038: * may have. Each column, like a column in a TableModel, has a name and a type associated with it. Each node in the
039: * TreeTableModel can return a value for each of the columns and set that value if isCellEditable() returns true.
040: *
041: * @author Philip Milne
042: * @author Scott Violet
043: */
044: public class TreeTableModelAdapter extends AbstractTableModel {
045:
046: private static final long serialVersionUID = 1531337763071850568L;
047:
048: private JTree tree;
049: private OutlineModel treeTableModel;
050:
051: public TreeTableModelAdapter(OutlineModel treeTableModel, JTree tree) {
052:
053: this .tree = tree;
054: this .treeTableModel = treeTableModel;
055:
056: tree.addTreeExpansionListener(new TreeExpansionListener() {
057:
058: // Don't use fireTableRowsInserted() here; the selection model
059: // would get updated twice.
060: public void treeExpanded(TreeExpansionEvent event) {
061:
062: fireTableDataChanged();
063: }
064:
065: public void treeCollapsed(TreeExpansionEvent event) {
066:
067: fireTableDataChanged();
068: }
069: });
070:
071: // Install a TreeModelListener that can update the table when
072: // tree changes. We use delayedFireTableDataChanged as we can
073: // not be guaranteed the tree will have finished processing
074: // the event before us.
075: treeTableModel.addTreeModelListener(new TreeModelListener() {
076:
077: public void treeNodesChanged(TreeModelEvent e) {
078:
079: delayedFireTableDataChanged();
080: }
081:
082: public void treeNodesInserted(TreeModelEvent e) {
083:
084: delayedFireTableDataChanged();
085: }
086:
087: public void treeNodesRemoved(TreeModelEvent e) {
088:
089: delayedFireTableDataChanged();
090: }
091:
092: public void treeStructureChanged(TreeModelEvent e) {
093:
094: delayedFireTableDataChanged();
095: }
096: });
097: }
098:
099: // Wrappers, implementing TableModel interface.
100:
101: public int getColumnCount() {
102:
103: return treeTableModel.getColumnCount();
104: }
105:
106: @Override
107: public String getColumnName(int column) {
108:
109: return treeTableModel.getColumnName(column);
110: }
111:
112: @Override
113: public Class<?> getColumnClass(int column) {
114:
115: return treeTableModel.getColumnClass(column);
116: }
117:
118: public int getRowCount() {
119:
120: return tree.getRowCount();
121: }
122:
123: protected Object nodeForRow(int row) {
124:
125: TreePath treePath = tree.getPathForRow(row);
126: if (treePath == null) {
127: return null;
128: }
129: return treePath.getLastPathComponent();
130: }
131:
132: public Object getValueAt(int row, int column) {
133:
134: return treeTableModel.getValueAt(nodeForRow(row), column);
135: }
136:
137: @Override
138: public boolean isCellEditable(int row, int column) {
139:
140: return treeTableModel.isCellEditable(nodeForRow(row), column);
141: }
142:
143: @Override
144: public void setValueAt(Object value, int row, int column) {
145:
146: treeTableModel.setValueAt(value, nodeForRow(row), column);
147: }
148:
149: /**
150: * Invokes fireTableDataChanged after all the pending events have been processed. SwingUtilities.invokeLater is used
151: * to handle this.
152: */
153: protected void delayedFireTableDataChanged() {
154:
155: SwingUtilities.invokeLater(new Runnable() {
156:
157: public void run() {
158:
159: fireTableDataChanged();
160: }
161: });
162: }
163: }
|