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 2005/09/15 14:29:18 UTC
svn commit: r289222 -
/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java
Author: mmarinschek
Date: Thu Sep 15 05:29:12 2005
New Revision: 289222
URL: http://svn.apache.org/viewcvs?rev=289222&view=rev
Log:
BooleanConverter now implements stateHolder. Thanks to Ken Weiner for this patch.
Modified:
myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java
Modified: myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java
URL: http://svn.apache.org/viewcvs/myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java?rev=289222&r1=289221&r2=289222&view=diff
==============================================================================
--- myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java (original)
+++ myfaces/sandbox/trunk/src/java/org/apache/myfaces/custom/convertboolean/BooleanConverter.java Thu Sep 15 05:29:12 2005
@@ -1,6 +1,7 @@
package org.apache.myfaces.custom.convertboolean;
import javax.faces.component.UIComponent;
+import javax.faces.component.StateHolder;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
@@ -9,55 +10,76 @@
* Converter that translates between boolean values (true/false)
* and alternate versions of those boolean values like
* (yes/no), (1/0), and (way/no way).
- * <p>
+ * <p/>
* To customize the representation of a boolean true and false,
* use {@link #setTrueValue(String)}
- * and {@link #setFalseValue(String)}
+ * and {@link #setFalseValue(String)}
* respectively. If not configured with these setter methods,
* it defaults to <code>true</code> and <code>false</code>.
- * <p>
+ * <p/>
* The values are case sensitive.
- * <p>
+ * <p/>
+ *
* @author Ken Weiner
*/
-public class BooleanConverter implements Converter {
+public class BooleanConverter implements Converter, StateHolder
+{
private String trueValue = "true";
private String falseValue = "false";
+ private boolean isTransient;
+
public static final String CONVERTER_ID = BooleanConverter.class.getName();
- public String getFalseValue() {
+ public BooleanConverter()
+ {
+ // Default constructor
+ }
+
+ public String getFalseValue()
+ {
return falseValue;
}
- public void setFalseValue(String falseValue) {
+ public void setFalseValue(String falseValue)
+ {
this.falseValue = falseValue;
}
- public String getTrueValue() {
+ public String getTrueValue()
+ {
return trueValue;
}
- public void setTrueValue(String trueValue) {
+ public void setTrueValue(String trueValue)
+ {
this.trueValue = trueValue;
}
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
- throws ConverterException {
- if (facesContext == null) {
+ throws ConverterException
+ {
+ if (facesContext == null)
+ {
throw new NullPointerException("facesContext");
}
- if (uiComponent == null) {
+ if (uiComponent == null)
+ {
throw new NullPointerException("uiComponent");
}
- if (value != null) {
+ if (value != null)
+ {
value = value.trim();
- if (value.length() > 0) {
- try {
+ if (value.length() > 0)
+ {
+ try
+ {
return Boolean.valueOf(value.equals(trueValue));
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new ConverterException(e);
}
}
@@ -66,25 +88,61 @@
}
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
- throws ConverterException {
- if (facesContext == null) {
+ throws ConverterException
+ {
+ if (facesContext == null)
+ {
throw new NullPointerException("facesContext");
}
- if (uiComponent == null) {
+ if (uiComponent == null)
+ {
throw new NullPointerException("uiComponent");
}
- if (value == null) {
+ if (value == null)
+ {
return "";
}
- if (value instanceof String) {
+ if (value instanceof String)
+ {
return (String) value;
}
- try {
+ try
+ {
return ((Boolean) value).booleanValue() ? trueValue : falseValue;
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new ConverterException(e);
}
}
+
+ // StateHolder methods ////////////////////////////////////////////////////
+
+ public boolean isTransient()
+ {
+ return this.isTransient;
+ }
+
+ public void setTransient(boolean newTransientValue)
+ {
+ this.isTransient = newTransientValue;
+ }
+
+ public void restoreState(FacesContext context, Object state)
+ {
+ Object values[] = (Object[]) state;
+ this.trueValue = (String) values[0];
+ this.falseValue = (String) values[1];
+ }
+
+ public Object saveState(FacesContext context)
+ {
+ Object[] values = new Object[2];
+ values[0] = this.trueValue;
+ values[1] = this.falseValue;
+ return values;
+ }
+
}