[richfaces-svn-commits] JBoss Rich Faces SVN: r5185 - in trunk/sandbox/ui/progressBAR/src/main: java/org/richfaces/renderkit and 3 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Tue Jan 8 11:07:45 EST 2008


Author: andrei_exadel
Date: 2008-01-08 11:07:45 -0500 (Tue, 08 Jan 2008)
New Revision: 5185

Added:
   trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/
   trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js
Modified:
   trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml
   trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java
   trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx
Log:
Add client mode to progress bar component

Modified: trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml	2008-01-08 16:07:35 UTC (rev 5184)
+++ trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml	2008-01-08 16:07:45 UTC (rev 5185)
@@ -29,6 +29,11 @@
 		&html_universal_attributes;
 		&html_events;
 		<property>
+			<name>mode</name>
+			<classname>java.lang.String</classname>
+			<description>Attributes defines AJAX or CLIENT modes for component</description>
+		</property>
+		<property>
 			<name>styleClass</name>
 			<classname>java.lang.String</classname>
 			<description>CSS class that defines style for progress bar</description>

Modified: trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java	2008-01-08 16:07:35 UTC (rev 5184)
+++ trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java	2008-01-08 16:07:45 UTC (rev 5185)
@@ -7,6 +7,7 @@
 package org.richfaces.renderkit;
 
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
@@ -51,6 +52,7 @@
 
 	ResponseWriter writer = context.getResponseWriter();
 	UIProgressBar progressBar = (UIProgressBar) component;
+	String clientId = component.getClientId(context);
 	writer.startElement(HTML.SPAN_ELEM, component);
 	writer.writeAttribute(HTML.style_ATTRIBUTE, "display:none;", null);
 	writer.startElement(HTML.SCRIPT_ELEM, component);
@@ -67,7 +69,7 @@
 		    component);
 	    Integer interval = new Integer(progressBar.getInterval());
 	    options.put("pollinterval", interval);
-	    options.put("pollId", component.getClientId(context));
+	    options.put("pollId", clientId);
 	    Object onsubmit = component.getAttributes().get("onsubmit");
 	    if (null != onsubmit) {
 		JSFunctionDefinition onsubmitFunction = new JSFunctionDefinition();
@@ -82,26 +84,35 @@
 		    component.getClientId(context)).append("')");
 	}
 	script.append(";\n");
+	// script.append(getPercentValueScript(component));
 	writer.writeText(script.toString(), null);
 	writer.endElement(HTML.SCRIPT_ELEM);
 	writer.endElement(HTML.SPAN_ELEM);
     }
 
-    //TODO do not encode content twice, use node cloning on client
-    //TODO provide client script with percent value
-    public void encodePersent(FacesContext context, UIComponent component)
+    // private StringBuffer getPercentValueScript(UIComponent component)
+    // throws IOException {
+    // StringBuffer script = new StringBuffer();
+    // ComponentVariables variables = ComponentsVariableResolver.getVariables(
+    // this, component);
+    // String percent = getPercent(component);
+    // return script;
+    //
+    // }
+
+    // TODO do not encode content twice, use node cloning on client
+    // TODO provide client script with percent value
+    public String encodePersent(FacesContext context, UIComponent component)
 	    throws IOException {
-	ResponseWriter writer = context.getResponseWriter();
-	String determinedAttr = (String)component.getAttributes().get("determined");
+	String determinedAttr = (String) component.getAttributes().get(
+		"determined");
 	if (determinedAttr != null && "true".equals(determinedAttr)) {
-	    ComponentVariables variables = ComponentsVariableResolver
-		    .getVariables(this, component);
-	    Integer percent = (Integer) variables.getVariable("percent");
+	    String percent = getPercent(component);
 	    if (percent != null) {
-		writer.write(percent.toString() + "%");
+		return percent + "%";
 	    }
 	}
-
+	return "";
     }
 
     /**
@@ -114,21 +125,75 @@
      * @throws IOException -
      *                 IOException
      */
-    public void encodeInitialState(FacesContext context, UIComponent component)
-	    throws IOException {
+    public void encodeInitialState(FacesContext context, UIComponent component,
+	    Object data) throws IOException {
+	Boolean isAjax = Boolean.parseBoolean((String) data);
 	String clientId = component.getClientId(context);
 	ResponseWriter writer = context.getResponseWriter();
 	writer.startElement(HTML.DIV_ELEM, component);
-	writer.writeAttribute(HTML.id_ATTRIBUTE, clientId, null);
+	writer.writeAttribute(HTML.id_ATTRIBUTE, (isAjax ? clientId : clientId
+		+ ":initialState"), null);
+	
 	UIComponent initial = component.getFacet("initial");
 	if (initial != null) {
 	    renderChild(context, initial);
 	}
-	encodePollScript(context, component);
+	if (isAjax) {
+	    encodePollScript(context, component);
+	}
 	writer.endElement(HTML.DIV_ELEM);
 
     }
 
+    public void encodeProgressState(FacesContext context, UIComponent component)
+	    throws IOException {
+	ResponseWriter writer = context.getResponseWriter();
+	String clientId = component.getClientId(context);
+	ComponentVariables variables = ComponentsVariableResolver.getVariables(
+		this, component);
+	Number value = (Number) variables.getVariable("percent");
+	String width = String.valueOf(value.intValue());
+	String percent = encodePersent(context, component);
+	String style = (String) component.getAttributes().get("style");
+	String permanent = (String) component.getAttributes().get("permanent");
+
+	String completeClass = (component.getAttributes().get("completeClass") != null) ? (String) component
+		.getAttributes().get("completeClass")
+		: ((permanent != null && "true".equals(permanent)) ? "rich-progress-bar-permanent"
+			: "rich-progress-bar-completed");
+
+	String remainClass = (component.getAttributes().get("remainClass") != null) ? (String) component
+		.getAttributes().get("remainClass")
+		: "rich-progress-bar-remained";
+
+	writer.startElement("div", component);
+	getUtils().writeAttribute(writer, "class",
+		remainClass + " rich-progress-bar-base");
+	getUtils().writeAttribute(writer, "id", clientId + ":remain");
+	getUtils().writeAttribute(writer, "style", style);
+
+	writer.write(percent);
+
+	writer.endElement("div");
+	writer.startElement("div", component);
+	getUtils()
+		.writeAttribute(writer, "class", "rich-progress-bar-uploaded");
+	getUtils().writeAttribute(writer, "id", clientId + ":upload");
+	getUtils().writeAttribute(writer, "style",
+		style + "; width: " + width + "%;");
+
+	writer.startElement("div", component);
+	getUtils().writeAttribute(writer, "class",
+		completeClass + " rich-progress-bar-base");
+	getUtils().writeAttribute(writer, "id", clientId + ":complete");
+	getUtils().writeAttribute(writer, "style", style);
+
+	writer.write(percent);
+
+	writer.endElement("div");
+	writer.endElement("div");
+    }
+
     /**
      * Methods encodes finish facet of progress bar component
      * 
@@ -139,26 +204,30 @@
      * @throws IOException -
      *                 IOException
      */
-    public void encodeCompletedState(FacesContext context, UIComponent component)
-	    throws IOException {
+    public void encodeCompletedState(FacesContext context,
+	    UIComponent component, Object data) throws IOException {
+	Boolean isAjax = Boolean.parseBoolean((String) data);
+	if (isAjax) {
+	    Object rerenderAfterComplete = component.getAttributes().get(
+		    "reRenderAfterComplete");
+	    Set ajaxRegions = AjaxRendererUtils.asSet(rerenderAfterComplete);
 
-	Object rerenderAfterComplete = component.getAttributes().get(
-		"reRenderAfterComplete");
-	Set ajaxRegions = AjaxRendererUtils.asSet(rerenderAfterComplete);
-
-	if (ajaxRegions != null) {
-	    for (Iterator iter = ajaxRegions.iterator(); iter.hasNext();) {
-		String id = iter.next().toString();
-		AjaxContext ajaxContext = AjaxContext
-			.getCurrentInstance(context);
-		ajaxContext.addComponentToAjaxRender(component, id);
+	    if (ajaxRegions != null) {
+		for (Iterator iter = ajaxRegions.iterator(); iter.hasNext();) {
+		    String id = iter.next().toString();
+		    AjaxContext ajaxContext = AjaxContext
+			    .getCurrentInstance(context);
+		    ajaxContext.addComponentToAjaxRender(component, id);
+		}
 	    }
 	}
-
-	String clientId = component.getClientId(context);
+	String clientId = (isAjax) ? component.getClientId(context) : component
+		.getClientId(context)
+		+ ":completedState";
 	ResponseWriter writer = context.getResponseWriter();
 	writer.startElement(HTML.DIV_ELEM, component);
 	writer.writeAttribute(HTML.id_ATTRIBUTE, clientId, null);
+	
 	UIComponent completed = component.getFacet("completed");
 	if (completed != null) {
 	    renderChild(context, completed);
@@ -167,6 +236,27 @@
 
     }
 
+    private String getPercent(UIComponent component) {
+	ComponentVariables variables = ComponentsVariableResolver.getVariables(
+		this, component);
+	String result = "0";
+	Object percent = variables.getVariable("percent");
+	if (percent != null) {
+	    try {
+		Number n = (Number) percent;
+		if (n instanceof BigDecimal || n instanceof Double
+			|| n instanceof Float) {
+		    result = String.valueOf(n.floatValue());
+		} else if (n instanceof Integer) {
+		    result = String.valueOf(n.intValue());
+		}
+	    } catch (Exception e) {
+		// TODO: handle exception
+	    }
+	}
+	return result;
+    }
+
     /*
      * (non-Javadoc)
      * 

Added: trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js	                        (rev 0)
+++ trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js	2008-01-08 16:07:45 UTC (rev 5185)
@@ -0,0 +1,73 @@
+ProgressBar = {};
+
+ProgressBar = Class.create();
+
+Object.extend(ProgressBar.prototype, {
+    initialize: function(id) {
+    	this.id = id;
+    	this.mode = "initial";
+    },
+	getPercent: function () {
+		var d = $(this.id + ":remain");
+		if (d != null) {
+			var v = d.innerHTML; 
+			if (v != null) {
+				return v.replace("%","");
+			}
+		}
+		else return null;
+	},
+	setPercent: function (val) {
+		var p = val;
+		if (val != null) {
+			if (val.indexOf("%") < 0)
+				val = val + "%";
+		}
+		if (p <= 0) this.switchMode("initial");
+			else if (p > 0 && p < 100) this.switchMode("progress");
+					else if (p >= 100) this.switchMode("completed");			
+		
+		var d = $(this.id + ":remain");
+		if (d != null) d.innerHTML = val;
+		d = $(this.id + ":complete");
+		if (d != null) d.innerHTML = val;
+		d = $(this.id + ":upload");
+		if (d != null) d.style.width = val;
+	},
+	switchMode : function (mode) {
+		switch (mode) {
+			case "initial" : 
+				this.hideStates(this.id + ":progressState",this.id + ":completedState");
+				this.show($(this.id + ":initialState"));
+			break;
+		  	case "progress" :
+		  		this.hideStates(this.id + ":initialState",this.id + ":completedState");
+				this.show($(this.id + ":progressState"));
+		 	break;
+		 	case "completed" :
+		 		this.hideStates(this.id + ":progressState",this.id + ":initialState");
+				this.show($(this.id + ":completedState"));
+		 	break;		 	
+		}
+		this.mode = mode;
+	},
+	hideStates : function () {
+	var d = $(arguments[0]);
+		for (i = 0 ; i < arguments.length; i++) {
+			d = $(arguments[i]);
+			if (d != null) this.hide(d); 
+		}
+	},
+	init : function () {
+	var p = this.getPercent();
+		if (p <= 0) this.switchMode("initial");
+		 else if (p > 0 && p < 100) this.switchMode("progress");
+		 	else if (p >= 100) this.switchMode("completed");
+	},
+	hide : function (o) {
+		o.style.display = 'none';
+	},
+	show : function (o) {
+		o.style.display = '';
+	}
+	});

Modified: trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx	2008-01-08 16:07:35 UTC (rev 5184)
+++ trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx	2008-01-08 16:07:45 UTC (rev 5185)
@@ -1,64 +1,69 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<f:root 
-	xmlns:f="http://ajax4jsf.org/cdk/template" 
+<f:root xmlns:f="http://ajax4jsf.org/cdk/template"
 	xmlns:h=" http://ajax4jsf.org/cdk/headers"
 	class="org.richfaces.renderkit.ProgressBarRenderer"
 	baseclass="org.richfaces.renderkit.AbstractProgressBarRenderer"
-	component="org.richfaces.component.UIProgressBar" 
-	>	
+	component="org.richfaces.component.UIProgressBar">
 
-<h:styles>/org/richfaces/renderkit/html/css/progressBar.xcss</h:styles>
+	<h:styles>/org/richfaces/renderkit/html/css/progressBar.xcss</h:styles>
 
-<h:scripts>
+	<h:scripts>
 			new org.ajax4jsf.javascript.PrototypeScript(),
 			new org.ajax4jsf.javascript.AjaxScript(),
+			/org/richfaces/renderkit/html/js/progressBar.js,
 </h:scripts>
-	
-<f:clientId var="clientId" />
 
-<jsp:scriptlet>
+	<f:clientId var="clientId" />
+
+	<jsp:scriptlet>
 <![CDATA[
-    Integer percent = (Integer)component.getAttributes().get("value");
-    
-    variables.setVariable("percent",percent);
+    String mode = (String) component.getAttributes().get("mode");
+    boolean isAjax = !(mode != null && "client".equalsIgnoreCase(mode));
+    Number value = (Number)component.getAttributes().get("value");
+    Integer percent = value.intValue();
+    variables.setVariable("percent",value);
     variables.setVariable("style",(String)component.getAttributes().get("style"));
     variables.setVariable("styleClass",(String)component.getAttributes().get("styleClass"));
-    
-    String permanent = (String)component.getAttributes().get("permanent");
-    
-    variables.setVariable("completeClass",(component.getAttributes().get("completeClass") != null) ? 
-	    (String)component.getAttributes().get("completeClass") : 
-							((permanent != null && "true".equals(permanent)) ? 
-									"rich-progress-bar-permanent" : "rich-progress-bar-completed"));
-    
-    variables.setVariable("remainClass",(component.getAttributes().get("remainClass") != null) ? 
-		(String)component.getAttributes().get("remainClass"): "rich-progress-bar-remained" );
 
     
  ]]>
 </jsp:scriptlet>
 
-<jsp:scriptlet>
+	<jsp:scriptlet>
 <![CDATA[
+if (isAjax) {
 if (percent <= 0) { 
-    encodeInitialState(context, component);
+    encodeInitialState(context, component, "true");
 } else if (percent >= 100) {
-    encodeCompletedState(context, component);
+    encodeCompletedState(context, component, "true");
 } else {
  ]]>
 </jsp:scriptlet>
+	<div class="rich-progress-bar #{styleClass}" style="#{style}" id="#{clientId}">
+		<f:call name="encodeProgressState"/> 
+		<f:call name="encodePollScript" />
+	</div>
+<jsp:scriptlet>
+<![CDATA[
+}
+}else {
+]]>
+</jsp:scriptlet>
 <div class="rich-progress-bar #{styleClass}" style="#{style}" id="#{clientId}">
-	<div class="#{remainClass} rich-progress-bar-base" style="#{style}">
-		<f:call name="encodePersent"/>
+	<div id="#{clientId}:progressState">
+		<f:call name="encodeProgressState" />
 	</div>
-	<div class="rich-progress-bar-uploaded" style="#{style}; width: #{percent}%;">
-		<div class="#{completeClass} rich-progress-bar-base"  style="#{style}">
-			<f:call name="encodePersent"/>
-		</div>	
-	</div>
-<f:call name="encodePollScript"/>
+	<f:call name="encodeInitialState">
+		<f:parameter value="false"/>
+	</f:call>
+	<f:call name="encodeCompletedState">
+		<f:parameter value="false"/>
+	</f:call>
+	<script>
+		var pr = new ProgressBar('#{clientId}');
+		pr.init();
+	</script>
 </div>
-	
 <jsp:scriptlet>
 <![CDATA[
 }




More information about the richfaces-svn-commits mailing list