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 2009/06/10 01:37:19 UTC
svn commit: r783163 - in /myfaces/tomahawk/trunk/core12/src:
main/java/org/apache/myfaces/custom/selectitems/
test/java/org/apache/myfaces/custom/selectitems/
Author: lu4242
Date: Tue Jun 9 23:37:19 2009
New Revision: 783163
URL: http://svn.apache.org/viewvc?rev=783163&view=rev
Log:
TOMAHAWK-1318 Add itemLabelEscaped, itemDisabled attributes to t:selectItems
Added:
myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/
myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java (with props)
myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/
myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java (with props)
myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java (with props)
myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java (with props)
Added: myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java?rev=783163&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java (added)
+++ myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java Tue Jun 9 23:37:19 2009
@@ -0,0 +1,166 @@
+/*
+ * 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 org.apache.myfaces.custom.selectitems;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.faces.context.FacesContext;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
+/**
+ * An extended version of the standard UISelectItems. Populates the
+ * SelectItem collection from the given value automatically using
+ * the itemLabel and itemValue attributes. By using the component
+ * there is no need to manually create a SelectItem collection
+ * because component automatically populates SelectItem objects
+ * from types like Collection, Map and etc..
+ *
+ * @JSFComponent
+ * name = "t:selectItems"
+ * class = "org.apache.myfaces.custom.selectitems.UISelectItems"
+ * tagClass = "org.apache.myfaces.custom.selectitems.SelectItemsTag"
+ * @since 1.1.7
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public abstract class AbstractUISelectItems extends javax.faces.component.UISelectItems {
+
+ public static final String COMPONENT_TYPE = "org.apache.myfaces.UISelectItems";
+
+ /**
+ * name of the iterator
+ *
+ * @JSFProperty
+ */
+ public abstract String getVar();
+
+ /**
+ * name of the selectitem
+ *
+ * @JSFProperty
+ */
+ public abstract Object getItemLabel();
+
+ /**
+ * value of the selectitem
+ *
+ * @JSFProperty
+ */
+ public abstract Object getItemValue();
+
+ /**
+ * indicate if the label should be escaped of the selectitem
+ *
+ * @since 1.1.9
+ * @JSFProperty
+ */
+ public abstract Object getItemLabelEscaped();
+
+ /**
+ * name of the selectitem
+ *
+ * @since 1.1.9
+ * @JSFProperty
+ */
+ public abstract Object getItemDescription();
+
+ /**
+ * disabled state of the selectitem
+ *
+ * @since 1.1.9
+ * @JSFProperty
+ */
+ public abstract Object getItemDisabled();
+
+ public Object getValue() {
+ Object value = super.getValue();
+ return createSelectItems(value);
+ }
+
+ private SelectItem[] createSelectItems(Object value) {
+ List items = new ArrayList();
+
+ if (value instanceof SelectItem[]) {
+ return (SelectItem[]) value;
+ }
+ else if (value instanceof Collection) {
+ Collection collection = (Collection) value;
+ for (Iterator iter = collection.iterator(); iter.hasNext();) {
+ Object currentItem = (Object) iter.next();
+ if (currentItem instanceof SelectItemGroup) {
+ SelectItemGroup itemGroup = (SelectItemGroup) currentItem;
+ SelectItem[] itemsFromGroup = itemGroup.getSelectItems();
+ for (int i = 0; i < itemsFromGroup.length; i++) {
+ items.add(itemsFromGroup[i]);
+ }
+ }
+ else {
+ putIteratorToRequestParam(currentItem);
+ SelectItem selectItem = createSelectItem();
+ removeIteratorFromRequestParam();
+ items.add(selectItem);
+ }
+ }
+ }
+ else if (value instanceof Map) {
+ Map map = (Map) value;
+ for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
+ Entry currentItem = (Entry) iter.next();
+ putIteratorToRequestParam(currentItem.getValue());
+ SelectItem selectItem = createSelectItem();
+ removeIteratorFromRequestParam();
+ items.add(selectItem);
+ }
+ }
+
+ return (SelectItem[]) items.toArray(new SelectItem[0]);
+ }
+
+ private SelectItem createSelectItem() {
+ SelectItem item = null;
+ Object value = getItemValue();
+ String label = getItemLabel() != null ? getItemLabel().toString() : null;
+ String description = getItemDescription() != null ? getItemDescription().toString() : null;
+ Boolean disabled = (Boolean) (getItemDisabled() != null ? getItemDisabled() : Boolean.FALSE);
+ Boolean escaped = (Boolean) (getItemLabelEscaped() != null ? getItemLabelEscaped() : Boolean.TRUE);
+
+ if(label != null)
+ item = new SelectItem(value, label, description, disabled, escaped);
+ else
+ item = new SelectItem(value, value == null ? null : value.toString(), description, disabled, escaped);
+
+ return item;
+ }
+
+ private void putIteratorToRequestParam(Object object) {
+ FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(getVar(), object);
+ }
+
+ private void removeIteratorFromRequestParam() {
+ FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(getVar());
+ }
+
+}
+
Propchange: myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core12/src/main/java/org/apache/myfaces/custom/selectitems/AbstractUISelectItems.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java?rev=783163&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java (added)
+++ myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java Tue Jun 9 23:37:19 2009
@@ -0,0 +1,77 @@
+/*
+ * 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 org.apache.myfaces.custom.selectitems;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ * Simple test entity
+ */
+public class Movie {
+ private String name;
+
+ private String director;
+
+ private boolean disabled;
+
+ private Boolean escaped;
+
+ public Movie() {}
+
+ public Movie(String name, String director) {
+ this.name = name;
+ this.director = director;
+ this.disabled = false;
+ this.escaped = Boolean.TRUE;
+ }
+
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public boolean isDisabled()
+ {
+ return disabled;
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ this.disabled = disabled;
+ }
+
+ public Boolean getEscaped()
+ {
+ return escaped;
+ }
+
+ public void setEscaped(Boolean escaped)
+ {
+ this.escaped = escaped;
+ }
+}
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/Movie.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java?rev=783163&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java (added)
+++ myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java Tue Jun 9 23:37:19 2009
@@ -0,0 +1,146 @@
+/*
+ * 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 org.apache.myfaces.custom.selectitems;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.faces.el.ValueBinding;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class UISelectItems2Test extends AbstractJsfTestCase{
+
+ private UISelectItems selectItems;
+
+ private Collection movieCollection;
+
+ private Collection movieSelectItemsGroupCollection;
+
+ private Map movieMap;
+
+ private SelectItem[] movieSelectItems;
+
+ public UISelectItems2Test(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(UISelectItems2Test.class);
+ }
+
+ /**
+ * Sets up the test environment for <s:selectItems value="#{PossibleValueHere}" var="Movie" itemLabel="#{Movie.name} itemValue="#{Movie.director}" />
+ * Accepted possible values for the component can be a SelectItems array, a collection, a map and SelectItems group collection.
+ */
+ public void setUp() throws Exception{
+ super.setUp();
+ //component
+ selectItems = new UISelectItems();
+ ValueBinding itemLabelVb = facesContext.getApplication().createValueBinding("#{Movie.name}");
+ ValueBinding itemValueVb = facesContext.getApplication().createValueBinding("#{Movie.director}");
+ ValueBinding itemDisabledVb = facesContext.getApplication().createValueBinding("#{Movie.disabled}");
+ ValueBinding itemLabelEscapedVb = facesContext.getApplication().createValueBinding("#{Movie.escaped}");
+
+ selectItems.setValueBinding("itemLabel", itemLabelVb);
+ selectItems.setValueBinding("itemValue", itemValueVb);
+ selectItems.setValueBinding("itemDisabled", itemDisabledVb);
+ selectItems.setValueBinding("itemLabelEscaped", itemLabelEscapedVb);
+ selectItems.getAttributes().put("var", "Movie");
+
+ //entities
+ Movie movie1 = new Movie("Godfather", "Francis Ford Coppola");
+ Movie movie2 = new Movie("Goodfellas", "Martin Scorsese");
+ Movie movie3 = new Movie("Casino", "Martin Scorsese");
+ Movie movie4 = new Movie("Scarface", "Brian De Palma");
+
+ //different value types
+ movieSelectItems = new SelectItem[3];
+ movieSelectItems[0] = new SelectItem(movie2.getDirector(), movie2.getName());
+ movieSelectItems[1] = new SelectItem(movie3.getDirector(), movie3.getName());
+ movieSelectItems[2] = new SelectItem(movie4.getDirector(), movie4.getName());
+
+ movieCollection = new ArrayList();
+ movieCollection.add(movie1);
+ movieCollection.add(movie2);
+ movieCollection.add(movie3);
+ movieCollection.add(movie4);
+
+ movieMap = new HashMap();
+ movieMap.put(movie3.getName(), movie3);
+ movieMap.put(movie4.getName(), movie4);
+
+ movieSelectItemsGroupCollection = new ArrayList();
+ movieSelectItemsGroupCollection.add(createSelectItemGroup("group1", movieSelectItems));
+ movieSelectItemsGroupCollection.add(createSelectItemGroup("group2", movieSelectItems));
+ }
+
+ private SelectItemGroup createSelectItemGroup(String groupName, SelectItem[] items) {
+ SelectItemGroup group = new SelectItemGroup();
+ group.setLabel(groupName);
+ group.setSelectItems(items);
+ return group;
+ }
+
+ public void tearDown() throws Exception{
+ selectItems = null;
+ super.tearDown();
+ }
+
+ public void testCreateSelectItemsBySelectItems() {
+ selectItems.setValue(movieSelectItems);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[1].getValue(), new String("Martin Scorsese"));
+ assertEquals(items[1].getLabel(), new String("Casino"));
+ assertEquals(items.length, 3);
+ }
+
+ public void testCreateSelectItemsFromCollection() {
+ selectItems.setValue(movieCollection);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[0].getValue(), "Francis Ford Coppola");
+ assertEquals(items[0].getLabel(), "Godfather");
+ assertEquals(items.length, 4);
+ }
+
+ public void testCreateSelectItemsFromMap() {
+ selectItems.setValue(movieMap);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items.length, 2);
+ }
+
+ public void testCreateSelectItemsFromSelectItemsGroupCollection() {
+ selectItems.setValue(movieSelectItemsGroupCollection);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[5].getValue(), "Brian De Palma");
+ assertEquals(items[5].getLabel(),"Scarface");
+ assertEquals(items.length, 6);
+ }
+}
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItems2Test.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java?rev=783163&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java (added)
+++ myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java Tue Jun 9 23:37:19 2009
@@ -0,0 +1,142 @@
+/*
+ * 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 org.apache.myfaces.custom.selectitems;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.faces.el.ValueBinding;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class UISelectItemsTest extends AbstractJsfTestCase{
+
+ private UISelectItems selectItems;
+
+ private Collection movieCollection;
+
+ private Collection movieSelectItemsGroupCollection;
+
+ private Map movieMap;
+
+ private SelectItem[] movieSelectItems;
+
+ public UISelectItemsTest(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(UISelectItemsTest.class);
+ }
+
+ /**
+ * Sets up the test environment for <s:selectItems value="#{PossibleValueHere}" var="Movie" itemLabel="#{Movie.name} itemValue="#{Movie.director}" />
+ * Accepted possible values for the component can be a SelectItems array, a collection, a map and SelectItems group collection.
+ */
+ public void setUp() throws Exception{
+ super.setUp();
+ //component
+ selectItems = new UISelectItems();
+ ValueBinding itemLabelVb = facesContext.getApplication().createValueBinding("#{Movie.name}");
+ ValueBinding itemValueVb = facesContext.getApplication().createValueBinding("#{Movie.director}");
+
+ selectItems.setValueBinding("itemLabel", itemLabelVb);
+ selectItems.setValueBinding("itemValue", itemValueVb);
+ selectItems.getAttributes().put("var", "Movie");
+
+ //entities
+ Movie movie1 = new Movie("Godfather", "Francis Ford Coppola");
+ Movie movie2 = new Movie("Goodfellas", "Martin Scorsese");
+ Movie movie3 = new Movie("Casino", "Martin Scorsese");
+ Movie movie4 = new Movie("Scarface", "Brian De Palma");
+
+ //different value types
+ movieSelectItems = new SelectItem[3];
+ movieSelectItems[0] = new SelectItem(movie2.getDirector(), movie2.getName());
+ movieSelectItems[1] = new SelectItem(movie3.getDirector(), movie3.getName());
+ movieSelectItems[2] = new SelectItem(movie4.getDirector(), movie4.getName());
+
+ movieCollection = new ArrayList();
+ movieCollection.add(movie1);
+ movieCollection.add(movie2);
+ movieCollection.add(movie3);
+ movieCollection.add(movie4);
+
+ movieMap = new HashMap();
+ movieMap.put(movie3.getName(), movie3);
+ movieMap.put(movie4.getName(), movie4);
+
+ movieSelectItemsGroupCollection = new ArrayList();
+ movieSelectItemsGroupCollection.add(createSelectItemGroup("group1", movieSelectItems));
+ movieSelectItemsGroupCollection.add(createSelectItemGroup("group2", movieSelectItems));
+ }
+
+ private SelectItemGroup createSelectItemGroup(String groupName, SelectItem[] items) {
+ SelectItemGroup group = new SelectItemGroup();
+ group.setLabel(groupName);
+ group.setSelectItems(items);
+ return group;
+ }
+
+ public void tearDown() throws Exception{
+ selectItems = null;
+ super.tearDown();
+ }
+
+ public void testCreateSelectItemsBySelectItems() {
+ selectItems.setValue(movieSelectItems);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[1].getValue(), new String("Martin Scorsese"));
+ assertEquals(items[1].getLabel(), new String("Casino"));
+ assertEquals(items.length, 3);
+ }
+
+ public void testCreateSelectItemsFromCollection() {
+ selectItems.setValue(movieCollection);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[0].getValue(), "Francis Ford Coppola");
+ assertEquals(items[0].getLabel(), "Godfather");
+ assertEquals(items.length, 4);
+ }
+
+ public void testCreateSelectItemsFromMap() {
+ selectItems.setValue(movieMap);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items.length, 2);
+ }
+
+ public void testCreateSelectItemsFromSelectItemsGroupCollection() {
+ selectItems.setValue(movieSelectItemsGroupCollection);
+ SelectItem[] items = (SelectItem[]) selectItems.getValue();
+ assertEquals(items[5].getValue(), "Brian De Palma");
+ assertEquals(items[5].getLabel(),"Scarface");
+ assertEquals(items.length, 6);
+ }
+}
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core12/src/test/java/org/apache/myfaces/custom/selectitems/UISelectItemsTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL