You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2006/03/09 00:13:35 UTC
svn commit: r384369 - in /myfaces/tomahawk/trunk:
core/src/main/java/org/apache/myfaces/component/html/ext/
core/src/main/java/org/apache/myfaces/renderkit/html/ext/
core/src/main/java/org/apache/myfaces/taglib/html/ext/ core/src/main/tld/
examples/sim...
Author: mmarinschek
Date: Wed Mar 8 15:13:33 2006
New Revision: 384369
URL: http://svn.apache.org/viewcvs?rev=384369&view=rev
Log:
fix for MYFACES-1166. Thanks to Catalin Kormos!
Added:
myfaces/tomahawk/trunk/examples/simple/src/main/webapp/inputTextDisabled.jsp
Modified:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/HtmlInputText.java
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlTextRenderer.java
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/taglib/html/ext/HtmlInputTextTag.java
myfaces/tomahawk/trunk/core/src/main/tld/tomahawk.tld
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/HtmlInputText.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/HtmlInputText.java?rev=384369&r1=384368&r2=384369&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/HtmlInputText.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/HtmlInputText.java Wed Mar 8 15:13:33 2006
@@ -52,16 +52,17 @@
private String _enabledOnUserRole = null;
private String _visibleOnUserRole = null;
- private Boolean _displayValueOnly = null;
- private String _displayValueOnlyStyle = null;
- private String _displayValueOnlyStyleClass = null;
+ private Boolean _displayValueOnly = null;
+ private String _displayValueOnlyStyle = null;
+ private String _displayValueOnlyStyleClass = null;
+
+ private Boolean _disabledOnClientSide = null;
public HtmlInputText()
{
super();
}
-
public void setEnabledOnUserRole(String enabledOnUserRole)
{
_enabledOnUserRole = enabledOnUserRole;
@@ -114,7 +115,7 @@
public void setDisplayValueOnlyStyle(String displayValueOnlyStyle)
{
- _displayValueOnlyStyle = displayValueOnlyStyle;
+ _displayValueOnlyStyle = displayValueOnlyStyle;
}
public String getDisplayValueOnlyStyle()
@@ -126,7 +127,7 @@
public void setDisplayValueOnlyStyleClass(String displayValueOnlyStyleClass)
{
- _displayValueOnlyStyleClass = displayValueOnlyStyleClass;
+ _displayValueOnlyStyleClass = displayValueOnlyStyleClass;
}
public String getDisplayValueOnlyStyleClass()
@@ -135,16 +136,30 @@
ValueBinding vb = getValueBinding("displayValueOnlyStyleClass");
return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : null;
}
+
+ public void setDisabledOnClientSide(boolean disabledOnClientSide)
+ {
+ _disabledOnClientSide = Boolean.valueOf(disabledOnClientSide);
+ }
+
+ public boolean isDisabledOnClientSide()
+ {
+ if (_disabledOnClientSide != null) return _disabledOnClientSide.booleanValue();
+ ValueBinding vb = getValueBinding("disabledOnClientSide");
+ Boolean v = vb != null ? (Boolean)vb.getValue(getFacesContext()) : null;
+ return v != null ? v.booleanValue() : false;
+ }
public Object saveState(FacesContext context)
{
- Object values[] = new Object[6];
+ Object values[] = new Object[7];
values[0] = super.saveState(context);
values[1] = _enabledOnUserRole;
values[2] = _visibleOnUserRole;
- values[3] = _displayValueOnly;
- values[4] = _displayValueOnlyStyle;
- values[5] = _displayValueOnlyStyleClass;
+ values[3] = _displayValueOnly;
+ values[4] = _displayValueOnlyStyle;
+ values[5] = _displayValueOnlyStyleClass;
+ values[6] = _disabledOnClientSide;
return values;
}
@@ -154,9 +169,10 @@
super.restoreState(context, values[0]);
_enabledOnUserRole = (String)values[1];
_visibleOnUserRole = (String)values[2];
- _displayValueOnly = (Boolean)values[3];
- _displayValueOnlyStyle = (String)values[4];
- _displayValueOnlyStyleClass = (String)values[5];
+ _displayValueOnly = (Boolean)values[3];
+ _displayValueOnlyStyle = (String)values[4];
+ _displayValueOnlyStyleClass = (String)values[5];
+ _disabledOnClientSide = (Boolean)values[6];
}
//------------------ GENERATED CODE END ---------------------------------------
}
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlTextRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlTextRenderer.java?rev=384369&r1=384368&r2=384369&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlTextRenderer.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/renderkit/html/ext/HtmlTextRenderer.java Wed Mar 8 15:13:33 2006
@@ -15,15 +15,20 @@
*/
package org.apache.myfaces.renderkit.html.ext;
-import java.io.IOException;
+import org.apache.myfaces.component.UserRoleUtils;
+import org.apache.myfaces.component.html.ext.HtmlInputText;
+import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
+import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTextRendererBase;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
-
-import org.apache.myfaces.component.UserRoleUtils;
-import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
-import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTextRendererBase;
+import javax.faces.context.ResponseWriter;
+import java.io.IOException;
+import java.util.Map;
/**
@@ -33,6 +38,8 @@
public class HtmlTextRenderer
extends HtmlTextRendererBase
{
+ private static final String ONCHANGE_PREFIX = "onChange_";
+
//private static final Log log = LogFactory.getLog(HtmlTextRenderer.class);
protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
{
@@ -45,11 +52,23 @@
return super.isDisabled(facesContext, uiComponent);
}
}
+ /**
+ *
+ */
+ protected boolean isDisabledOnClientSide(FacesContext facesContext, UIComponent component)
+ {
+ if (component instanceof HtmlInputText)
+ {
+ return ((HtmlInputText)component).isDisabledOnClientSide();
+ }
+
+ return false;
+ }
public void encodeEnd(FacesContext facesContext, UIComponent component)
throws IOException
{
- if(HtmlRendererUtils.isDisplayValueOnly(component))
+ if (HtmlRendererUtils.isDisplayValueOnly(component))
{
renderInputValueOnly(facesContext, component);
}
@@ -58,6 +77,66 @@
renderNormal(facesContext, component);
}
}
+
+ protected void renderInput(FacesContext facesContext, UIComponent component) throws IOException
+ {
+ if (!isDisabledOnClientSide(facesContext, component))
+ {
+ super.renderInput(facesContext, component);
+ return;
+ }
+
+ //render the input component as if it was disabled
+ ResponseWriter writer = facesContext.getResponseWriter();
+
+ String clientId = component.getClientId(facesContext);
+ String value = RendererUtils.getStringValue(facesContext, component);
+
+ writer.startElement(HTML.INPUT_ELEM, component);
+ writer.writeAttribute(HTML.ID_ATTR, clientId, null);
+ writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
+ writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT, null);
+ if (value != null)
+ {
+ writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
+ }
+
+ HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
+ //render as disabled on the client side
+ writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
+ //render the JS function that will change the hidden input field's value when his changes
+ writer.writeAttribute(HTML.ONCHANGE_ATTR, getOnChangeFunctionName(component.getId())+"();", null);
+
+ writer.endElement(HTML.INPUT_ELEM);
+
+ //render the hidden input field that will hold this component's value
+ renderHiddenInput(facesContext, (UIInput)component);
+ }
+ /**
+ *
+ */
+ protected void renderHiddenInput(FacesContext facesContext, UIComponent component) throws IOException
+ {
+ ResponseWriter writer = facesContext.getResponseWriter();
+
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+
+ writer.startElement(HTML.SCRIPT_ELEM, null);
+ writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
+ writer.writeText(createOnChangeListenerJS(component.getId(), component.getClientId(facesContext)), null);
+ writer.endElement(HTML.SCRIPT_ELEM);
+
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+
+ writer.startElement(HTML.INPUT_ELEM, null);
+ writer.writeAttribute(HTML.ID_ATTR, component.getId(), null);
+ writer.writeAttribute(HTML.NAME_ATTR, component.getId(), null);
+ writer.writeAttribute(HTML.VALUE_ATTR, RendererUtils.getStringValue(facesContext, component), null);
+ writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
+ writer.endElement(HTML.INPUT_ELEM);
+
+ HtmlRendererUtils.writePrettyLineSeparator(facesContext);
+ }
protected void renderInputValueOnly(FacesContext facesContext, UIComponent component)
throws IOException
@@ -70,5 +149,49 @@
throws IOException
{
super.encodeEnd(facesContext, component);
+ }
+
+ /**
+ *
+ */
+ protected String getOnChangeFunctionName(String inputDisabledId)
+ {
+ StringBuffer buf = new StringBuffer();
+ buf.append(ONCHANGE_PREFIX);
+ buf.append(inputDisabledId);
+
+ return buf.toString();
+ }
+ /**
+ *
+ */
+ protected String createOnChangeListenerJS(String inputDisabledId, String inputDisabledClientId)
+ {
+ StringBuffer script = new StringBuffer();
+ script.append("function ");
+ script.append(getOnChangeFunctionName(inputDisabledId));
+ script.append("() {\n");
+ script.append("var hiddenInput = document.getElementById(\"").append(inputDisabledId).append("\");\n");
+ script.append("var disabledInput = document.getElementById(\"").append(inputDisabledClientId).append("\");\n");
+ script.append("hiddenInput.value=disabledInput.value;\n");
+ script.append("}\n");
+
+ return script.toString();
+ }
+
+ public void decode(FacesContext facesContext, UIComponent component)
+ {
+ if (isDisabledOnClientSide(facesContext, component))
+ {
+ //in this case we don't need to check the request parameters validity,
+ //the submitted value comes from the hidden input field
+ Map paramValuesMap = facesContext.getExternalContext().getRequestParameterMap();
+ Object reqValue = paramValuesMap.get(component.getId());
+
+ ((UIInput)component).setSubmittedValue(reqValue);
+ return;
+ }
+
+ super.decode(facesContext, component);
}
}
Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/taglib/html/ext/HtmlInputTextTag.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/taglib/html/ext/HtmlInputTextTag.java?rev=384369&r1=384368&r2=384369&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/taglib/html/ext/HtmlInputTextTag.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/taglib/html/ext/HtmlInputTextTag.java Wed Mar 8 15:13:33 2006
@@ -45,10 +45,11 @@
private String _datafld;
private String _datasrc;
private String _dataformatas;
+ private String _disabledOnClientSide;
private String _displayValueOnly;
- private String _displayValueOnlyStyle;
- private String _displayValueOnlyStyleClass;
+ private String _displayValueOnlyStyle;
+ private String _displayValueOnlyStyleClass;
public void release() {
super.release();
@@ -58,6 +59,7 @@
_datafld=null;
_datasrc=null;
_dataformatas=null;
+ _disabledOnClientSide=null;
_displayValueOnly=null;
_displayValueOnlyStyle=null;
_displayValueOnlyStyleClass=null;
@@ -71,6 +73,7 @@
setStringProperty(component, HTML.DATAFLD_ATTR, _datafld);
setStringProperty(component, HTML.DATASRC_ATTR, _datasrc);
setStringProperty(component, HTML.DATAFORMATAS_ATTR, _dataformatas);
+ setBooleanProperty(component, "disabledOnClientSide", _disabledOnClientSide);
setBooleanProperty(component, DisplayValueOnlyCapable.DISPLAY_VALUE_ONLY_ATTR, _displayValueOnly);
setStringProperty(component, DisplayValueOnlyCapable.DISPLAY_VALUE_ONLY_STYLE_ATTR, _displayValueOnlyStyle);
setStringProperty(component, DisplayValueOnlyCapable.DISPLAY_VALUE_ONLY_STYLE_CLASS_ATTR, _displayValueOnlyStyleClass);
@@ -114,5 +117,10 @@
public void setDataformatas(String dataformatas)
{
_dataformatas = dataformatas;
+ }
+
+ public void setDisabledOnClientSide(String disabledOnClientSide)
+ {
+ _disabledOnClientSide = disabledOnClientSide;
}
}
Modified: myfaces/tomahawk/trunk/core/src/main/tld/tomahawk.tld
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/core/src/main/tld/tomahawk.tld?rev=384369&r1=384368&r2=384369&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/tld/tomahawk.tld (original)
+++ myfaces/tomahawk/trunk/core/src/main/tld/tomahawk.tld Wed Mar 8 15:13:33 2006
@@ -321,10 +321,21 @@
Extends standard inputText by user role support.
Unless otherwise specified, all attributes accept static values or EL expressions.
- </description>
+ </description>
+ <attribute>
+ <name>disabledOnClientSide</name>
+ <required>false</required>
+ <rtexprvalue>false</rtexprvalue>
+ <type>java.lang.String</type>
+ <description>
+ If true the input is rendered disabled on the client side
+ and a hidden input is used to actualy submit his value back
+ to the server.
+ </description>
+ </attribute>
&html_datafld_datasrc_dataformatas_attributes;
&standard_input_text_attributes;
- &user_role_attributes;
+ &user_role_attributes;
&ext_forceId_attribute;
&display_value_only_attributes;
</tag>
Added: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/inputTextDisabled.jsp
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/examples/simple/src/main/webapp/inputTextDisabled.jsp?rev=384369&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/examples/simple/src/main/webapp/inputTextDisabled.jsp (added)
+++ myfaces/tomahawk/trunk/examples/simple/src/main/webapp/inputTextDisabled.jsp Wed Mar 8 15:13:33 2006
@@ -0,0 +1,81 @@
+<%@ page session="false" contentType="text/html;charset=utf-8"%>
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+
+<html>
+
+<%@include file="inc/head.inc" %>
+
+<!--
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+//-->
+
+<body>
+
+<f:view>
+ <h:form>
+ <h:panelGrid>
+ <t:messages showDetail="true" showSummary="false"/>
+ <h:outputText value="Example for disabled input value submit"/>
+
+ <f:verbatim>
+ <script type="text/javascript"><!--
+ function changeDisabledInputValue() {
+ var fName = document.getElementById("_idJsp0:firstName");
+ var lName = document.getElementById("_idJsp0:lastName");
+ var fullName = document.getElementById("_idJsp0:fullName");
+ fullName.value = fName.value+" "+lName.value;
+ fullName.onchange();
+ }
+ //--></script>
+ </f:verbatim>
+
+ <h:outputText value="Person" style="font-weight:bold;"/>
+ <h:panelGrid columns="2">
+ <h:outputLabel for="firstName" value="First Name:"/>
+ <h:inputText id="firstName" value="#{inputTextDisabled.firstName}"/>
+ <h:outputLabel for="lastName" value="Last Name:"/>
+ <h:inputText id="lastName" value="#{inputTextDisabled.lastName}"/>
+ <h:outputLabel for="fullName" value="Full Name:"/>
+ <t:inputText id="fullName" value="#{inputTextDisabled.fullName}" disabledOnClientSide="true">
+ <f:validateLength maximum="5" minimum="2"/>
+ </t:inputText>
+ </h:panelGrid>
+ <h:outputLink value="#" onclick="changeDisabledInputValue();"><f:verbatim>Change disabled input field's value</f:verbatim></h:outputLink>
+ <h:commandButton value="Show current values of Person"/>
+ </h:panelGrid>
+
+
+ <h:panelGrid columns="2">
+ <h:outputText value="First name of Person:"/>
+ <h:outputText value="#{inputTextDisabled.firstName}"/>
+ <h:outputText value="Last name of Person:"/>
+ <h:outputText value="#{inputTextDisabled.lastName}"/>
+ <h:outputText value="Full name of Person:"/>
+ <h:outputText value="#{inputTextDisabled.fullName}"/>
+ </h:panelGrid>
+
+ </h:form>
+</f:view>
+
+<%@include file="inc/page_footer.jsp" %>
+
+</body>
+
+</html>