Author: konstantin.mishin
Date: 2010-03-04 13:24:19 -0500 (Thu, 04 Mar 2010)
New Revision: 16528
Modified:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/FilterField.java
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Modifiable.java
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ModifiableState.java
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableModel.java
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableStateDefaultImpl.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIColumn.java
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java
Log:
RF-8118 RF-8119
Modified:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/FilterField.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/FilterField.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/FilterField.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -32,9 +32,9 @@
private Filter<?> filter;
- private String filterValue;
+ private Object filterValue;
- public FilterField(ValueExpression filterExpression, Filter<?> filter, String
filterValue) {
+ public FilterField(ValueExpression filterExpression, Filter<?> filter, Object
filterValue) {
super(filterExpression);
this.filter = filter;
this.filterValue = filterValue;
@@ -48,7 +48,7 @@
return filter;
}
- public String getFilterValue() {
+ public Object getFilterValue() {
return filterValue;
}
}
Modified:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Modifiable.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Modifiable.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Modifiable.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -22,11 +22,13 @@
package org.richfaces.model;
+import javax.faces.context.FacesContext;
+
/**
* @author Konstantin Mishin
*
*/
public interface Modifiable {
- void modify(ModifiableState state);
+ void modify(FacesContext context, ModifiableState state);
}
Modified:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ModifiableState.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ModifiableState.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ModifiableState.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -23,6 +23,7 @@
package org.richfaces.model;
import java.util.List;
+import java.util.Locale;
/**
* @author Konstantin Mishin
@@ -33,4 +34,6 @@
List<FilterField> getFilterFields();
List<SortField> getSortFields();
+
+ Locale getLocale();
}
Modified:
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableModel.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableModel.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableModel.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -31,12 +31,12 @@
import java.util.Locale;
import java.util.Map;
-import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModelListener;
import javax.swing.SortOrder;
+import org.ajax4jsf.context.ContextInitParameters;
import org.ajax4jsf.model.DataVisitResult;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
@@ -47,14 +47,8 @@
* @author Konstantin Mishin
*
*/
-public class ModifiableModel extends ExtendedDataModel<Object> implements
Modifiable, LocaleAware {
+public class ModifiableModel extends ExtendedDataModel<Object> implements
Modifiable {
- protected FacesContext context;
-
- protected ELContext elContext;
-
- protected Map<String, Object> requestMap;
-
protected ModifiableState state;
protected List<Object> rowKeys;
@@ -65,18 +59,9 @@
protected String filterVar;
- protected Locale locale;
-
private Comparator<? super String> stringComparator;
public ModifiableModel(ExtendedDataModel<?> originalModel, String var, String
filterVar) {
- this(FacesContext.getCurrentInstance(), originalModel, var, filterVar);
- }
-
- public ModifiableModel(FacesContext context, ExtendedDataModel<?>
originalModel, String var, String filterVar) {
- this.context = context;
- elContext = context.getELContext();
- requestMap = context.getExternalContext().getRequestMap();
this.originalModel = originalModel;
this.var = var;
this.filterVar = filterVar;
@@ -88,6 +73,11 @@
}
@Override
+ public void removeDataModelListener(DataModelListener listener) {
+ originalModel.removeDataModelListener(listener);
+ }
+
+ @Override
public DataModelListener[] getDataModelListeners() {
return originalModel.getDataModelListeners();
}
@@ -218,10 +208,32 @@
*
* @see org.richfaces.model.Modifiable#modify(org.richfaces.model.ModifiableState)
*/
- public void modify(ModifiableState state) {
- this.state = state;
- int rowCount = originalModel.getRowCount();
+ public void modify(FacesContext context, ModifiableState state) {
+ initializeRowKeys(context);
+ if (state != null) {
+ this.state = state;
+ Map<Object, Object> attributes = context.getAttributes();
+ Object value = null;
+ Object filterValue = null;
+ if (var != null && var.length() > 0) {
+ value = attributes.get(var);
+ }
+ if (filterVar != null && filterVar.length() > 0) {
+ filterValue = attributes.get(filterVar);
+ }
+ filter(context);
+ sort(context);
+ if (var != null && var.length() > 0) {
+ attributes.put(var, value);
+ }
+ if (filterVar != null && filterVar.length() > 0) {
+ attributes.put(filterVar, filterValue);
+ }
+ }
+ }
+ private void initializeRowKeys(FacesContext context) {
+ int rowCount = originalModel.getRowCount();
if (rowCount > 0) {
rowKeys = new ArrayList<Object>(rowCount);
} else {
@@ -237,19 +249,15 @@
return DataVisitResult.CONTINUE;
}
}, new SequenceRange(0, -1), null);
- if (this.state != null) {
- filter();
- sort();
- }
originalModel.setRowKey(rowKey);
}
- protected void filter() {
+ protected void filter(FacesContext context) {
List<FilterField> filterFields = state.getFilterFields();
if (filterFields != null && !filterFields.isEmpty()) {
List<Object> filteredCollection = new ArrayList<Object>();
for (Object rowKey : rowKeys) {
- if (accept(rowKey)) {
+ if (accept(context, rowKey)) {
filteredCollection.add(rowKey);
}
}
@@ -257,31 +265,31 @@
}
}
- protected void sort() {
+ protected void sort(final FacesContext context) {
List<SortField> sortFields = state.getSortFields();
if (sortFields != null && !sortFields.isEmpty()) {
Collections.sort(rowKeys, new Comparator<Object>() {
public int compare(Object rowKey1, Object rowKey2) {
- return ModifiableModel.this.compare(rowKey1, rowKey2);
+ return ModifiableModel.this.compare(context, rowKey1, rowKey2);
}
});
}
}
@SuppressWarnings("unchecked")
- protected boolean accept(Object rowKey) {
+ protected boolean accept(FacesContext context, Object rowKey) {
originalModel.setRowKey(rowKey);
Object object = originalModel.getRowData();
- updateVar(var, object);
+ updateVar(context, var, object);
for (FilterField filterField : state.getFilterFields()) {
Filter filter = filterField.getFilter();
- if (filter != null) {
- return filter.accept(object);
+ if (filter != null && !filter.accept(object)) {
+ return false;
} else {
ValueExpression filterExpression = filterField.getFilterExpression();
if (filterExpression != null) {
- updateVar(filterVar, filterField.getFilterValue());
- if (Boolean.FALSE.equals(filterExpression.getValue(elContext))) {
+ updateVar(context, filterVar, filterField.getFilterValue());
+ if
(Boolean.FALSE.equals(filterExpression.getValue(context.getELContext()))) {
return false;
}
}
@@ -291,7 +299,7 @@
}
@SuppressWarnings("unchecked")
- protected int compare(Object rowKey1, Object rowKey2) {
+ protected int compare(FacesContext context, Object rowKey1, Object rowKey2) {
originalModel.setRowKey(rowKey1);
Object object1 = originalModel.getRowData();
originalModel.setRowKey(rowKey2);
@@ -307,11 +315,11 @@
} else {
ValueExpression sortBy = sortField.getSortBy();
if (sortBy != null) {
- updateVar(var, object1);
- Object value1 = sortBy.getValue(elContext);
- updateVar(var, object2);
- Object value2 = sortBy.getValue(elContext);
- result = compareSortByValues(value1, value2);
+ updateVar(context, var, object1);
+ Object value1 = sortBy.getValue(context.getELContext());
+ updateVar(context, var, object2);
+ Object value2 = sortBy.getValue(context.getELContext());
+ result = compareSortByValues(context, value1, value2);
}
}
if (SortOrder.DESCENDING.equals(sortOrder)) {
@@ -323,11 +331,11 @@
}
@SuppressWarnings("unchecked")
- private int compareSortByValues(Object value1, Object value2) {
+ private int compareSortByValues(FacesContext context, Object value1, Object value2)
{
int result = 0;
if (value1 instanceof String && value2 instanceof String) {
if (stringComparator == null) {
- stringComparator = createStringComparator();
+ stringComparator = createStringComparator(context);
}
result = stringComparator.compare(((String) value1).trim(), ((String)
value2).trim());
} else if (value1 instanceof Comparable<?>) {
@@ -340,9 +348,10 @@
return result;
}
- private Comparator<? super String> createStringComparator() {
+ private Comparator<? super String> createStringComparator(FacesContext context)
{
Comparator<? super String> comparator = null;
- if (locale != null) {
+ Locale locale = state.getLocale();
+ if (locale != null &&
ContextInitParameters.isDatatableUsesViewLocale(context)) {
comparator = Collator.getInstance(locale);
} else {
comparator = new Comparator<String>() {
@@ -354,28 +363,9 @@
return comparator;
}
- private void updateVar(String var, Object value) {
+ private void updateVar(FacesContext context, String var, Object value) {
if (var != null && var.length() > 0) {
- requestMap.put(var, value);
+ context.getAttributes().put(var, value);
}
}
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.model.LocaleAware#getLocale()
- */
- public Locale getLocale() {
- return locale;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.model.LocaleAware#setLocale(java.util.Locale)
- */
- public void setLocale(Locale locale) {
- this.locale = locale;
- }
-
}
Modified:
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableStateDefaultImpl.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableStateDefaultImpl.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableStateDefaultImpl.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -23,6 +23,7 @@
package org.richfaces.model;
import java.util.List;
+import java.util.Locale;
/**
* @author Konstantin Mishin
@@ -34,9 +35,12 @@
private List<SortField> sortFields;
- public ModifiableStateDefaultImpl(List<FilterField> filterFields,
List<SortField> sortFields) {
+ protected Locale locale;
+
+ public ModifiableStateDefaultImpl(List<FilterField> filterFields,
List<SortField> sortFields, Locale locale) {
this.filterFields = filterFields;
this.sortFields = sortFields;
+ this.locale = locale;
}
public List<FilterField> getFilterFields() {
@@ -46,4 +50,8 @@
public List<SortField> getSortFields() {
return sortFields;
}
+
+ public Locale getLocale() {
+ return locale;
+ }
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIColumn.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIColumn.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIColumn.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -52,7 +52,7 @@
Filter<?> filter = (Filter<?>) attributes.get("filter");
ValueExpression filterExpression =
getValueExpression("filterExpression");
if (filter != null || filterExpression != null) {
- field = new FilterField(filterExpression, filter, (String)
attributes.get("filterValue"));
+ field = new FilterField(filterExpression, filter,
attributes.get("filterValue"));
}
return field;
}
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java 2010-03-04
17:11:54 UTC (rev 16527)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java 2010-03-04
18:24:19 UTC (rev 16528)
@@ -30,13 +30,10 @@
import java.util.Map;
import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
-import org.ajax4jsf.context.ContextInitParameters;
import org.ajax4jsf.model.ExtendedDataModel;
import org.richfaces.model.FilterField;
-import org.richfaces.model.LocaleAware;
import org.richfaces.model.Modifiable;
import org.richfaces.model.ModifiableModel;
import org.richfaces.model.ModifiableState;
@@ -46,6 +43,10 @@
public abstract class UIDataTableBase extends UISequence implements Row {
+ protected enum PropertyKeys {
+ filterVar
+ }
+
public Iterator<UIComponent> columns() {
return new ColumnsIterator(this);
}
@@ -85,35 +86,28 @@
protected ExtendedDataModel<?> createExtendedDataModel() {
ExtendedDataModel<?> dataModel = super.createExtendedDataModel();
Modifiable modifiable = null;
- ModifiableState modifiableState = createModifiableState();
+ FacesContext context = getFacesContext();
+ ModifiableState modifiableState = createModifiableState(context);
if (dataModel instanceof Modifiable) {
modifiable = (Modifiable) dataModel;
} else if (modifiableState != null) {
- ModifiableModel modifiableModel = new ModifiableModel(dataModel, getVar(),
(String) getAttributes().get(
- "filterVar"));
+ ModifiableModel modifiableModel = new ModifiableModel(dataModel, getVar(),
getFilterVar());
dataModel = modifiableModel;
modifiable = modifiableModel;
}
- if (dataModel instanceof LocaleAware) {
- FacesContext facesContext = getFacesContext();
- if (ContextInitParameters.isDatatableUsesViewLocale(facesContext)) {
- UIViewRoot viewRoot = facesContext.getViewRoot();
- ((LocaleAware) dataModel).setLocale(viewRoot.getLocale());
- }
- }
-
if (modifiable != null) {
- modifiable.modify(modifiableState);
+ modifiable.modify(context, modifiableState);
}
return dataModel;
}
- private ModifiableState createModifiableState() {
+ private ModifiableState createModifiableState(FacesContext context) {
ModifiableState modifiableState = null;
List<FilterField> filterFields = new LinkedList<FilterField>();
Map<Object, SortField> sortFieldsMap = new LinkedHashMap<Object,
SortField>();
- for (UIComponent component : getChildren()) {
+ for (Iterator<UIComponent> iterator = columns(); iterator.hasNext();) {
+ UIComponent component = iterator.next();
if (component instanceof UIColumn && component.isRendered()) {
UIColumn column = (UIColumn) component;
FilterField filterField = column.getFilterField();
@@ -139,8 +133,17 @@
}
sortFields.addAll(sortFieldsMap.values());
if (!filterFields.isEmpty() || !sortFields.isEmpty()) {
- modifiableState = new ModifiableStateDefaultImpl(filterFields, sortFields);
+ modifiableState = new ModifiableStateDefaultImpl(filterFields, sortFields,
+ context.getViewRoot().getLocale());
}
return modifiableState;
}
+
+ public String getFilterVar() {
+ return (String) getStateHelper().get(PropertyKeys.filterVar);
+ }
+
+ public void setFilterVar(String filterVar) {
+ getStateHelper().put(PropertyKeys.filterVar, filterVar);
+ }
}