1 7 8 package org.jdesktop.dataset; 9 import java.beans.PropertyChangeEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.beans.PropertyChangeSupport ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.LinkedHashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.logging.Logger ; 19 import org.jdesktop.dataset.NameGenerator; 20 import org.jdesktop.dataset.event.DataTableListener; 21 import org.jdesktop.dataset.event.TableChangeEvent; 22 23 24 25 29 public class DataTable { 30 33 private static final Logger LOG = Logger.getLogger(DataTable.class.getName()); 34 35 protected static final String DEFAULT_NAME_PREFIX = "DataTable"; 37 private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX); 38 39 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 42 43 46 private DataSet dataSet; 47 52 private DataProvider dataProvider; 53 56 private String name; 57 70 protected Map <String ,DataColumn> columns = new LinkedHashMap <String ,DataColumn>(); 71 75 protected List <DataRow> rows = new ArrayList <DataRow>(); 76 82 protected Map <String ,DataSelector> selectors = new HashMap <String ,DataSelector>(); 83 87 private boolean deleteRowSupported = true; 88 92 private boolean appendRowSupported = true; 93 96 private List <DataTableListener> listeners = new ArrayList <DataTableListener>(); 97 98 104 private final PropertyChangeListener nameChangeListener = new PropertyChangeListener () { 105 public void propertyChange(PropertyChangeEvent evt) { 106 if (evt.getSource() instanceof DataSelector) { 107 DataSelector sel = selectors.remove(evt.getOldValue()); 108 if (sel != null) { 109 selectors.put((String )evt.getNewValue(), sel); 111 } 112 } else if (evt.getSource() instanceof DataColumn) { 113 DataColumn c = columns.remove(evt.getOldValue()); 114 if (c != null) { 115 columns.put((String )evt.getNewValue(), c); 117 } 118 } 119 } 120 }; 121 122 125 protected DataTable(DataSet ds) { 126 assert ds != null; 127 this.dataSet = ds; 128 name = NAMEGEN.generateName(this); 129 } 130 131 137 public DataColumn createColumn() { 138 return createColumn(null); 139 } 140 141 150 public DataColumn createColumn(String colName) { 151 DataColumn col = new DataColumn(this); 152 if (colName != null) { 153 col.setName(colName); 154 } 155 columns.put(col.getName(), col); 156 col.addPropertyChangeListener("name", nameChangeListener); 157 158 fireColumnAdded(col); 159 return col; 160 } 161 162 168 public void dropColumn(String colName) { 169 DataColumn col = columns.remove(colName); 170 if (col != null) { 171 col.removePropertyChangeListener("name", nameChangeListener); 172 fireColumnRemoved(col); 173 } 174 } 175 176 180 public List <DataColumn> getColumns() { 181 return Collections.unmodifiableList(new ArrayList <DataColumn>(columns.values())); 182 } 183 184 189 public DataColumn getColumn(String colName) { 190 return columns.get(colName); 191 } 192 193 197 public List <DataRow> getRows() { 198 return Collections.unmodifiableList(rows); 199 } 200 201 206 public DataRow getRow(int index) { 207 assert index > 0 && index < rows.size(); 208 return rows.get(index); 209 } 210 211 215 public DataSelector createSelector() { 216 return createSelector(null); 217 } 218 219 224 public DataSelector createSelector(String name) { 225 DataSelector sel = new DataSelector(this); 226 if (name != null) { 227 sel.setName(name); 228 } 229 selectors.put(sel.getName(), sel); 230 sel.addPropertyChangeListener("name", nameChangeListener); 231 return sel; 232 } 233 234 237 public List <DataSelector> getSelectors() { 238 return Collections.unmodifiableList(new ArrayList <DataSelector>(selectors.values())); 239 } 240 241 247 public DataSelector getSelector(String name) { 248 if (!selectors.containsKey(name)) { 250 return createSelector(name); 251 } else { 252 return selectors.get(name); 253 } 254 } 255 256 260 public void dropSelector(DataSelector selector) { 261 dropSelector(selector.getName()); 262 } 263 264 269 public void dropSelector(String selectorName) { 270 DataSelector sel = selectors.remove(selectorName); 271 if (sel != null) { 272 sel.removePropertyChangeListener("name", nameChangeListener); 273 fireSelectorRemoved(sel); 274 } 275 } 276 277 281 public void setName(String name) { 282 if (this.name != name) { 283 assert DataSetUtils.isValidName(name); 284 assert !dataSet.hasElement(name); 285 String oldName = this.name; 286 this.name = name; 287 pcs.firePropertyChange("name", oldName, name); 288 } 289 } 290 291 294 public String getName() { 295 return name; 296 } 297 298 301 public boolean isDeleteRowSupported() { 302 return deleteRowSupported; 303 } 304 305 309 public void setDeleteRowSupported(boolean deleteRowSupported) { 310 if (this.deleteRowSupported != deleteRowSupported) { 311 boolean oldValue = this.deleteRowSupported; 312 this.deleteRowSupported = deleteRowSupported; 313 pcs.firePropertyChange("deleteRowSupported", oldValue, deleteRowSupported); 314 } 315 } 316 317 320 public boolean isAppendRowSupported() { 321 return appendRowSupported; 322 } 323 324 328 public void setAppendRowSupported(boolean appendRowSupported) { 329 if (this.appendRowSupported != appendRowSupported) { 330 boolean oldValue = this.appendRowSupported; 331 this.appendRowSupported = appendRowSupported; 332 pcs.firePropertyChange("appendRowSupported", oldValue, appendRowSupported); 333 } 334 } 335 336 339 public DataProvider getDataProvider() { 340 return dataProvider; 341 } 342 343 347 public void setDataProvider(DataProvider dataProvider) { 348 if (this.dataProvider != dataProvider) { 349 DataProvider oldValue = this.dataProvider; 350 this.dataProvider = dataProvider; 351 pcs.firePropertyChange("dataProvider", oldValue, dataProvider); 352 } 353 } 354 355 358 public DataSet getDataSet() { 359 return dataSet; 360 } 361 362 367 public DataRow appendRow() { 368 DataRow row = appendRowNoEvent(); 369 if (row != null) { 370 fireRowAdded(row); 371 } 372 return row; 373 } 374 375 381 public DataRow appendRowNoEvent() { 382 if (appendRowSupported) { 383 DataRow row = new DataRow(this); 384 rows.add(row); 385 return row; 386 } else { 387 return null; 388 } 389 } 390 391 397 public void deleteRow(int rowIndex) { 398 assert rowIndex > 0 && rowIndex < rows.size(); 399 if (deleteRowSupported) { 400 rows.get(rowIndex).setStatus(DataRow.DataRowStatus.DELETED); 401 } 402 } 403 404 410 public void load() { 411 if (dataProvider != null) { 412 dataProvider.load(this); 413 fireDataTableChanged(new TableChangeEvent(this)); 414 } 415 } 416 417 422 public void save() { 423 if (dataProvider != null) { 424 dataProvider.save(this); 425 } 426 } 427 428 434 public void clear() { 435 rows.clear(); 436 fireDataTableChanged(new TableChangeEvent(this)); 437 } 438 439 446 public void refresh() { 447 clear(); 448 load(); 449 } 450 451 458 public Object getValue(int index, String columnName) { 459 assert index > 0 && index < rows.size(); 460 assert columns.containsKey(columnName); 461 return rows.get(index).getValue(columnName); 462 } 463 464 472 public void setValue(int index, String columnName, Object value) { 473 assert index > 0 && index < rows.size(); 474 assert columns.containsKey(columnName); 475 rows.get(index).setValue(columnName, value); 476 } 477 478 489 public Object getValue(DataRow row, DataColumn col) { 490 assert row.getTable() == this; 491 assert col.getTable() == this; 492 return row.getValue(col); 493 } 494 495 public void addPropertyChangeListener(PropertyChangeListener listener) { 496 pcs.addPropertyChangeListener(listener); 497 } 498 499 public void addPropertyChangeListener(String property, PropertyChangeListener listener) { 500 pcs.addPropertyChangeListener(property, listener); 501 } 502 503 public void removePropertyChangeListener(PropertyChangeListener listener) { 504 pcs.removePropertyChangeListener(listener); 505 } 506 507 public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { 508 pcs.removePropertyChangeListener(propertyName, listener); 509 } 510 511 public void addDataTableListener(DataTableListener listener) { 512 if (!listeners.contains(listener)) { 513 listeners.add(listener); 514 } 515 } 516 517 public void removeDataTableListener(DataTableListener listener) { 518 listeners.remove(listener); 519 } 520 521 protected void fireColumnAdded(DataColumn col) { 522 fireDataTableChanged(new TableChangeEvent(this)); 523 } 524 525 protected void fireRowAdded(DataRow row) { 526 fireDataTableChanged(new TableChangeEvent(this)); 527 } 528 529 public void fireDataTableChanged(TableChangeEvent evt) { 530 for (DataTableListener listener : listeners) { 531 listener.tableChanged(evt); 532 } 533 } 534 535 protected void fireSelectorRemoved(DataSelector sel) { 536 fireDataTableChanged(new TableChangeEvent(this)); 537 } 538 539 protected void fireColumnRemoved(DataColumn col) { 540 fireDataTableChanged(new TableChangeEvent(this)); 541 } 542 543 547 protected int indexOfRow(DataRow row) { 548 return rows.indexOf(row); 549 } 550 } | Popular Tags |