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: /*
043: * ConverterTableModel.java
044: *
045: * Created on January 28, 2004, 7:02 PM
046: */
047:
048: package org.netbeans.swing.outline;
049:
050: import java.util.ArrayList;
051: import java.util.List;
052: import javax.swing.event.TableModelEvent;
053: import javax.swing.event.TableModelListener;
054: import javax.swing.table.TableModel;
055:
056: /** A TableModel which is driven by a RowModel - the RowModel
057: * supplies row contents, based on nodes suppled by the tree
058: * column of an OutlineModel. This model supplies the additional
059: * rows of the TableModel to the OutlineModel.
060: *
061: * @author Tim Boudreau
062: */
063: final class ProxyTableModel implements TableModel, NodeRowModel {
064: private List listeners = new ArrayList();
065: private RowModel rowmodel;
066: private OutlineModel outlineModel;
067:
068: /** Creates a new instance of ProxyTableModel that will use the supplied
069: * RowModel to produce its values. */
070: public ProxyTableModel(RowModel rowmodel) {
071: this .rowmodel = rowmodel;
072: }
073:
074: /** Set the OutlineModel that will be used to find nodes for
075: * rows. DefaultOutlineModel will do this in its constructor. */
076: void setOutlineModel(OutlineModel mdl) {
077: this .outlineModel = mdl;
078: }
079:
080: /** Get the outline model used to provide column 0 nodes to the
081: * RowModel for setting the values. */
082: OutlineModel getOutlineModel() {
083: return outlineModel;
084: }
085:
086: public Class getColumnClass(int columnIndex) {
087: return rowmodel.getColumnClass(columnIndex);
088: }
089:
090: public int getColumnCount() {
091: return rowmodel.getColumnCount();
092: }
093:
094: public String getColumnName(int columnIndex) {
095: return rowmodel.getColumnName(columnIndex);
096: }
097:
098: public int getRowCount() {
099: //not interesting, will never be called - the outline model
100: //handles this
101: return -1;
102: }
103:
104: public Object getValueAt(int rowIndex, int columnIndex) {
105: Object node = getNodeForRow(rowIndex);
106: return rowmodel.getValueFor(node, columnIndex);
107: }
108:
109: public boolean isCellEditable(int rowIndex, int columnIndex) {
110: Object node = getNodeForRow(rowIndex);
111: return rowmodel.isCellEditable(node, columnIndex);
112: }
113:
114: public synchronized void removeTableModelListener(
115: TableModelListener l) {
116: listeners.remove(l);
117: }
118:
119: public synchronized void addTableModelListener(TableModelListener l) {
120: listeners.add(l);
121: }
122:
123: private void fire(TableModelEvent e) {
124: TableModelListener[] l;
125: synchronized (this ) {
126: l = new TableModelListener[listeners.size()];
127: l = (TableModelListener[]) listeners.toArray(l);
128: }
129: for (int i = 0; i < l.length; i++) {
130: l[i].tableChanged(e);
131: }
132: }
133:
134: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
135: Object node = getNodeForRow(rowIndex);
136: rowmodel.setValueFor(node, columnIndex, aValue);
137: TableModelEvent e = new TableModelEvent(this , rowIndex,
138: rowIndex, columnIndex);
139: fire(e);
140: }
141:
142: /** Get the object that will be passed to the RowModel to fetch values
143: * for the given row.
144: * changed to public 4/19/2004 so a cell editor can figure out information
145: * about the node being edited. - David Botterill
146: * @param row The row we need the tree node for */
147: public Object getNodeForRow(int row) {
148: return getOutlineModel().getValueAt(row, 0);
149: }
150:
151: }
|