Hi,
The ordinal can be just as easy to handle. Instead of ordinalToEnum() you
could do:
int pos = value.indexOf('@');
String clazz = value.substring(0, pos);
int ordinal = Integer.parseInt(value.substring(pos+1), 10);
Class enumType = Class.forName(clazz);
return (Enum) enumType.getEnumConstants()[ordinal];
Michael
-----Original Message-----
From: Yee CN [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 18, 2006 3:30 PM
To: 'MyFaces Discussion'
Subject: RE: JSF and Java 5.0 Enums
***********************
Your mail has been scanned by InterScan MSS.
***********************
Hi Mario,
Yes - name() is better than toString(). That's what I do myself.
What is appealing in Brodsema blog is really the following:
Class enumType = comp.getValueBinding("value").getType(context);
This instantly eliminates the need for one converter per enum. It is a very
nice 'default' pattern.
I see your point that there are other variations. I actually have a couple
of Enums that I like to have a more user friendly display string. Your
Ordinal converter would render this nicely.
Also why use ordinal? I would think that using name() (i.e. [EMAIL PROTECTED])
would be easier to handle - i.e. instead of ordinalToEnum(), you could do:
int pos = value.indexOf('@');
String clazz = value.substring(0, pos);
String name = value.substring(pos+1);
Class enumType = Class.forName(clazz);
return Enum.valueOf(enumType, name);
I think these two patterns would make a very nice Wiki page. What do you
think?
Cheers,
Yee
-----Original Message-----
From: Mario Ivankovits [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 18, 2006 4:11 PM
To: MyFaces Discussion
Subject: Re: JSF and Java 5.0 Enums
Hi Yee!
> I found some useful hints on how to handle Enum in JSF.
>
> http://brondsema.net/blog/index.php/2005/09/30/jsf_and_java_5_0_enums
>
> I think this is good material for Wiki, for the "Convertion and
> Validation" section.
>
I am not sure if we should add this blog entry as "official way" how to
handle enums.
I commented the blog, though, here my are my doubts again:
I think its better to use "name()" as "toString()" as toString might be
customized by the developer.
public enum MyEnum
{
ABC("myAbc"),
DEF("myDef"),
UIO("myAbc");
private final String description;
MyEnum(String description)
{
this.description = description;
}
public String toString()
{
return this.description;
}
}
Though, I'd even used ordinal() as you can see here:
http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox15/core/src/main/
java/org/apache/myfaces/custom/dynaForm/jsfext/EnumConverter.java?view=marku
p
As you can see, I render a special string with "[EMAIL PROTECTED]" that way
I avoid the use of valueBinding.getType() thing which can fail if it
e.g. points to a list or method with a simple Object as return type
(Though, not very likely I admit ;-) )
For sure, that way the user isn't really able to input the value of an
enum, though, I think the main use case of an enum in JSF is to show the
user a selection menu.
Just additional ideas ...
Ciao,
Mario