[richfaces-svn-commits] JBoss Rich Faces SVN: r4532 - branches/3.1.x/ui/listShuttle/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:45 EST 2007


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

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

Modified: branches/3.1.x/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java
===================================================================
--- branches/3.1.x/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java	2007-12-06 04:45:37 UTC (rev 4531)
+++ branches/3.1.x/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java	2007-12-06 04:45:44 UTC (rev 4532)
@@ -16,6 +16,7 @@
 
 import javax.faces.FacesException;
 import javax.faces.application.FacesMessage;
+import javax.faces.component.StateHolder;
 import javax.faces.component.UIInput;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.ConverterException;
@@ -27,6 +28,7 @@
 
 import org.ajax4jsf.Messages;
 import org.ajax4jsf.model.DataVisitor;
+import org.richfaces.component.UIOrderingList.ValueHolder;
 import org.richfaces.component.util.MessageUtil;
 import org.richfaces.model.ListShuttleDataModel;
 import org.richfaces.model.ListShuttleRowKey;
@@ -41,76 +43,6 @@
 
 	public static final String COMPONENT_FAMILY = "org.richfaces.ListShuttle";
 
-	private Collection sourceSelection;
-	private boolean sourceSelectionSet;
-	
-	private Collection targetSelection;
-	private boolean targetSelectionSet;
-
-	protected void processDecodes(FacesContext faces, Object argument) {
-		if (!this.isRendered())
-			return;
-		this.decode(faces);
-
-		SubmittedValue submittedValue = UIListShuttle.this.submittedValueHolder;
-		if (submittedValue != null) {
-			if (submittedValue != null) {
-				Object modelSourceValue = getSourceValue();
-				Object modelTargetValue = getTargetValue();
-				
-				Iterator iterator = submittedValue.map.entrySet().iterator();
-				while (iterator.hasNext()) {
-					Entry entry = (Entry) iterator.next();
-					Object value = entry.getValue();
-					
-					if (!isSuitableValue(modelSourceValue, value) && !isSuitableValue(modelTargetValue, value)) {
-						String messageText = Messages.getMessage(
-								Messages.INVALID_VALUE, MessageUtil.getLabel(faces, this), value);
-						
-						FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, messageText, null);
-						faces.addMessage(this.getClientId(faces), message);
-
-						setValid(false);
-						break;
-					}
-				}
-			}
-		}
-
-		if (isImmediate()) {
-			executeValidate(faces);
-		}
-		
-        if (!isValid()) {
-            faces.renderResponse();
-        }
-
-        this.iterate(faces, decodeVisitor, argument);
-	}
-
-	private final class ModelItemState implements ItemState {
-		private Collection sourceSelectedItems;
-		private Collection targetSelectedItems;
-		private Object activeItem;
-
-		public ModelItemState(Collection sourceSelectedItems, Collection targetSelectedItems, Object activeItem) {
-			super();
-			this.sourceSelectedItems = sourceSelectedItems;
-			this.targetSelectedItems = targetSelectedItems;
-			this.activeItem = activeItem;
-		}
-
-		public boolean isSelected() {
-			Object rowData = getRowData();
-			return ((sourceSelectedItems != null && sourceSelectedItems.contains(rowData)) || 
-			(targetSelectedItems != null && targetSelectedItems.contains(rowData)));
-		}
-
-		public boolean isActive() {
-			return activeItem != null && activeItem.equals(getRowData());
-		}
-	}
-
 	protected static final class SubmittedValue implements Serializable {
 		/**
 		 * 
@@ -124,6 +56,8 @@
 		
 		private Object activeItem;
 		
+		private boolean _null = false;
+		
 		public SubmittedValue(Map map, Set sourceSelection, Set targetSelection, Object activeItem) {
 			this.map = map;
 		
@@ -133,13 +67,22 @@
 			this.activeItem = activeItem;
 		}
 
+		public void setNull() {
+			_null = true;
+		}
+		
+		public boolean isNull() {
+			return _null;
+		}
+		
+		public void resetDataModel() {
+			if (_null) {
+				this.map = null;
+			}
+		}
 	}
 
-	private transient Map map;
-
-	private transient SubmittedValue submittedValueHolder = null;
-
-	protected static final class ValueHolder implements Serializable {
+	public static final class ValueHolder implements Serializable {
 		/**
 		 * 
 		 */
@@ -157,62 +100,178 @@
 		private Collection targetSelection;
 		private boolean targetSelectionSet;
 
-		private Map map;
-	}
+		private Object activeItem;
+		private boolean activeItemSet;
 
-	private Object sourceValue;
-	private boolean sourceValueSet;
+		private boolean collectionIsEmpty(Collection collection) {
+			return collection == null || collection.isEmpty();
+		}
+		
+		private Object saveBoolean(boolean b) {
+			return b ? Boolean.TRUE : Boolean.FALSE;
+		}
+		
+		public boolean isTransient() {
+			//TODO null collection == [] ?
+			return sourceValue == null && !sourceValueSet && targetValue == null && !targetValueSet && 
+			collectionIsEmpty(sourceSelection) && !sourceSelectionSet && 
+			collectionIsEmpty(targetSelection) && !targetSelectionSet &&
+			activeItem == null && !activeItemSet;
+		}
 
-	private Object targetValue;
-	private boolean targetValueSet;
+		public void restoreState(FacesContext context, UIListShuttle list, Object _state) {
+			Object[] state = (Object[]) _state;
 
-	public Object saveState(FacesContext context) {
-		Object[] state = new Object[8];
+			sourceValue = state[0];
+			sourceValueSet = Boolean.TRUE.equals(state[1]);
 
-		state[0] = super.saveState(context);
-		state[1] = saveIterationState();
+			targetValue = state[2];
+			targetValueSet = Boolean.TRUE.equals(state[3]);
 
-		final HashSet sourceSelectionKeySet = new HashSet();
-		final HashSet targetSelectionKeySet = new HashSet();
-		final HashSet activeItemSet = new HashSet(1);
-		
-		Object rowKey = getRowKey();
-		try {
-			walk(context, new DataVisitor() {
+			sourceSelection = (Collection) state[4];
+			sourceSelectionSet = Boolean.TRUE.equals(state[5]);
 
-				public void process(FacesContext context, Object rowKey,
-						Object argument) throws IOException {
+			targetSelection = (Collection) state[6];
+			targetSelectionSet = Boolean.TRUE.equals(state[7]);
 
-					setRowKey(context, rowKey);
-					Object data = getRowData();
-					
-					if (data != null) {
-						if (data.equals(activeItem)) {
-							activeItemSet.add(rowKey);
-						}
+			activeItem = state[8];
+			activeItemSet = Boolean.TRUE.equals(state[9]);
+		}
+
+		public Object saveState(FacesContext context, final UIListShuttle list) {
+			final HashSet sourceSelectionKeySet = new HashSet();
+			final HashSet targetSelectionKeySet = new HashSet();
+			final Object[] activeItemKeys = new Object[1];
+			
+			Object rowKey = list.getRowKey();
+			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 (sourceSelection != null && sourceSelection.contains(data)) {
-							sourceSelectionKeySet.add(rowKey);
-						} else if (targetSelection != null && targetSelection.contains(data)){
-							targetSelectionKeySet.add(rowKey);
+						if (data != null) {
+							if (data.equals(activeItem)) {
+								activeItemKeys[0] = rowKey;
+							}
+							
+							if (sourceSelection != null && sourceSelection.contains(data)) {
+								sourceSelectionKeySet.add(rowKey);
+							} else if (targetSelection != null && targetSelection.contains(data)){
+								targetSelectionKeySet.add(rowKey);
+							}
 						}
 					}
+					
+				}, null);
+			} catch (IOException e) {
+				throw new FacesException(e.getLocalizedMessage(), e);
+			}
+
+			Object[] state = new Object[10];
+
+			state[0] = sourceValue;
+			state[1] = sourceValueSet ? Boolean.TRUE : Boolean.FALSE;
+
+			state[2] = targetValue;
+			state[3] = targetValueSet ? Boolean.TRUE : Boolean.FALSE;
+
+			state[4] = sourceSelectionKeySet;
+			state[5] = sourceSelectionSet ? Boolean.TRUE : Boolean.FALSE;
+
+			state[6] = targetSelectionKeySet;
+			state[7] = targetSelectionSet ? Boolean.TRUE : Boolean.FALSE;
+
+			state[8] = activeItemKeys[0];
+			state[9] = activeItemSet ? Boolean.TRUE : Boolean.FALSE;
+
+			return state;
+		}
+
+		public void setTransient(boolean newTransientValue) {
+			if (newTransientValue) {
+				throw new IllegalArgumentException();
+			}
+		}
+	}
+
+	private transient SubmittedValue submittedValueHolder = null;
+
+	private ValueHolder valueHolder;
+	
+	protected void processDecodes(FacesContext faces, Object argument) {
+		if (!this.isRendered())
+			return;
+		this.decode(faces);
+
+		SubmittedValue submittedValue = UIListShuttle.this.submittedValueHolder;
+		if (submittedValue != null) {
+			Object modelSourceValue = getSourceValue();
+			Object modelTargetValue = getTargetValue();
+			
+			Iterator iterator = submittedValue.map.entrySet().iterator();
+			while (iterator.hasNext()) {
+				Entry entry = (Entry) iterator.next();
+				Object value = entry.getValue();
+				
+				if (!isSuitableValue(modelSourceValue, value) && !isSuitableValue(modelTargetValue, value)) {
+					String messageText = Messages.getMessage(
+							Messages.INVALID_VALUE, MessageUtil.getLabel(faces, this), value);
+					
+					FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, messageText, null);
+					faces.addMessage(this.getClientId(faces), message);
+
+					setValid(false);
+					break;
 				}
-				
-			}, null);
-		} catch (IOException e) {
-			throw new FacesException(e.getLocalizedMessage(), e);
+			}
 		}
 
-		state[2] = sourceSelectionKeySet;
-		state[3] = this.sourceSelectionSet ? Boolean.TRUE : Boolean.FALSE;
+		if (isImmediate()) {
+			executeValidate(faces);
+		}
 		
-		state[4] = targetSelectionKeySet;
-		state[5] = this.targetSelectionSet ? Boolean.TRUE : Boolean.FALSE;
+        if (!isValid()) {
+            faces.renderResponse();
+        }
 
-		state[6] = activeItemSet.isEmpty() ? null : activeItemSet.iterator().next();
-		state[7] = this.activeItemSet ? Boolean.TRUE : Boolean.FALSE;
+        this.iterate(faces, decodeVisitor, argument);
+	}
 
+	private final class ModelItemState implements ItemState {
+		private Collection sourceSelectedItems;
+		private Collection targetSelectedItems;
+		private Object activeItem;
+
+		public ModelItemState(Collection sourceSelectedItems, Collection targetSelectedItems, Object activeItem) {
+			super();
+			this.sourceSelectedItems = sourceSelectedItems;
+			this.targetSelectedItems = targetSelectedItems;
+			this.activeItem = activeItem;
+		}
+
+		public boolean isSelected() {
+			Object rowData = getRowData();
+			return ((sourceSelectedItems != null && sourceSelectedItems.contains(rowData)) || 
+			(targetSelectedItems != null && targetSelectedItems.contains(rowData)));
+		}
+
+		public boolean isActive() {
+			return activeItem != null && activeItem.equals(getRowData());
+		}
+	}
+
+	public Object saveState(FacesContext context) {
+		Object[] state = new Object[2];
+
+		state[0] = super.saveState(context);
+
+		if (this.valueHolder != null) {
+			state[1] = this.valueHolder.saveState(context, this);
+		}
 		return state;
 	}
 
@@ -220,21 +279,15 @@
 		Object[] state = (Object[]) object;
 
 		super.restoreState(context, state[0]);
-		restoreIterationState(state[1]);
-
-		this.sourceSelection = (Collection) state[2];
-		this.sourceSelectionSet = ((Boolean) state[3]).booleanValue();
-		
-		this.targetSelection = (Collection) state[4];
-		this.targetSelectionSet = ((Boolean) state[5]).booleanValue();
-	
-		this.activeItem = state[6];
-		this.activeItemSet = ((Boolean) state[7]).booleanValue();
+		if (state[1] != null) {
+			this.valueHolder = new ValueHolder();
+			this.valueHolder.restoreState(context, this, state[1]);
+		}
 	}
 
 	public Object getSourceValue() {
-		if (sourceValue != null) {
-			return sourceValue;
+		if (valueHolder != null && valueHolder.sourceValue != null) {
+			return valueHolder.sourceValue;
 		}
 
 		ValueBinding vb = getValueBinding("sourceValue");
@@ -247,13 +300,14 @@
 
 	public void setSourceValue(Object sourceValue) {
 		setExtendedDataModel(null);
-		this.sourceValue = sourceValue;
-		this.sourceValueSet = true;
+		createValueHolder();
+		valueHolder.sourceValue = sourceValue;
+		valueHolder.sourceValueSet = true;
 	}
 
 	public Object getTargetValue() {
-		if (targetValue != null) {
-			return targetValue;
+		if (valueHolder != null && valueHolder.targetValue != null) {
+			return valueHolder.targetValue;
 		}
 
 		ValueBinding vb = getValueBinding("targetValue");
@@ -266,67 +320,32 @@
 
 	public void setTargetValue(Object targetValue) {
 		setExtendedDataModel(null);
-		this.targetValue = targetValue;
-		this.targetValueSet = true;
+		createValueHolder();
+		valueHolder.targetValue = targetValue;
+		valueHolder.targetValueSet = true;
 	}
 
 	public void setSubmittedStrings(Map map, Set sourceSelection, Set targetSelection, Object activeItem) {
 		this.submittedValueHolder = new SubmittedValue(map, sourceSelection, targetSelection, activeItem);
 	}
 
-	protected Object saveIterationSubmittedState() {
+	public Object getSubmittedValue() {
 		return submittedValueHolder;
 	}
 
+	public void setSubmittedValue(Object object) {
+		this.submittedValueHolder = (SubmittedValue) object;
+	}
+	
 	protected void restoreIterationSubmittedState(Object object) {
 		this.submittedValueHolder = (SubmittedValue) object;
 	}
 
-	protected Object saveIterationState() {
-		ValueHolder holder = new ValueHolder();
-
-		holder.sourceValue = sourceValue;
-		holder.sourceValueSet = sourceValueSet;
-
-		holder.targetValue = targetValue;
-		holder.targetValueSet = targetValueSet;
-
-		holder.sourceSelection = this.sourceSelection;
-		holder.sourceSelectionSet = this.sourceSelectionSet;
-		
-		holder.targetSelection = this.targetSelection;
-		holder.targetSelectionSet = this.targetSelectionSet;
-
-		holder.map = map;
-
-		return holder;
-	}
-
-	protected void restoreIterationState(Object object) {
-		ValueHolder holder = (ValueHolder) object;
-
-		this.sourceValue = holder.sourceValue ;
-		this.sourceValueSet = holder.sourceValueSet;
-
-		this.targetValue = holder.targetValue;
-		this.targetValueSet = holder.targetValueSet;
-
-		this.sourceSelection = holder.sourceSelection;
-		this.sourceSelectionSet = holder.sourceSelectionSet;
-		
-		this.targetSelection = holder.targetSelection;
-		this.targetSelectionSet = holder.targetSelectionSet;
-
-		map = holder.map;
-	}
-
 	public org.ajax4jsf.model.ExtendedDataModel createDataModel() {
 		Map source = null;
 		
 		if (submittedValueHolder != null) {
 			source = submittedValueHolder.map;
-		} else {
-			source = this.map;
 		}
 		
 		if (source != null) {
@@ -361,27 +380,42 @@
 	protected final UpdateModelCommand updateTargetSelectionCommand = new UpdateModelCommand() {
 
 		public void execute(FacesContext context) {
-			if (targetSelectionSet) {
+			if (valueHolder.targetSelectionSet) {
 				ValueBinding vb = getValueBinding("targetSelection");
 				if (vb != null) {
-					vb.setValue(context, targetSelection);
-					targetSelection = null;
-					targetSelectionSet = false;
+					vb.setValue(context, valueHolder.targetSelection);
+					valueHolder.targetSelection = null;
+					valueHolder.targetSelectionSet = false;
 				}
 			}
 		}
 		
 	};
 	
+	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 updateSourceSelectionCommand = new UpdateModelCommand() {
 
 		public void execute(FacesContext context) {
-			if (sourceSelectionSet) {
+			if (valueHolder.sourceSelectionSet) {
 				ValueBinding vb = getValueBinding("sourceSelection");
 				if (vb != null) {
-					vb.setValue(context, sourceSelection);
-					sourceSelection = null;
-					sourceSelectionSet = false;
+					vb.setValue(context, valueHolder.sourceSelection);
+					valueHolder.sourceSelection = null;
+					valueHolder.sourceSelectionSet = false;
 				}
 			}
 		}
@@ -391,12 +425,12 @@
 	private final UpdateModelCommand updateSourceCommand = new UpdateModelCommand() {
 
 		public void execute(FacesContext context) {
-			if (sourceValueSet) {
+			if (valueHolder.sourceValueSet) {
 				ValueBinding vb = getValueBinding("sourceValue");
 				if (vb != null) {
-					vb.setValue(context, sourceValue);
-					sourceValue = null;
-					sourceValueSet = false;
+					vb.setValue(context, valueHolder.sourceValue);
+					valueHolder.sourceValue = null;
+					valueHolder.sourceValueSet = false;
 				}
 			}
 		}
@@ -406,12 +440,12 @@
 	private final UpdateModelCommand updateTargetCommand = new UpdateModelCommand() {
 
 		public void execute(FacesContext context) {
-			if (targetValueSet) {
+			if (valueHolder.targetValueSet) {
 				ValueBinding vb = getValueBinding("targetValue");
 				if (vb != null) {
-					vb.setValue(context, targetValue);
-					targetValue = null;
-					targetValueSet = false;
+					vb.setValue(context, valueHolder.targetValue);
+					valueHolder.targetValue = null;
+					valueHolder.targetValueSet = false;
 				}
 			}
 		}
@@ -460,11 +494,13 @@
 			return;
 		}
 
-		updateModel(context, updateActiveItemCommand);
-		updateModel(context, updateSourceSelectionCommand);
-		updateModel(context, updateTargetSelectionCommand);
-		updateModel(context, updateSourceCommand);
-		updateModel(context, updateTargetCommand);
+		if (valueHolder != null) {
+			updateModel(context, updateActiveItemCommand);
+			updateModel(context, updateSourceSelectionCommand);
+			updateModel(context, updateTargetSelectionCommand);
+			updateModel(context, updateSourceCommand);
+			updateModel(context, updateTargetCommand);
+		}
 	}
 
 
@@ -572,33 +608,26 @@
 			setSourceValue(newSourceValue);
 			setTargetValue(newTargetValue);
 
-			setTranslatedState();
-
 			if (compareValues(previousSource, newSourceValue) || compareValues(previousTarget, newTargetValue)) {
 				queueEvent(new ValueChangeEvent(this, 
 						new Object[]{previousSource,previousTarget}, 
 						new Object[]{newSourceValue, newTargetValue}));
 			}
 
-			this.map = this.submittedValueHolder.map;
-
-			this.submittedValueHolder = null;
-
+			this.submittedValueHolder.setNull();
 		}
 	}
 
 	protected void resetDataModel() {
 		super.resetDataModel();
 
-		this.map = null;
-
-		if (this.submittedValueHolder != null) {
-			setTranslatedRenderingState();
+		if (submittedValueHolder != null) {
+			submittedValueHolder.resetDataModel();
 		}
 	}
 
 	public ItemState getItemState() {
-		if (submittedValueHolder != null) {
+		if (submittedValueHolder != null && !submittedValueHolder.isNull()) {
 			return new ModelItemState(submittedValueHolder.sourceSelection, submittedValueHolder.targetSelection, 
 					submittedValueHolder.activeItem);
 		} else {
@@ -618,9 +647,15 @@
 	public abstract boolean isFastMoveControlsVisible();
 	public abstract void setFastMoveControlsVisible(boolean visible);
 
+	private void createValueHolder() {
+		if (valueHolder == null) {
+			valueHolder = new ValueHolder();
+		}
+	}
+	
 	public Collection getSourceSelection() {
-		if (this.sourceSelection != null) {
-			return this.sourceSelection;
+		if (valueHolder != null && valueHolder.sourceSelection != null) {
+			return valueHolder.sourceSelection;
 		} else {
 			ValueBinding vb = getValueBinding("sourceSelection");
 			if (vb != null) {
@@ -632,13 +667,14 @@
 	}
 	
 	public void setSourceSelection(Collection collection) {
-		this.sourceSelection = collection;
-		this.sourceSelectionSet = true;
+		createValueHolder();
+		valueHolder.sourceSelection = collection;
+		valueHolder.sourceSelectionSet = true;
 	}
 
 	public Collection getTargetSelection() {
-		if (this.targetSelection != null) {
-			return this.targetSelection;
+		if (valueHolder != null && valueHolder.targetSelection != null) {
+			return valueHolder.targetSelection;
 		} else {
 			ValueBinding vb = getValueBinding("targetSelection");
 			if (vb != null) {
@@ -650,10 +686,30 @@
 	}
 	
 	public void setTargetSelection(Collection collection) {
-		this.targetSelection = collection;
-		this.targetSelectionSet = true;
+		createValueHolder();
+		valueHolder.targetSelection = collection;
+		valueHolder.targetSelectionSet = true;
 	}
 	
+	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());
+			}
+		}
+		
+		return null;
+	}
+	
+	public void setActiveItem(Object activeItem) {
+		createValueHolder();
+		valueHolder.activeItem = activeItem;
+		valueHolder.activeItemSet = true;
+	}
+	
 	public abstract String getSourceCaptionLabel();
 	public abstract void setSourceCaptionLabel(String label);
 	
@@ -667,4 +723,12 @@
 		
 		return super.getValueBinding(name);
 	}
+	
+	public void setValue(Object value) {
+		this.valueHolder = (ValueHolder) value;
+	}
+	
+	public Object getLocalValue() {
+		return this.valueHolder;
+	}
 }




More information about the richfaces-svn-commits mailing list