Author: nbelaevski
Date: 2007-11-20 08:31:05 -0500 (Tue, 20 Nov 2007)
New Revision: 4102
Added:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleDataModel.java
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleRowKey.java
Modified:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/renderkit/ListShuttleRendererBase.java
Log:
listShuttle uses refactored orderingList base renderer
Modified:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java
===================================================================
---
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java 2007-11-20
13:29:51 UTC (rev 4101)
+++
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/component/UIListShuttle.java 2007-11-20
13:31:05 UTC (rev 4102)
@@ -4,11 +4,20 @@
package org.richfaces.component;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.event.ValueChangeListener;
+import org.richfaces.model.ListShuttleRowKey;
+
/**
* JSF component class
*
@@ -19,6 +28,71 @@
public static final String COMPONENT_FAMILY = "org.richfaces.ListShuttle";
+ public static final class SubmittedValue {
+ //ListShuttleRowKey -> ListShuttleRowKey
+ private Map translationTable = new LinkedHashMap();
+ private Set selectedItems = new HashSet();
+ private ListShuttleRowKey[] activeItems;
+
+ public SubmittedValue(String submittedString) {
+ super();
+ fromString(submittedString);
+ }
+
+ private void fromString(String string) {
+// String[] valueOrder = string.split(",");
+// permutationOrder = new int[valueOrder.length];
+// for (int i = 0; i < valueOrder.length; i++) {
+// Matcher matcher =
Pattern.compile("(\\d+)(s?a?)").matcher(valueOrder[i]);
+// matcher.matches();
+//
+// try {
+// permutationOrder[i] = Integer.valueOf(matcher.group(1)).intValue();
+// String group = matcher.group(2);
+// for (int j = 0; j < group.length(); j++) {
+// char c = group.charAt(j);
+// if ('s' == c) {
+// selectedItems.add(new Integer(permutationOrder[i]));
+// } else if ('a' == c) {
+// activeItem = new Integer(permutationOrder[i]);
+// } else {
+// break ;
+// }
+//
+// }
+// } catch (NumberFormatException e) {
+// // TODO: handle exception
+// }
+// }
+ }
+
+ public String toString() {
+ StringBuffer result = new StringBuffer();
+// for (int i = 0; i < permutationOrder.length; i++) {
+// result.append(permutationOrder[i]);
+//
+// Integer key = new Integer(i);
+//
+// if (selectedItems.contains(key)) {
+// result.append('s');
+// }
+//
+// if (key.equals(activeItem)) {
+// result.append('a');
+// }
+//
+// if (i < permutationOrder.length - 1) {
+// result.append(',');
+// }
+// }
+ return result.toString();
+ }
+
+ public Map getTranslationTable() {
+ return translationTable;
+ }
+ }
+
private Object sourceValue;
private boolean sourceValueSet;
@@ -57,15 +131,15 @@
}
public Object getLocalTargetValue() {
- return sourceValue;
+ return targetValue;
}
public Object getTargetValue() {
- if (sourceValue != null) {
- return sourceValue;
+ if (targetValue != null) {
+ return targetValue;
}
- ValueBinding vb = getValueBinding("sourceValue");
+ ValueBinding vb = getValueBinding("targetValue");
if (vb != null) {
return vb.getValue(FacesContext.getCurrentInstance());
}
@@ -110,10 +184,11 @@
public abstract MethodBinding getValueChangeListener();
public ValueChangeListener[] getValueChangeListeners() {
- return null;
+ return (ValueChangeListener[]) getFacesListeners(ValueChangeListener.class);
}
public void removeValueChangeListener(ValueChangeListener listener) {
+ removeFacesListener(listener);
}
}
Added:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleDataModel.java
===================================================================
---
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleDataModel.java
(rev 0)
+++
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleDataModel.java 2007-11-20
13:31:05 UTC (rev 4102)
@@ -0,0 +1,206 @@
+/**
+ *
+ */
+package org.richfaces.model;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.faces.model.DataModel;
+
+import org.ajax4jsf.model.DataVisitor;
+import org.ajax4jsf.model.ExtendedDataModel;
+import org.ajax4jsf.model.Range;
+import org.ajax4jsf.model.SequenceDataModel;
+import org.ajax4jsf.model.SequenceRange;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ListShuttleDataModel extends ExtendedDataModel {
+
+ private Object rowKey;
+
+ private boolean translatedModel;
+ private Map translationTable;
+ private SequenceDataModel sourceModel;
+ private SequenceDataModel targetModel;
+
+ protected static final class TranslatedRowKey {
+ private Object rowKey;
+
+ private String asString;
+
+ public TranslatedRowKey(Object key, String asString) {
+ super();
+ this.rowKey = key;
+ this.asString = asString;
+ }
+
+ public Object getRowKey() {
+ return rowKey;
+ }
+
+ public String getAsString() {
+ return asString;
+ }
+
+ public String toString() {
+ return getAsString();
+ }
+ };
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
+ */
+ @Override
+ public Object getRowKey() {
+ return rowKey;
+ }
+
+ private void setShuttleRowKey(ListShuttleRowKey shuttleRowKey) {
+ if (shuttleRowKey == null) {
+ this.sourceModel.setRowKey(null);
+ this.targetModel.setRowKey(null);
+ } else {
+ if (shuttleRowKey.isSource()) {
+ this.targetModel.setRowKey(null);
+ this.sourceModel.setRowKey(shuttleRowKey.getRowKey());
+ } else {
+ this.sourceModel.setRowKey(null);
+ this.targetModel.setRowKey(shuttleRowKey.getRowKey());
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)
+ */
+ @Override
+ public void setRowKey(Object key) {
+ this.rowKey = key;
+
+ if (key instanceof TranslatedRowKey) {
+ ListShuttleRowKey shuttleRowKey = (ListShuttleRowKey) ((TranslatedRowKey)
key).getRowKey();
+ setShuttleRowKey(shuttleRowKey);
+ } else {
+ if (this.translatedModel) {
+ if (rowKey != null) {
+ setShuttleRowKey((ListShuttleRowKey) this.translationTable.get(key));
+ } else {
+ setShuttleRowKey(null);
+ }
+ } else {
+ setShuttleRowKey((ListShuttleRowKey) key);
+ }
+ }
+ }
+
+ public ListShuttleDataModel(DataModel source, DataModel target,
+ boolean translatedModel, Map translationTable) {
+ super();
+ this.translatedModel = translatedModel;
+ this.translationTable = translationTable;
+
+ this.sourceModel = new org.ajax4jsf.model.SequenceDataModel(source);
+ this.targetModel = new org.ajax4jsf.model.SequenceDataModel(target);
+ }
+
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.model.ExtendedDataModel#walk(javax.faces.context.FacesContext,
org.ajax4jsf.model.DataVisitor, org.ajax4jsf.model.Range, java.lang.Object)
+ */
+ @Override
+ public void walk(final FacesContext context, final DataVisitor visitor, Range range,
+ Object argument) throws IOException {
+
+ this.sourceModel.walk(context, new DataVisitor() { @Override
+ public void process(FacesContext context, Object rowKey,
+ Object argument) throws IOException {
+
+ ListShuttleRowKey shuttleRowKey = new ListShuttleRowKey(rowKey, true);
+
+ if (translatedModel) {
+ visitor.process(context, new TranslatedRowKey(
+ shuttleRowKey, String.valueOf(translationTable.get(shuttleRowKey))), argument);
+ } else {
+ visitor.process(context, translationTable.get(shuttleRowKey), argument);
+ }
+ }
+ }, new SequenceRange(0, -1), null);
+
+ this.targetModel.walk(context, new DataVisitor(){
+ public void process(FacesContext context, Object rowKey,
+ Object argument) throws IOException {
+
+ ListShuttleRowKey shuttleRowKey = new ListShuttleRowKey(rowKey, false);
+
+ if (translatedModel) {
+ visitor.process(context, new TranslatedRowKey(
+ shuttleRowKey, String.valueOf(translationTable.get(shuttleRowKey))), argument);
+ } else {
+ visitor.process(context, translationTable.get(shuttleRowKey), argument);
+ }
+ }
+ }, new SequenceRange(0, -1), null);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getRowCount()
+ */
+ @Override
+ public int getRowCount() {
+ return sourceModel.getRowCount() + targetModel.getRowCount();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getRowData()
+ */
+ @Override
+ public Object getRowData() {
+ return sourceModel.isRowAvailable() ? sourceModel.getRowData() :
targetModel.getRowData();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getRowIndex()
+ */
+ @Override
+ public int getRowIndex() {
+ return -1;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getWrappedData()
+ */
+ @Override
+ public Object getWrappedData() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#isRowAvailable()
+ */
+ @Override
+ public boolean isRowAvailable() {
+ return sourceModel.isRowAvailable() || targetModel.isRowAvailable();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#setRowIndex(int)
+ */
+ @Override
+ public void setRowIndex(int rowIndex) {
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
+ */
+ @Override
+ public void setWrappedData(Object data) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleRowKey.java
===================================================================
---
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleRowKey.java
(rev 0)
+++
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/model/ListShuttleRowKey.java 2007-11-20
13:31:05 UTC (rev 4102)
@@ -0,0 +1,66 @@
+/**
+ *
+ */
+package org.richfaces.model;
+
+import java.io.Serializable;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ListShuttleRowKey implements Serializable {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 3308741255288495879L;
+
+ private boolean source;
+
+ private Object rowKey;
+
+ public boolean isSource() {
+ return source;
+ }
+
+ public Object getRowKey() {
+ return rowKey;
+ }
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((rowKey == null) ? 0 : rowKey.hashCode());
+ result = prime * result + (source ? 1231 : 1237);
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ListShuttleRowKey other = (ListShuttleRowKey) obj;
+ if (rowKey == null) {
+ if (other.rowKey != null)
+ return false;
+ } else if (!rowKey.equals(other.rowKey))
+ return false;
+ if (source != other.source)
+ return false;
+ return true;
+ }
+
+ public String toString() {
+ return rowKey.toString() + (source ? "y" : "n");
+ }
+
+ public ListShuttleRowKey(Object rowKey, boolean source) {
+ super();
+ this.rowKey = rowKey;
+ this.source = source;
+ }
+}
Modified:
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/renderkit/ListShuttleRendererBase.java
===================================================================
---
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/renderkit/ListShuttleRendererBase.java 2007-11-20
13:29:51 UTC (rev 4101)
+++
branches/3.1.x/sandbox/ui/listShuttle/src/main/java/org/richfaces/renderkit/ListShuttleRendererBase.java 2007-11-20
13:31:05 UTC (rev 4102)
@@ -3,36 +3,83 @@
*/
package org.richfaces.renderkit;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.component.UIColumn;
+import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
import org.richfaces.component.UIListShuttle;
/**
* @author Nick Belaevski
*
*/
-public abstract class ListShuttleRendererBase extends OrderingListRendererBase {
+public abstract class ListShuttleRendererBase extends OrderingComponentRendererBase {
+ private static final String MESSAGE_BUNDLE_NAME =
OrderingListRendererBase.class.getPackage().getName() + "ListShuttle";
+
+ public ListShuttleRendererBase() {
+ super(MESSAGE_BUNDLE_NAME);
+ }
+
public void encodeTLCaption(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeTLHeader(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeTLRows(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeSLCaption(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeSLControlsFacets(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeSLHeader(FacesContext context, UIListShuttle shuttle) {
-
+
}
public void encodeSLRows(FacesContext context, UIListShuttle shuttle) {
-
+
}
+
+ public void encodeOneRow(FacesContext context, TableHolder holder)
+ throws IOException {
+ ResponseWriter writer = context.getResponseWriter();
+ UIListShuttle table = (UIListShuttle) holder.getTable();
+ String clientId = holder.getTable().getClientId(context);
+ writer.startElement(HTML.TR_ELEMENT, table);
+ writer.writeAttribute("id", clientId, null);
+
+ StringBuffer rowClassName = new StringBuffer("ol_normal
rich-ordering-list-row");
+ StringBuffer cellClassName = new StringBuffer("ol_cell
rich-ordering-list-cell");
+
+ writer.writeAttribute("class", rowClassName.toString(), null);
+
+ List children = table.getChildren();
+ for (Iterator iterator = children.iterator(); iterator.hasNext();) {
+ UIComponent component = (UIComponent) iterator.next();
+
+ if (component instanceof UIColumn && component.isRendered()) {
+ UIColumn column = (UIColumn) component;
+
+ writer.startElement(HTML.td_ELEM, table);
+
+ writer.writeAttribute("class", cellClassName.toString(), null);
+
+ renderChildren(context, column);
+
+ writer.endElement(HTML.td_ELEM);
+ }
+ }
+
+ writer.endElement(HTML.TR_ELEMENT);
+ }
}