You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2013/01/22 16:58:10 UTC

svn commit: r1437008 - in /myfaces/core/branches/2.2.x: api/src/main/java/javax/faces/component/UIData.java api/src/main/java/javax/faces/model/CollectionDataModel.java impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java

Author: lu4242
Date: Tue Jan 22 15:58:10 2013
New Revision: 1437008

URL: http://svn.apache.org/viewvc?rev=1437008&view=rev
Log:
MYFACES-3681 Implement CollectionDataModel 

Added:
    myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java   (with props)
Modified:
    myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/component/UIData.java
    myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java

Modified: myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/component/UIData.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/component/UIData.java?rev=1437008&r1=1437007&r2=1437008&view=diff
==============================================================================
--- myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/component/UIData.java (original)
+++ myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/component/UIData.java Tue Jan 22 15:58:10 2013
@@ -43,6 +43,7 @@ import javax.faces.event.PhaseId;
 import javax.faces.event.PostValidateEvent;
 import javax.faces.event.PreValidateEvent;
 import javax.faces.model.ArrayDataModel;
+import javax.faces.model.CollectionDataModel;
 import javax.faces.model.DataModel;
 import javax.faces.model.ListDataModel;
 import javax.faces.model.ResultDataModel;
@@ -2002,6 +2003,10 @@ public class UIData extends UIComponentB
         {
             return new ResultDataModel((Result) value);
         }
+        else if (value instanceof Collection)
+        {
+            return new CollectionDataModel((Collection) value);
+        }
         else
         {
             return new ScalarDataModel(value);

Added: myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java?rev=1437008&view=auto
==============================================================================
--- myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java (added)
+++ myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java Tue Jan 22 15:58:10 2013
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package javax.faces.model;
+
+import java.util.Collection;
+
+/**
+ *
+ * @author Leonardo Uribe
+ */
+public class CollectionDataModel<E> extends DataModel<E>
+{
+
+    // FIELDS
+    private int _rowIndex = -1;
+    private Collection<E> _data;
+    private E[] _dataArray;
+
+    // CONSTRUCTORS
+    public CollectionDataModel()
+    {
+        super();
+    }
+
+    public CollectionDataModel(Collection<E> collection)
+    {
+        if (collection == null)
+        {
+            throw new NullPointerException("collection");
+        }
+        setWrappedData(collection);
+    }
+
+    // METHODS
+    @Override
+    public int getRowCount()
+    {
+        if (_data == null)
+        {
+            return -1;
+        }
+        return _data.size();
+    }
+
+    @Override
+    public E getRowData()
+    {
+        if (_data == null)
+        {
+            return null;
+        }
+        if (!isRowAvailable())
+        {
+            throw new IllegalArgumentException("row is unavailable");
+        }
+        return _dataArray[_rowIndex];
+    }
+
+    @Override
+    public int getRowIndex()
+    {
+        return _rowIndex;
+    }
+
+    @Override
+    public Object getWrappedData()
+    {
+        return _data;
+    }
+
+    @Override
+    public boolean isRowAvailable()
+    {
+        return _data != null && _rowIndex >= 0 && _rowIndex < _data.size();
+    }
+
+    @Override
+    public void setRowIndex(int rowIndex)
+    {
+        if (rowIndex < -1)
+        {
+            throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
+        }
+        int oldRowIndex = _rowIndex;
+        _rowIndex = rowIndex;
+        if (_data != null && oldRowIndex != _rowIndex)
+        {
+            E data = isRowAvailable() ? getRowData() : null;
+            DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
+            DataModelListener[] listeners = getDataModelListeners();
+            for (int i = 0; i < listeners.length; i++)
+            {
+                listeners[i].rowSelected(event);
+            }
+        }
+    }
+
+    @Override
+    public void setWrappedData(Object data)
+    {
+        if (data == null)
+        {
+            setRowIndex(-1);
+            _data = null;
+            _dataArray = null;
+        }
+        else
+        {
+            _data = (Collection<E>)data;
+            _dataArray = _data.toArray((E[]) new Object[_data.size()]);
+            _rowIndex = -1;
+            setRowIndex(0);
+        }
+    }
+
+}

Propchange: myfaces/core/branches/2.2.x/api/src/main/java/javax/faces/model/CollectionDataModel.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java?rev=1437008&r1=1437007&r2=1437008&view=diff
==============================================================================
--- myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java (original)
+++ myfaces/core/branches/2.2.x/impl/src/main/java/org/apache/myfaces/view/facelets/component/UIRepeat.java Tue Jan 22 15:58:10 2013
@@ -48,6 +48,7 @@ import javax.faces.event.FacesEvent;
 import javax.faces.event.FacesListener;
 import javax.faces.event.PhaseId;
 import javax.faces.model.ArrayDataModel;
+import javax.faces.model.CollectionDataModel;
 import javax.faces.model.DataModel;
 import javax.faces.model.ListDataModel;
 import javax.faces.model.ResultSetDataModel;
@@ -212,6 +213,10 @@ public class UIRepeat extends UIComponen
         {
             return new ResultSetDataModel((ResultSet) value);
         }
+        else if (value instanceof Collection)
+        {
+            return new CollectionDataModel((Collection) value);
+        }
         else
         {
             return new ScalarDataModel(value);