View Javadoc
1 /* 2 * PropertyTable.java 3 * 4 * Copyright (C) 2002-2003, Mark Diggory 5 * 6 * This file is part of the Repast Taglibrary Package for use with Jelly. 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. License 21 * information is also available at http://www.gnu.org. 22 */ 23 24 package org.mrd.repast.util; 25 26 import java.util.*; 27 import javax.swing.*; 28 import javax.swing.table.*; 29 import java.awt.*; 30 import java.awt.event.*; 31 32 /*** 33 * 34 * @author administrator 35 */ 36 public class ArrayWidget extends JPanel implements uchicago.src.reflector.PropertyWidget { 37 38 private ArrayList listeners = new ArrayList(); 39 40 protected String propertyName = null; 41 42 protected JTable table = null; 43 44 protected ArrayTableModel model = null; 45 46 protected JButton add = new JButton(new ImageIcon(ArrayWidget.class.getResource("plus.gif"))); 47 48 protected JButton remove = new JButton(new ImageIcon(ArrayWidget.class.getResource("minus.gif"))); 49 50 /*** Creates a new instance of PropertyTable */ 51 public ArrayWidget(Object obj) { 52 super(new BorderLayout()); 53 model = new ArrayTableModel(obj); 54 table = new JTable(model); 55 56 57 JPanel tpanel = new JPanel(new BorderLayout()); 58 tpanel.add(table.getTableHeader(), BorderLayout.NORTH); 59 tpanel.add(table,BorderLayout.CENTER); 60 this.add(tpanel,BorderLayout.CENTER); 61 62 add.setPreferredSize(new Dimension(16,16)); 63 remove.setPreferredSize(new Dimension(16,16)); 64 65 add.addActionListener(new ActionListener() { 66 public void actionPerformed(ActionEvent evt) { 67 System.out.print("adding column: "); 68 addColumn(); 69 System.out.println(model.data.toString()); 70 fireActionPerformed(); 71 } 72 }); 73 74 remove.addActionListener(new ActionListener() { 75 public void actionPerformed(ActionEvent evt) { 76 System.out.print("removing column: "); 77 removeColumn(); 78 System.out.println(model.data.toString()); 79 fireActionPerformed(); 80 } 81 }); 82 83 JPanel buttons = new JPanel(); 84 buttons.setLayout(new BoxLayout(buttons,BoxLayout.Y_AXIS)); 85 buttons.add(add); 86 buttons.add(remove); 87 this.add(buttons, BorderLayout.EAST); 88 } 89 90 protected void addColumn(){ 91 92 Object obj = null; 93 94 if(model.data instanceof int[]){ 95 int[] src = (int[])model.getData(); 96 int[] dst = new int[src.length + 1]; 97 System.arraycopy(src,0,dst,0,src.length); 98 obj = (Object)dst; 99 }else if(model.data instanceof long[]){ 100 long[] src = (long[])model.getData(); 101 long[] dst = new long[src.length + 1]; 102 System.arraycopy(src,0,dst,0,src.length); 103 obj = (Object)dst; 104 }else if(model.data instanceof float[]){ 105 float[] src = (float[])model.getData(); 106 float[] dst = new float[src.length + 1]; 107 System.arraycopy(src,0,dst,0,src.length); 108 obj = (Object)dst; 109 }else if(model.data instanceof double[]){ 110 double[] src = (double[])model.getData(); 111 double[] dst = new double[src.length + 1]; 112 System.arraycopy(src,0,dst,0,src.length); 113 obj = (Object)dst; 114 }else if(model.data instanceof short[]){ 115 short[] src = (short[])model.getData(); 116 short[] dst = new short[src.length + 1]; 117 System.arraycopy(src,0,dst,0,src.length); 118 obj = (Object)dst; 119 }else if(model.data instanceof char[]){ 120 char[] src = (char[])model.getData(); 121 char[] dst = new char[src.length + 1]; 122 System.arraycopy(src,0,dst,0,src.length); 123 obj = (Object)dst; 124 }else if(model.data instanceof byte[]){ 125 byte[] src = (byte[])model.getData(); 126 byte[] dst = new byte[src.length + 1]; 127 System.arraycopy(src,0,dst,0,src.length); 128 obj = (Object)dst; 129 }else if(model.data instanceof boolean[]){ 130 boolean[] src = (boolean[])model.getData(); 131 boolean[] dst = new boolean[src.length + 1]; 132 System.arraycopy(src,0,dst,0,src.length); 133 obj = (Object)dst; 134 }else{ 135 /* maybe I can get around this issue with reflection */ 136 Object[] src = (Object[])model.getData(); 137 Object[] dst = (Object[])java.lang.reflect.Array.newInstance(src[0].getClass(),src.length + 1); 138 System.arraycopy(src,0,dst,0,src.length); 139 obj = (Object)dst; 140 } 141 this.model = new ArrayTableModel(obj); 142 table.setModel(model); 143 } 144 145 protected void removeColumn(){ 146 147 Object obj = null; 148 if(model.data instanceof int[]){ 149 int[] src = (int[])model.getData(); 150 int[] dst = new int[(src.length > 1) ? src.length - 1 : src.length]; 151 System.arraycopy(src,0,dst,0,dst.length); 152 obj = (Object)dst; 153 }else if(model.data instanceof long[]){ 154 long[] src = (long[])model.getData(); 155 long[] dst = new long[(src.length > 1) ? src.length - 1 : src.length]; 156 System.arraycopy(src,0,dst,0,dst.length); 157 obj = (Object)dst; 158 }else if(model.data instanceof float[]){ 159 float[] src = (float[])model.getData(); 160 float[] dst = new float[(src.length > 1) ? src.length - 1 : src.length]; 161 System.arraycopy(src,0,dst,0,dst.length); 162 obj = (Object)dst; 163 }else if(model.data instanceof double[]){ 164 double[] src = (double[])model.getData(); 165 double[] dst = new double[(src.length > 1) ? src.length - 1 : src.length]; 166 System.arraycopy(src,0,dst,0,dst.length); 167 obj = (Object)dst; 168 }else if(model.data instanceof short[]){ 169 short[] src = (short[])model.getData(); 170 short[] dst = new short[(src.length > 1) ? src.length - 1 : src.length]; 171 System.arraycopy(src,0,dst,0,dst.length); 172 obj = (Object)dst; 173 }else if(model.data instanceof char[]){ 174 char[] src = (char[])model.getData(); 175 char[] dst = new char[(src.length > 1) ? src.length - 1 : src.length]; 176 System.arraycopy(src,0,dst,0,dst.length); 177 obj = (Object)dst; 178 }else if(model.data instanceof byte[]){ 179 byte[] src = (byte[])model.getData(); 180 byte[] dst = new byte[(src.length > 1) ? src.length - 1 : src.length]; 181 System.arraycopy(src,0,dst,0,dst.length); 182 obj = (Object)dst; 183 }else if(model.data instanceof boolean[]){ 184 boolean[] src = (boolean[])model.getData(); 185 boolean[] dst = new boolean[(src.length > 1) ? src.length - 1 : src.length]; 186 System.arraycopy(src,0,dst,0,dst.length); 187 obj = (Object)dst; 188 }else{ 189 Object[] src = (Object[])model.getData(); 190 Object[] dst = (Object[])java.lang.reflect.Array.newInstance(src[0].getClass(),(src.length > 1) ? src.length - 1 : src.length); 191 System.arraycopy(src,0,dst,0,dst.length); 192 obj = (Object)dst; 193 } 194 195 this.model = new ArrayTableModel(obj); 196 table.setModel(model); 197 } 198 199 public String getPropertyName() { 200 return propertyName; 201 } 202 203 public Object getValue() { 204 return this.model.getData(); 205 } 206 207 public void setPropertyName(String str) { 208 propertyName = str; 209 } 210 211 public void setValue(Object obj) { 212 this.model = new ArrayTableModel(obj); 213 table.setModel(model); 214 } 215 216 public void requestFocus() { 217 table.requestFocus(); 218 } 219 220 public void setEnabled(boolean b) { 221 table.setEnabled(b); 222 add.setEnabled(b); 223 remove.setEnabled(b); 224 model.setEditable(b); 225 } 226 227 public void addActionListener(ActionListener l) { 228 listeners.add(l); 229 } 230 231 private void fireActionPerformed() { 232 ArrayList list; 233 synchronized (listeners) { 234 list = (ArrayList)listeners.clone(); 235 } 236 237 for (int i = 0; i < list.size(); i++) { 238 ActionListener l = (ActionListener)list.get(i); 239 l.actionPerformed(new ActionEvent(this, 0, "action")); 240 } 241 } 242 243 /*** Getter for property table. 244 * @return Value of property table. 245 */ 246 public JTable getTable() { 247 return this.table; 248 } 249 250 /*** Getter for property model. 251 * @return Value of property model. 252 */ 253 public TableModel getModel() { 254 return this.model; 255 } 256 257 public class ArrayTableModel extends javax.swing.table.AbstractTableModel{ 258 259 private Object data; 260 261 /*** Holds value of property editable. */ 262 private boolean editable = true; 263 264 public ArrayTableModel(Object data){ 265 this.data = data; 266 } 267 268 public String getColumnName(int column){ 269 return Integer.toString(column); 270 } 271 272 public int getColumnCount() { 273 if(data != null){ 274 if(data instanceof int[]){ 275 return ((int[])data).length; 276 }else if(data instanceof long[]){ 277 return ((long[])data).length; 278 }else if(data instanceof float[]){ 279 return ((float[])data).length; 280 }else if(data instanceof double[]){ 281 return ((double[])data).length; 282 }else if(data instanceof short[]){ 283 return ((short[])data).length; 284 }else if(data instanceof char[]){ 285 return ((char[])data).length; 286 }else if(data instanceof byte[]){ 287 return ((byte[])data).length; 288 }else if(data instanceof boolean[]){ 289 return ((boolean[])data).length; 290 }else{ 291 return ((Object[])data).length; 292 } 293 }else{ 294 return 0; 295 } 296 } 297 298 public int getRowCount() { 299 return 1; 300 } 301 302 public boolean isCellEditable(int row, int col){ 303 return editable; 304 } 305 306 public Object getValueAt(int row, int col) { 307 308 if(data instanceof int[]){ 309 return new Integer(((int[])data)[col]); 310 }else if(data instanceof long[]){ 311 return new Long(((long[])data)[col]); 312 }else if(data instanceof float[]){ 313 return new Float(((float[])data)[col]); 314 }else if(data instanceof double[]){ 315 return new Double(((double[])data)[col]); 316 }else if(data instanceof short[]){ 317 return new Short(((short[])data)[col]); 318 }else if(data instanceof byte[]){ 319 return new Byte(((byte[])data)[col]); 320 }else if(data instanceof boolean[]){ 321 return new Boolean(((boolean[])data)[col]); 322 }else if(data instanceof char[]){ 323 return new Character(((char[])data)[col]).toString(); 324 }else if(data instanceof Character[]){ 325 return (((Character[])data)[col]).toString(); 326 }else{ 327 return ((Object[])data)[col]; 328 } 329 } 330 331 /* 332 * Don't need to implement this method unless your table's 333 * data can change. 334 */ 335 public void setValueAt(Object value, int row, int col) { 336 try{ 337 if(data instanceof int[]){ 338 ((int[])data)[col] = ((Integer)value).intValue(); 339 }else if(data instanceof long[]){ 340 ((long[])data)[col] = ((Long)value).longValue(); 341 }else if(data instanceof float[]){ 342 ((float[])data)[col] = ((Float)value).floatValue(); 343 }else if(data instanceof double[]){ 344 ((double[])data)[col] = ((Double)value).doubleValue(); 345 }else if(data instanceof short[]){ 346 ((short[])data)[col] = ((Short)value).shortValue(); 347 }else if(data instanceof byte[]){ 348 ((byte[])data)[col] = ((Byte)value).byteValue(); 349 }else if(data instanceof boolean[]){ 350 ((boolean[])data)[col] = ((Boolean)value).booleanValue(); 351 }else if(data instanceof char[]){ 352 ((char[])data)[col] = ((String)value).charAt(0); 353 }else if(data instanceof Character[]){ 354 ((Character[])data)[col] = new Character(((String)value).charAt(0)); 355 }else{ 356 ((Object[])data)[col] = value; 357 } 358 fireTableCellUpdated(row, col); 359 } catch (NumberFormatException e) { 360 JOptionPane.showMessageDialog(ArrayWidget.this, 361 "Bad Number Format: " + e.getMessage() 362 ); 363 } 364 } 365 366 public Class getColumnClass(int c) { 367 if(data instanceof int[]){ 368 return Integer.class; 369 }else if(data instanceof long[]){ 370 return Long.class; 371 }else if(data instanceof float[]){ 372 return Float.class; 373 }else if(data instanceof double[]){ 374 return Double.class; 375 }else if(data instanceof short[]){ 376 return Short.class; 377 }else if(data instanceof char[]){ 378 return String.class; 379 }else if(data instanceof Character[]){ 380 return String.class; 381 }else if(data instanceof byte[]){ 382 return Byte.class; 383 }else if(data instanceof boolean[]){ 384 return Boolean.class; 385 }else{ 386 return getValueAt(0, 0).getClass(); 387 } 388 } 389 /*** Getter for property data. 390 * @return Value of property data. 391 */ 392 public Object getData() { 393 return this.data; 394 } 395 396 /*** Setter for property data. 397 * @param data New value of property data. 398 */ 399 public void setData(Object data) { 400 this.data = data; 401 } 402 403 /*** Getter for property editable. 404 * @return Value of property editable. 405 */ 406 public boolean isEditable() { 407 return this.editable; 408 } 409 410 /*** Setter for property editable. 411 * @param editable New value of property editable. 412 */ 413 public void setEditable(boolean editable) { 414 this.editable = editable; 415 } 416 417 } 418 }

This page was automatically generated by Maven