Revision: 395
Author:   tfenne
Date:     2006-08-28 17:54:40 -0700 (Mon, 28 Aug 2006)
ViewCVS:  http://svn.sourceforge.net/stripes/?rev=395&view=rev

Log Message:
-----------
Fix for STS-252: need an EnumFormatter to ensure that enum values are always 
pulled using name() instead of toString()

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java

Added Paths:
-----------
    trunk/stripes/src/net/sourceforge/stripes/format/EnumFormatter.java
Modified: 
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java   
    2006-08-29 00:30:20 UTC (rev 394)
+++ 
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java   
    2006-08-29 00:54:40 UTC (rev 395)
@@ -21,7 +21,7 @@
 
 /**
  * Very simple default implementation of a formatter factory that is aware of 
how to format
- * dates and numbers.
+ * dates, numbers and enums.
  *
  * @author Tim Fennell
  */
@@ -69,6 +69,11 @@
             formatter.init();
             return formatter;
         }
+        else if (clazz.isEnum()) {
+            formatter = new EnumFormatter();
+            formatter.init();
+            return formatter;
+        }
         else {
             return null;
         }

Added: trunk/stripes/src/net/sourceforge/stripes/format/EnumFormatter.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/format/EnumFormatter.java         
                (rev 0)
+++ trunk/stripes/src/net/sourceforge/stripes/format/EnumFormatter.java 
2006-08-29 00:54:40 UTC (rev 395)
@@ -0,0 +1,53 @@
+/* Copyright 2005-2006 Tim Fennell
+ *
+ * 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.
+ */
+package net.sourceforge.stripes.format;
+
+import java.util.Locale;
+
+/**
+ * A simple formatter for Enum classes that always returns the value of 
Enum.name(). Intended
+ * really only to enable the seamless usage of enums as values in hidden 
fields, radio
+ * buttons, checkboxes etc. it is not intended that this will be used to 
format Enum
+ * values into text fields where a localized value might be more appropriate.
+ *
+ * @author Tim Fennell
+ * @since Stripes 1.4.1
+ */
+public class EnumFormatter implements Formatter<Enum> {
+    /** Does nothing. Format types are not supported for Enums. */
+    public void setFormatType(String formatType) { }
+
+    /** Does nothing. Format patterns are not supported for Enums. */
+    public void setFormatPattern(String formatPattern) { }
+
+    /** Does nothing. Enums values are always formatted using name() which is 
not localizable. */
+    public void setLocale(Locale locale) { }
+
+    /** Does nothing since no initialization is needed. */
+    public void init() { }
+
+    /**
+     * Formats the supplied value as a String.  If the value cannot be 
formatted because it is
+     * an inappropriate type, or because faulty pattern information was 
supplied, should fail
+     * loudly by throwing a RuntimeException or subclass thereof.
+     *
+     * @param input an object of a type that the formatter knows how to format
+     * @return a String version of the input, formatted for the chosen locale
+     */
+    public String format(Enum input) {
+        if (input != null) return input.name();
+        else return null;
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to