[richfaces-svn-commits] JBoss Rich Faces SVN: r4531 - branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Wed Dec 5 23:45:38 EST 2007


Author: nbelaevski
Date: 2007-12-05 23:45:37 -0500 (Wed, 05 Dec 2007)
New Revision: 4531

Modified:
   branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingBaseComponent.java
   branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingList.java
Log:
code refactoring & optimization done for orderingList & listShuttle

Modified: branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingBaseComponent.java
===================================================================
--- branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingBaseComponent.java	2007-12-06 03:18:37 UTC (rev 4530)
+++ branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingBaseComponent.java	2007-12-06 04:45:37 UTC (rev 4531)
@@ -3,7 +3,6 @@
  */
 package org.richfaces.component;
 
-import java.io.Serializable;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -46,12 +45,8 @@
  */
 public abstract class UIOrderingBaseComponent extends UIDataAdaptor implements EditableValueHolder {
 
-	private Object value;
 	private boolean localValueSet;
 
-	protected Object activeItem;
-	protected boolean activeItemSet;
-	
 	private List validators = null;
 	private MethodBinding validator;
 
@@ -83,92 +78,21 @@
 		}
 	}
 	
-	private static class EditableState implements Serializable {
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = -3276243811945890889L;
-
-		private boolean translated = false;
-
-		//setting this flag to true means we should reorder elements
-		private boolean translatedRendering = false;
-	}
-
-	private static final class ValueHolder implements Serializable {
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = 2349104220087787755L;
-
-		private Object value;
-
-		private Object activeItem;
-		private boolean activeItemSet;
-
-		private Object state;
-	}
-
-	private transient EditableState editableState = new EditableState();
-	
-	protected abstract void restoreIterationState(Object object);
-	protected abstract Object saveIterationState();
-
-	protected abstract void restoreIterationSubmittedState(Object object);
-	protected abstract Object saveIterationSubmittedState();
-	
 	public abstract boolean isOrderControlsVisible();
 	public abstract void setOrderControlsVisible(boolean visible);
 	
 	public abstract boolean isFastOrderControlsVisible();
 	public abstract void setFastOrderControlsVisible(boolean visible);
 
-	protected void setTranslatedState() {
-		this.editableState.translated = true;
-	}
-
-	protected boolean isTranslatedState() {
-		return this.editableState.translated;
-	}
-	
-	protected void setTranslatedRenderingState() {
-		this.editableState.translatedRendering = true;
-	}
-
-	protected boolean isTranslatedRenderingState() {
-		return this.editableState.translatedRendering;
-	}
-
-	public final Object getSubmittedValue() {
-		Object[] state = new Object[2];
-
-		state[0] = saveIterationSubmittedState();
-		state[1] = editableState;
-		
-		return state;
-	}
-	
-	public final void setSubmittedValue(Object object) {
-		if (object != null) {
-			Object[] state = (Object[]) object;
-
-			restoreIterationSubmittedState(state[0]);
-			editableState = (EditableState) state[1];
-		} else {
-			restoreIterationSubmittedState(null);
-		}
-	}
-	
 	public Object saveState(FacesContext faces) {
-		Object[] state = new Object[5];
+		Object[] state = new Object[4];
 
 		state[0] = super.saveState(faces);
 	
 		state[1] = saveAttachedState(faces, validators);
 		state[2] = saveAttachedState(faces, validator);
 
-		state[3] = this.value;
-		state[4] = localValueSet ? Boolean.TRUE : Boolean.FALSE;
+		state[3] = localValueSet ? Boolean.TRUE : Boolean.FALSE;
 
 		return state;
 	}
@@ -181,9 +105,7 @@
 		validators = (List) restoreAttachedState(faces, state[1]);
 		validator = (MethodBinding) restoreAttachedState(faces, state[2]);
 
-		value = state[3];
-		localValueSet = ((Boolean) state[4]).booleanValue();
-
+		localValueSet = ((Boolean) state[3]).booleanValue();
 	}
 
 	protected DataComponentState createComponentState() {
@@ -248,52 +170,6 @@
 	}
 	//validators end
 	
-	//value
-	protected Object getLocalValueFieldValue() {
-		return value;
-	}
-	
-	public Object getLocalValue() {
-		ValueHolder holder = new ValueHolder();
-		holder.value = this.value;
-
-		holder.activeItem = this.activeItem;
-		holder.activeItemSet = this.activeItemSet;
-
-		holder.state = saveIterationState();
-		
-		return holder;
-	}
-
-	public void setValue(Object value) {
-		if (value instanceof ValueHolder) {
-			ValueHolder holder = (ValueHolder) value;
-			
-			setValue(holder.value);
-			restoreIterationState(holder.state);
-		
-			this.activeItem = holder.activeItem;
-			this.activeItemSet = holder.activeItemSet;
-		} else {
-			super.setValue(value);
-			this.value = value;
-			setLocalValueSet(true);
-		}
-	}
-
-	public Object getValue() {
-		if (this.value != null) {
-			return (this.value);
-		}
-		ValueBinding ve = getValueBinding("value");
-		if (ve != null) {
-			return (ve.getValue(getFacesContext()));
-		} else {
-			return (null);
-		}
-	}
-	//value end
-
 	public boolean isLocalValueSet() {
 		return localValueSet;
 	}
@@ -457,36 +333,6 @@
 		void add(Object object);
 	}
 
-	protected final UpdateModelCommand updateValueCommand = new UpdateModelCommand() {
-
-		public void execute(FacesContext context) {
-			if (isLocalValueSet()) {
-				ValueBinding vb = getValueBinding("value");
-				if (vb != null) {
-					vb.setValue(context, getLocalValueFieldValue());
-					setValue(null);
-					setLocalValueSet(false);
-				}
-			}
-		}
-		
-	};
-	
-	protected final UpdateModelCommand updateActiveItemCommand = new UpdateModelCommand() {
-
-		public void execute(FacesContext context) {
-			if (activeItemSet) {
-				ValueBinding vb = getValueBinding("activeItem");
-				if (vb != null) {
-					vb.setValue(context, activeItem);
-					activeItem = null;
-					activeItemSet = false;
-				}
-			}
-		}
-		
-	};
-
 	protected void updateModel(FacesContext context, UpdateModelCommand command) {
 		try {
 			command.execute(context);
@@ -583,24 +429,6 @@
 		return (false);
 	}
 
-	public Object getActiveItem() {
-		if (this.activeItem != null) {
-			return this.activeItem;
-		} else {
-			ValueBinding vb = getValueBinding("activeItem");
-			if (vb != null) {
-				return vb.getValue(FacesContext.getCurrentInstance());
-			}
-		}
-		
-		return null;
-	}
-	
-	public void setActiveItem(Object activeItem) {
-		this.activeItem = activeItem;
-		this.activeItemSet = true;
-	}
-	
 	protected Object createContainer(ArrayList data, Object object) {
 		if (object != null) {
 			Class objectClass = object.getClass();

Modified: branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingList.java
===================================================================
--- branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingList.java	2007-12-06 03:18:37 UTC (rev 4530)
+++ branches/3.1.x/ui/orderingList/src/main/java/org/richfaces/component/UIOrderingList.java	2007-12-06 04:45:37 UTC (rev 4531)
@@ -13,6 +13,7 @@
 
 import javax.faces.FacesException;
 import javax.faces.application.FacesMessage;
+import javax.faces.component.StateHolder;
 import javax.faces.component.UIComponent;
 import javax.faces.component.UIComponentBase;
 import javax.faces.component.UIInput;
@@ -76,7 +77,7 @@
         this.iterate(faces, decodeVisitor, argument);
 	}
 
-	private transient Map dataMap;
+	private ValueHolder valueHolder;
 
 	protected static final class SubmittedValue implements Serializable {
 		/**
@@ -88,11 +89,27 @@
 		private Collection selection;
 		private Object activeItem;
 		
+		private boolean _null = false;
+
 		public SubmittedValue(Map dataMap, Set selection, Object activeItem) {
 			this.dataMap = dataMap;
 			this.selection = selection;
 			this.activeItem = activeItem;
 		}
+
+		public void setNull() {
+			_null = true;
+		}
+		
+		public boolean isNull() {
+			return _null;
+		}
+		
+		public void resetDataModel() {
+			if (_null) {
+				this.dataMap = null;
+			}
+		}
 	}
 
 	private final class ModelItemState implements ItemState {
@@ -119,8 +136,6 @@
 
 		if (submittedValueHolder != null) {
 			modelMap = submittedValueHolder.dataMap;
-		} else {
-			modelMap = this.dataMap;
 		}
 		
 		if (modelMap != null) {
@@ -135,22 +150,89 @@
 
 	private transient SubmittedValue submittedValueHolder = null;
 	
-	protected static final class ValueHolder implements Serializable {
+	public final static class ValueHolder implements Serializable {
 
 		/**
 		 * 
 		 */
 		private static final long serialVersionUID = -4216115242421460529L;
 		
+		private Object value;
+		
 		private Collection selection;
 		private boolean selectionSet;
-		
-		private Map map;
+
+		private Object activeItem;
+		private boolean activeItemSet;
+
+		public boolean isTransient() {
+			//TODO null collection == [] ?
+			return value == null && (selection == null || selection.isEmpty()) && !selectionSet && 
+			activeItem == null && !activeItemSet;
+		}
+
+		public void restoreState(FacesContext context, UIOrderingList list, Object _state) {
+			Object[] state = (Object[]) _state;
+			
+			value = state[0];
+			
+			selection = (Collection) state[1];
+			selectionSet = Boolean.TRUE.equals(state[2]);
+			
+			activeItem = state[3];
+			activeItemSet = Boolean.TRUE.equals(state[4]);
+		}
+
+		public Object saveState(FacesContext context, final UIOrderingList list) {
+			Object rowKey = list.getRowKey();
+
+			final HashSet selectionKeySet = new HashSet();
+			final Object[] activeItemSet = new Object[1];
+			try {
+				list.walk(context, new DataVisitor() {
+
+					public void process(FacesContext context, Object rowKey,
+							Object argument) throws IOException {
+
+						list.setRowKey(context, rowKey);
+						Object data = list.getRowData();
+						
+						if (data != null) {
+							if (data.equals(activeItem)) {
+								activeItemSet[0] = rowKey;
+							}
+							
+							if (selection != null && selection.contains(data)) {
+								selectionKeySet.add(rowKey);
+							}
+						}
+					}
+					
+				}, null);
+			} catch (IOException e) {
+				throw new FacesException(e.getLocalizedMessage(), e);
+			}
+
+			Object[] state = new Object[5];
+			
+			state[0] = value;
+			
+			state[1] = selectionKeySet;
+			state[2] = this.selectionSet ? Boolean.TRUE : Boolean.FALSE;
+			
+			state[3] = activeItemSet[0];
+			state[4] = this.activeItemSet ? Boolean.TRUE : Boolean.FALSE;
+
+			return state;
+		}
+
+		public void setTransient(boolean newTransientValue) {
+			if (newTransientValue) {
+				throw new IllegalArgumentException();
+			}
+		}
 	}
 
-	private Collection selection;
-	private boolean selectionSet;
-
 	public void addValueChangeListener(ValueChangeListener listener) {
 		addFacesListener(listener);
 	}
@@ -177,27 +259,20 @@
 		this.submittedValueHolder = new SubmittedValue(submittedString, selection, activeItem);
 	}
 
-	protected Object saveIterationSubmittedState() {
+	public Object getSubmittedValue() {
 		return submittedValueHolder;
 	}
 
-	protected void restoreIterationSubmittedState(Object object) {
+	public void setSubmittedValue(Object object) {
 		this.submittedValueHolder = (SubmittedValue) object;
 	}
 
 	protected Object saveIterationState() {
-		ValueHolder valueHolder = new ValueHolder();
-		valueHolder.map = dataMap;
-		valueHolder.selection = selection;
-		valueHolder.selectionSet = selectionSet;
 		return valueHolder;
 	}
 
 	protected void restoreIterationState(Object object) {
-		ValueHolder valueHolder = (ValueHolder) object;
-		dataMap = valueHolder.map;
-		selection = valueHolder.selection;
-		selectionSet = valueHolder.selectionSet;
+		this.valueHolder = (ValueHolder) object;
 	}
 
 	public abstract void setImmediate(boolean immediate);
@@ -319,21 +394,51 @@
 
 	}
 
+	protected final UpdateModelCommand updateActiveItemCommand = new UpdateModelCommand() {
+
+		public void execute(FacesContext context) {
+			if (valueHolder.activeItemSet) {
+				ValueBinding vb = getValueBinding("activeItem");
+				if (vb != null) {
+					vb.setValue(context, valueHolder.activeItem);
+					valueHolder.activeItem = null;
+					valueHolder.activeItemSet = false;
+				}
+			}
+		}
+		
+	};
+
 	protected final UpdateModelCommand updateSelectionCommand = new UpdateModelCommand() {
 
 		public void execute(FacesContext context) {
-			if (selectionSet) {
+			if (valueHolder.selectionSet) {
 				ValueBinding vb = getValueBinding("selection");
 				if (vb != null) {
-					vb.setValue(context, selection);
-					selection = null;
-					selectionSet = false;
+					vb.setValue(context, valueHolder.selection);
+					valueHolder.selection = null;
+					valueHolder.selectionSet = false;
 				}
 			}
 		}
 		
 	};
 	
+	protected final UpdateModelCommand updateValueCommand = new UpdateModelCommand() {
+
+		public void execute(FacesContext context) {
+			if (isLocalValueSet() && valueHolder != null) {
+				ValueBinding vb = getValueBinding("value");
+				if (vb != null) {
+					vb.setValue(context, valueHolder.value);
+					setValue(null);
+					setLocalValueSet(false);
+				}
+			}
+		}
+		
+	};
+	
 	/**
 	 * <p>Perform the following algorithm to update the model dataMap
 	 * associated with this {@link UIInput}, if any, as appropriate.</p>
@@ -376,9 +481,11 @@
 			return;
 		}
 
-		updateModel(context, updateValueCommand);
-		updateModel(context, updateSelectionCommand);
-		updateModel(context, updateActiveItemCommand);
+		if (valueHolder != null) {
+			updateModel(context, updateValueCommand);
+			updateModel(context, updateSelectionCommand);
+			updateModel(context, updateActiveItemCommand);
+		}
 	}
 
 
@@ -475,24 +582,20 @@
 			setActiveItem(submittedValueHolder.activeItem);
 			
 			setValue(newValue);
-			setTranslatedState();
 
 			if (compareValues(previousValue, newValue)) {
 				queueEvent(new ValueChangeEvent(this, previousValue, newValue));
 			}
 
-			this.dataMap = this.submittedValueHolder.dataMap;
-			this.submittedValueHolder = null;
+			this.submittedValueHolder.setNull();
 		}
 	}
 
 	protected void resetDataModel() {
 		super.resetDataModel();
 
-		this.dataMap = null;
-
 		if (this.submittedValueHolder != null) {
-			setTranslatedRenderingState();
+			this.submittedValueHolder.resetDataModel();
 		}
 	}
 
@@ -587,7 +690,7 @@
 	}
 
 	public ItemState getItemState() {
-		if (submittedValueHolder != null) {
+		if (submittedValueHolder != null && !submittedValueHolder.isNull()) {
 			return new ModelItemState(submittedValueHolder.selection, 
 					submittedValueHolder.activeItem);
 		} else {
@@ -599,8 +702,8 @@
 	public abstract void setControlsType(String type);
 
 	public Collection getSelection() {
-		if (this.selection != null) {
-			return this.selection;
+		if (valueHolder != null && valueHolder.selection != null) {
+			return valueHolder.selection;
 		} else {
 			ValueBinding vb = getValueBinding("selection");
 			if (vb != null) {
@@ -612,49 +715,68 @@
 	}
 	
 	public void setSelection(Collection collection) {
-		this.selection = collection;
-		this.selectionSet = true;
+		createValueHolder();
+		valueHolder.selection = collection;
+		valueHolder.selectionSet = true;
 	}
 	
-	public Object saveState(FacesContext faces) {
-		Object[] state = new Object[5];
-		state[0] = super.saveState(faces);
+	public Object getActiveItem() {
+		if (valueHolder != null && valueHolder.activeItem != null) {
+			return valueHolder.activeItem;
+		} else {
+			ValueBinding vb = getValueBinding("activeItem");
+			if (vb != null) {
+				return vb.getValue(FacesContext.getCurrentInstance());
+			}
+		}
 		
-		Object rowKey = getRowKey();
+		return null;
+	}
+	
+	public void setActiveItem(Object activeItem) {
+		createValueHolder();
+		valueHolder.activeItem = activeItem;
+		valueHolder.activeItemSet = true;
+	}
+	
+	private void createValueHolder() {
+		if (valueHolder == null) {
+			valueHolder = new ValueHolder();
+		}
+	}
 
-		final HashSet selectionKeySet = new HashSet();
-		final HashSet activeItemSet = new HashSet(1);
-		try {
-			walk(faces, new DataVisitor() {
+	public void setValue(Object value) {
+		if (value instanceof ValueHolder) {
+			this.valueHolder = (ValueHolder) value;
+		} else {
+			createValueHolder();
+			valueHolder.value = value;
+			setLocalValueSet(true);
+		}
+	}
 
-				public void process(FacesContext context, Object rowKey,
-						Object argument) throws IOException {
+	public Object getLocalValue() {
+		return valueHolder;
+	}
+	
+	public Object getValue() {
+		if (valueHolder != null && valueHolder.value != null) {
+			return valueHolder.value;
+		}
+		ValueBinding ve = getValueBinding("value");
+		if (ve != null) {
+			return (ve.getValue(getFacesContext()));
+		} else {
+			return (null);
+		}
+	}
 
-					setRowKey(context, rowKey);
-					Object data = getRowData();
-					
-					if (data != null) {
-						if (data.equals(activeItem)) {
-							activeItemSet.add(rowKey);
-						}
-						
-						if (selection != null && selection.contains(data)) {
-							selectionKeySet.add(rowKey);
-						}
-					}
-				}
-				
-			}, null);
-		} catch (IOException e) {
-			throw new FacesException(e.getLocalizedMessage(), e);
+	public Object saveState(FacesContext faces) {
+		Object[] state = new Object[2];
+		state[0] = super.saveState(faces);
+		if (this.valueHolder != null) {
+			state[1] = this.valueHolder.saveState(faces, this);
 		}
-
-		state[1] = selectionKeySet;
-		state[2] = this.selectionSet ? Boolean.TRUE : Boolean.FALSE;
-		
-		state[3] = activeItemSet.isEmpty() ? null : activeItemSet.iterator().next();
-		state[4] = this.activeItemSet ? Boolean.TRUE : Boolean.FALSE;
-
 		return state;
 	}
 	
@@ -663,11 +785,10 @@
 		
 		super.restoreState(faces, state[0]);
 		
-		this.selection = (Collection) state[1];
-		this.selectionSet = ((Boolean) state[2]).booleanValue();
-		
-		this.activeItem = state[3];
-		this.activeItemSet = ((Boolean) state[4]).booleanValue();
+		if (state[1] != null) {
+			this.valueHolder = new ValueHolder();
+			this.valueHolder.restoreState(faces, this, state[1]);
+		}
 	}
 
 }




More information about the richfaces-svn-commits mailing list