Author: maksimkaszynski
Date: 2007-06-14 15:19:27 -0400 (Thu, 14 Jun 2007)
New Revision: 1192
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ArrayDataModel.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ListDataModel.java
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/SimpleGridDataModel.java
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
Log:
added implementations for array and list data models
Modified:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-06-14
17:45:11 UTC (rev 1191)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-06-14
19:19:27 UTC (rev 1192)
@@ -6,14 +6,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.event.PhaseId;
-import javax.faces.model.DataModel;
import org.ajax4jsf.ajax.repeat.DataComponentState;
import org.ajax4jsf.ajax.repeat.DataVisitor;
@@ -24,12 +25,16 @@
import org.ajax4jsf.framework.ajax.AjaxContext;
import org.ajax4jsf.framework.ajax.AjaxEvent;
import org.apache.commons.collections.iterators.IteratorChain;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.richfaces.event.scroll.ScrollEvent;
import org.richfaces.event.sort.SortEvent;
-import org.richfaces.model.ScrollableGridRange;
import org.richfaces.model.DataModelCache;
import org.richfaces.model.GridDataModel;
+import org.richfaces.model.ScrollableGridRange;
import org.richfaces.model.SortOrder;
+import org.richfaces.model.impl.ArrayDataModel;
+import org.richfaces.model.impl.ListDataModel;
@@ -41,8 +46,11 @@
public abstract class UIScrollableGrid extends UIRepeat implements AjaxComponent,
Sortable, Selectable{
+
public static final String COMPONENT_TYPE =
"org.richfaces.component.ScrollableGrid";
+ private final static Log log = LogFactory.getLog(UIScrollableGrid.class);
+
private transient Collection partialUpdateChildren;
private Collection responseData = new ArrayList();
@@ -108,11 +116,57 @@
Object value = getValue();
+ GridDataModel model = null;
+
if (value instanceof GridDataModel) {
- return new DataModelCache((GridDataModel) value);
+
+ if (log.isDebugEnabled()) {
+ log.debug("Found instanceof " + value.getClass() + " will use it
unwrapped");
+ }
+
+ model = (GridDataModel) value;
+
+ } else if (value instanceof List) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Found instanceof " + value.getClass() + " - will wrap it
into " + ListDataModel.class);
+ }
+
+ model = new ListDataModel((List) value);
+
+ }else if (value instanceof Object[]) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Found instanceof " + value.getClass() + " - will wrap it
into " + ArrayDataModel.class);
+ }
+
+ model = new ArrayDataModel((Object[]) value);
+
+ } else if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("Found null value - will wrap it into empty " +
ListDataModel.class);
+ }
+
+ model = new ListDataModel(Collections.EMPTY_LIST);
+ } else {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Found instanceof " + value.getClass() + " - will wrap it
into singleton " + ListDataModel.class);
+ }
+
+ model = new ListDataModel(Collections.singletonList(value));
}
- return null;
+ if (isCacheable()) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Initializing cache of type " + DataModelCache.class);
+ }
+
+ model = new DataModelCache(model);
+ }
+
+ return model;
}
@@ -223,10 +277,6 @@
super.walk(context, visitor, argument);
}
- public DataModel getStoredDataModel() {
- return super.getDataModel();
- }
-
public Collection getPartialUpdateChildren() {
if (partialUpdateChildren == null) {
@@ -241,4 +291,8 @@
super.encodeEnd(context);
partialUpdateChildren = null;
}
+
+ public boolean isCacheable() {
+ return true;
+ }
}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ArrayDataModel.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ArrayDataModel.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ArrayDataModel.java 2007-06-14
19:19:27 UTC (rev 1192)
@@ -0,0 +1,90 @@
+/**
+ *
+ */
+package org.richfaces.model.impl;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.richfaces.model.SortOrder;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class ArrayDataModel extends SimpleGridDataModel {
+
+ private Object [] data;
+
+ public ArrayDataModel() {
+ this(null);
+ }
+
+ public ArrayDataModel(Object [] data) {
+ super();
+ setWrappedData(data);
+ }
+
+
+
+ /* (non-Javadoc)
+ * @see org.richfaces.model.GridDataModel#loadData(int, int,
org.richfaces.model.SortOrder)
+ */
+ public List loadData(int startRow, int endRow, SortOrder sortOrder) {
+
+ if (data != null && getRowCount() > 0) {
+ Object [] sortedList = data;
+
+ if (sortOrder != null) {
+
+ sortedList = new Object[data.length];
+
+ System.arraycopy(data, 0, sortedList, 0, data.length);
+
+ Comparator comparator = createComparator(sortOrder);
+
+ if (comparator == null) {
+
+ Arrays.sort(sortedList);
+
+ } else {
+
+ Arrays.sort(sortedList, comparator);
+
+ }
+
+ }
+
+ Object [] subArray = new Object[endRow - startRow];
+
+ System.arraycopy(sortedList, startRow, subArray, 0, subArray.length);
+
+ return Arrays.asList(subArray);
+ }
+
+ return Collections.EMPTY_LIST;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getRowCount()
+ */
+ public int getRowCount() {
+ return data == null ? 0 : data.length;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getWrappedData()
+ */
+ public Object getWrappedData() {
+ return data;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
+ */
+ public void setWrappedData(Object data) {
+ this.data = (Object[]) data;
+ }
+}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ListDataModel.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ListDataModel.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/ListDataModel.java 2007-06-14
19:19:27 UTC (rev 1192)
@@ -0,0 +1,84 @@
+/**
+ *
+ */
+package org.richfaces.model.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.richfaces.model.SortOrder;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class ListDataModel extends SimpleGridDataModel {
+
+ private List data;
+
+ public ListDataModel() {
+ this(null);
+ }
+
+ public ListDataModel(List data) {
+ super();
+ setWrappedData(data);
+ }
+
+
+
+ /* (non-Javadoc)
+ * @see org.richfaces.model.GridDataModel#loadData(int, int,
org.richfaces.model.SortOrder)
+ */
+ public List loadData(int startRow, int endRow, SortOrder sortOrder) {
+
+ if (data != null && getRowCount() > 0) {
+ List sortedList = data;
+
+ if (sortOrder != null) {
+ sortedList = new ArrayList(data);
+
+ Comparator comparator = createComparator(sortOrder);
+
+ if (comparator == null) {
+
+ Collections.sort(sortedList);
+
+ } else {
+
+ Collections.sort(sortedList, comparator);
+
+ }
+
+ }
+
+ return sortedList.subList(startRow, endRow);
+ }
+
+ return Collections.EMPTY_LIST;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getRowCount()
+ */
+ public int getRowCount() {
+ return data == null ? 0 : data.size();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#getWrappedData()
+ */
+ public Object getWrappedData() {
+ return data;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
+ */
+ public void setWrappedData(Object data) {
+ this.data = (List) data;
+ }
+
+}
Added:
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/SimpleGridDataModel.java
===================================================================
---
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/SimpleGridDataModel.java
(rev 0)
+++
trunk/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/SimpleGridDataModel.java 2007-06-14
19:19:27 UTC (rev 1192)
@@ -0,0 +1,14 @@
+package org.richfaces.model.impl;
+
+import java.util.Comparator;
+
+import org.richfaces.model.GridDataModel;
+import org.richfaces.model.SortOrder;
+
+public abstract class SimpleGridDataModel extends GridDataModel {
+
+ protected Comparator createComparator(SortOrder sortOrder) {
+ return new PropertyResolverComparator(sortOrder);
+ }
+
+}
Show replies by date