[jboss-user] [JBoss Seam] - Re: different behaviour when clicking on a button or using e

spyman74 do-not-reply at jboss.com
Fri Sep 7 05:04:31 EDT 2007


Maybe this helps

  | /**
  |  * This JSF component marks the enclosing action component (e.g. h:commandButton) as default, i.e.
  |  * if the user presses Enter key the enclosing component's action will be performed, e.g.:
  |  * <code>
  |  * <h:commandButton action="#{myBean.myAction}">
  |  * <myprefix:defaultAction//>
  |  * </h:commandButton>
  |  * <code>
  |  * Only one defaultAction per scope is allowed. You can specify the scope by setting 
  |  * the scope attribute. If you set scope="document" (default) then only one component can be marked as
  |  * defaultAction in the entire HTML document. If you set scope="form" you can have one defaultAction per
  |  * HTML form.
  |  *
  |  */
  | public class UIDefaultAction extends UIOutput {
  | 
  | 	public static final String SCOPE_DOCUMENT = "document";
  | 
  | 	public static final String SCOPE_FORM = "form";
  | 
  | 	private String scope = SCOPE_DOCUMENT;
  | 
  | 	public void encodeBegin(FacesContext context) throws IOException {
  | 		return;
  | 	}
  | 
  | 	public void decode(FacesContext context) {
  | 		return;
  | 	}
  | 
  | 	public void encodeEnd(FacesContext context) throws IOException {
  | 		ResponseWriter writer = context.getResponseWriter();
  | 		UIComponent actionComponent = super.getParent();
  | 		String acId = actionComponent.getClientId(context);
  | 		UIForm form = getForm(actionComponent);
  | 		if (form != null) {
  | 
  | 			String formId = form.getClientId(context);
  | 
  | 			writer.startElement("script", null);
  | 
  | 			StringBuilder javascript = new StringBuilder();
  | 
  | 			javascript.append("function processKeyPress(keyCode) {");
  | 			javascript.append(" if (keyCode == 13) {");
  | 			javascript.append("   document.getElementById('" + acId + "').click();");
  | 			javascript.append(" }");
  | 			javascript.append("}");
  | 
  | 			javascript.append("function clickEnter(event) {");
  | 			javascript.append("	processKeyPress(event.which);");
  | 			javascript.append("}");
  | 
  | 			javascript.append("function clickEnterIE() {");
  | 			javascript.append("	processKeyPress(event.keyCode);");
  | 			javascript.append("}");
  | 
  | 			javascript.append("if (document.all) {");
  | 			javascript.append("	" + getKeyPressEventSource(formId) + " = clickEnterIE;");
  | 			javascript.append("} else {");
  | 			javascript.append("	" + getKeyPressEventSource(formId) + " = clickEnter;");
  | 			javascript.append("}");
  | 
  | 			writer.write(javascript.toString());
  | 
  | 			writer.endElement("script");
  | 		}
  | 	}
  | 
  | 	public String getScope() {
  | 		return scope;
  | 	}
  | 
  | 	public void setScope(String scope) {
  | 		this.scope = scope;
  | 	}
  | 
  | 	@Override
  | 	public void restoreState(FacesContext context, Object state) {
  | 		Object[] values = (Object[]) state;
  | 		super.restoreState(context, values[0]);
  | 		this.scope = (String) values[1];
  | 	}
  | 
  | 	/**
  | 	 * @see javax.faces.component.UIComponentBase#saveState(javax.faces.context.FacesContext)
  | 	 */
  | 	@Override
  | 	public Object saveState(FacesContext context) {
  | 		Object[] values = new Object[2];
  | 		values[0] = super.saveState(context);
  | 		values[1] = this.scope;
  | 		return values;
  | 
  | 	}
  | 
  | 	private String getKeyPressEventSource(String formId) {
  | 		if (SCOPE_FORM.equals(getScope())) {
  | 			return "document.forms['" + formId + "'].onkeypress";
  | 		} else {
  | 			return "document.onkeypress";
  | 		}
  | 	}
  | 
  | 	private UIForm getForm(UIComponent component) {
  | 		while (component != null) {
  | 			if (component instanceof UIForm) {
  | 				break;
  | 			}
  | 			component = component.getParent();
  | 		}
  | 		return (UIForm) component;
  | 	}
  | 
  | }
  | 
Works for me in myfaces

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4082004#4082004

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4082004



More information about the jboss-user mailing list