Author: konstantin.mishin
Date: 2010-03-05 10:54:16 -0500 (Fri, 05 Mar 2010)
New Revision: 16530
Added:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Arrangeble.java
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ArrangebleState.java
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleModel.java
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleStateDefaultImpl.java
Removed:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/LocaleAware.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
Modified:
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java
Log:
RF-8118 RF-8119
Copied:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Arrangeble.java
(from rev 16528,
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/Arrangeble.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Arrangeble.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.model;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * @author Konstantin Mishin
+ *
+ */
+public interface Arrangeble {
+
+ void arrange(FacesContext context, ArrangebleState state);
+}
Copied:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ArrangebleState.java
(from rev 16528,
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/ArrangebleState.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ArrangebleState.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.model;
+
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * @author Konstantin Mishin
+ *
+ */
+public interface ArrangebleState {
+
+ List<FilterField> getFilterFields();
+
+ List<SortField> getSortFields();
+
+ Locale getLocale();
+}
Deleted:
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/LocaleAware.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/LocaleAware.java 2010-03-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/LocaleAware.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -1,39 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.model;
-
-import java.util.Locale;
-
-/**
- * Models that are based on locale-dependent operations should implement this interface
- *
- * @author Nick Belaevski
- * @since 3.3.0
- */
-
-public interface LocaleAware {
-
- Locale getLocale();
-
- void setLocale(Locale locale);
-}
Deleted:
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-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/Modifiable.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -1,34 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.model;
-
-import javax.faces.context.FacesContext;
-
-/**
- * @author Konstantin Mishin
- *
- */
-public interface Modifiable {
-
- void modify(FacesContext context, ModifiableState state);
-}
Deleted:
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-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/api/src/main/java/org/richfaces/model/ModifiableState.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -1,39 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.model;
-
-import java.util.List;
-import java.util.Locale;
-
-/**
- * @author Konstantin Mishin
- *
- */
-public interface ModifiableState {
-
- List<FilterField> getFilterFields();
-
- List<SortField> getSortFields();
-
- Locale getLocale();
-}
Copied:
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleModel.java
(from rev 16528,
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/ArrangebleModel.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleModel.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -0,0 +1,371 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.model;
+
+import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+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;
+import org.ajax4jsf.model.Range;
+import org.ajax4jsf.model.SequenceRange;
+
+/**
+ * @author Konstantin Mishin
+ *
+ */
+public class ArrangebleModel extends ExtendedDataModel<Object> implements
Arrangeble {
+
+ protected ArrangebleState state;
+
+ protected List<Object> rowKeys;
+
+ protected ExtendedDataModel<?> originalModel;
+
+ protected String var;
+
+ protected String filterVar;
+
+ private Comparator<? super String> stringComparator;
+
+ public ArrangebleModel(ExtendedDataModel<?> originalModel, String var, String
filterVar) {
+ this.originalModel = originalModel;
+ this.var = var;
+ this.filterVar = filterVar;
+ }
+
+ @Override
+ public void addDataModelListener(DataModelListener listener) {
+ originalModel.addDataModelListener(listener);
+ }
+
+ @Override
+ public void removeDataModelListener(DataModelListener listener) {
+ originalModel.removeDataModelListener(listener);
+ }
+
+ @Override
+ public DataModelListener[] getDataModelListeners() {
+ return originalModel.getDataModelListeners();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
+ */
+ @Override
+ public Object getRowKey() {
+ return originalModel.getRowKey();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)
+ */
+ @Override
+ public void setRowKey(Object key) {
+ originalModel.setRowKey(key);
+ }
+
+ /*
+ * (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(FacesContext context, DataVisitor visitor, Range range, Object
argument) {
+ final SequenceRange seqRange = (SequenceRange) range;
+ int rows = seqRange.getRows();
+ int rowCount = getRowCount();
+ int currentRow = seqRange.getFirstRow();
+ if (rows > 0) {
+ rows += currentRow;
+ rows = Math.min(rows, rowCount);
+ } else {
+ rows = rowCount;
+ }
+ for (; currentRow < rows; currentRow++) {
+ visitor.process(context, rowKeys.get(currentRow), argument);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#getRowCount()
+ */
+ @Override
+ public int getRowCount() {
+ if (rowKeys == null) {
+ return -1;
+ } else {
+ return rowKeys.size();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#getRowData()
+ */
+ @Override
+ public Object getRowData() {
+ return originalModel.getRowData();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#getRowIndex()
+ */
+ @Override
+ public int getRowIndex() {
+ return rowKeys.indexOf(originalModel.getRowKey());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#getWrappedData()
+ */
+ @Override
+ public Object getWrappedData() {
+ return originalModel.getWrappedData();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#isRowAvailable()
+ */
+ @Override
+ public boolean isRowAvailable() {
+ return originalModel.isRowAvailable();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#setRowIndex(int)
+ */
+ @Override
+ public void setRowIndex(int rowIndex) {
+ Object originalKey = null;
+ if (rowIndex >= 0 && rowIndex < rowKeys.size()) {
+ originalKey = rowKeys.get(rowIndex);
+ }
+ originalModel.setRowKey(originalKey);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
+ */
+ @Override
+ public void setWrappedData(Object data) {
+ originalModel.setWrappedData(data);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.model.Modifiable#modify(org.richfaces.model.ModifiableState)
+ */
+ public void arrange(FacesContext context, ArrangebleState 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 {
+ rowKeys = new ArrayList<Object>();
+ }
+ Object rowKey = originalModel.getRowKey();
+ originalModel.walk(context, new DataVisitor() {
+ public DataVisitResult process(FacesContext context, Object rowKey, Object
argument) {
+ originalModel.setRowKey(rowKey);
+ if (originalModel.isRowAvailable()) {
+ rowKeys.add(rowKey);
+ }
+ return DataVisitResult.CONTINUE;
+ }
+ }, new SequenceRange(0, -1), null);
+ originalModel.setRowKey(rowKey);
+ }
+
+ 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(context, rowKey)) {
+ filteredCollection.add(rowKey);
+ }
+ }
+ rowKeys = filteredCollection;
+ }
+ }
+
+ 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 ArrangebleModel.this.compare(context, rowKey1, rowKey2);
+ }
+ });
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected boolean accept(FacesContext context, Object rowKey) {
+ originalModel.setRowKey(rowKey);
+ Object object = originalModel.getRowData();
+ updateVar(context, var, object);
+ for (FilterField filterField : state.getFilterFields()) {
+ Filter filter = filterField.getFilter();
+ if (filter != null && !filter.accept(object)) {
+ return false;
+ } else {
+ ValueExpression filterExpression = filterField.getFilterExpression();
+ if (filterExpression != null) {
+ updateVar(context, filterVar, filterField.getFilterValue());
+ if
(Boolean.FALSE.equals(filterExpression.getValue(context.getELContext()))) {
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected int compare(FacesContext context, Object rowKey1, Object rowKey2) {
+ originalModel.setRowKey(rowKey1);
+ Object object1 = originalModel.getRowData();
+ originalModel.setRowKey(rowKey2);
+ Object object2 = originalModel.getRowData();
+ int result = 0;
+ for (Iterator<SortField> iterator = state.getSortFields().iterator();
iterator.hasNext() && result == 0;) {
+ SortField sortField = iterator.next();
+ SortOrder sortOrder = sortField.getSortOrder();
+ if (sortOrder != null && !SortOrder.UNSORTED.equals(sortOrder)) {
+ Comparator comparator = sortField.getComparator();
+ if (comparator != null) {
+ result = comparator.compare(object1, object2);
+ } else {
+ ValueExpression sortBy = sortField.getSortBy();
+ if (sortBy != null) {
+ 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)) {
+ result = -result;
+ }
+ }
+ }
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ private int compareSortByValues(FacesContext context, Object value1, Object value2)
{
+ int result = 0;
+ if (value1 instanceof String && value2 instanceof String) {
+ if (stringComparator == null) {
+ stringComparator = createStringComparator(context);
+ }
+ result = stringComparator.compare(((String) value1).trim(), ((String)
value2).trim());
+ } else if (value1 instanceof Comparable<?>) {
+ result = ((Comparable) value1).compareTo(value2);
+ } else if (value1 == null && value2 != null) {
+ result = -1;
+ } else if (value2 == null && value1 != null) {
+ result = 1;
+ }
+ return result;
+ }
+
+ private Comparator<? super String> createStringComparator(FacesContext context)
{
+ Comparator<? super String> comparator = null;
+ Locale locale = state.getLocale();
+ if (locale != null &&
ContextInitParameters.isDatatableUsesViewLocale(context)) {
+ comparator = Collator.getInstance(locale);
+ } else {
+ comparator = new Comparator<String>() {
+ public int compare(String o1, String o2) {
+ return o1.compareToIgnoreCase(o2);
+ }
+ };
+ }
+ return comparator;
+ }
+
+ private void updateVar(FacesContext context, String var, Object value) {
+ if (var != null && var.length() > 0) {
+ context.getAttributes().put(var, value);
+ }
+ }
+}
Copied:
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleStateDefaultImpl.java
(from rev 16528,
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/ArrangebleStateDefaultImpl.java
(rev 0)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ArrangebleStateDefaultImpl.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.model;
+
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * @author Konstantin Mishin
+ *
+ */
+public class ArrangebleStateDefaultImpl implements ArrangebleState {
+
+ private List<FilterField> filterFields;
+
+ private List<SortField> sortFields;
+
+ private Locale locale;
+
+ public ArrangebleStateDefaultImpl(List<FilterField> filterFields,
List<SortField> sortFields, Locale locale) {
+ this.filterFields = filterFields;
+ this.sortFields = sortFields;
+ this.locale = locale;
+ }
+
+ public List<FilterField> getFilterFields() {
+ return filterFields;
+ }
+
+ public List<SortField> getSortFields() {
+ return sortFields;
+ }
+
+ public Locale getLocale() {
+ return locale;
+ }
+}
Deleted:
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-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableModel.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -1,371 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.model;
-
-import java.text.Collator;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-
-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;
-import org.ajax4jsf.model.Range;
-import org.ajax4jsf.model.SequenceRange;
-
-/**
- * @author Konstantin Mishin
- *
- */
-public class ModifiableModel extends ExtendedDataModel<Object> implements
Modifiable {
-
- protected ModifiableState state;
-
- protected List<Object> rowKeys;
-
- protected ExtendedDataModel<?> originalModel;
-
- protected String var;
-
- protected String filterVar;
-
- private Comparator<? super String> stringComparator;
-
- public ModifiableModel(ExtendedDataModel<?> originalModel, String var, String
filterVar) {
- this.originalModel = originalModel;
- this.var = var;
- this.filterVar = filterVar;
- }
-
- @Override
- public void addDataModelListener(DataModelListener listener) {
- originalModel.addDataModelListener(listener);
- }
-
- @Override
- public void removeDataModelListener(DataModelListener listener) {
- originalModel.removeDataModelListener(listener);
- }
-
- @Override
- public DataModelListener[] getDataModelListeners() {
- return originalModel.getDataModelListeners();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
- */
- @Override
- public Object getRowKey() {
- return originalModel.getRowKey();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)
- */
- @Override
- public void setRowKey(Object key) {
- originalModel.setRowKey(key);
- }
-
- /*
- * (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(FacesContext context, DataVisitor visitor, Range range, Object
argument) {
- final SequenceRange seqRange = (SequenceRange) range;
- int rows = seqRange.getRows();
- int rowCount = getRowCount();
- int currentRow = seqRange.getFirstRow();
- if (rows > 0) {
- rows += currentRow;
- rows = Math.min(rows, rowCount);
- } else {
- rows = rowCount;
- }
- for (; currentRow < rows; currentRow++) {
- visitor.process(context, rowKeys.get(currentRow), argument);
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#getRowCount()
- */
- @Override
- public int getRowCount() {
- if (rowKeys == null) {
- return -1;
- } else {
- return rowKeys.size();
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#getRowData()
- */
- @Override
- public Object getRowData() {
- return originalModel.getRowData();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#getRowIndex()
- */
- @Override
- public int getRowIndex() {
- return rowKeys.indexOf(originalModel.getRowKey());
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#getWrappedData()
- */
- @Override
- public Object getWrappedData() {
- return originalModel.getWrappedData();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#isRowAvailable()
- */
- @Override
- public boolean isRowAvailable() {
- return originalModel.isRowAvailable();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#setRowIndex(int)
- */
- @Override
- public void setRowIndex(int rowIndex) {
- Object originalKey = null;
- if (rowIndex >= 0 && rowIndex < rowKeys.size()) {
- originalKey = rowKeys.get(rowIndex);
- }
- originalModel.setRowKey(originalKey);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
- */
- @Override
- public void setWrappedData(Object data) {
- originalModel.setWrappedData(data);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.model.Modifiable#modify(org.richfaces.model.ModifiableState)
- */
- 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 {
- rowKeys = new ArrayList<Object>();
- }
- Object rowKey = originalModel.getRowKey();
- originalModel.walk(context, new DataVisitor() {
- public DataVisitResult process(FacesContext context, Object rowKey, Object
argument) {
- originalModel.setRowKey(rowKey);
- if (originalModel.isRowAvailable()) {
- rowKeys.add(rowKey);
- }
- return DataVisitResult.CONTINUE;
- }
- }, new SequenceRange(0, -1), null);
- originalModel.setRowKey(rowKey);
- }
-
- 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(context, rowKey)) {
- filteredCollection.add(rowKey);
- }
- }
- rowKeys = filteredCollection;
- }
- }
-
- 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(context, rowKey1, rowKey2);
- }
- });
- }
- }
-
- @SuppressWarnings("unchecked")
- protected boolean accept(FacesContext context, Object rowKey) {
- originalModel.setRowKey(rowKey);
- Object object = originalModel.getRowData();
- updateVar(context, var, object);
- for (FilterField filterField : state.getFilterFields()) {
- Filter filter = filterField.getFilter();
- if (filter != null && !filter.accept(object)) {
- return false;
- } else {
- ValueExpression filterExpression = filterField.getFilterExpression();
- if (filterExpression != null) {
- updateVar(context, filterVar, filterField.getFilterValue());
- if
(Boolean.FALSE.equals(filterExpression.getValue(context.getELContext()))) {
- return false;
- }
- }
- }
- }
- return true;
- }
-
- @SuppressWarnings("unchecked")
- protected int compare(FacesContext context, Object rowKey1, Object rowKey2) {
- originalModel.setRowKey(rowKey1);
- Object object1 = originalModel.getRowData();
- originalModel.setRowKey(rowKey2);
- Object object2 = originalModel.getRowData();
- int result = 0;
- for (Iterator<SortField> iterator = state.getSortFields().iterator();
iterator.hasNext() && result == 0;) {
- SortField sortField = iterator.next();
- SortOrder sortOrder = sortField.getSortOrder();
- if (sortOrder != null && !SortOrder.UNSORTED.equals(sortOrder)) {
- Comparator comparator = sortField.getComparator();
- if (comparator != null) {
- result = comparator.compare(object1, object2);
- } else {
- ValueExpression sortBy = sortField.getSortBy();
- if (sortBy != null) {
- 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)) {
- result = -result;
- }
- }
- }
- return result;
- }
-
- @SuppressWarnings("unchecked")
- private int compareSortByValues(FacesContext context, Object value1, Object value2)
{
- int result = 0;
- if (value1 instanceof String && value2 instanceof String) {
- if (stringComparator == null) {
- stringComparator = createStringComparator(context);
- }
- result = stringComparator.compare(((String) value1).trim(), ((String)
value2).trim());
- } else if (value1 instanceof Comparable<?>) {
- result = ((Comparable) value1).compareTo(value2);
- } else if (value1 == null && value2 != null) {
- result = -1;
- } else if (value2 == null && value1 != null) {
- result = 1;
- }
- return result;
- }
-
- private Comparator<? super String> createStringComparator(FacesContext context)
{
- Comparator<? super String> comparator = null;
- Locale locale = state.getLocale();
- if (locale != null &&
ContextInitParameters.isDatatableUsesViewLocale(context)) {
- comparator = Collator.getInstance(locale);
- } else {
- comparator = new Comparator<String>() {
- public int compare(String o1, String o2) {
- return o1.compareToIgnoreCase(o2);
- }
- };
- }
- return comparator;
- }
-
- private void updateVar(FacesContext context, String var, Object value) {
- if (var != null && var.length() > 0) {
- context.getAttributes().put(var, value);
- }
- }
-}
Deleted:
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-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/impl/src/main/java/org/richfaces/model/ModifiableStateDefaultImpl.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -1,57 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.richfaces.model;
-
-import java.util.List;
-import java.util.Locale;
-
-/**
- * @author Konstantin Mishin
- *
- */
-public class ModifiableStateDefaultImpl implements ModifiableState {
-
- private List<FilterField> filterFields;
-
- private 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() {
- return filterFields;
- }
-
- 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/UIDataTableBase.java
===================================================================
---
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java 2010-03-05
01:18:15 UTC (rev 16529)
+++
root/ui-sandbox/trunk/components/tables/ui/src/main/java/org/richfaces/component/UIDataTableBase.java 2010-03-05
15:54:16 UTC (rev 16530)
@@ -34,10 +34,10 @@
import org.ajax4jsf.model.ExtendedDataModel;
import org.richfaces.model.FilterField;
-import org.richfaces.model.Modifiable;
-import org.richfaces.model.ModifiableModel;
-import org.richfaces.model.ModifiableState;
-import org.richfaces.model.ModifiableStateDefaultImpl;
+import org.richfaces.model.Arrangeble;
+import org.richfaces.model.ArrangebleModel;
+import org.richfaces.model.ArrangebleState;
+import org.richfaces.model.ArrangebleStateDefaultImpl;
import org.richfaces.model.SortField;
import org.richfaces.model.SortMode;
@@ -85,25 +85,25 @@
@Override
protected ExtendedDataModel<?> createExtendedDataModel() {
ExtendedDataModel<?> dataModel = super.createExtendedDataModel();
- Modifiable modifiable = null;
+ Arrangeble arrangeble = null;
FacesContext context = getFacesContext();
- ModifiableState modifiableState = createModifiableState(context);
- if (dataModel instanceof Modifiable) {
- modifiable = (Modifiable) dataModel;
- } else if (modifiableState != null) {
- ModifiableModel modifiableModel = new ModifiableModel(dataModel, getVar(),
getFilterVar());
- dataModel = modifiableModel;
- modifiable = modifiableModel;
+ ArrangebleState state = createArrangebleState(context);
+ if (dataModel instanceof Arrangeble) {
+ arrangeble = (Arrangeble) dataModel;
+ } else if (state != null) {
+ ArrangebleModel arrangebleModel = new ArrangebleModel(dataModel, getVar(),
getFilterVar());
+ dataModel = arrangebleModel;
+ arrangeble = arrangebleModel;
}
- if (modifiable != null) {
- modifiable.modify(context, modifiableState);
+ if (arrangeble != null) {
+ arrangeble.arrange(context, state);
}
return dataModel;
}
- private ModifiableState createModifiableState(FacesContext context) {
- ModifiableState modifiableState = null;
+ private ArrangebleState createArrangebleState(FacesContext context) {
+ ArrangebleState state = null;
List<FilterField> filterFields = new LinkedList<FilterField>();
Map<Object, SortField> sortFieldsMap = new LinkedHashMap<Object,
SortField>();
for (Iterator<UIComponent> iterator = columns(); iterator.hasNext();) {
@@ -133,10 +133,10 @@
}
sortFields.addAll(sortFieldsMap.values());
if (!filterFields.isEmpty() || !sortFields.isEmpty()) {
- modifiableState = new ModifiableStateDefaultImpl(filterFields, sortFields,
+ state = new ArrangebleStateDefaultImpl(filterFields, sortFields,
context.getViewRoot().getLocale());
}
- return modifiableState;
+ return state;
}
public String getFilterVar() {