1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.lang.reflect.Constructor ; 22 import java.util.List ; 23 24 import javax.swing.table.DefaultTableModel ; 25 26 import org.apache.jorphan.collections.Data; 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 30 34 public class PowerTableModel extends DefaultTableModel 35 { 36 private static Logger log = LoggingManager.getLoggerForClass(); 37 Data model = new Data(); 38 Class [] columnClasses; 39 40 public PowerTableModel(String [] headers, Class [] cc) 41 { 42 model.setHeaders(headers); 43 columnClasses = cc; 44 } 45 46 public PowerTableModel() 47 { 48 } 49 50 public void setRowValues(int row, Object [] values) 51 { 52 model.setCurrentPos(row); 53 for (int i = 0; i < values.length; i++) 54 { 55 model.addColumnValue(model.getHeaders()[i], values[i]); 56 } 57 } 58 59 public Data getData() 60 { 61 return model; 62 } 63 64 public void addNewColumn(String colName, Class colClass) 65 { 66 model.addHeader(colName); 67 Class [] newClasses = new Class [columnClasses.length + 1]; 68 System.arraycopy(columnClasses, 0, newClasses, 0, columnClasses.length); 69 newClasses[newClasses.length - 1] = colClass; 70 columnClasses = newClasses; 71 Object defaultValue = createDefaultValue(columnClasses.length - 1); 72 model.setColumnData(colName, defaultValue); 73 this.fireTableStructureChanged(); 74 } 75 76 public void removeRow(int row) 77 { 78 log.debug("remove row: " + row); 79 if (model.size() > row) 80 { 81 log.debug("Calling remove row on Data"); 82 model.removeRow(row); 83 } 84 } 85 86 public void removeColumn(int col) 87 { 88 model.removeColumn(col); 89 this.fireTableStructureChanged(); 90 } 91 92 public void setColumnData(int col, List data) 93 { 94 model.setColumnData(col, data); 95 } 96 97 public List getColumnData(String colName) 98 { 99 return model.getColumnAsObjectArray(colName); 100 } 101 102 public void clearData() 103 { 104 String [] headers = model.getHeaders(); 105 model = new Data(); 106 model.setHeaders(headers); 107 this.fireTableDataChanged(); 108 } 109 110 public void addRow(Object data[]) 111 { 112 model.setCurrentPos(model.size()); 113 for (int i = 0; i < data.length; i++) 114 { 115 model.addColumnValue(model.getHeaders()[i], data[i]); 116 } 117 } 118 119 public void addNewRow() 120 { 121 addRow(createDefaultRow()); 122 } 123 124 private Object [] createDefaultRow() 125 { 126 Object [] rowData = new Object [getColumnCount()]; 127 for (int i = 0; i < rowData.length; i++) 128 { 129 rowData[i] = createDefaultValue(i); 130 } 131 return rowData; 132 } 133 134 public Object [] getRowData(int row) 135 { 136 Object [] rowData = new Object [getColumnCount()]; 137 for (int i = 0; i < rowData.length; i++) 138 { 139 rowData[i] = model.getColumnValue(i, row); 140 } 141 return rowData; 142 } 143 144 private Object createDefaultValue(int i) 145 { 146 Class colClass = getColumnClass(i); 147 try 148 { 149 return colClass.newInstance(); 150 } 151 catch (Exception e) 152 { 153 try 154 { 155 Constructor constr = 156 colClass.getConstructor(new Class [] { String .class }); 157 return constr.newInstance(new Object [] { "" }); 158 } 159 catch (Exception err) 160 { 161 } 162 try 163 { 164 Constructor constr = 165 colClass.getConstructor(new Class [] { Integer.TYPE }); 166 return constr.newInstance(new Object [] { new Integer (0)}); 167 } 168 catch (Exception err) 169 { 170 } 171 try 172 { 173 Constructor constr = 174 colClass.getConstructor(new Class [] { Long.TYPE }); 175 return constr.newInstance(new Object [] { new Long (0L)}); 176 } 177 catch (Exception err) 178 { 179 } 180 try 181 { 182 Constructor constr = 183 colClass.getConstructor(new Class [] { Boolean.TYPE }); 184 return constr.newInstance(new Object [] { Boolean.FALSE}); 185 } 186 catch (Exception err) 187 { 188 } 189 try 190 { 191 Constructor constr = 192 colClass.getConstructor(new Class [] { Float.TYPE }); 193 return constr.newInstance(new Object [] { new Float (0F)}); 194 } 195 catch (Exception err) 196 { 197 } 198 try 199 { 200 Constructor constr = 201 colClass.getConstructor(new Class [] { Double.TYPE }); 202 return constr.newInstance(new Object [] { new Double (0D)}); 203 } 204 catch (Exception err) 205 { 206 } 207 try 208 { 209 Constructor constr = 210 colClass.getConstructor(new Class [] { Character.TYPE }); 211 return constr.newInstance(new Object [] { new Character (' ')}); 212 } 213 catch (Exception err) 214 { 215 } 216 try 217 { 218 Constructor constr = 219 colClass.getConstructor(new Class [] { Byte.TYPE }); 220 return constr.newInstance( 221 new Object [] { new Byte (Byte.MIN_VALUE)}); 222 } 223 catch (Exception err) 224 { 225 } 226 try 227 { 228 Constructor constr = 229 colClass.getConstructor(new Class [] { Short.TYPE }); 230 return constr.newInstance( 231 new Object [] { new Short (Short.MIN_VALUE)}); 232 } 233 catch (Exception err) 234 { 235 } 236 } 237 return ""; 238 } 239 240 245 public int getRowCount() 246 { 247 if (model == null) 248 { 249 return 0; 250 } 251 return model.size(); 252 } 253 254 259 public int getColumnCount() 260 { 261 return model.getHeaders().length; 262 } 263 264 269 public String getColumnName(int column) 270 { 271 return model.getHeaders()[column]; 272 } 273 274 public boolean isCellEditable(int row, int column) 275 { 276 return true; 278 } 279 280 public Class getColumnClass(int column) 281 { 282 return columnClasses[column]; 283 } 284 285 289 public Object getValueAt(int row, int column) 290 { 291 return model.getColumnValue(column, row); 292 } 293 294 299 public void setValueAt(Object value, int row, int column) 300 { 301 if (row < model.size()) 302 { 303 model.setCurrentPos(row); 304 model.addColumnValue(model.getHeaders()[column], value); 305 } 306 } 307 } | Popular Tags |