You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2006/07/17 16:39:43 UTC

svn commit: r422732 - in /myfaces/tomahawk/trunk/sandbox: core/src/main/java/org/apache/myfaces/custom/selectitems/ core/src/main/resources-facesconfig/META-INF/ core/src/main/tld/ core/src/main/tld/entities/ examples/src/main/java/org/apache/myfaces/e...

Author: mmarinschek
Date: Mon Jul 17 07:39:42 2006
New Revision: 422732

URL: http://svn.apache.org/viewvc?rev=422732&view=rev
Log:
patch for TOMAHAWK-151. Thanks to Cagatay Civici for this excellent addition to the MyFaces component set.

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/SelectItemsTag.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/UISelectItems.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/select_items_attributes.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/SelectItemsBean.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/selectItems.jsp
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/SelectItemsTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/SelectItemsTag.java?rev=422732&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/SelectItemsTag.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/SelectItemsTag.java Mon Jul 17 07:39:42 2006
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2004 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.
+ */
+package org.apache.myfaces.custom.selectitems;
+
+import javax.faces.component.UIComponent;
+import javax.faces.el.ValueBinding;
+
+import org.apache.myfaces.shared_tomahawk.taglib.UIComponentTagBase;
+
+public class SelectItemsTag extends UIComponentTagBase {
+	
+	private String _var;
+	
+	private String _itemLabel;
+	
+	private String _itemValue;
+	
+	protected void setProperties(UIComponent component) {
+		super.setProperties(component);
+		
+		if (_var != null) {
+			if (isValueReference(_var)) {
+				ValueBinding vb = getFacesContext().getApplication()
+						.createValueBinding(_var);
+				component.setValueBinding("var", vb);
+			} else {
+				component.getAttributes().put("var", _var);
+			}
+		}
+		
+		if (_itemLabel != null) {
+			if (isValueReference(_itemLabel)) {
+				ValueBinding vb = getFacesContext().getApplication()
+						.createValueBinding(_itemLabel);
+				component.setValueBinding("itemLabel", vb);
+			} else {
+				component.getAttributes().put("itemLabel", _itemLabel);
+			}
+		}
+		
+		if (_itemValue != null) {
+			if (isValueReference(_itemValue)) {
+				ValueBinding vb = getFacesContext().getApplication()
+						.createValueBinding(_itemValue);
+				component.setValueBinding("itemValue", vb);
+			} else {
+				component.getAttributes().put("itemValue", _itemValue);
+			}
+		}
+		
+	}
+	
+	public void release() {
+		super.release();
+		_var = null;
+		_itemLabel = null;
+		_itemValue = null;
+	}
+	
+	public String getComponentType() {
+		return UISelectItems.COMPONENT_TYPE;
+	}
+	
+	public String getVar() {
+		return _var;
+	}
+
+	public void setVar(String var) {
+		this._var = var;
+	}
+
+	public String getItemLabel() {
+		return _itemLabel;
+	}
+
+	public void setItemLabel(String itemLabel) {
+		this._itemLabel = itemLabel;
+	}
+
+	public String getItemValue() {
+		return _itemValue;
+	}
+
+	public void setItemValue(String itemValue) {
+		this._itemValue = itemValue;
+	}
+
+	public String getRendererType() {
+		return null;
+	}	
+}

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/UISelectItems.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/UISelectItems.java?rev=422732&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/UISelectItems.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/selectitems/UISelectItems.java Mon Jul 17 07:39:42 2006
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2004 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.
+ */
+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.el.ValueBinding;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
+public class UISelectItems extends javax.faces.component.UISelectItems {
+	
+    public static final String COMPONENT_TYPE = "org.apache.myfaces.UISelectItems";
+    
+    private String _var;
+    
+    private Object _itemLabel;
+    
+    private Object _itemValue;
+    
+    public UISelectItems()   {}
+    
+	public String getVar() {
+		if(_var != null)
+			return _var;
+		
+		ValueBinding vb = getValueBinding("var");
+		String v = vb != null ? (String)vb.getValue(getFacesContext()) : null;
+		return v;
+	}
+
+	public void setVar(String var) {
+		this._var = var;
+	}
+
+	public Object getItemLabel() {
+		if(_itemLabel != null)
+			return _itemLabel;
+		
+		ValueBinding vb = getValueBinding("itemLabel");
+		Object v = vb != null ? vb.getValue(getFacesContext()) : null;
+		return v;
+	}
+
+	public void setItemLabel(Object itemLabel) {
+		this._itemLabel = itemLabel;
+	}
+
+	public Object getItemValue() {
+		if(_itemValue != null)
+			return _itemValue;
+		
+		ValueBinding vb = getValueBinding("itemValue");
+		Object v = vb != null ? vb.getValue(getFacesContext()) : null;
+		return v;
+	}
+
+	public void setItemValue(Object itemValue) {
+		this._itemValue = itemValue;
+	}
+    
+    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() {
+		Object value = getItemValue();
+		String label = getItemLabel() != null ? getItemLabel().toString() : null;
+		SelectItem item = new SelectItem(value, label);
+		return item;
+	}
+	
+	private void putIteratorToRequestParam(Object object) {
+		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(getVar(), object);
+	}
+	
+	private void removeIteratorFromRequestParam() {
+		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove(getVar());
+	}
+	
+	public Object saveState(FacesContext context)  {
+        Object values[] = new Object[4];
+        values[0] = super.saveState(context);
+        values[1] = _var;
+        values[2] = _itemLabel;
+        values[3] = _itemValue;
+        return ((Object) (values));
+    }
+
+    public void restoreState(FacesContext context, Object state)   {
+        Object values[] = (Object[]) state;
+        super.restoreState(context, values[0]);
+        _var = (String)values[1];
+        _itemLabel = values[2];
+        _itemValue = values[3];
+    }
+}
+

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml?rev=422732&r1=422731&r2=422732&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/resources-facesconfig/META-INF/faces-config.xml Mon Jul 17 07:39:42 2006
@@ -185,6 +185,12 @@
     <component-class>org.apache.myfaces.custom.template.XmlTemplate</component-class>
   </component>
 
+
+  <component>
+    <component-type>org.apache.myfaces.UISelectItems</component-type>
+    <component-class>org.apache.myfaces.custom.selectitems.UISelectItems</component-class>
+  </component>
+
   <!-- sandbox converters -->
 
   <converter>

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/select_items_attributes.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/select_items_attributes.xml?rev=422732&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/select_items_attributes.xml (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/select_items_attributes.xml Mon Jul 17 07:39:42 2006
@@ -0,0 +1,24 @@
+<attribute>
+    <name>var</name>
+    <required>true</required>
+    <rtexprvalue>false</rtexprvalue>
+    <description>
+       name of the iterator
+    </description>
+</attribute>
+<attribute>
+    <name>itemLabel</name>
+    <required>true</required>
+    <rtexprvalue>false</rtexprvalue>
+    <description>
+        name of the selectitem
+    </description>
+</attribute>
+<attribute>
+    <name>itemValue</name>
+    <required>true</required>
+    <rtexprvalue>false</rtexprvalue>
+    <description>
+		value of the selectitem
+    </description>
+</attribute>
\ No newline at end of file

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld?rev=422732&r1=422731&r2=422732&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld Mon Jul 17 07:39:42 2006
@@ -134,8 +134,10 @@
 <!ENTITY convertNumber_attributes SYSTEM "entities/standard_convertNumber_attributes.xml">
 <!ENTITY ui_convertNumber_attributes SYSTEM "entities/ui_convertNumber_attributes.xml">
 <!ENTITY xml_template_attributes SYSTEM "entities/xml_template_attributes.xml">
+<!ENTITY select_items_attributes SYSTEM "entities/select_items_attributes.xml">
 ]>
 
+
 <taglib>
 
     <tlib-version>1.0.10</tlib-version>
@@ -1170,5 +1172,18 @@
 		&convertNumber_attributes;
 		&ui_convertNumber_attributes;
 		
+	</tag>
+
+	<tag>
+		<name>selectitems</name>
+		<tag-class>org.apache.myfaces.custom.selectitems.SelectItemsTag</tag-class>
+		<body-content>JSP</body-content>
+		<description>
+            An extended version of the standard UISelectItems.
+
+            Populates the SelectItem collection from the given value automatically using the itemLabel and itemValue attributes.
+		</description>
+		&ui_select_items_attributes;
+		&select_items_attributes;
 	</tag>
 </taglib>

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/SelectItemsBean.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/SelectItemsBean.java?rev=422732&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/SelectItemsBean.java (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/selectitems/SelectItemsBean.java Mon Jul 17 07:39:42 2006
@@ -0,0 +1,42 @@
+package org.apache.myfaces.examples.selectitems;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.myfaces.examples.selectOneRow.SimpleCar;
+
+public class SelectItemsBean {
+
+	private List carList;
+	
+	private String selectedCarColor;
+
+	public List getCarList() {
+		if(carList == null) {
+			carList = createCarList();
+		}		
+		return carList;
+	}
+
+	public void setCarList(List list) {
+		carList = list;
+	}
+	
+	private List createCarList() {
+		List list = new ArrayList();
+		list.add(new SimpleCar(1, "Car 1", "blue"));
+		list.add(new SimpleCar(2, "Car 2", "white"));
+		list.add(new SimpleCar(3, "Car 3", "red"));
+		list.add(new SimpleCar(4, "Car 4", "green"));
+		return list;
+	}
+
+	public String getSelectedCarColor() {
+		return selectedCarColor;
+	}
+
+	public void setSelectedCarColor(String selectedCarColor) {
+		this.selectedCarColor = selectedCarColor;
+	}
+	
+}

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml?rev=422732&r1=422731&r2=422732&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/WEB-INF/examples-config.xml Mon Jul 17 07:39:42 2006
@@ -328,14 +328,22 @@
     <managed-bean-scope>application</managed-bean-scope>
   </managed-bean>
 
-	<!--  manage bean for the selectManyPicklist component -->
+	<!--  Managed bean for the selectManyPicklist component -->
 	<managed-bean>
 		<managed-bean-name>picklistBean</managed-bean-name>
 		<managed-bean-class>org.apache.myfaces.examples.picklist.PicklistBean</managed-bean-class>
 		<managed-bean-scope>session</managed-bean-scope>
 	</managed-bean>
 
-	<!-- Managed beans for the scope shop  -->
+	
+	<!--  Managed bean for the selectItems component -->
+	<managed-bean>
+		<managed-bean-name>selectItemsBean</managed-bean-name>
+		<managed-bean-class>org.apache.myfaces.examples.selectitems.SelectItemsBean</managed-bean-class>
+		<managed-bean-scope>request</managed-bean-scope>
+	</managed-bean>
+
+    <!-- Managed beans for the scope shop  -->
 	<managed-bean>
 		<managed-bean-name>ScopeBean</managed-bean-name>
 		<managed-bean-class>

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp?rev=422732&r1=422731&r2=422732&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/home.jsp Mon Jul 17 07:39:42 2006
@@ -36,7 +36,7 @@
 
 	            <h:outputLink value="dateTimeConverter.jsf"><f:verbatim>DateTimeConverter - a datetime converter that uses system timezone as default</f:verbatim></h:outputLink>
 	            <h:outputLink value="valueChangeNotifier.jsf"><f:verbatim>valueChangeNotifier - Calls a custom valueChangeEvent method during MODEL_UPDATE Phase of the Faces Lifecycle</f:verbatim></h:outputLink>
-	            <h:outputLink value="form.jsf"><f:verbatim>form component. Standard form vs. Tomahawk/Sandbox form</f:verbatim></h:outputLink>
+	            <h:outputLink value="form.jsf"><f:verbatim>Form component. Standard form vs. Tomahawk/Sandbox form</f:verbatim></h:outputLink>
 
 	            <h:outputText value="Validation"/>
 	            <h:panelGrid style="padding-left:25px">
@@ -70,6 +70,7 @@
             <h:outputText value="Selection Lists"/>
             <h:panelGrid style="padding-left:25px">
                <h:outputLink value="picklist.jsf"><f:verbatim>selectManyPicklist - A picklist, where you select components from a list and the selected items are displayed in another list</f:verbatim></h:outputLink>
+               <h:outputLink value="selectItems.jsf"><f:verbatim>selectitems - An extended version of the UISelectItems, implicitly populates the SelectItem collection from the given value</f:verbatim></h:outputLink>
             </h:panelGrid>
 
             <h:outputText value="Messages"/>

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/selectItems.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/selectItems.jsp?rev=422732&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/selectItems.jsp (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/selectItems.jsp Mon Jul 17 07:39:42 2006
@@ -0,0 +1,51 @@
+<%@ page session="false" contentType="text/html;charset=utf-8"%>
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+<html>
+
+<!--
+/*
+ * Copyright 2004-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.
+ */
+//-->
+
+<%@include file="inc/head.inc" %>
+
+<body>
+
+<f:view>
+
+    <h:form>
+		<h:selectOneMenu id="menu1" value="#{selectItemsBean.selectedCarColor}">
+   			<s:selectitems value="#{selectItemsBean.carList}" var="Car" itemLabel="#{Car.type}" itemValue="#{Car.color}" />
+		</h:selectOneMenu>
+		
+        <h:selectOneRadio id="radio1" value="#{selectItemsBean.selectedCarColor}">
+   			<s:selectitems value="#{selectItemsBean.carList}" var="Car" itemLabel="#{Car.type}" itemValue="#{Car.color}" />
+        </h:selectOneRadio>
+        
+        <h:selectOneListbox id="list1" value="#{selectItemsBean.selectedCarColor}">
+   			<s:selectitems value="#{selectItemsBean.carList}" var="Car" itemLabel="#{Car.type}" itemValue="#{Car.color}" />
+        </h:selectOneListbox>
+    </h:form>
+</f:view>
+
+<%@include file="inc/page_footer.jsp" %>
+
+</body>
+
+</html>