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: package org.netbeans.modules.vmd.midpnb.propertyeditors.table;
042:
043: import java.util.Vector;
044: import javax.swing.table.DefaultTableModel;
045:
046: /**
047: *
048: * @author Anton Chechel
049: */
050: class CustomEditorTableModel extends DefaultTableModel {
051:
052: private Vector<String> header = new Vector<String>();
053: private boolean hasHeader;
054:
055: public void removeLastColumn() {
056: if (hasHeader && header.size() > 0) {
057: header.remove(header.size() - 1);
058: }
059:
060: int columnCount = getColumnCount();
061: if (columnCount > 0) {
062: int size = dataVector.size();
063: for (int i = 0; i < size; i++) {
064: Vector row = (Vector) dataVector.elementAt(i);
065: row.remove(columnCount - 1);
066: }
067: }
068: fireTableStructureChanged();
069: }
070:
071: @Override
072: public void removeRow(int row) {
073: dataVector.removeElementAt(row);
074: fireTableStructureChanged();
075: }
076:
077: @Override
078: @SuppressWarnings(value="unchecked")
079: // NOI18N
080: public void addRow(Object[] rowData) {
081: dataVector.addElement(convertToVector(rowData));
082: fireTableStructureChanged();
083: }
084:
085: @SuppressWarnings(value="unchecked")
086: // NOI18N
087: public void addColumn(String columnName, boolean needRow) {
088: if (hasHeader) {
089: header.addElement(columnName);
090: }
091:
092: if (dataVector.size() > 0) {
093: for (int i = 0; i < dataVector.size(); i++) {
094: Vector row = (Vector) dataVector.elementAt(i);
095: row.addElement(columnName);
096: }
097: } else if (needRow) {
098: Vector row = new Vector(1);
099: row.add(columnName);
100: dataVector.addElement(row);
101: }
102: fireTableStructureChanged();
103: }
104:
105: @Override
106: public int getRowCount() {
107: return dataVector.size() + (hasHeader ? 1 : 0);
108: }
109:
110: @Override
111: public int getColumnCount() {
112: if (hasHeader) {
113: return header.size();
114: } else if (dataVector.size() > 0) {
115: return ((Vector) dataVector.get(0)).size();
116: }
117: return 0;
118: }
119:
120: @Override
121: public Object getValueAt(int row, int column) {
122: Object value;
123: if (hasHeader) {
124: if (row == 0) {
125: value = header.elementAt(column);
126: } else {
127: value = super .getValueAt(row - 1, column);
128: }
129: } else {
130: value = super .getValueAt(row, column);
131: }
132: return value;
133: }
134:
135: @Override
136: public void setValueAt(Object aValue, int row, int column) {
137: if (hasHeader) {
138: if (row == 0) {
139: header.setElementAt((String) aValue, column);
140: fireTableStructureChanged();
141: } else {
142: super .setValueAt(aValue, row - 1, column);
143: }
144: } else {
145: super .setValueAt(aValue, row, column);
146: }
147: }
148:
149: @Override
150: public void setDataVector(Object[][] dataArrays,
151: Object[] columnArray) {
152: if (hasHeader) {
153: header.clear();
154: for (int i = 0; i < columnArray.length; i++) {
155: header.addElement((String) columnArray[i]);
156: }
157: }
158: dataVector = nonNullVector(convertToVector(dataArrays));
159: fireTableStructureChanged();
160: }
161:
162: private static Vector nonNullVector(Vector v) {
163: return (v != null) ? v : new Vector();
164: }
165:
166: public void clear() {
167: dataVector.clear();
168: fireTableStructureChanged();
169: }
170:
171: public void setUseHeader(boolean useHeader) {
172: if (useHeader && header.size() != getColumnCount()) {
173: header.clear();
174: for (int i = 0; i < getColumnCount(); i++) {
175: header.addElement(""); // NOI18N
176: }
177: }
178: this .hasHeader = useHeader;
179: fireTableStructureChanged();
180: }
181:
182: public boolean hasHeader() {
183: return hasHeader;
184: }
185:
186: public Vector<String> getHeader() {
187: return header;
188: }
189: }
|