001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.util;
020:
021: import java.lang.reflect.Constructor;
022: import java.lang.reflect.InvocationTargetException;
023: import java.util.List;
024:
025: import javax.swing.table.DefaultTableModel;
026:
027: import org.apache.jorphan.collections.Data;
028: import org.apache.jorphan.logging.LoggingManager;
029: import org.apache.log.Logger;
030:
031: /**
032: * @author mstover
033: */
034: public class PowerTableModel extends DefaultTableModel {
035: private static final Logger log = LoggingManager
036: .getLoggerForClass();
037:
038: Data model = new Data();
039:
040: Class[] columnClasses;
041:
042: public PowerTableModel(String[] headers, Class[] cc) {
043: if (headers.length != cc.length) {
044: throw new IllegalArgumentException(
045: "Header and column array sizes differ");
046: }
047: model.setHeaders(headers);
048: columnClasses = cc;
049: }
050:
051: public PowerTableModel() {
052: }
053:
054: public void setRowValues(int row, Object[] values) {
055: if (values.length != model.getHeaderCount()) {
056: throw new IllegalArgumentException(
057: "Incorrect number of data items");
058: }
059: model.setCurrentPos(row);
060: for (int i = 0; i < values.length; i++) {
061: model.addColumnValue(model.getHeaders()[i], values[i]);
062: }
063: }
064:
065: public Data getData() {
066: return model;
067: }
068:
069: public void addNewColumn(String colName, Class colClass) {
070: model.addHeader(colName);
071: Class[] newClasses = new Class[columnClasses.length + 1];
072: System.arraycopy(columnClasses, 0, newClasses, 0,
073: columnClasses.length);
074: newClasses[newClasses.length - 1] = colClass;
075: columnClasses = newClasses;
076: Object defaultValue = createDefaultValue(columnClasses.length - 1);
077: model.setColumnData(colName, defaultValue);
078: this .fireTableStructureChanged();
079: }
080:
081: public void removeRow(int row) {
082: log.debug("remove row: " + row);
083: if (model.size() > row) {
084: log.debug("Calling remove row on Data");
085: model.removeRow(row);
086: }
087: }
088:
089: public void removeColumn(int col) {
090: model.removeColumn(col);
091: this .fireTableStructureChanged();
092: }
093:
094: public void setColumnData(int col, List data) {
095: model.setColumnData(col, data);
096: }
097:
098: public List getColumnData(String colName) {
099: return model.getColumnAsObjectArray(colName);
100: }
101:
102: public void clearData() {
103: String[] headers = model.getHeaders();
104: model = new Data();
105: model.setHeaders(headers);
106: this .fireTableDataChanged();
107: }
108:
109: public void addRow(Object data[]) {
110: if (data.length != model.getHeaderCount()) {
111: throw new IllegalArgumentException(
112: "Incorrect number of data items");
113: }
114: model.setCurrentPos(model.size());
115: for (int i = 0; i < data.length; i++) {
116: model.addColumnValue(model.getHeaders()[i], data[i]);
117: }
118: }
119:
120: public void addNewRow() {
121: addRow(createDefaultRow());
122: }
123:
124: private Object[] createDefaultRow() {
125: Object[] rowData = new Object[getColumnCount()];
126: for (int i = 0; i < rowData.length; i++) {
127: rowData[i] = createDefaultValue(i);
128: }
129: return rowData;
130: }
131:
132: public Object[] getRowData(int row) {
133: Object[] rowData = new Object[getColumnCount()];
134: for (int i = 0; i < rowData.length; i++) {
135: rowData[i] = model.getColumnValue(i, row);
136: }
137: return rowData;
138: }
139:
140: private Object createDefaultValue(int i) {
141: Class colClass = getColumnClass(i);
142: try {
143: return colClass.newInstance();
144: } catch (Exception e) {
145: try {
146: Constructor constr = colClass
147: .getConstructor(new Class[] { String.class });
148: return constr.newInstance(new Object[] { "" });
149: } catch (NoSuchMethodException err) {
150: } catch (InstantiationException err) {
151: } catch (IllegalAccessException err) {
152: } catch (InvocationTargetException err) {
153: }
154: try {
155: Constructor constr = colClass
156: .getConstructor(new Class[] { Integer.TYPE });
157: return constr
158: .newInstance(new Object[] { new Integer(0) });
159: } catch (NoSuchMethodException err) {
160: } catch (InstantiationException err) {
161: } catch (IllegalAccessException err) {
162: } catch (InvocationTargetException err) {
163: }
164: try {
165: Constructor constr = colClass
166: .getConstructor(new Class[] { Long.TYPE });
167: return constr
168: .newInstance(new Object[] { new Long(0L) });
169: } catch (NoSuchMethodException err) {
170: } catch (InstantiationException err) {
171: } catch (IllegalAccessException err) {
172: } catch (InvocationTargetException err) {
173: }
174: try {
175: Constructor constr = colClass
176: .getConstructor(new Class[] { Boolean.TYPE });
177: return constr
178: .newInstance(new Object[] { Boolean.FALSE });
179: } catch (NoSuchMethodException err) {
180: } catch (InstantiationException err) {
181: } catch (IllegalAccessException err) {
182: } catch (InvocationTargetException err) {
183: }
184: try {
185: Constructor constr = colClass
186: .getConstructor(new Class[] { Float.TYPE });
187: return constr
188: .newInstance(new Object[] { new Float(0F) });
189: } catch (NoSuchMethodException err) {
190: } catch (InstantiationException err) {
191: } catch (IllegalAccessException err) {
192: } catch (InvocationTargetException err) {
193: }
194: try {
195: Constructor constr = colClass
196: .getConstructor(new Class[] { Double.TYPE });
197: return constr
198: .newInstance(new Object[] { new Double(0D) });
199: } catch (NoSuchMethodException err) {
200: } catch (InstantiationException err) {
201: } catch (IllegalAccessException err) {
202: } catch (InvocationTargetException err) {
203: }
204: try {
205: Constructor constr = colClass
206: .getConstructor(new Class[] { Character.TYPE });
207: return constr.newInstance(new Object[] { new Character(
208: ' ') });
209: } catch (NoSuchMethodException err) {
210: } catch (InstantiationException err) {
211: } catch (IllegalAccessException err) {
212: } catch (InvocationTargetException err) {
213: }
214: try {
215: Constructor constr = colClass
216: .getConstructor(new Class[] { Byte.TYPE });
217: return constr.newInstance(new Object[] { new Byte(
218: Byte.MIN_VALUE) });
219: } catch (NoSuchMethodException err) {
220: } catch (InstantiationException err) {
221: } catch (IllegalAccessException err) {
222: } catch (InvocationTargetException err) {
223: }
224: try {
225: Constructor constr = colClass
226: .getConstructor(new Class[] { Short.TYPE });
227: return constr.newInstance(new Object[] { new Short(
228: Short.MIN_VALUE) });
229: } catch (NoSuchMethodException err) {
230: } catch (InstantiationException err) {
231: } catch (IllegalAccessException err) {
232: } catch (InvocationTargetException err) {
233: }
234: }
235: return "";
236: }
237:
238: /**
239: * Required by table model interface.
240: *
241: * @return the RowCount value
242: */
243: public int getRowCount() {
244: if (model == null) {
245: return 0;
246: }
247: return model.size();
248: }
249:
250: /**
251: * Required by table model interface.
252: *
253: * @return the ColumnCount value
254: */
255: public int getColumnCount() {
256: return model.getHeaders().length;
257: }
258:
259: /**
260: * Required by table model interface.
261: *
262: * @return the ColumnName value
263: */
264: public String getColumnName(int column) {
265: return model.getHeaders()[column];
266: }
267:
268: public boolean isCellEditable(int row, int column) {
269: // all table cells are editable
270: return true;
271: }
272:
273: public Class getColumnClass(int column) {
274: return columnClasses[column];
275: }
276:
277: /**
278: * Required by table model interface. return the ValueAt value
279: */
280: public Object getValueAt(int row, int column) {
281: return model.getColumnValue(column, row);
282: }
283:
284: /**
285: * Sets the ValueAt attribute of the Arguments object.
286: *
287: * @param value
288: * the new ValueAt value
289: */
290: public void setValueAt(Object value, int row, int column) {
291: if (row < model.size()) {
292: model.setCurrentPos(row);
293: model.addColumnValue(model.getHeaders()[column], value);
294: }
295: }
296: }
|