From 9b1f71c86041ddf3454d3f02abc553d1dd1adf6f Mon Sep 17 00:00:00 2001 From: Bernard Labno Date: Wed, 16 Mar 2011 13:50:11 +0000 Subject: [PATCH] Added FocusModifier to modify client id for components. --- src/main/config/component/focus.xml | 30 ++++++++++++++++-------------- src/main/config/component/focusModifier.xml | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/org/richfaces/component/UIFocus.java | 12 ++++++++++-- src/main/java/org/richfaces/component/UIFocusModifier.java | 21 +++++++++++++++++++++ src/main/java/org/richfaces/renderkit/html/HtmlFocusModifierRenderer.java | 35 +++++++++++++++++++++++++++++++++++ src/main/java/org/richfaces/renderkit/html/HtmlFocusRenderer.java | 71 +++++++++++++++++++++++++++++++++++++++++++++++------------------------ 6 files changed, 176 insertions(+), 40 deletions(-) create mode 100644 src/main/config/component/focusModifier.xml create mode 100644 src/main/java/org/richfaces/component/UIFocusModifier.java create mode 100644 src/main/java/org/richfaces/renderkit/html/HtmlFocusModifierRenderer.java diff --git a/src/main/config/component/focus.xml b/src/main/config/component/focus.xml index 9c86f3b..61f895e 100644 --- a/src/main/config/component/focus.xml +++ b/src/main/config/component/focus.xml @@ -1,5 +1,4 @@ - - @@ -28,8 +27,7 @@ org.ajax4jsf.tests.AbstractJspTestCase - - &ui_component_attributes; + &ui_component_attributes; for java.lang.String @@ -42,17 +40,23 @@ priority java.lang.Integer - If there are more components requesting focus, - then component with lowest priority will be focused. + If there are more components requesting focus, then component with lowest priority will be focused. + targetClientId + java.lang.String + + If some other element then the one matching clintId of target component should be focused and it cannot be achieved with suffix, use this + attribute. + + "" + + suffix java.lang.String - Suffix added to clientId. Useful for focusing radio elements. - Example : suffix=":0" puts focus on first radio if target is - radio. + Suffix added to clientId. Useful for focusing radio elements. Example : suffix=":0" puts focus on first radio if target is radio. "" @@ -60,9 +64,8 @@ timing java.lang.String - Moment when focus should be put. The possible values are "onJScall" and "onload". - If "onJScall" is used then you must manually call Richfaces.FocusManager.focus(). - The default value is "onload". + Moment when focus should be put. The possible values are "onJScall" and "onload". If "onJScall" is used then you must manually call + Richfaces.FocusManager.focus(). The default value is "onload". "onload" @@ -70,8 +73,7 @@ name java.lang.String - Name of JavaScript function generated to trigger focus. This is required if "timing" attribute - is set to "onJScall". + Name of JavaScript function generated to trigger focus. This is required if "timing" attribute is set to "onJScall". null diff --git a/src/main/config/component/focusModifier.xml b/src/main/config/component/focusModifier.xml new file mode 100644 index 0000000..7a17a81 --- /dev/null +++ b/src/main/config/component/focusModifier.xml @@ -0,0 +1,47 @@ + + + + org.richfaces.FocusModifier + org.richfaces.FocusModifier + org.richfaces.component.html.HtmlFocusModifier + org.richfaces.component.UIFocusModifier + + + + + org.richfaces.FocusModifierRenderer + org.richfaces.renderkit.html.HtmlFocusModifierRenderer + + + focusModifier + org.richfaces.taglib.FocusModifierTag + + org.ajax4jsf.webapp.taglib.HtmlComponentTagBase + + + org.richfaces.taglib.HtmlFocusModifierTagTest + org.ajax4jsf.tests.AbstractJspTestCase + + &ui_component_attributes; + + targetClientId + java.lang.String + + If some other element then the one matching clintId of target component should be focused and it cannot be achieved with suffix, use this + attribute. + + "" + + + suffix + java.lang.String + + Suffix added to clientId. Useful for focusing radio elements. Example : suffix=":0" puts focu on first radio if target is radio. + + "" + + + diff --git a/src/main/java/org/richfaces/component/UIFocus.java b/src/main/java/org/richfaces/component/UIFocus.java index 7701941..4387e7b 100644 --- a/src/main/java/org/richfaces/component/UIFocus.java +++ b/src/main/java/org/richfaces/component/UIFocus.java @@ -12,22 +12,26 @@ import java.util.Set; public abstract class UIFocus extends UIComponentBase { public static final String COMPONENT_TYPE = "org.richfaces.Focus"; + public static final String COMPONENT_FAMILY = "org.richfaces.Focus"; + public static final String TIMING_ON_LOAD = "onload"; + public static final String FOCUS_MODIFIER_FACET_NAME = "focusModifier"; + public Integer getDefaultPriority() { UIComponent parentForm = getParent(); while (parentForm != null && !(parentForm instanceof UIForm)) { parentForm = parentForm.getParent(); } if (parentForm != null) { - return getUIInputChildrenCount(parentForm, getForComponentId()); + return getUIInputChildrenCount(parentForm, getTargetComponentId()); } else { return Integer.MAX_VALUE; } } - public String getForComponentId() { + public String getTargetComponentId() { String aFor = getFor(); if (aFor != null && !"".equals(aFor)) { @@ -67,6 +71,10 @@ public abstract class UIFocus extends UIComponentBase { public abstract void setFor(String value); + public abstract String getTargetClientId(); + + public abstract void setTargetClientId(String targetClientId); + public abstract String getSuffix(); public abstract void setSuffix(String value); diff --git a/src/main/java/org/richfaces/component/UIFocusModifier.java b/src/main/java/org/richfaces/component/UIFocusModifier.java new file mode 100644 index 0000000..387d111 --- /dev/null +++ b/src/main/java/org/richfaces/component/UIFocusModifier.java @@ -0,0 +1,21 @@ +package org.richfaces.component; + +import javax.faces.component.UIComponentBase; + +public abstract class UIFocusModifier extends UIComponentBase { +// ------------------------------ FIELDS ------------------------------ + + public static final String COMPONENT_FAMILY = "org.richfaces.Focus"; + + public static final String COMPONENT_TYPE = "org.richfaces.FocusModifier"; + +// -------------------------- OTHER METHODS -------------------------- + + public abstract String getTargetClientId(); + + public abstract String getSuffix(); + + public abstract void setTargetClientId(String focusClientId); + + public abstract void setSuffix(String value); +} diff --git a/src/main/java/org/richfaces/renderkit/html/HtmlFocusModifierRenderer.java b/src/main/java/org/richfaces/renderkit/html/HtmlFocusModifierRenderer.java new file mode 100644 index 0000000..6375d96 --- /dev/null +++ b/src/main/java/org/richfaces/renderkit/html/HtmlFocusModifierRenderer.java @@ -0,0 +1,35 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright , Red Hat, Inc. and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.richfaces.renderkit.html; + +import org.ajax4jsf.renderkit.RendererBase; +import org.richfaces.component.UIFocusModifier; + +import javax.faces.component.UIComponent; + +public class HtmlFocusModifierRenderer extends RendererBase { + + protected Class getComponentClass() { + return UIFocusModifier.class; + } +} diff --git a/src/main/java/org/richfaces/renderkit/html/HtmlFocusRenderer.java b/src/main/java/org/richfaces/renderkit/html/HtmlFocusRenderer.java index bc63b0a..2e61367 100644 --- a/src/main/java/org/richfaces/renderkit/html/HtmlFocusRenderer.java +++ b/src/main/java/org/richfaces/renderkit/html/HtmlFocusRenderer.java @@ -25,9 +25,10 @@ package org.richfaces.renderkit.html; import org.ajax4jsf.javascript.JSFunction; import org.ajax4jsf.javascript.JSFunctionDefinition; import org.ajax4jsf.renderkit.HeaderResourcesRendererBase; -import org.ajax4jsf.renderkit.RendererUtils.HTML; +import org.ajax4jsf.renderkit.RendererUtils; import org.ajax4jsf.resource.InternetResource; import org.richfaces.component.UIFocus; +import org.richfaces.component.UIFocusModifier; import javax.faces.FacesException; import javax.faces.component.UIComponent; @@ -42,6 +43,7 @@ public class HtmlFocusRenderer extends HeaderResourcesRendererBase { getResource("/org/ajax4jsf/javascript/scripts/prototype.js"), getResource("/org/richfaces/renderkit/html/scripts/focus.js"), }; + private final InternetResource[] styles = {}; // --------------------- GETTER / SETTER METHODS --------------------- @@ -69,37 +71,42 @@ public class HtmlFocusRenderer extends HeaderResourcesRendererBase { if (priority == null) { priority = uiFocus.getDefaultPriority(); } - - String sid = uiFocus.getForComponentId(); - String target; - if (sid != null && !"".equals(sid)) { - try { - UIComponent forcomp = getUtils().findComponentFor(component, sid); - if (forcomp != null) { - target = forcomp.getClientId(context); + String targetClientId = uiFocus.getTargetClientId(); + if (targetClientId == null || "".equals(targetClientId)) { + String targetComponentId = uiFocus.getTargetComponentId(); + String suffix = uiFocus.getSuffix(); + if (targetComponentId == null || "".equals(targetComponentId)) { + return; + } + UIComponent forcomp = getUtils().findComponentFor(component, targetComponentId); + if (forcomp == null) { + throw new FacesException("No component with id=" + targetComponentId + " found!"); + } + targetClientId = forcomp.getClientId(context); + UIFocusModifier modifier = findModifier(forcomp); + if (modifier != null) { + final String modifiedTargetClientId = modifier.getTargetClientId(); + if (modifiedTargetClientId != null && !modifiedTargetClientId.equals("")) { + targetClientId = modifiedTargetClientId; } else { - target = sid; + suffix = modifier.getSuffix(); } - } catch (IllegalArgumentException e) { - target = sid; } - String suffix = uiFocus.getSuffix(); if (suffix != null && !"".equals(suffix)) { - target += suffix; + targetClientId += suffix; } - } else { - target = component.getParent().getClientId(context); } - - - writer.startElement(HTML.SCRIPT_ELEM, null); - writer.writeAttribute(HTML.TYPE_ATTR, "text/javascript", "type"); - writer.writeAttribute(HTML.id_ATTRIBUTE, clientId, HTML.id_ATTRIBUTE); + if (targetClientId == null || targetClientId.equals("")) { + return; + } + writer.startElement(RendererUtils.HTML.SCRIPT_ELEM, null); + writer.writeAttribute(RendererUtils.HTML.TYPE_ATTR, "text/javascript", "type"); + writer.writeAttribute(RendererUtils.HTML.id_ATTRIBUTE, clientId, RendererUtils.HTML.id_ATTRIBUTE); if (UIFocus.TIMING_ON_LOAD.equals(uiFocus.getTiming())) { - writer.write(new JSFunction("Richfaces.FocusManager.setFocus", target, priority).toScript()); + writer.write(new JSFunction("Richfaces.FocusManager.setFocus", targetClientId, priority).toScript()); writer.write(";"); } else { - writer.write(new JSFunction("Richfaces.FocusManager.setFocus", target, priority, clientId, + writer.write(new JSFunction("Richfaces.FocusManager.setFocus", targetClientId, priority, clientId, uiFocus.getTiming()).toScript()); writer.write(";"); } @@ -109,7 +116,7 @@ public class HtmlFocusRenderer extends HeaderResourcesRendererBase { writer.write(definition.toScript()); writer.write(";"); } - writer.endElement(HTML.SCRIPT_ELEM); + writer.endElement(RendererUtils.HTML.SCRIPT_ELEM); } private void checkValidity(String clientId, String name, String timing) { @@ -126,4 +133,20 @@ public class HtmlFocusRenderer extends HeaderResourcesRendererBase { protected Class getComponentClass() { return UIFocus.class; } + + private UIFocusModifier findModifier(UIComponent component) { + if (component instanceof UIFocusModifier) { + return (UIFocusModifier) component; + } + UIFocusModifier modifier = (UIFocusModifier) component.getFacet(UIFocus.FOCUS_MODIFIER_FACET_NAME); + if (modifier == null) { + for (UIComponent child : component.getChildren()) { + modifier = findModifier(child); + if (modifier != null) { + break; + } + } + } + return modifier; + } } -- libgit2 0.24.0