Author: maksimkaszynski
Date: 2007-07-06 08:01:09 -0400 (Fri, 06 Jul 2007)
New Revision: 1513
Added:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/MultiColumnSortListener.java
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/SingleColumnSortListener.java
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortField.java
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGridColumn.java
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortOrder.java
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/PropertyResolverComparator.java
branches/3.0.2/sandbox/scrollable-grid/src/test/java/org/richfaces/component/html/HtmlScrollableGridComponentTest.java
Log:
support for multiple scrolling
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-07-06
10:39:38 UTC (rev 1512)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGrid.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -28,7 +28,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.event.scroll.ScrollEvent;
+import org.richfaces.event.sort.MultiColumnSortListener;
+import org.richfaces.event.sort.SingleColumnSortListener;
import org.richfaces.event.sort.SortEvent;
+import org.richfaces.event.sort.SortListener;
import org.richfaces.model.DataModelCache;
import org.richfaces.model.GridDataModel;
import org.richfaces.model.ScrollableGridRange;
@@ -46,7 +49,10 @@
public static final String COMPONENT_TYPE =
"org.richfaces.component.ScrollableGrid";
+ public static final String SORT_SINGLE = "single";
+ public static final String SORT_MULTI = "multi";
+
private boolean MODEL_RANGE = false;
private final static Log log = LogFactory.getLog(UIScrollableGrid.class);
@@ -61,6 +67,8 @@
private String scrollPos;
+ private SortListener sortListener;
+
public int getReqRowsCount() {
return reqRowsCount;
}
@@ -202,11 +210,12 @@
public Object saveState(FacesContext context) {
- Object values[] = new Object[3];
+ Object values[] = new Object[4];
values[0] = super.saveState(context);
values[1] = getRanges();
values[2] = scrollPos;
-
+ values[3] = saveAttachedState(context, sortListener);
+
return (Object)values;
}
@@ -216,6 +225,7 @@
super.restoreState(context, values[0]);
setRanges((List)values[1]);
scrollPos = (String)values[2];
+ sortListener = (SortListener) restoreAttachedState(context, values[3]);
}
protected Iterator dataChildren() {
@@ -290,19 +300,10 @@
public void processSortingChange(FacesEvent event){
SortEvent e = (SortEvent)event;
- int sortColumn = e.getSortColumn();
- boolean asc = true;
+ getSortListener().processSort(e);
- SortOrder sortOrder = getSortOrder();
-
- if (sortOrder != null) {
- if (sortOrder.getSortColumn() == sortColumn) {
- asc = !sortOrder.isAscending();
- }
- }
- SortOrder newSortOrder = new SortOrder(sortColumn,asc);
- setSortOrder(newSortOrder);
+
resetDataModel();
getFacesContext().renderResponse();
}
@@ -366,5 +367,22 @@
public void setScrollPos(String scrollPos) {
this.scrollPos = scrollPos;
}
+
+ public SortListener getSortListener() {
+ if (sortListener != null) return sortListener;
+ if (SORT_MULTI.equals(getSortMode())) {
+ return MultiColumnSortListener.INSTANCE;
+ } else {
+ return SingleColumnSortListener.INSTANCE;
+ }
+ }
+
+ public void setSortListener(SortListener sortListener) {
+ this.sortListener = sortListener;
+ }
+ public abstract String getSortMode();
+ public abstract void setSortMode(String mode);
+
+
}
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGridColumn.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGridColumn.java 2007-07-06
10:39:38 UTC (rev 1512)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/component/UIScrollableGridColumn.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -37,4 +37,9 @@
public abstract boolean isSortable();
public abstract void setSortable(boolean sortable);
+
+ public abstract String getName();
+
+ public abstract void setName(String name);
+
}
Added:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/MultiColumnSortListener.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/MultiColumnSortListener.java
(rev 0)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/MultiColumnSortListener.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -0,0 +1,87 @@
+/**
+ *
+ */
+package org.richfaces.event.sort;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.faces.context.FacesContext;
+
+import org.richfaces.component.UIScrollableGrid;
+import org.richfaces.component.UIScrollableGridColumn;
+import org.richfaces.model.SortField;
+import org.richfaces.model.SortOrder;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class MultiColumnSortListener implements SortListener {
+
+ public static final MultiColumnSortListener INSTANCE = new MultiColumnSortListener();
+
+ private MultiColumnSortListener() {
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.event.sort.SortListener#processSort(org.richfaces.event.sort.SortEvent)
+ */
+ public void processSort(SortEvent e) {
+ UIScrollableGrid grid = (UIScrollableGrid) e.getComponent();
+ int columnIndex = e.getSortColumn();
+ UIScrollableGridColumn column =
+ (UIScrollableGridColumn) grid.getChildren().get(columnIndex);
+ String name = column.getName();
+
+ SortOrder sortOrder = grid.getSortOrder();
+ if (sortOrder == null) {
+ sortOrder = new SortOrder();
+ grid.setSortOrder(sortOrder);
+
+ if (grid.getValueBinding("sortOrder") != null) {
+ grid.getValueBinding("sortOrder").setValue(FacesContext.getCurrentInstance(),
sortOrder);
+ }
+
+ }
+
+ SortField[] fields = sortOrder.getFields();
+
+ if (fields == null) {
+ //If no sorting was applied at all, set sorting to current
+ fields = new SortField[] {new SortField(name, columnIndex, Boolean.TRUE)};
+ } else {
+
+ List newFields = Arrays.asList(fields);
+ SortField newField = null;
+
+ for (Iterator iterator = newFields.iterator(); iterator.hasNext() && newField
== null; ) {
+ SortField sortField = (SortField) iterator.next();
+ if (sortField.getIndex() == columnIndex ||
+ (sortField.getName() != null &&
+ name != null &&
+ name.equals(sortField.getName()))) {
+
+ Boolean asc = sortField.getAscending();
+ asc = Boolean.TRUE.equals(asc) ? Boolean.FALSE : Boolean.TRUE;
+
+ newField = new SortField(name, columnIndex, asc);
+ iterator.remove();
+
+ }
+ }
+
+ if (newField == null) {
+ newField = new SortField(name, columnIndex, Boolean.TRUE);
+ }
+
+ newFields.add(newField);
+ fields = (SortField[]) newFields.toArray(new SortField[newFields.size()]);
+ }
+
+ sortOrder.setFields(fields);
+ }
+
+
+}
Added:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/SingleColumnSortListener.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/SingleColumnSortListener.java
(rev 0)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/event/sort/SingleColumnSortListener.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -0,0 +1,71 @@
+/**
+ *
+ */
+package org.richfaces.event.sort;
+
+import javax.faces.context.FacesContext;
+
+import org.richfaces.component.UIScrollableGrid;
+import org.richfaces.component.UIScrollableGridColumn;
+import org.richfaces.model.SortField;
+import org.richfaces.model.SortOrder;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class SingleColumnSortListener implements SortListener {
+
+ public static final SingleColumnSortListener INSTANCE = new SingleColumnSortListener();
+
+ private SingleColumnSortListener() {
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.event.sort.SortListener#processSort(org.richfaces.event.sort.SortEvent)
+ */
+ public void processSort(SortEvent e) {
+ UIScrollableGrid grid = (UIScrollableGrid) e.getComponent();
+ int columnIndex = e.getSortColumn();
+ UIScrollableGridColumn column =
+ (UIScrollableGridColumn) grid.getChildren().get(columnIndex);
+ String name = column.getName();
+
+ SortOrder sortOrder = grid.getSortOrder();
+ if (sortOrder == null) {
+ sortOrder = new SortOrder();
+ grid.setSortOrder(sortOrder);
+
+ if (grid.getValueBinding("sortOrder") != null) {
+ grid.getValueBinding("sortOrder").setValue(FacesContext.getCurrentInstance(),
sortOrder);
+ }
+
+ }
+
+ SortField[] fields = sortOrder.getFields();
+
+ SortField newField = new SortField(name, columnIndex, Boolean.TRUE);
+
+ if (fields != null) {
+ for (int i = 0; i < fields.length; i++) {
+ SortField sortField = fields[i];
+ if (sortField.getIndex() == columnIndex ||
+ (sortField.getName() != null &&
+ name != null &&
+ name.equals(sortField.getName()))) {
+
+ Boolean asc = sortField.getAscending();
+ asc = Boolean.TRUE.equals(asc) ? Boolean.FALSE : Boolean.TRUE;
+
+ newField = new SortField(name, columnIndex, asc);
+ break;
+
+ }
+ }
+ }
+
+ sortOrder.setFields(new SortField[] {newField});
+ }
+
+
+}
Added:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortField.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortField.java
(rev 0)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortField.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -0,0 +1,89 @@
+/**
+ *
+ */
+package org.richfaces.model;
+
+import java.io.Serializable;
+
+/**
+ * @author Maksim Kaszynski
+ *
+ */
+public class SortField implements Serializable{
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+ private String name = null;
+ private int index = -1;
+ private Boolean ascending = null;
+
+
+
+ public SortField(String name, int index, Boolean ascending) {
+ super();
+ this.name = name;
+ this.index = index;
+ this.ascending = ascending;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public void setIndex(int index) {
+ this.index = index;
+ }
+
+ public Boolean getAscending() {
+ return ascending;
+ }
+
+ public void setAscending(Boolean ascending) {
+ this.ascending = ascending;
+ }
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((ascending == null) ? 0 : ascending.hashCode());
+ result = prime * result + index;
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final SortField other = (SortField) obj;
+ if (ascending == null) {
+ if (other.ascending != null)
+ return false;
+ } else if (!ascending.equals(other.ascending))
+ return false;
+ if (index != other.index)
+ return false;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
+
+
+
+}
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortOrder.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortOrder.java 2007-07-06
10:39:38 UTC (rev 1512)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/SortOrder.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -1,6 +1,7 @@
package org.richfaces.model;
import java.io.Serializable;
+import java.util.Arrays;
/**
*
* @author Maksim Kaszynski
@@ -9,66 +10,61 @@
*/
public class SortOrder implements Serializable {
+ private static int hashCode(Object[] array) {
+ final int prime = 31;
+ if (array == null)
+ return 0;
+ int result = 1;
+ for (int index = 0; index < array.length; index++) {
+ result = prime * result
+ + (array[index] == null ? 0 : array[index].hashCode());
+ }
+ return result;
+ }
+
private static final long serialVersionUID = 2423450561570551363L;
- private boolean ascending;
-
- private int sortColumn = -1;
+ private SortField [] fields;
+ public SortOrder() {
+
+ }
- public SortOrder( int sortColumn, boolean ascending) {
+ public SortOrder(SortField[] fields) {
super();
- this.sortColumn = sortColumn;
- this.ascending = ascending;
+ this.fields = fields;
}
- public SortOrder() {
- super();
- }
- public boolean isAscending() {
- return ascending;
- }
- public void setAscending(boolean ascending) {
- this.ascending = ascending;
+ public SortField[] getFields() {
+ return fields;
}
- public int getSortColumn() {
- return sortColumn;
+ public void setFields(SortField[] fields) {
+ this.fields = fields;
}
- public void setSortColumn(int sortColumn) {
- this.sortColumn = sortColumn;
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + SortOrder.hashCode(fields);
+ return result;
}
- /**
- * @see java.lang.Object#toString()
- */
- public String toString() {
- return "SortOrder: {field:" + sortColumn + ", ascending: " +
ascending
- + "}";
- }
-
- /**
- * @see java.lang.Object#equals(java.lang.Object)
- */
public boolean equals(Object obj) {
-
- if (obj instanceof SortOrder) {
- SortOrder sortOrder = (SortOrder) obj;
- return sortColumn == sortOrder.sortColumn
- && ascending == sortOrder.ascending;
-
- }
- return super.equals(obj);
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final SortOrder other = (SortOrder) obj;
+ if (!Arrays.equals(fields, other.fields))
+ return false;
+ return true;
}
- public int hashCode() {
- int c = (new Integer(sortColumn)).hashCode();
- if (ascending) {
- c = -c;
- }
- return c;
- }
+
+
}
\ No newline at end of file
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/PropertyResolverComparator.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/PropertyResolverComparator.java 2007-07-06
10:39:38 UTC (rev 1512)
+++
branches/3.0.2/sandbox/scrollable-grid/src/main/java/org/richfaces/model/impl/PropertyResolverComparator.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -18,6 +18,7 @@
import javax.faces.el.EvaluationException;
import javax.faces.el.PropertyResolver;
+import org.richfaces.model.SortField;
import org.richfaces.model.SortOrder;
@@ -41,8 +42,11 @@
public int compare(Object arg0, Object arg1) {
int result = 0;
- Object prop1 = null;
+ /*Object prop1 = null;
+ SortField [ sortOrder.getFields();
+ while (result == 0 && )
+
try {
prop1 = resolver.getValue(arg0, sortOrder.getSortColumn());
} catch (EvaluationException e) {
@@ -77,7 +81,7 @@
}
if (!sortOrder.isAscending()) {
result = -result;
- }
+ }*/
return result;
}
Modified:
branches/3.0.2/sandbox/scrollable-grid/src/test/java/org/richfaces/component/html/HtmlScrollableGridComponentTest.java
===================================================================
---
branches/3.0.2/sandbox/scrollable-grid/src/test/java/org/richfaces/component/html/HtmlScrollableGridComponentTest.java 2007-07-06
10:39:38 UTC (rev 1512)
+++
branches/3.0.2/sandbox/scrollable-grid/src/test/java/org/richfaces/component/html/HtmlScrollableGridComponentTest.java 2007-07-06
12:01:09 UTC (rev 1513)
@@ -7,6 +7,7 @@
import java.lang.String ;
import org.ajax4jsf.tests.AbstractAjax4JsfTestCase ;
+import org.richfaces.model.SortField;
import org.richfaces.model.SortOrder ;
import java.lang.Object ;
import org.richfaces.model.selection.Selection ;
@@ -183,8 +184,7 @@
assertEquals("oncomplete_oncomplete", component.getOncomplete());
component.setValueBinding("sortOrder", new
MockValueBinding("sortOrder", org.richfaces.model.SortOrder.class));
SortOrder sortOrder = new SortOrder();
- sortOrder.setSortColumn(2);
- sortOrder.setAscending(true);
+ sortOrder.setFields(new SortField[] {new SortField(null, 2, Boolean.TRUE)});
component.setSortOrder(sortOrder);
assertEquals(sortOrder, component.getSortOrder());
component.setValueBinding("footerClass", new
MockValueBinding("footerClass", java.lang.String.class));