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-2006 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: package org.netbeans.modules.web.wizards;
043:
044: import javax.swing.table.AbstractTableModel;
045: import javax.swing.event.TableModelEvent;
046:
047: import org.openide.util.NbBundle;
048:
049: /**
050: *
051: * @author mk115033
052: */
053: public class AttrTableModel extends AbstractTableModel {
054:
055: public AttrTableModel() {
056: }
057:
058: private boolean debug;
059: private String[] colheaders = null;
060: private Object[][] data = null;
061: private int numCols;
062: private int numRows = 0;
063:
064: /** Creates a new instance of AttrTableModel */
065: AttrTableModel(String[] headers) {
066: this .colheaders = headers;
067: numCols = colheaders.length;
068:
069: }
070:
071: AttrTableModel(String[] headers, Object[][] data) {
072: this .colheaders = headers;
073: numCols = colheaders.length;
074: this .data = data;
075: numRows = data.length;
076: }
077:
078: public String getColumnName(int col) {
079: String key = "LBL_".concat(colheaders[col]); //NOI18N
080: return NbBundle.getMessage(AttrTableModel.class, key);
081: }
082:
083: public int getRowCount() {
084: return numRows;
085: }
086:
087: public int getColumnCount() {
088: return numCols;
089: }
090:
091: public Object getValueAt(int row, int col) {
092: return data[row][col];
093: }
094:
095: public int addRow(String name, String type, boolean required,
096: boolean rtexpr) {
097:
098: Object[][] data2 = new Object[numRows + 1][numCols];
099: int i = 0, j = 0;
100:
101: if (numRows > 0) {
102: for (j = 0; j < numRows; ++j)
103: data2[j] = data[j];
104: }
105:
106: data2[j][0] = name;
107: data2[j][1] = type;
108: data2[j][2] = Boolean.valueOf(required);
109: data2[j][3] = Boolean.valueOf(rtexpr);
110: data = data2;
111: numRows++;
112: return j;
113: }
114:
115: public void removeRow(int row) {
116:
117: if (debug) {
118: log("::removeRow()"); //NOI18N
119: log("row is " + row); //NOI18N
120: log("numRows is " + numRows); //NOI18N
121: }
122:
123: Object[][] data2 = new Object[numRows - 1][numCols];
124: int newRowIndex = 0;
125: for (int i = 0; i < numRows; ++i) {
126: if (debug)
127: log("\tExamining row " + i); //NOI18N
128: if (i == row)
129: continue;
130: if (debug)
131: log("\tKeep this row"); //NOI18N
132: data2[newRowIndex] = data[i];
133: newRowIndex++;
134: if (debug)
135: log("\tnewRowIndex is " + newRowIndex); //NOI18N
136: }
137: data = data2;
138: numRows = --numRows;
139: }
140:
141: public void setData(String name, String value, boolean required,
142: boolean rtexpr, int row) {
143: data[row][0] = name;
144: data[row][1] = value;
145: data[row][2] = Boolean.valueOf(required);
146: data[row][3] = Boolean.valueOf(rtexpr);
147: fireTableChanged(new TableModelEvent(this , row));
148: }
149:
150: public void setValueAt(Object value, int row, int col) {
151:
152: if (debug)
153: log("::setValueAt(): value = " + value + //NOI18N
154: " at " + row + ", " + col); //NOI18N
155:
156: data[row][col] = value;
157:
158: if (debug) {
159: for (int i = 0; i < data.length; ++i) {
160: for (int j = 0; j < numCols; ++j) {
161: log("\t" + String.valueOf(i) + "," + //NOI18N
162: String.valueOf(j) + ": " + data[i][j]); //NOI18N
163: }
164: }
165: }
166: // Commenting this out since the value is set twice.
167: fireTableCellUpdated(row, col);
168: }
169:
170: private void log(String s) {
171: System.out.println("AttrTableModel:" + s); //NOI18N
172: }
173:
174: public Object[][] getAttributes() {
175: if (data == null)
176: return new Object[][] {};
177: else
178: return data;
179: }
180: }
|