001: /*
002: * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved. Redistribution and use in source and binary
003: * forms, with or without modification, are permitted provided that the following conditions are met: -
004: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
005: * following disclaimer. - Redistribution in binary form must reproduce the above copyright notice, this list
006: * of conditions and the following disclaimer in the documentation and/or other materials provided with the
007: * distribution. Neither the name of Sun Microsystems, Inc. or the names of contributors may be used to
008: * endorse or promote products derived from this software without specific prior written permission. This
009: * software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR IMPLIED CONDITIONS,
010: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
011: * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
012: * OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS
013: * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
014: * OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
015: * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
016: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You acknowledge that this software is not
017: * designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear
018: * facility.
019: */
020:
021: package br.com.igor.beans.table;
022:
023: import javax.swing.JTree;
024: import javax.swing.SwingUtilities;
025: import javax.swing.event.TreeExpansionEvent;
026: import javax.swing.event.TreeExpansionListener;
027: import javax.swing.event.TreeModelEvent;
028: import javax.swing.event.TreeModelListener;
029: import javax.swing.table.AbstractTableModel;
030: import javax.swing.tree.TreePath;
031:
032: /**
033: * This is a wrapper class takes a TreeTableModel and implements the table model interface. The implementation
034: * is trivial, with all of the event dispatching support provided by the superclass: the AbstractTableModel.
035: *
036: * @version 1.2 10/27/98
037: * @author Philip Milne
038: * @author Scott Violet
039: */
040: public class TreeTableModelAdapter extends AbstractTableModel {
041: JTree tree;
042:
043: TreeTableModel treeTableModel;
044:
045: /**
046: * Contrutor
047: * @param treeTableModel Model
048: * @param tree JTree
049: */
050: public TreeTableModelAdapter(TreeTableModel treeTableModel,
051: JTree tree) {
052: this .tree = tree;
053: this .treeTableModel = treeTableModel;
054:
055: tree.addTreeExpansionListener(new TreeExpansionListener() {
056: // Don't use fireTableRowsInserted() here; the selection model
057: // would get updated twice.
058: public void treeExpanded(TreeExpansionEvent event) {
059: fireTableDataChanged();
060: }
061:
062: public void treeCollapsed(TreeExpansionEvent event) {
063: fireTableDataChanged();
064: }
065: });
066:
067: // Installs a TreeModelListener that can update the table when
068: // the tree changes. We use delayedFireTableDataChanged as we can
069: // not be guaranteed the tree will have finished processing
070: // the event before us.
071: treeTableModel.addTreeModelListener(new TreeModelListener() {
072: public void treeNodesChanged(TreeModelEvent e) {
073: delayedFireTableDataChanged();
074: }
075:
076: public void treeNodesInserted(TreeModelEvent e) {
077: delayedFireTableDataChanged();
078: }
079:
080: public void treeNodesRemoved(TreeModelEvent e) {
081: delayedFireTableDataChanged();
082: }
083:
084: public void treeStructureChanged(TreeModelEvent e) {
085: delayedFireTableDataChanged();
086: }
087: });
088: }
089:
090: // Wrappers, implementing TableModel interface.
091:
092: /**
093: * @see javax.swing.table.TableModel#getColumnCount()
094: */
095: public int getColumnCount() {
096: return treeTableModel.getColumnCount();
097: }
098:
099: /**
100: * @see javax.swing.table.AbstractTableModel#getColumnName(int)
101: */
102: @Override
103: public String getColumnName(int column) {
104: return treeTableModel.getColumnName(column);
105: }
106:
107: /**
108: * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
109: */
110: @Override
111: public Class getColumnClass(int column) {
112: return treeTableModel.getColumnClass(column);
113: }
114:
115: /**
116: * @see javax.swing.table.TableModel#getRowCount()
117: */
118: public int getRowCount() {
119: return tree.getRowCount();
120: }
121:
122: protected Object nodeForRow(int row) {
123: TreePath treePath = tree.getPathForRow(row);
124: return treePath.getLastPathComponent();
125: }
126:
127: /**
128: * @see javax.swing.table.TableModel#getValueAt(int, int)
129: */
130: public Object getValueAt(int row, int column) {
131: return treeTableModel.getValueAt(nodeForRow(row), column);
132: }
133:
134: /**
135: * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
136: */
137: @Override
138: public boolean isCellEditable(int row, int column) {
139: return treeTableModel.isCellEditable(nodeForRow(row), column);
140: }
141:
142: /**
143: * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
144: */
145: @Override
146: public void setValueAt(Object value, int row, int column) {
147: treeTableModel.setValueAt(value, nodeForRow(row), column);
148: }
149:
150: /**
151: * Invokes fireTableDataChanged after all the pending events have been processed.
152: * SwingUtilities.invokeLater is used to handle this.
153: */
154: protected void delayedFireTableDataChanged() {
155: SwingUtilities.invokeLater(new Runnable() {
156: public void run() {
157: fireTableDataChanged();
158: }
159: });
160: }
161:
162: DBTreeTableModel getDBTreeTableModel() {
163: return (DBTreeTableModel) treeTableModel;
164: }
165: }
|