1 7 8 package org.jdesktop.dataset; 9 import java.beans.PropertyChangeListener ; 10 import java.beans.PropertyChangeSupport ; 11 import java.util.Collection ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 15 16 20 public class DataRow { 21 24 public enum DataRowStatus {INSERTED, DELETED, UPDATED, UNCHANGED}; 25 26 private PropertyChangeSupport pcs = new PropertyChangeSupport (this); 29 30 33 private DataTable table; 34 40 private DataRowStatus status = DataRowStatus.INSERTED; 41 46 private Map <DataColumn,DataCell> cells = new HashMap <DataColumn,DataCell>(); 47 48 51 protected DataRow(DataTable table) { 52 assert table != null; 53 this.table = table; 54 55 for (DataColumn c : this.table.getColumns()) { 58 DataCell cell = new DataCell(); 59 cell.value = c.getDefaultValue(); 60 cells.put(c, cell); 61 } 62 } 63 64 70 public void setValue(String colName, Object value) { 71 DataColumn col = table.getColumn(colName); 72 assert col != null; 73 DataCell cell = cells.get(col); 74 cell.setValue(value); 75 if (status == DataRowStatus.UNCHANGED && cell.changed) { 76 setStatus(DataRowStatus.UPDATED); 77 } 78 } 79 80 83 public Object getValue(String colName) { 84 return getValue(table.getColumn(colName)); 85 } 86 87 public Object getValue(DataColumn col) { 88 return cells.get(col).value; 89 } 90 91 public DataTable getTable() { 92 return table; 93 } 94 95 public DataRowStatus getStatus() { 96 return status; 97 } 98 99 102 public void setStatus(DataRowStatus status) { 103 if (this.status != status) { 104 DataRowStatus oldValue = this.status; 105 this.status = status; 106 pcs.firePropertyChange("status", oldValue, status); 107 } 108 } 109 110 public void addPropertyChangeListener(PropertyChangeListener listener) { 111 pcs.addPropertyChangeListener(listener); 112 } 113 114 public void addPropertyChangeListener(String property, PropertyChangeListener listener) { 115 pcs.addPropertyChangeListener(property, listener); 116 } 117 118 public void removePropertyChangeListener(PropertyChangeListener listener) { 119 pcs.removePropertyChangeListener(listener); 120 } 121 122 public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { 123 pcs.removePropertyChangeListener(propertyName, listener); 124 } 125 126 public String toString() { 127 StringBuilder buffer = new StringBuilder (); 128 buffer.append("Row #"); 129 buffer.append(table.indexOfRow(this)); 130 buffer.append(" [ "); 131 int i=0; 132 for (DataCell c : cells.values()) { 133 buffer.append(c.value); 134 if (i < cells.size() -1) { 135 buffer.append(", "); 136 } 137 i++; 138 } 139 return buffer.toString(); 140 } 141 142 private static final class DataCell { 143 Object originalValue; 144 Object value; 145 boolean changed; 146 147 public void setValue(Object newValue) { 148 if (newValue != value && !changed) { 149 originalValue = value; 150 value = newValue; 151 changed = true; 152 } else if (newValue != value) { 153 value = newValue; 154 } 155 } 156 } 157 } | Popular Tags |