View Javadoc
1 package edu.psu.geovista.category; 2 3 import javax.swing.*; 4 import javax.swing.table.*; 5 import java.util.Vector; 6 7 class InfoModel extends AbstractTableModel { 8 protected static int NUM_COLUMNS = 4; 9 protected static int START_NUM_ROWS = 5; 10 protected int nextEmptyRow = 0; 11 protected int numRows = 0; 12 13 static final public String userNameStr = "UserInfo"; 14 static final public String preferedColorStr = "PreferedColor"; 15 static final public String recordsStr = "Records"; 16 static final public String exampleTypeStr = "ExampleType"; 17 18 protected Vector data = null; 19 20 public InfoModel() { 21 data = new Vector(); 22 } 23 24 public String getColumnName(int column) { 25 switch (column) { 26 case 0: 27 return userNameStr; 28 case 1: 29 return preferedColorStr; 30 case 2: 31 return recordsStr; 32 case 3: 33 return exampleTypeStr; 34 } 35 return ""; 36 } 37 38 //XXX Should this really be synchronized? 39 public synchronized int getColumnCount() { 40 return NUM_COLUMNS; 41 } 42 43 public synchronized int getRowCount() { 44 /*if (numRows < START_NUM_ROWS) { 45 return START_NUM_ROWS; 46 } else { 47 return numRows; 48 }*/ 49 return data.size(); 50 } 51 52 /* 53 * JTable uses this method to determine the default renderer/ 54 * editor for each cell. If we didn't implement this method, 55 * then the last column would contain text ("true"/"false"), 56 * rather than a check box. 57 */ 58 public Class getColumnClass(int c) { 59 return getValueAt(0, c).getClass(); 60 } 61 62 public boolean isCellEditable(int row, int col) { 63 //Note that the data/cell address is constant, 64 //no matter where the cell appears onscreen. 65 if (col < 1) { 66 return false; 67 } else { 68 return true; 69 } 70 } 71 72 /* 73 * Don't need to implement this method unless your table's 74 * data can change. 75 */ 76 public void setValueAt(Object value, int row, int col) { 77 78 if (((Vector)data.elementAt(0)).get(col) instanceof Integer 79 && !(value instanceof Integer)) { 80 try { 81 //data[row][col] = new Integer(value.toString()); 82 //((Vector)data.elementAt(row)).set(col, new Integer(value.toString())); 83 //fireTableCellUpdated(row, col); 84 } catch (NumberFormatException e) { 85 //JOptionPane.showMessageDialog(CategoryRecords.this, 86 // "The \"" + getColumnName(col) 87 // + "\" column accepts only integer values."); 88 } 89 } else { 90 //data[row][col] = value; 91 ((Vector)data.elementAt(row)).set(col, value); 92 fireTableCellUpdated(row, col); 93 } 94 } 95 96 public synchronized Object getValueAt(int row, int column) { 97 try { 98 Vector p = (Vector)data.elementAt(row); 99 return p.get(column); 100 } catch (Exception e) { 101 } 102 return ""; 103 } 104 105 public void deleteRows (int[] rowsSelected){ 106 if (rowsSelected == null){ 107 return; 108 } 109 int[] rowsDeleted = rowsSelected; 110 for (int i=0; i < rowsDeleted.length; i++){ 111 data.removeElementAt(rowsDeleted[i]); 112 fireTableRowsDeleted(rowsDeleted[i], rowsDeleted[i]); 113 } 114 } 115 116 public synchronized void updateRecord(Vector selectionRecord) { 117 String userInfo = (String) selectionRecord.get(0); //find the ID 118 Vector p = null; 119 int index = -1; 120 boolean found = false; 121 boolean addedRow = false; 122 123 if (found) { //update old player 124 data.setElementAt(selectionRecord, index); 125 } else { //add new player 126 if (numRows <= nextEmptyRow) { 127 //add a row 128 numRows++; 129 addedRow = true; 130 } 131 index = nextEmptyRow; 132 data.addElement(selectionRecord.clone()); 133 } 134 135 nextEmptyRow++; 136 137 fireTableRowsInserted(index, index); 138 } 139 140 public synchronized void clear() { 141 int oldNumRows = numRows; 142 143 numRows = START_NUM_ROWS; 144 data.removeAllElements(); 145 nextEmptyRow = 0; 146 147 if (oldNumRows > START_NUM_ROWS) { 148 fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1); 149 } 150 fireTableRowsUpdated(0, START_NUM_ROWS - 1); 151 } 152 }

This page was automatically generated by Maven