KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > AttrTableModel


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
21 package org.netbeans.modules.web.wizards;
22
23 import javax.swing.table.AbstractTableModel JavaDoc;
24 import javax.swing.event.TableModelEvent JavaDoc;
25
26 import org.openide.util.NbBundle;
27
28 /**
29  *
30  * @author mk115033
31  */

32 public class AttrTableModel extends AbstractTableModel JavaDoc {
33
34     public AttrTableModel() {
35     }
36
37     private boolean debug;
38     private String JavaDoc[] colheaders = null;
39     private Object JavaDoc[][] data = null;
40     private int numCols;
41     private int numRows=0;
42
43     /** Creates a new instance of AttrTableModel */
44     AttrTableModel(String JavaDoc[] headers) {
45         this.colheaders = headers;
46         numCols = colheaders.length;
47         
48     }
49     
50     AttrTableModel(String JavaDoc[] headers, Object JavaDoc[][] data) {
51         this.colheaders = headers;
52         numCols = colheaders.length;
53         this.data=data;
54         numRows = data.length;
55     }
56     
57     public String JavaDoc getColumnName(int col) {
58         String JavaDoc key = "LBL_".concat(colheaders[col]); //NOI18N
59
return NbBundle.getMessage(AttrTableModel.class, key);
60     }
61
62     public int getRowCount() { return numRows; }
63     public int getColumnCount() { return numCols; }
64
65     public Object JavaDoc getValueAt(int row, int col) {
66         return data[row][col];
67     }
68     public int addRow(String JavaDoc name, String JavaDoc type, boolean required, boolean rtexpr) {
69
70         Object JavaDoc[][] data2 = new Object JavaDoc[numRows+1][numCols];
71         int i=0, j=0;
72
73         if(numRows > 0) {
74             for(j=0; j<numRows; ++j)
75                 data2[j] = data[j];
76         }
77
78         data2[j][0] = name;
79         data2[j][1] = type;
80         data2[j][2] = Boolean.valueOf(required);
81         data2[j][3] = Boolean.valueOf(rtexpr);
82         data = data2;
83         numRows++;
84         return j;
85     }
86
87     public void removeRow(int row) {
88
89         if(debug) {
90             log("::removeRow()"); //NOI18N
91
log("row is " + row); //NOI18N
92
log("numRows is " + numRows); //NOI18N
93
}
94
95         Object JavaDoc[][] data2 = new Object JavaDoc[numRows-1][numCols];
96         int newRowIndex = 0;
97         for(int i=0; i<numRows; ++i) {
98             if(debug) log("\tExamining row " + i); //NOI18N
99
if(i==row) continue;
100             if(debug) log("\tKeep this row"); //NOI18N
101
data2[newRowIndex]=data[i];
102             newRowIndex++;
103             if(debug) log("\tnewRowIndex is " + newRowIndex); //NOI18N
104
}
105         data = data2;
106         numRows = --numRows;
107     }
108
109     public void setData(String JavaDoc name, String JavaDoc value, boolean required, boolean rtexpr, int row) {
110         data[row][0] = name;
111         data[row][1] = value;
112         data[row][2] = Boolean.valueOf(required);
113         data[row][3] = Boolean.valueOf(rtexpr);
114         fireTableChanged(new TableModelEvent JavaDoc(this, row));
115     }
116
117     public void setValueAt(Object JavaDoc value, int row, int col) {
118
119         if(debug)
120             log("::setValueAt(): value = " + value + //NOI18N
121
" at " + row + ", " + col); //NOI18N
122

123         data[row][col] = value;
124
125         if(debug) {
126             for(int i=0; i<data.length; ++i) {
127                 for(int j=0; j<numCols; ++j) {
128                     log("\t" + String.valueOf(i) + "," + //NOI18N
129
String.valueOf(j) + ": " + data[i][j]); //NOI18N
130
}
131             }
132         }
133         // Commenting this out since the value is set twice.
134
fireTableCellUpdated(row, col);
135     }
136
137     private void log(String JavaDoc s) {
138         System.out.println("AttrTableModel:"+s); //NOI18N
139
}
140     
141     public Object JavaDoc[][] getAttributes() {
142         if (data==null) return new Object JavaDoc[][]{};
143         else return data;
144     }
145 }
146
Popular Tags