📄 datarow.java
字号:
cell = addCell(col); } return cell; } /** * Returns the DataTable that owns this row. * @return The DataTable that owns this DataRow. */ public DataTable getTable() { return table; } /** * Returns the current row status. * @return The current DataRowStatus enumerated value for this DataRow; * see class comments. */ public DataRowStatus getStatus() { return status; } /** * Returns true if the current value for the column is different from its reference value. * @return true if the given column has been modified; see class comments. * @param colName The column name to check for modification. */ public boolean isModified(String colName) { return isModified(table.getColumn(colName)); } /** * Returns true if the current value for the column is different from its reference value. * @return true if the given column has been modified; see class comments. * @param col The DataColumn to check for modification. */ public boolean isModified(DataColumn col) { return cells.get(col).changed; } /** * CLEAN: remove these once getReference... methods have been approved (they replace getOriginal...) * * @return the original value for the column in this row. If the cell was not * assigned a value explicitly, this is the column's default value. Otherwise it is * the first value assigned to the column on this row. * @deprecated use getReferenceValue(String colName) * @param colName */ public Object getOriginalValue(String colName) { return getReferenceValue(colName); } /** * CLEAN: remove these once getReference... methods have been approved (they replace getOriginal...) * * * @return the original value for the column in this row. If the cell was not * assigned a value explicitly, this is the column's default value. Otherwise it is * the first value assigned to the column on this row. * @deprecated use getReferenceValue(DataColumn col) * @param col */ public Object getOriginalValue(DataColumn col) { return getReferenceValue(col); } /** * Changes the row's status; a status change event will be broadcast if the status is * different from the current status. Changing an UPDATED row's status to UNCHANGED will * overwrite reference values in all cells with the current values; modifying any column * subsequently will change the row status if the columns new value is different than * this reference value. * * @param status The new status for the row. */ public void setStatus(DataRowStatus status) { if (this.status != status) { DataRowStatus priorStatus = this.status; this.status = status; pcs.firePropertyChange("status", priorStatus, status); if (this.status == DataRowStatus.UNCHANGED) { // overwrite all reference values with current values List<DataColumn> cols = table.getColumns(); for ( DataColumn col : cols ) { // note: we don't send a row-cell change event here because it's // the reference val, not the current val, that is changing DataCell cell = getCell(col); if ( cell.changed ) cell.overwriteReference(); } } } } /** * Used internally to fire events for row changes. * @param evt The RowChangeEvent to fire. */ public void fireDataRowChanged(RowChangeEvent evt) { table.fireRowChanged(evt); } /** * Adds a PropertyChangeListener to this class for any changes to bean * properties. * * @param listener The PropertyChangeListener to notify of changes to this * instance. */ public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } /** * Adds a PropertyChangeListener to this class for specific property changes. * * @param property The name of the property to listen to changes for. * @param listener The PropertyChangeListener to notify of changes to this * instance. */ public void addPropertyChangeListener(String property, PropertyChangeListener listener) { pcs.addPropertyChangeListener(property, listener); } /** * Stops notifying a specific listener of any changes to bean properties. * * @param listener The listener to stop receiving notifications. */ public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } /** * Stops notifying a specific listener of changes to a specific property. * * @param propertyName The name of the property to ignore from now on. * @param listener The listener to stop receiving notifications. */ public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } /** * {@inheritDoc} */ public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append("Row #"); buffer.append(table.indexOfRow(this)); buffer.append(" [ "); int i=0; for (DataCell c : cells.values()) { buffer.append(c.value); if (i < cells.size() -1) { buffer.append(", "); } i++; } buffer.append(" ]"); return buffer.toString(); } /** * Used internally to add a cell to the row. If a cell for this column * already exists, returns that cell. * * @param col The DataColumn for which to add the cell. */ protected DataCell addCell(DataColumn col) { DataCell cell = cells.get(col); if ( cell == null ) { cell = new DataCell(col); cells.put(col, cell); } return cell; } /** * Used internally to determine the row's current status, called after a cell's * value is changed. The general rule is that UNCHANGED rows will be marked UPDATED if * at least one cell is changed, and UPDATED rows will be marked UNCHANGED if no cells * are changed. INSERTED and DELETED rows do not change status in this method. */ protected DataRowStatus deriveRowStatus() { DataRowStatus derived; switch (status) { case INSERTED: // fall-thru case DELETED: derived = status; break; case UPDATED: // fall-thru case UNCHANGED: boolean any = false; for ( DataCell cell : cells.values()) { if ( cell.changed ) { any = true; break; } } derived = ( any ? DataRowStatus.UPDATED : DataRowStatus.UNCHANGED ); break; default: throw new RuntimeException("deriveRowStatus() has no case for row status of " + status); } return derived; } /** * Class used internally to intersect a DataColumn for the row; holds the reference * and current values. As these are populated per-column per-row, a minimum of state * information is kept in each cell, and methods receive contextual information necessary * for the task to reduce stored state. */ private static final class DataCell { /** * The reference value for the cell. */ Object referenceValue; /** * The current value for the cell. */ Object value; /** * True if the current value is different from the reference value. */ boolean changed; /** * True is the current value was set for the first time. */ boolean valueSet; /** * Instantiates a cell for a given column. * @param col The DataColumn for which to create a cell. */ DataCell(DataColumn col){ this.value = this.referenceValue = col.getDefaultValue(); } /** * Sets the reference value for the cell. * @param table The DataTable for the row. * @param col The DataColumn for the cell. * @param newRef The new reference value for the cell. */ public void setReferenceValue(DataTable table, DataColumn col, Object newRef) { referenceValue = newRef; changed = isSame(table, col, referenceValue, newRef); } /** * Sets the current value for the cell. * @param table The DataTable for the row. * @param col The DataColumn for the cell. * @param newValue The new current value for the cell. */ public void setValue(DataTable table, DataColumn col, Object newValue) { // if this is the first call, set reference value for cell if ( !valueSet ) { value = referenceValue = newValue; changed = false; valueSet = true; return; } // if the new value is our current reference value, then cell is // no longer changed if ( isSame(table, col, referenceValue, newValue )) { value = referenceValue; changed = false; return; } else { // not the same as our reference value if ( ! isSame( table, col, value, newValue )) { value = newValue; changed = true; } } } /** * Overwrites the reference value for the cell with its current value. */ public void overwriteReference() { referenceValue = value; changed = false; } /** * Returns true if the two values are the same, using either <CODE>==</CODE>, or a <CODE>Comparator</CODE>; see * class docs on DataRow. * @param table The DataTable for the row. * @param col The DataColumn for the cell. * @param baseValue The base value we are comparing. * @param newValue The new value we are comparing with. * @return True if the value are the same. */ private boolean isSame(DataTable table, DataColumn col, Object baseValue, Object newValue) { if ( table.isIdentityComparisonEnabled()) { return newValue == baseValue; } else { Comparator comp = null; if ( table.hasColumnComparator(col)) { comp = table.getColumnComparator(col); } else { // this returns a default .equals() comparator if none // is assigned to the column type comp = table.getClassComparator(col.getType()); } return comp.compare(baseValue, newValue) == 0; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -