1 7 8 package org.jdesktop.swing.table; 9 10 import java.beans.PropertyChangeListener ; 11 import java.beans.PropertyChangeSupport ; 12 13 import java.util.Iterator ; 14 15 import org.jdesktop.swing.data.Converter; 16 17 18 25 public class TabularDataMetaData { 26 private Column columns[]; 27 28 private PropertyChangeSupport pcs; 29 30 33 public TabularDataMetaData() { 34 this(0); 35 } 36 37 41 public TabularDataMetaData(int columnCount) { 42 pcs = new PropertyChangeSupport (this); 43 setColumnCount(columnCount); 44 } 45 46 52 public void addPropertyChangeListener(PropertyChangeListener listener) { 53 pcs.addPropertyChangeListener(listener); 54 } 55 56 60 public void removePropertyChangeListener(PropertyChangeListener listener) { 61 pcs.removePropertyChangeListener(listener); 62 } 63 64 69 public void setColumnCount(int columnCount) { 70 int oldColumnCount = columns != null? columns.length : 0; 71 columns = new Column[columnCount]; 72 for (int i = 0; i < columnCount; i++) { 73 columns[i] = new Column("column"+i, java.lang.String .class, true); 74 } 75 pcs.firePropertyChange("columnCount", oldColumnCount, columnCount); 76 } 77 78 81 public int getColumnCount() { 82 return columns.length; 83 } 84 85 public int getColumnIndex(String name) { 86 int index = 0; 87 for (int i = 0; i < columns.length; i++) { 88 if (columns[i].name.equals(name)) { 89 return i+1; 90 } 91 } 92 return index; 93 } 94 95 99 public String getColumnName(int columnIndex) { 100 return columns[columnIndex-1].name; 101 } 102 103 108 public void setColumnName(int columnIndex, String columnName) { 109 String oldColumnName = columns[columnIndex-1].name; 110 columns[columnIndex-1].name = columnName; 111 pcs.firePropertyChange("columnName"+columnIndex, oldColumnName, columnName); 112 } 113 114 118 public String getColumnLabel(int columnIndex) { 119 return columns[columnIndex-1].label; 120 } 121 122 129 public void setColumnLabel(int columnIndex, String columnLabel) { 130 String oldColumnLabel = columns[columnIndex-1].label; 131 columns[columnIndex-1].label = columnLabel; 132 pcs.firePropertyChange("columnLabel"+columnIndex, oldColumnLabel, columnLabel); 133 } 134 135 136 140 public Class getColumnClass(int columnIndex) { 141 return columns[columnIndex-1].klass; 142 } 143 144 149 public void setColumnClass(int columnIndex, Class columnClass) { 150 Class oldColumnClass = columns[columnIndex-1].klass; 151 columns[columnIndex-1].klass = columnClass; 152 pcs.firePropertyChange("columnClass"+columnIndex, oldColumnClass, columnClass); 153 } 154 155 public void setColumnDisplaySize(int columnIndex, int numChars) { 156 columns[columnIndex-1].displayLength = numChars; 157 } 158 159 public int getColumnDisplaySize(int columnIndex) { 160 return columns[columnIndex-1].displayLength; 161 } 162 163 167 public boolean isColumnWritable(int columnIndex) { 168 return columns[columnIndex-1].writable; 169 } 170 171 177 public void setColumnWritable(int columnIndex, boolean writable) { 178 columns[columnIndex-1].writable = writable; 179 } 180 181 185 public boolean isColumnNullable(int columnIndex) { 186 return columns[columnIndex-1].nullable; 187 } 188 189 195 public void setColumnNullable(int columnIndex, boolean nullable) { 196 columns[columnIndex-1].nullable = nullable; 197 } 198 199 204 public Object getColumnMinimum(int columnIndex) { 205 return columns[columnIndex-1].minimum; 206 } 207 208 215 public void setColumnMinimum(int columnIndex, Object minimum) { 216 columns[columnIndex-1].minimum = minimum; 217 } 218 219 224 public Object getColumnMaximum(int columnIndex) { 225 return columns[columnIndex-1].maximum; 226 } 227 228 235 public void setColumnMaximum(int columnIndex, Object maximum) { 236 columns[columnIndex-1].maximum = maximum; 237 } 238 239 244 public Iterator getColumnValues(int columnIndex) { 245 final Object values[] = new Object [(columns[columnIndex-1].values.length)]; 246 247 System.arraycopy(columns[columnIndex-1].values, 0, 248 values, 0, 249 columns[columnIndex-1].values.length); 250 251 return new Iterator () { 252 int current = 0; 253 public boolean hasNext() { 254 return current < values.length; 255 } 256 public Object next() { 257 return values[current++]; 258 } 259 public void remove() { 260 } 262 }; 263 } 264 265 272 public void setColumnValues(int columnIndex, Object values[]) { 273 Object newValues[] = new Object [values.length]; 274 System.arraycopy(values, 0, newValues, 0, values.length); 275 columns[columnIndex-1].values = newValues; 276 } 277 278 285 public void setColumnConverter(int columnIndex, Converter converter) { 286 columns[columnIndex-1].converter = converter; 287 } 288 289 294 public Converter getColumnConverter(int columnIndex) { 295 return columns[columnIndex-1].converter; 296 } 297 298 private class Column { 300 public String name; 301 public Class klass; 302 public String label; 303 public Converter converter; 304 public boolean writable = false; 305 public boolean nullable = false; 306 public int displayLength = -1; 307 public Object minimum; 308 public Object maximum; 309 public Object [] values; 310 311 public Column(String name, Class klass, boolean writable) { 312 this.name = name; 313 this.klass = klass; 314 this.writable = writable; 315 } 316 } 317 } 318 | Popular Tags |