You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2006/02/28 19:21:04 UTC
svn commit: r381728 - in
/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago:
component/Sorter.java model/SheetState.java util/BeanComparator.java
util/ComparatorBase.java util/ValueBindingComparator.java
Author: weber
Date: Tue Feb 28 10:21:02 2006
New Revision: 381728
URL: http://svn.apache.org/viewcvs?rev=381728&view=rev
Log:
improve sorting
Added:
incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ComparatorBase.java
Modified:
incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Sorter.java
incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/model/SheetState.java
incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java
incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ValueBindingComparator.java
Modified: incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Sorter.java
URL: http://svn.apache.org/viewcvs/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Sorter.java?rev=381728&r1=381727&r2=381728&view=diff
==============================================================================
--- incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Sorter.java (original)
+++ incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/component/Sorter.java Tue Feb 28 10:21:02 2006
@@ -25,16 +25,22 @@
import org.apache.myfaces.tobago.util.BeanComparator;
import org.apache.myfaces.tobago.util.ValueBindingComparator;
-import javax.faces.component.*;
+import javax.faces.component.UIColumn;
+import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
+import javax.faces.component.UISelectBoolean;
import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.MethodBinding;
import javax.faces.el.MethodNotFoundException;
import javax.faces.el.ValueBinding;
import javax.faces.model.DataModel;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
/**
* User: weber
@@ -45,6 +51,7 @@
private static final Log LOG = LogFactory.getLog(Sorter.class);
+ private Comparator comparator;
public Object invoke(FacesContext facesContext, Object[] aobj)
throws EvaluationException {
@@ -62,13 +69,13 @@
}
SheetState sheetState = data.getSheetState(facesContext);
- Comparator comparator = null;
+ Comparator actualComparator = null;
if (value instanceof List || value instanceof Object[]) {
String sortProperty;
try {
- if (!updateSheetState(data, column, sheetState)) {
+ if (!sheetState.updateSortState(sortEvent)) {
return null;
}
@@ -88,15 +95,15 @@
}
sortProperty = expressionString.substring(var.length() + 1);
- comparator = new BeanComparator(
- sortProperty, null, !sheetState.isAscending());
+ actualComparator = new BeanComparator(
+ sortProperty, comparator, !sheetState.isAscending());
if (LOG.isDebugEnabled()) {
LOG.debug("Sort property is " + sortProperty);
}
} else {
- comparator = new ValueBindingComparator(
- facesContext, var, valueBinding, !sheetState.isAscending());
+ actualComparator = new ValueBindingComparator(facesContext, var,
+ valueBinding, !sheetState.isAscending(), comparator);
}
}
@@ -119,9 +126,9 @@
// comparator = new RowComparator(ascending, method);
if (value instanceof List) {
- Collections.sort((List) value, comparator);
+ Collections.sort((List) value, actualComparator);
} else { // value is instanceof Object[]
- Arrays.sort((Object[]) value, comparator);
+ Arrays.sort((Object[]) value, actualComparator);
}
} else { // DataModel?, ResultSet, Result or Object
@@ -132,33 +139,6 @@
return null;
}
- private boolean updateSheetState(UIData data, UIColumn uiColumn, SheetState sheetState) {
- int actualColumn = -1;
- List<UIColumn> rendererdColumns = data.getRendererdColumns();
- for (int i = 0; i < rendererdColumns.size(); i++) {
- if (uiColumn == rendererdColumns.get(i)) {
- actualColumn = i;
- break;
- }
- }
- if (actualColumn == -1) {
- LOG.warn("Can't find column to sort in rendered columns of sheet!");
- return false;
- }
-
- int column = sheetState.getSortedColumn();
- boolean ascending = sheetState.isAscending();
- if (actualColumn == column) {
- ascending = !ascending;
- } else {
- ascending = true;
- column = actualColumn;
- }
- sheetState.setAscending(ascending);
- sheetState.setSortedColumn(column);
- return true;
- }
-
private boolean isSimpleProperty(String expressionString) {
return expressionString.matches("^#\\{(\\w+(\\.\\w)*)\\}$");
}
@@ -196,5 +176,12 @@
return String.class;
}
+ public Comparator getComparator() {
+ return comparator;
+ }
+
+ public void setComparator(Comparator comparator) {
+ this.comparator = comparator;
+ }
}
Modified: incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/model/SheetState.java
URL: http://svn.apache.org/viewcvs/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/model/SheetState.java?rev=381728&r1=381727&r2=381728&view=diff
==============================================================================
--- incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/model/SheetState.java (original)
+++ incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/model/SheetState.java Tue Feb 28 10:21:02 2006
@@ -18,7 +18,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.component.UIData;
+import org.apache.myfaces.tobago.event.SortActionEvent;
+import javax.faces.component.UIColumn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@@ -38,6 +41,8 @@
resetSelected();
}
+
+
public void resetSelected() {
selectedRows = new ArrayList<Integer>();
}
@@ -80,5 +85,30 @@
public void setFirst(int first) {
this.first = first;
+ }
+
+ public boolean updateSortState(SortActionEvent sortEvent) {
+ UIData sheet = sortEvent.getSheet();
+ UIColumn uiColumn = sortEvent.getColumn();
+ int actualColumn = -1;
+ List<UIColumn> rendererdColumns = sheet.getRendererdColumns();
+ for (int i = 0; i < rendererdColumns.size(); i++) {
+ if (uiColumn == rendererdColumns.get(i)) {
+ actualColumn = i;
+ break;
+ }
+ }
+ if (actualColumn == -1) {
+ LOG.warn("Can't find column to sort in rendered columns of sheet!");
+ return false;
+ }
+
+ if (actualColumn == sortedColumn) {
+ ascending = !ascending;
+ } else {
+ ascending = true;
+ sortedColumn = actualColumn;
+ }
+ return true;
}
}
Modified: incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java
URL: http://svn.apache.org/viewcvs/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java?rev=381728&r1=381727&r2=381728&view=diff
==============================================================================
--- incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java (original)
+++ incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/BeanComparator.java Tue Feb 28 10:21:02 2006
@@ -20,8 +20,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import java.text.CollationKey;
-import java.text.Collator;
import java.util.Comparator;
/**
@@ -29,34 +27,30 @@
* @author Volker Weber
*/
-public class BeanComparator implements Comparator {
+public class BeanComparator extends ComparatorBase {
private static final Log LOG = LogFactory.getLog(BeanComparator.class);
private String property;
- private Comparator comparator;
-
- private boolean reverse;
public BeanComparator(String property) {
this.property = property;
}
public BeanComparator(String property, boolean reverse) {
+ super(reverse);
this.property = property;
- this.reverse = reverse;
}
public BeanComparator(String property, Comparator comparator) {
+ super(comparator);
this.property = property;
- this.comparator = comparator;
}
public BeanComparator(String property, Comparator comparator, boolean reverse) {
+ super(reverse, comparator);
this.property = property;
- this.comparator = comparator;
- this.reverse = reverse;
}
/**
@@ -65,9 +59,12 @@
* @return <description>
*/
public boolean equals(Object param1) {
+ if (this == param1) {
+ return true;
+ }
if (param1 instanceof BeanComparator) {
return ((BeanComparator) param1).getProperty().equals(property)
- && ((BeanComparator) param1).getComparator().equals(comparator);
+ && super.equals(param1);
}
return false;
}
@@ -75,7 +72,7 @@
public int hashCode() {
int result;
result = (property != null ? property.hashCode() : 0);
- result = 29 * result + (comparator != null ? comparator.hashCode() : 0);
+ result = 29 * result + super.hashCode();
return result;
}
@@ -99,49 +96,10 @@
return 0;
}
- if (obj1 == null || obj2 == null) {
- if (obj1 == null && obj2 == null) {
- return 0;
- }
- if (obj1 == null) {
- return reverse ? 1 : -1;
- } else {
- return reverse ? -1 : 1;
- }
- }
-
- if (!obj1.getClass().isInstance(obj2)) {
- throw new ClassCastException(obj1.getClass().getName() + " != "
- + obj2.getClass().getName());
- }
-
- int result;
-
-
- if (comparator instanceof Collator) {
- CollationKey collationKey1
- = ((Collator) comparator).getCollationKey(obj1.toString());
- CollationKey collationKey2
- = ((Collator) comparator).getCollationKey(obj2.toString());
- result = collationKey1.compareTo(collationKey2);
-
- } else if (comparator != null) {
- result = comparator.compare(obj1, obj2);
- } else {
- if (obj1 instanceof Comparable) {
- result = ((Comparable) obj1).compareTo(obj2);
- } else {
- result = obj1.toString().compareTo(obj2.toString());
- }
- }
- return reverse ? -result : result;
+ return internalCompare(obj1, obj2);
}
public String getProperty() {
return this.property;
- }
-
- public Comparator getComparator() {
- return comparator;
}
}
Added: incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ComparatorBase.java
URL: http://svn.apache.org/viewcvs/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ComparatorBase.java?rev=381728&view=auto
==============================================================================
--- incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ComparatorBase.java (added)
+++ incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ComparatorBase.java Tue Feb 28 10:21:02 2006
@@ -0,0 +1,99 @@
+package org.apache.myfaces.tobago.util;
+
+import java.text.CollationKey;
+import java.text.Collator;
+import java.util.Comparator;
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public abstract class ComparatorBase implements Comparator {
+
+
+ private Comparator comparator;
+
+ private boolean reverse;
+
+
+ protected ComparatorBase() {
+ }
+
+ protected ComparatorBase(boolean reverse, Comparator comparator) {
+ this.comparator = comparator;
+ this.reverse = reverse;
+ }
+ protected ComparatorBase(boolean reverse) {
+ this.reverse = reverse;
+ }
+
+ protected ComparatorBase(Comparator comparator) {
+ this.comparator = comparator;
+ }
+
+ protected int internalCompare(Object obj1, Object obj2){
+
+ if (obj1 == null || obj2 == null) {
+ if (obj1 == null && obj2 == null) {
+ return 0;
+ }
+ if (obj1 == null) {
+ return reverse ? 1 : -1;
+ } else {
+ return reverse ? -1 : 1;
+ }
+ }
+
+ if (!obj1.getClass().isInstance(obj2)) {
+ throw new ClassCastException(obj1.getClass().getName() + " != "
+ + obj2.getClass().getName());
+ }
+
+ int result;
+
+
+ if (comparator instanceof Collator) {
+ CollationKey collationKey1
+ = ((Collator) comparator).getCollationKey(obj1.toString());
+ CollationKey collationKey2
+ = ((Collator) comparator).getCollationKey(obj2.toString());
+ result = collationKey1.compareTo(collationKey2);
+
+ } else if (comparator != null) {
+ result = comparator.compare(obj1, obj2);
+ } else {
+ if (obj1 instanceof String) {
+ result = ((String) obj1).compareToIgnoreCase((String) obj2);
+ } else if (obj1 instanceof Comparable) {
+ result = ((Comparable) obj1).compareTo(obj2);
+ } else {
+ result = obj1.toString().compareTo(obj2.toString());
+ }
+ }
+ return reverse ? -result : result;
+ }
+
+
+ public boolean equals(Object o) {
+ return ((ComparatorBase) o).getComparator().equals(comparator);
+ }
+
+ public int hashCode() {
+ return (comparator != null ? comparator.hashCode() : 0);
+ }
+
+ public Comparator getComparator() {
+ return comparator;
+ }
+}
Modified: incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ValueBindingComparator.java
URL: http://svn.apache.org/viewcvs/incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ValueBindingComparator.java?rev=381728&r1=381727&r2=381728&view=diff
==============================================================================
--- incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ValueBindingComparator.java (original)
+++ incubator/tobago/trunk/tobago-core/src/main/java/org/apache/myfaces/tobago/util/ValueBindingComparator.java Tue Feb 28 10:21:02 2006
@@ -21,8 +21,6 @@
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
-import java.text.CollationKey;
-import java.text.Collator;
import java.util.Comparator;
import java.util.Map;
@@ -31,7 +29,7 @@
* @author Volker Weber
*/
-public class ValueBindingComparator implements Comparator {
+public class ValueBindingComparator extends ComparatorBase {
private static final Log LOG = LogFactory.getLog(ValueBindingComparator.class);
@@ -41,10 +39,6 @@
private ValueBinding valueBinding;
- private Comparator comparator;
-
- private boolean reverse;
-
public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding) {
this.facesContext = facesContext;
this.var = var;
@@ -52,27 +46,26 @@
}
public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding, boolean reverse) {
+ super(reverse);
this.facesContext = facesContext;
this.var = var;
this.valueBinding = valueBinding;
- this.reverse = reverse;
}
public ValueBindingComparator(FacesContext facesContext, String var,
ValueBinding valueBinding, Comparator comparator) {
+ super(comparator);
this.facesContext = facesContext;
this.var = var;
this.valueBinding = valueBinding;
- this.comparator = comparator;
}
public ValueBindingComparator(FacesContext facesContext, String var,
ValueBinding valueBinding, boolean reverse, Comparator comparator) {
+ super(reverse, comparator);
this.facesContext = facesContext;
this.var = var;
this.valueBinding = valueBinding;
- this.reverse = reverse;
- this.comparator = comparator;
}
public boolean equals(Object o) {
@@ -85,10 +78,7 @@
final ValueBindingComparator that = (ValueBindingComparator) o;
- if (reverse != that.reverse) {
- return false;
- }
- if (comparator != null ? !comparator.equals(that.comparator) : that.comparator != null) {
+ if (! super.equals(o)) {
return false;
}
if (facesContext != null ? !facesContext.equals(that.facesContext) : that.facesContext != null) {
@@ -109,8 +99,7 @@
result = (facesContext != null ? facesContext.hashCode() : 0);
result = 29 * result + (var != null ? var.hashCode() : 0);
result = 29 * result + (valueBinding != null ? valueBinding.hashCode() : 0);
- result = 29 * result + (comparator != null ? comparator.hashCode() : 0);
- result = 29 * result + (reverse ? 1 : 0);
+ result = 29 * result + super.hashCode();
return result;
}
@@ -136,43 +125,7 @@
ValueBindingComparator.LOG.error(e.getMessage(), e);
return 0;
}
-
- if (obj1 == null || obj2 == null) {
- if (obj1 == null && obj2 == null) {
- return 0;
- }
- if (obj1 == null) {
- return reverse ? 1 : -1;
- } else {
- return reverse ? -1 : 1;
- }
- }
-
- if (!obj1.getClass().isInstance(obj2)) {
- throw new ClassCastException(obj1.getClass().getName() + " != "
- + obj2.getClass().getName());
- }
-
- int result;
-
-
- if (comparator instanceof Collator) {
- CollationKey collationKey1
- = ((Collator) comparator).getCollationKey(obj1.toString());
- CollationKey collationKey2
- = ((Collator) comparator).getCollationKey(obj2.toString());
- result = collationKey1.compareTo(collationKey2);
-
- } else if (comparator != null) {
- result = comparator.compare(obj1, obj2);
- } else {
- if (obj1 instanceof Comparable) {
- result = ((Comparable) obj1).compareTo(obj2);
- } else {
- result = obj1.toString().compareTo(obj2.toString());
- }
- }
- return reverse ? -result : result;
+return super.internalCompare(obj1, obj2);
}
}