Default spring-faces component support now based on Dojo. Ext support moved to faces-ext tag namespace.

This commit is contained in:
Jeremy Grelle
2007-10-26 19:34:06 +00:00
parent 42598c2044
commit 392074ffca
497 changed files with 55550 additions and 71 deletions

View File

@@ -1,12 +1,152 @@
package org.springframework.faces.ui;
import javax.el.ValueExpression;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
public class DojoAdvisor extends UIComponentBase {
public abstract class DojoAdvisor extends UIComponentBase {
protected static final String[] DOJO_ATTRS = new String[] { "disabled", "intermediateChanges", "tabIndex",
"required", "promptMessage", "invalidMessage", "constraints", "regExp", "regExpGen" };
private Boolean disabled;
private Boolean intermediateChanges;
private Integer tabIndex;
private Boolean required;
private String promptMessage;
private String invalidMessage;
private String constraints;
private String regExp;
private String regExpGen;
public Boolean getDisabled() {
if (disabled != null) {
return disabled;
}
ValueExpression exp = getValueExpression("disabled");
return exp != null ? (Boolean) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
public Boolean getIntermediateChanges() {
return intermediateChanges;
}
public void setIntermediateChanges(Boolean intermediateChanges) {
this.intermediateChanges = intermediateChanges;
}
public Integer getTabIndex() {
return tabIndex;
}
public void setTabIndex(Integer tabIndex) {
this.tabIndex = tabIndex;
}
public Boolean getRequired() {
return required;
}
public void setRequired(Boolean required) {
this.required = required;
}
public String getPromptMessage() {
if (promptMessage != null) {
return promptMessage;
}
ValueExpression exp = getValueExpression("promptMessage");
return exp != null ? (String) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setPromptMessage(String promptMessage) {
this.promptMessage = promptMessage;
}
public String getInvalidMessage() {
if (invalidMessage != null) {
return invalidMessage;
}
ValueExpression exp = getValueExpression("invalidMessage");
return exp != null ? (String) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setInvalidMessage(String invalidMessage) {
this.invalidMessage = invalidMessage;
}
public String getConstraints() {
return constraints;
}
public void setConstraints(String constraints) {
this.constraints = constraints;
}
public String getRegExp() {
return regExp;
}
public void setRegExp(String regExp) {
this.regExp = regExp;
}
public String getRegExpGen() {
return regExpGen;
}
public void setRegExpGen(String regExpGen) {
this.regExpGen = regExpGen;
}
protected abstract String[] getDojoAttributes();
public abstract String getDojoComponentType();
public Object saveState(FacesContext context) {
Object[] values = new Object[10];
values[0] = super.saveState(context);
values[1] = this.constraints;
values[2] = this.disabled;
values[3] = this.intermediateChanges;
values[4] = this.invalidMessage;
values[5] = this.promptMessage;
values[6] = this.regExp;
values[7] = this.regExpGen;
values[8] = this.required;
values[9] = this.tabIndex;
return values;
}
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
this.constraints = (String) values[1];
this.disabled = (Boolean) values[2];
this.intermediateChanges = (Boolean) values[3];
this.invalidMessage = (String) values[4];
this.promptMessage = (String) values[5];
this.regExp = (String) values[6];
this.regExpGen = (String) values[7];
this.required = (Boolean) values[8];
this.tabIndex = (Integer) values[9];
}
public String getFamily() {
return "spring.faces.DojoAdvisor";
return "spring.faces.Advisor";
}
}

View File

@@ -0,0 +1,109 @@
package org.springframework.faces.ui;
import java.io.IOException;
import java.util.Date;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.springframework.faces.ui.resource.FlowResourceHelper;
import org.springframework.util.StringUtils;
public class DojoAdvisorRenderer extends DojoRenderer {
private static final String SCRIPT_ELEMENT = "script";
private FlowResourceHelper resourceHelper = new FlowResourceHelper();
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
if (component.getChildCount() == 0)
throw new FacesException("A Spring Faces advisor expects to have at least one child component.");
UIComponent advisedChild = component.getChildren().get(0);
resourceHelper.renderDojoInclude(context, ((DojoAdvisor) component).getDojoComponentType());
writer.startElement(SCRIPT_ELEMENT, component);
StringBuffer script = new StringBuffer();
script.append(" SpringFaces.advisors.push(new SpringFaces.DojoGenericFieldAdvisor({ ");
script.append(" targetElId : '" + advisedChild.getClientId(context) + "', ");
script.append(" msgElId : '" + advisedChild.getClientId(context) + ":msg', ");
script.append(" decoratorType : '" + ((DojoAdvisor) component).getDojoComponentType() + "', ");
script.append(" decoratorAttrs : \"{ ");
String nodeAttrs = getNodeAttributesAsString(context, advisedChild);
String dojoAttrs = getDojoAttributesAsString(context, component);
script.append(nodeAttrs);
if (StringUtils.hasText(dojoAttrs)) {
script.append(", ");
}
script.append(dojoAttrs);
script.append(" }\"})); ");
writer.writeText(script, null);
writer.endElement(SCRIPT_ELEMENT);
}
protected String getNodeAttributesAsString(FacesContext context, UIComponent component) {
StringBuffer attrs = new StringBuffer();
attrs.append("name : '" + component.getClientId(context) + "'");
ValueHolder valueHolder = (ValueHolder) component;
if (valueHolder.getValue() != null) {
attrs.append(", value : ");
String strValue;
if (valueHolder.getValue() instanceof String) {
strValue = "'" + (String) valueHolder.getValue() + "'";
} else {
strValue = "'" + valueHolder.getConverter().getAsString(context, component, valueHolder.getValue())
+ "'";
if (valueHolder.getValue() instanceof Date) {
strValue = "dojo.date.locale.parse(" + strValue
+ ", {selector : 'date', datePattern : 'yyyy-MM-dd'})";
}
}
attrs.append(strValue);
}
return attrs.toString();
}
protected String getDojoAttributesAsString(FacesContext context, UIComponent component) {
DojoAdvisor advisor = (DojoAdvisor) component;
StringBuffer attrs = new StringBuffer();
for (int i = 0; i < advisor.getDojoAttributes().length; i++) {
String key = advisor.getDojoAttributes()[i];
Object value = advisor.getAttributes().get(key);
if (value != null) {
if (attrs.length() > 0)
attrs.append(", ");
attrs.append(key + " : ");
if (value instanceof String) {
attrs.append("'" + value + "'");
} else {
attrs.append(value.toString());
}
}
}
return attrs.toString();
}
}

View File

@@ -0,0 +1,55 @@
package org.springframework.faces.ui;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
public class DojoClientCurrencyValidator extends DojoAdvisor {
private static final String DOJO_COMPONENT_TYPE = "dijit.form.CurrencyTextBox";
private static final String[] DOJO_ATTRS_INTERNAL = new String[] { "currency" };
private static final String[] DOJO_ATTRS;
static {
DOJO_ATTRS = new String[DojoAdvisor.DOJO_ATTRS.length + DOJO_ATTRS_INTERNAL.length];
System.arraycopy(DojoAdvisor.DOJO_ATTRS, 0, DOJO_ATTRS, 0, DojoAdvisor.DOJO_ATTRS.length);
System.arraycopy(DOJO_ATTRS_INTERNAL, 0, DOJO_ATTRS, DojoAdvisor.DOJO_ATTRS.length, DOJO_ATTRS_INTERNAL.length);
}
private String currency;
public String getCurrency() {
if (currency != null) {
return currency;
}
ValueExpression exp = getValueExpression("currency");
return exp != null ? (String) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setCurrency(String currency) {
this.currency = currency;
}
protected String[] getDojoAttributes() {
return DOJO_ATTRS;
}
public String getDojoComponentType() {
return DOJO_COMPONENT_TYPE;
}
public Object saveState(FacesContext context) {
Object[] values = new Object[2];
values[0] = super.saveState(context);
values[1] = this.currency;
return values;
}
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[]) state;
super.restoreState(context, values[0]);
this.currency = (String) values[1];
}
}

View File

@@ -0,0 +1,15 @@
package org.springframework.faces.ui;
public class DojoClientDateValidator extends DojoAdvisor {
private static final String DOJO_COMPONENT_TYPE = "dijit.form.DateTextBox";
protected String[] getDojoAttributes() {
return DojoAdvisor.DOJO_ATTRS;
}
public String getDojoComponentType() {
return DOJO_COMPONENT_TYPE;
}
}

View File

@@ -0,0 +1,15 @@
package org.springframework.faces.ui;
public class DojoClientNumberValidator extends DojoAdvisor {
private static final String DOJO_COMPONENT_TYPE = "dijit.form.NumberTextBox";
protected String[] getDojoAttributes() {
return DojoAdvisor.DOJO_ATTRS;
}
public String getDojoComponentType() {
return DOJO_COMPONENT_TYPE;
}
}

View File

@@ -0,0 +1,15 @@
package org.springframework.faces.ui;
public class DojoClientTextValidator extends DojoAdvisor {
private static final String DOJO_COMPONENT_TYPE = "dijit.form.ValidationTextBox";
protected String[] getDojoAttributes() {
return DojoAdvisor.DOJO_ATTRS;
}
public String getDojoComponentType() {
return DOJO_COMPONENT_TYPE;
}
}

View File

@@ -4,36 +4,29 @@ import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;
import org.springframework.faces.ui.resource.FlowResourceHelper;
public class DojoRenderer extends Renderer {
public class DojoRenderer extends SpringFacesRenderer {
private String dojoJsResourceUri = "/dojo/dojo.js";
private String dojoStyleResourceUri = "/dojo/dojo.css";
private String dijitThemePath = "/dijit/themes/";
private String dijitTheme = "tundra";
private String springFacesJsResourceUri = "/spring-faces/SpringFaces.js";
private String springFacesDojoJsResourceUri = "/spring-faces/SpringFaces-Dojo.js";
private FlowResourceHelper resourceHelper = new FlowResourceHelper();
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
super.encodeBegin(context, component);
resourceHelper.renderStyleLink(context, dijitThemePath + "/" + dijitTheme + "/" + dijitTheme + ".css");
resourceHelper.renderStyleLink(context, dojoStyleResourceUri);
resourceHelper.renderScriptLink(context, dojoJsResourceUri);
resourceHelper.renderScriptLink(context, springFacesJsResourceUri);
resourceHelper.renderScriptLink(context, springFacesDojoJsResourceUri);
}
}

View File

@@ -1,7 +1,7 @@
package org.springframework.faces.ui;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
public abstract class ExtAdvisor extends ExtJsComponent {
@@ -108,8 +108,8 @@ public abstract class ExtAdvisor extends ExtJsComponent {
if (disabled != null) {
return disabled;
}
ValueBinding vb = getValueBinding("disabled");
return vb != null ? (Boolean) vb.getValue(getFacesContext()) : null;
ValueExpression exp = getValueExpression("disabled");
return exp != null ? (Boolean) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setDisabled(Boolean disabled) {
@@ -152,8 +152,8 @@ public abstract class ExtAdvisor extends ExtJsComponent {
if (invalidText != null) {
return invalidText;
}
ValueBinding vb = getValueBinding("invalidText");
return vb != null ? (String) vb.getValue(getFacesContext()) : null;
ValueExpression exp = getValueExpression("invalidText");
return exp != null ? (String) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setInvalidText(String invalidText) {
@@ -164,8 +164,8 @@ public abstract class ExtAdvisor extends ExtJsComponent {
if (msgClass != null) {
return msgClass;
}
ValueBinding vb = getValueBinding("msgClass");
return vb != null ? (String) vb.getValue(getFacesContext()) : null;
ValueExpression exp = getValueExpression("msgClass");
return exp != null ? (String) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setMsgClass(String msgClass) {
@@ -184,12 +184,12 @@ public abstract class ExtAdvisor extends ExtJsComponent {
if (readOnly != null) {
return readOnly;
}
ValueBinding vb = getValueBinding("readOnly");
return vb != null ? (Boolean) vb.getValue(getFacesContext()) : null;
ValueExpression exp = getValueExpression("readOnly");
return exp != null ? (Boolean) exp.getValue(getFacesContext().getELContext()) : null;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = new Boolean(readOnly);
public void setReadOnly(Boolean readOnly) {
this.readOnly = readOnly;
}
public Boolean getValidateOnBlur() {

View File

@@ -3,7 +3,7 @@ package org.springframework.faces.ui;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
public class ClientDateValidator extends ClientTextValidator {
public class ExtClientDateValidator extends ExtClientTextValidator {
private static final String EXT_COMPONENT_TYPE = "Ext.form.DateField";
@@ -14,9 +14,9 @@ public class ClientDateValidator extends ClientTextValidator {
protected static final String[] EXT_ATTRS;
static {
EXT_ATTRS = new String[ClientTextValidator.EXT_ATTRS.length + EXT_ATTRS_INTERNAL.length];
System.arraycopy(ClientTextValidator.EXT_ATTRS, 0, EXT_ATTRS, 0, ClientTextValidator.EXT_ATTRS.length);
System.arraycopy(EXT_ATTRS_INTERNAL, 0, EXT_ATTRS, ClientTextValidator.EXT_ATTRS.length,
EXT_ATTRS = new String[ExtClientTextValidator.EXT_ATTRS.length + EXT_ATTRS_INTERNAL.length];
System.arraycopy(ExtClientTextValidator.EXT_ATTRS, 0, EXT_ATTRS, 0, ExtClientTextValidator.EXT_ATTRS.length);
System.arraycopy(EXT_ATTRS_INTERNAL, 0, EXT_ATTRS, ExtClientTextValidator.EXT_ATTRS.length,
EXT_ATTRS_INTERNAL.length);
}

View File

@@ -3,7 +3,7 @@ package org.springframework.faces.ui;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
public class ClientNumberValidator extends ClientTextValidator {
public class ExtClientNumberValidator extends ExtClientTextValidator {
private static final String EXT_COMPONENT_TYPE = "Ext.form.NumberField";
@@ -13,9 +13,9 @@ public class ClientNumberValidator extends ClientTextValidator {
protected static final String[] EXT_ATTRS;
static {
EXT_ATTRS = new String[ClientTextValidator.EXT_ATTRS.length + EXT_ATTRS_INTERNAL.length];
System.arraycopy(ClientTextValidator.EXT_ATTRS, 0, EXT_ATTRS, 0, ClientTextValidator.EXT_ATTRS.length);
System.arraycopy(EXT_ATTRS_INTERNAL, 0, EXT_ATTRS, ClientTextValidator.EXT_ATTRS.length,
EXT_ATTRS = new String[ExtClientTextValidator.EXT_ATTRS.length + EXT_ATTRS_INTERNAL.length];
System.arraycopy(ExtClientTextValidator.EXT_ATTRS, 0, EXT_ATTRS, 0, ExtClientTextValidator.EXT_ATTRS.length);
System.arraycopy(EXT_ATTRS_INTERNAL, 0, EXT_ATTRS, ExtClientTextValidator.EXT_ATTRS.length,
EXT_ATTRS_INTERNAL.length);
}

View File

@@ -3,7 +3,7 @@ package org.springframework.faces.ui;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
public class ClientTextValidator extends ExtAdvisor {
public class ExtClientTextValidator extends ExtAdvisor {
private static final String EXT_COMPONENT_TYPE = "Ext.form.TextField";

View File

@@ -20,7 +20,7 @@ public class ExtJsComponent extends UIComponentBase {
public String getFamily() {
return "spring.faces.ExtAdvisor";
return "spring.faces.Advisor";
}
public Boolean getIncludeExtStyles() {

View File

@@ -4,24 +4,23 @@ import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;
import org.springframework.faces.ui.resource.FlowResourceHelper;
public class ExtJsRenderer extends Renderer {
public class ExtJsRenderer extends SpringFacesRenderer {
private static final String EXT_CSS = "/ext/resources/css/ext-all.css";
private static final String EXT_SCRIPT = "/ext/ext.js";
private static final String SPRING_FACES_SCRIPT = "/spring-faces/SpringFaces.js";
private static final String SPRING_FACES_EXT_SCRIPT = "/spring-faces/SpringFaces-Ext.js";
private FlowResourceHelper resourceHelper = new FlowResourceHelper();
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
super.encodeBegin(context, component);
ExtJsComponent extJsComponent = (ExtJsComponent) component;
if (extJsComponent.getIncludeExtStyles().equals(Boolean.TRUE)) {
@@ -32,8 +31,6 @@ public class ExtJsRenderer extends Renderer {
resourceHelper.renderScriptLink(context, EXT_SCRIPT);
}
resourceHelper.renderScriptLink(context, SPRING_FACES_SCRIPT);
resourceHelper.renderScriptLink(context, SPRING_FACES_EXT_SCRIPT);
}
}

View File

@@ -0,0 +1,22 @@
package org.springframework.faces.ui;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.render.Renderer;
import org.springframework.faces.ui.resource.FlowResourceHelper;
public class SpringFacesRenderer extends Renderer {
private String springFacesJsResourceUri = "/spring-faces/SpringFaces.js";
private FlowResourceHelper resourceHelper = new FlowResourceHelper();
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
resourceHelper.renderScriptLink(context, springFacesJsResourceUri);
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.faces.ui;
import javax.faces.component.UIComponentBase;
public class ValidateAll extends UIComponentBase {
public String getFamily() {
return "spring.faces.Advisor";
}
}

View File

@@ -8,7 +8,7 @@ import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class ExtValidateAllRenderer extends ExtJsRenderer {
public class ValidateAllRenderer extends SpringFacesRenderer {
private static final String SCRIPT_ELEMENT = "script";

View File

@@ -87,6 +87,33 @@ public class FlowResourceHelper {
markRendered(facesContext, cssPath);
}
/**
* Render a <code><script/></code> tag for a given dojo include.
* @param facesContext
* @param module
* @throws IOException
*/
public void renderDojoInclude(FacesContext facesContext, String module) throws IOException {
if (alreadyRendered(facesContext, module)) {
return;
}
RequestContext requestContext = RequestContextHolder.getRequestContext();
ResponseWriter writer = facesContext.getResponseWriter();
writer.startElement("script", null);
writer.writeAttribute("type", "text/javascript", null);
writer.writeText("dojo.require('" + module + "');", null);
writer.endElement("script");
markRendered(facesContext, module);
}
private void markRendered(FacesContext facesContext, String scriptPath) {
Set renderedResources = (Set) facesContext.getExternalContext().getRequestMap().get(RENDERED_RESOURCES_KEY);
if (renderedResources == null) {

View File

@@ -46,13 +46,18 @@ public class ResolveAndRenderResourceAction implements Action {
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
String resourcePath = "META-INF" + context.getExternalContext().getRequestPath().toString();
String localResourcePath = context.getExternalContext().getRequestPath().toString();
String jarResourcePath = "META-INF" + context.getExternalContext().getRequestPath().toString();
URL resource = ClassUtils.getDefaultClassLoader().getResource(resourcePath);
URL resource;
resource = servletContext.getResource(localResourcePath);
if (resource == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return new Event(this, "success");
resource = ClassUtils.getDefaultClassLoader().getResource(jarResourcePath);
if (resource == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return new Event(this, "success");
}
}
URLConnection resourceConn = resource.openConnection();
@@ -64,9 +69,9 @@ public class ResolveAndRenderResourceAction implements Action {
return new Event(this, "success");
}
String mimeType = servletContext.getMimeType(resourcePath);
String mimeType = servletContext.getMimeType(jarResourcePath);
if (mimeType == null) {
String extension = resourcePath.substring(resourcePath.lastIndexOf('.'));
String extension = jarResourcePath.substring(jarResourcePath.lastIndexOf('.'));
mimeType = defaultMimeTypes.get(extension);
}
response.setContentType(mimeType);