Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Daniel Murphy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
Created at Jun 21, 2010, 3:54:44 AM
 
 package com.dmurph.mvc.model;
 
 
This model keeps track of original and changed property values through the firePropertyChange(java.lang.String,java.lang.Object,java.lang.Object) method. Assuming an extending class correctly implements firePropertyChange(java.lang.String,java.lang.Object,java.lang.Object) and setProperty(java.lang.String,java.lang.Object) correctly, this class will keep track of any changes to the model so the model can save or revert changes. This also makes the isDirty() method more accurate, as it can check all the properties to see if they have changed.

Author(s):
Daniel Murphy
 
 public abstract class AbstractRevertableModel extends AbstractModel implements IDirtyableICloneableIRevertable{
 
 	private boolean overridingDirty = false;

    
If the model is "dirty", or changed since last save. This method can be expensive, as it checks all changed properties to see if they are dirty. Will also return true if setDirty(boolean) was recently called with true.

 
 	public boolean isDirty(){
 			return ;
 		}
 		for(String key : .keySet()){
 			if(.get(key).isDirty()){
 				return true;
 			}
 		}
 		return false;
 	}

If called with false, this is the equivalent of saveChanges(). If called with true, the model is dirty until saveChanges(), revertChanges(), or this method is called with false.

 
 	public boolean setDirty(boolean argDirty) {
 		boolean dirty = isDirty();
 		if(argDirty){
 			 = true;
 			return dirty;
 		}else{
 			 = false;
 			return dirty;
 		}
 	}

 
 	protected void firePropertyChange(String argPropertyNameObject argOldValueObject argNewValue) {
 		if(.containsKey(argPropertyName)){
 			PropertyWrapper wrapper = .get(argPropertyName);
 			wrapper.dirtyObject = argNewValue;
 		}else{
 			PropertyWrapper wrapper = new PropertyWrapper();
 			wrapper.name = argPropertyName;
			wrapper.cleanObject = argOldValue;
			wrapper.dirtyObject = argNewValue;
			.put(argPropertyNamewrapper);
		}
		super.firePropertyChange(argPropertyNameargOldValueargNewValue);
	}



Used to reset variables when revertChanges() is called.

Parameters:
argPropertyName the property name corresponding to the name given to the firePropertyChange(java.lang.String,java.lang.Object,java.lang.Object) method
argValue the value to set the property
	protected abstract void setProperty(String argPropertyNameObject argValue);


	public boolean saveChanges() {
		boolean saved = false;
		for(String key : .keySet()){
			PropertyWrapper wrapper = .get(key);
			if(wrapper.isDirty()){
				saved = true;
				wrapper.cleanObject = wrapper.dirtyObject;
			}
		}
		return saved;
	}

	public boolean revertChanges() {
		boolean reverted = false;
		for(String key : .keySet()){
			PropertyWrapper wrapper = .get(key);
			if(wrapper.isDirty()){
				reverted = true;
				wrapper.dirtyObject = wrapper.cleanObject;
				setProperty(wrapper.namewrapper.cleanObject);
			}
		}
		return reverted;
	}

	public abstract ICloneable clone();
	private static class PropertyWrapper{
		String name = null;
		public boolean isDirty(){
				return false;
			}
			if( == null){
				return true;
			}
				return false;
			}
			return true;
		}
	}
New to GrepCode? Check out our FAQ X