[richfaces-svn-commits] JBoss Rich Faces SVN: r13750 - in trunk/ui/inplaceSelect/src/main: templates and 1 other directory.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Tue Apr 21 19:57:19 EDT 2009


Author: nbelaevski
Date: 2009-04-21 19:57:19 -0400 (Tue, 21 Apr 2009)
New Revision: 13750

Modified:
   trunk/ui/inplaceSelect/src/main/java/org/richfaces/renderkit/InplaceSelectBaseRenderer.java
   trunk/ui/inplaceSelect/src/main/templates/inplaceselect.jspx
Log:
https://jira.jboss.org/jira/browse/RF-6650

Modified: trunk/ui/inplaceSelect/src/main/java/org/richfaces/renderkit/InplaceSelectBaseRenderer.java
===================================================================
--- trunk/ui/inplaceSelect/src/main/java/org/richfaces/renderkit/InplaceSelectBaseRenderer.java	2009-04-21 23:05:53 UTC (rev 13749)
+++ trunk/ui/inplaceSelect/src/main/java/org/richfaces/renderkit/InplaceSelectBaseRenderer.java	2009-04-21 23:57:19 UTC (rev 13750)
@@ -27,8 +27,14 @@
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
 import javax.faces.model.SelectItem;
 
+import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.ScriptString;
+import org.ajax4jsf.javascript.ScriptUtils;
+import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
 import org.ajax4jsf.util.InputUtils;
 import org.ajax4jsf.util.SelectUtils;
 import org.apache.commons.logging.Log;
@@ -40,7 +46,7 @@
  * @author Anton Belevich
  * @since 3.2.0	
  */
-public class InplaceSelectBaseRenderer extends ComboBoxBaseRenderer{
+public class InplaceSelectBaseRenderer extends HeaderResourcesRendererBase {
 
 	private static Log logger = LogFactory.getLog(InplaceSelectBaseRenderer.class);
 	//TODO: move duplicate constants to superclass
@@ -48,6 +54,33 @@
 	private static final String CONTROLS_FACET = "controls";
 	private static final String EMPTY_DEFAULT_LABEL = "\u00a0\u00a0\u00a0";
 
+	protected static final class PreparedSelectItem implements ScriptString {
+		private String label;
+		private String convertedValue;
+
+		public PreparedSelectItem(String convertedValue, String label) {
+			super();
+			this.convertedValue = convertedValue;
+			this.label = label;
+		}
+
+		public String getLabel() {
+			return label;
+		}
+
+		public String getConvertedValue() {
+			return convertedValue;
+		}
+
+		public void appendScript(StringBuffer functionString) {
+			functionString.append(this.toScript());
+		}
+
+		public String toScript() {
+			return "[" + ScriptUtils.toScript(label) + ", " + ScriptUtils.toScript(convertedValue) + "]";
+		}
+	}
+
 	@Override
 	protected void doDecode(FacesContext context, UIComponent component) {
 		UIInplaceSelect inplaceSelect = null;
@@ -83,30 +116,53 @@
 		}
 	}
 
-	public List<Object> encodeItems(FacesContext context, UIComponent component) throws IOException, IllegalArgumentException {
+	protected boolean isAcceptableComponent(UIComponent component) {
+		return component != null && this.getComponentClass().isAssignableFrom(component.getClass());
+	}
 
+	protected String getConvertedStringValue(FacesContext context, UIComponent component, Object value) {
+		return InputUtils.getConvertedStringValue(context, component, value);
+	}
+
+	protected void encodeSuggestion(FacesContext context, UIComponent component, String value, String classes) throws IOException {
+		ResponseWriter writer = context.getResponseWriter();
+		if(writer != null) {
+			writer.startElement(HTML.SPAN_ELEM, component);
+			writer.writeAttribute(HTML.class_ATTRIBUTE, classes, null);
+			writer.writeText(value, null);
+			writer.endElement(HTML.SPAN_ELEM);
+		}
+	}	
+
+	public List<PreparedSelectItem> prepareItems(FacesContext context, UIComponent component) {
 		if (!isAcceptableComponent(component)) {
 			return null;
 		}
 
-		List<Object> parentList = new ArrayList<Object>();
-		List<String> labels = new ArrayList<String>();
+		List<PreparedSelectItem> itemsList = new ArrayList<PreparedSelectItem>();
 
 		UIInplaceSelect inplaceSelect = (UIInplaceSelect) component;
 		List<SelectItem> selectItems = SelectUtils.getSelectItems(context, inplaceSelect);
 		for (SelectItem selectItem : selectItems) {
-			String value = getConvertedStringValue(context, inplaceSelect, selectItem.getValue());
+			String convertedValue = getConvertedStringValue(context, inplaceSelect, selectItem.getValue());
 			String label = selectItem.getLabel().trim();
-			labels.add(label);
-			encodeSuggestion(context, inplaceSelect, label, RICH_INPLACE_SELECT_CLASSES);
-			Object[] child = new Object[2]; 
-			child[0] = label;
-			child[1] = value;
-			parentList.add(child);
+			itemsList.add(new PreparedSelectItem(convertedValue, label));
 		}
-		return parentList;
+
+		return itemsList;
 	}
 
+	public void encodeItems(FacesContext context, UIComponent component, List<PreparedSelectItem> items) throws IOException, IllegalArgumentException {
+		if (items != null) {
+			UIInplaceSelect inplaceSelect = (UIInplaceSelect) component;
+
+			for (PreparedSelectItem preparedSelectItem : items) {
+
+				encodeSuggestion(context, inplaceSelect, preparedSelectItem.getLabel(), RICH_INPLACE_SELECT_CLASSES);
+			}
+		}
+	}
+
 	public String encodeScriptAttributes(FacesContext context, UIComponent component) {
 		StringBuilder attributes = new StringBuilder();
 		attributes.append("var attributes = ");
@@ -168,6 +224,15 @@
 	//	return selectedItemLabel;
 	//    }
 
+	public String getAsEventHandler(FacesContext context, UIComponent component, String attributeName) {
+		JSFunctionDefinition script = getUtils().getAsEventHandler(context, component, attributeName, null);  
+		return ScriptUtils.toScript(script);
+	}
+
+    public String encodeJS(Object object) {
+    	return ScriptUtils.toScript(object);
+    }
+    
 	protected String getItemLabel(FacesContext context, UIInplaceSelect component, Object value) {
 		String itemLabel = null;
 		// TODO: SelectUtils.getSelectItems is called minimum twice during encode

Modified: trunk/ui/inplaceSelect/src/main/templates/inplaceselect.jspx
===================================================================
--- trunk/ui/inplaceSelect/src/main/templates/inplaceselect.jspx	2009-04-21 23:05:53 UTC (rev 13749)
+++ trunk/ui/inplaceSelect/src/main/templates/inplaceselect.jspx	2009-04-21 23:57:19 UTC (rev 13750)
@@ -29,13 +29,31 @@
 <f:clientid var="clientId" />
 <f:resource var="saveIcon" name="org.richfaces.renderkit.html.images.SaveControlIcon"/>
 <f:resource var="cancelIcon" name="org.richfaces.renderkit.html.images.CancelControlIcon"/>
+
+<c:object var="preparedItems" type="java.util.List" value="#{this:prepareItems(context, component)}" />
+
 <jsp:scriptlet>
 		<![CDATA[
-		         
-		Object value = component.getAttributes().get("value");
-		String selectedItemLabel = getItemLabel(context, component, value);
-		value = getConvertedStringValue(context, component,value);
+		Object value = null;
+		String selectedItemLabel = null;
 
+		Object submittedValue = component.getSubmittedValue();
+		if (submittedValue != null) {
+			if (preparedItems != null) {
+				for (Object o: preparedItems) {
+					PreparedSelectItem item = (PreparedSelectItem) o;
+					if (submittedValue.equals(item.getConvertedValue())) {
+						selectedItemLabel = item.getLabel();
+						value = submittedValue;
+						break;
+					}
+				}
+			}
+		} else {
+			value = component.getAttributes().get("value");
+			selectedItemLabel = getItemLabel(context, component, value);
+			value = getConvertedStringValue(context, component,value);
+		}
 		
 		String fieldValue = null;
 		String fieldLabel = null;
@@ -51,7 +69,7 @@
 			fieldInputLabel = fieldLabel; 
 		}
 		
-	   	String encodedFieldValue = encodeValue(fieldValue);
+	   	String encodedFieldValue = encodeJS(fieldValue);
 	   	
 	   	variables.setVariable("fieldLabel", fieldLabel);
 	   	variables.setVariable("fieldValue", fieldValue);
@@ -219,8 +237,11 @@
 			<div id="#{clientId}listPosition" class="rich-inplace-select-list-position">
 				<div id="#{clientId}listDecoration" class="rich-inplace-select-list-decoration">
 					<div id="#{clientId}list" class="rich-inplace-select-list-scroll">
-						<c:object type="java.util.List" var="items" value="#{this:encodeItems(context,component)}">
-						</c:object>
+						<jsp:scriptlet>
+						<![CDATA[
+						encodeItems(context, component, preparedItems);
+						]]>
+						</jsp:scriptlet>
 					</div>
 				</div>
 			</div>
@@ -246,7 +267,7 @@
 				var inplaceSelectCommonStyles = new Richfaces.InplaceSelectStyles();
 				
 				new Richfaces.InplaceSelect(new Richfaces.InplaceSelectList('#{clientId}list', '#{clientId}listParent', true,
-															inplaceSelectUserStyles.combolist, inplaceSelectCommonStyles.getCommonStyles().combolist, '#{component.attributes["listWidth"]}', '#{component.attributes["listHeight"]}',  #{this:getItemsTextAsJSArray(context, component,items)}, null, 
+															inplaceSelectUserStyles.combolist, inplaceSelectCommonStyles.getCommonStyles().combolist, '#{component.attributes["listWidth"]}', '#{component.attributes["listHeight"]}',  #{this:encodeJS(preparedItems)}, null, 
 															'#{clientId}inplaceTmpValue', '#{clientId}shadow', 0, 0, #{encodedFieldValue}), 
 															'#{clientId}', '#{clientId}inplaceTmpValue', 
 															'#{clientId}inplaceValue', '#{clientId}tabber',




More information about the richfaces-svn-commits mailing list