You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Yee CN <ye...@streamyx.com> on 2006/10/18 05:53:09 UTC
JSF and Java 5.0 Enums
***********************
Your mail has been scanned by InterScan MSS.
***********************
Hi,
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. This page is 'inmutable' - if somebody can create a blank page for
it I will fill it the rest.
Cheers
Best regards,
Yee
Re: JSF and Java 5.0 Enums
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi Yee!
> Also why use ordinal?
hehe, maybe I am an old "school developer", in the past nearly
everything was a number ;-) even on the GUI side, so, maybe I still feel
more comfortable to use numbers, no longer on the GUI for sure, but for
things like this one.
For now I moved the EnumConverter to its own package and I'll open a
wiki page for it soon, you could add stuff as you like then.
Ciao,
Mario
Re: JSF and Java 5.0 Enums
Posted by Jeff Bischoff <jb...@klkurz.com>.
> Thanks for the tip, I changed our converter to use getEnumConstants()
> now, though, did you have a look at its implementation? I'd say "butt
> ugly" ;-)
Well you piqued my curiosity. I took a look, and everything was looking
good... until I got to the contents of that try clause. Massive brain
trauma ensued! :P
But here's the kicker - you don't have much choice! You were already
calling this method, albeit indirectly.
Your code:
EnumSet es = EnumSet.allOf(type);
Calls:
(EnumSet.allOf) EnumSet<E> result = noneOf(elementType);
(EnumSet.noneOf) Enum[] universe = elementType.getEnumConstants();
There's no escape!! Guess you might as well be using it directly...
Regards,
Jeff Bischoff
Kenneth L Kurz & Associates, Inc.
Mario Ivankovits wrote:
> Hi Michael!
>> Instead of ordinalToEnum() you
>> could do:
>> return (Enum) enumType.getEnumConstants()[ordinal];
>>
> Thanks for the tip, I changed our converter to use getEnumConstants()
> now, though, did you have a look at its implementation? I'd say "butt
> ugly" ;-)
>
> Ciao,
> Mario
>
>
>
>
Re: JSF and Java 5.0 Enums
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi Michael!
> Instead of ordinalToEnum() you
> could do:
> return (Enum) enumType.getEnumConstants()[ordinal];
>
Thanks for the tip, I changed our converter to use getEnumConstants()
now, though, did you have a look at its implementation? I'd say "butt
ugly" ;-)
Ciao,
Mario
RE: JSF and Java 5.0 Enums
Posted by Michael Matz <mm...@kamakuraco.com>.
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:yeecn@streamyx.com]
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. clazz@name)
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:mario@ops.co.at]
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 "class@ordinal" 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
RE: JSF and Java 5.0 Enums
Posted by Yee CN <ye...@streamyx.com>.
***********************
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. clazz@name)
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:mario@ops.co.at]
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 "class@ordinal" 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
Re: JSF and Java 5.0 Enums
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi Yee!
> Where else could the converter be used?
>
You can use the converter for output fields too, think about a date
converter which you can use to format the date accordingly, even for output.
Or a number converter to limit the number of decimals, sign, etc.
So its pretty valid to use the converter for output fields to, though,
often a type="" will be used instead of this "automatic" detection.
We can think about this too here, though, I still need (would like to
have) the automatic detection too.
Something like: type="auto|enum|text"
Where "auto" is the current behavior, "enum" to format it the reversible
way and "text" for the toString() stuff.
Ciao,
Mario
RE: JSF and Java 5.0 Enums
Posted by Yee CN <ye...@streamyx.com>.
***********************
Your mail has been scanned by InterScan MSS.
***********************
Hi Mario,
I thought SelectItem/converter is always used in conjunction with an input
component? Using it in conjunction with SelectOneMenu, SelectOneRadio etc
would not require this test. The following would be good enough:
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null) return "";
Enum e = (Enum) value;
return e.getClass().getName() + "@" + e.ordinal();
}
Where else could the converter be used? Sorry if this is JSF 101.
Cheers,
Yee
-----Original Message-----
From: Mario Ivankovits [mailto:mario@ops.co.at]
Sent: Friday, October 20, 2006 1:58 PM
To: MyFaces Discussion
Subject: Re: JSF and Java 5.0 Enums
Hi Yee!
> Qestion - what is the purpose of th e following test?
>
> if (component instanceof UIInput ||
> UIInput.COMPONENT_FAMILY.equals(component.getFamily())) {
>
The idea behind this converter is, that in case of an input component
the conversion has to be reversible (or symmetric), thus, the
class@ordinal rendering, in any other case - usually output components
only - it uses enum.toString() to have a nice user-readable
representation (assumed that toString() has been overloaded)
Ciao,
Mario
Re: JSF and Java 5.0 Enums
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi Yee!
> Qestion - what is the purpose of th e following test?
>
> if (component instanceof UIInput ||
> UIInput.COMPONENT_FAMILY.equals(component.getFamily())) {
>
The idea behind this converter is, that in case of an input component
the conversion has to be reversible (or symmetric), thus, the
class@ordinal rendering, in any other case - usually output components
only - it uses enum.toString() to have a nice user-readable
representation (assumed that toString() has been overloaded)
Ciao,
Mario
RE: JSF and Java 5.0 Enums
Posted by Yee CN <ye...@streamyx.com>.
***********************
Your mail has been scanned by InterScan MSS.
***********************
Qestion - what is the purpose of th e following test?
if (component instanceof UIInput ||
UIInput.COMPONENT_FAMILY.equals(component.getFamily())) {
Regards,
Yee
-----Original Message-----
From: Mario Ivankovits [mailto:mario@ops.co.at]
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 "class@ordinal" 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
Re: JSF and Java 5.0 Enums
Posted by Mario Ivankovits <ma...@ops.co.at>.
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=markup
As you can see, I render a special string with "class@ordinal" 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