KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > outline > ProxyTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.outline;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.event.TableModelEvent JavaDoc;
25 import javax.swing.event.TableModelListener JavaDoc;
26 import javax.swing.table.TableModel JavaDoc;
27
28 /** A TableModel which is driven by a RowModel - the RowModel
29  * supplies row contents, based on nodes suppled by the tree
30  * column of an OutlineModel. This model supplies the additional
31  * rows of the TableModel to the OutlineModel.
32  *
33  * @author Tim Boudreau
34  */

35 final class ProxyTableModel implements TableModel JavaDoc, NodeRowModel {
36     private List JavaDoc listeners = new ArrayList JavaDoc();
37     private RowModel rowmodel;
38     private OutlineModel outlineModel;
39     /** Creates a new instance of ProxyTableModel that will use the supplied
40      * RowModel to produce its values. */

41     public ProxyTableModel(RowModel rowmodel) {
42         this.rowmodel = rowmodel;
43     }
44     
45     /** Set the OutlineModel that will be used to find nodes for
46      * rows. DefaultOutlineModel will do this in its constructor. */

47     void setOutlineModel (OutlineModel mdl) {
48         this.outlineModel = mdl;
49     }
50     
51     /** Get the outline model used to provide column 0 nodes to the
52      * RowModel for setting the values. */

53     OutlineModel getOutlineModel () {
54         return outlineModel;
55     }
56     
57     public Class JavaDoc getColumnClass(int columnIndex) {
58         return rowmodel.getColumnClass(columnIndex);
59     }
60     
61     public int getColumnCount() {
62         return rowmodel.getColumnCount();
63     }
64     
65     public String JavaDoc getColumnName(int columnIndex) {
66         return rowmodel.getColumnName(columnIndex);
67     }
68     
69     public int getRowCount() {
70         //not interesting, will never be called - the outline model
71
//handles this
72
return -1;
73     }
74     
75     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
76         Object JavaDoc node = getNodeForRow(rowIndex);
77         return rowmodel.getValueFor(node, columnIndex);
78     }
79     
80     public boolean isCellEditable(int rowIndex, int columnIndex) {
81         Object JavaDoc node = getNodeForRow(rowIndex);
82         return rowmodel.isCellEditable (node, columnIndex);
83     }
84     
85     public synchronized void removeTableModelListener(TableModelListener JavaDoc l) {
86         listeners.remove(l);
87     }
88     
89     public synchronized void addTableModelListener(TableModelListener JavaDoc l) {
90         listeners.add(l);
91     }
92     
93     private void fire (TableModelEvent JavaDoc e) {
94         TableModelListener JavaDoc[] l;
95         synchronized (this) {
96             l = new TableModelListener JavaDoc[listeners.size()];
97             l = (TableModelListener JavaDoc[]) listeners.toArray(l);
98         }
99         for (int i=0; i < l.length; i++) {
100             l[i].tableChanged(e);
101         }
102     }
103     
104     public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
105         Object JavaDoc node = getNodeForRow(rowIndex);
106         rowmodel.setValueFor (node, columnIndex, aValue);
107         TableModelEvent JavaDoc e = new TableModelEvent JavaDoc (this, rowIndex, rowIndex,
108             columnIndex);
109         fire(e);
110     }
111     
112     /** Get the object that will be passed to the RowModel to fetch values
113      * for the given row.
114      * changed to public 4/19/2004 so a cell editor can figure out information
115      * about the node being edited. - David Botterill
116      * @param row The row we need the tree node for */

117     public Object JavaDoc getNodeForRow(int row) {
118         return getOutlineModel().getValueAt(row, 0);
119     }
120
121     
122 }
123
Popular Tags