1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package net.sf.beanform.util; |
16 |
|
|
17 |
|
import java.util.ArrayList; |
18 |
|
import java.util.Arrays; |
19 |
|
import java.util.List; |
20 |
|
|
21 |
|
import org.apache.hivemind.Messages; |
22 |
|
import org.apache.tapestry.form.IPropertySelectionModel; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
public class EnumPropertySelectionModel<T extends Enum<T>> implements IPropertySelectionModel { |
30 |
|
|
31 |
|
private List<T> values; |
32 |
|
private List<String> labels; |
33 |
|
|
34 |
8 |
public EnumPropertySelectionModel( Class<T> type, boolean includeEmptyOption, Messages messages ) { |
35 |
|
|
36 |
8 |
this.values = new ArrayList<T>(); |
37 |
8 |
if( includeEmptyOption ) this.values.add( null ); |
38 |
8 |
T[] enums = type.getEnumConstants(); |
39 |
8 |
this.values.addAll( Arrays.asList( enums ) ); |
40 |
|
|
41 |
8 |
this.labels = new ArrayList<String>( this.values.size() ); |
42 |
8 |
for( T value : this.values ) { |
43 |
23 |
if( value == null ) this.labels.add( "" ); |
44 |
16 |
else this.labels.add( messages.getMessage( value.toString() ) ); |
45 |
23 |
} |
46 |
8 |
} |
47 |
|
|
48 |
|
public int getOptionCount() { |
49 |
2 |
return this.values.size(); |
50 |
|
} |
51 |
|
|
52 |
|
public Object getOption( int index ) { |
53 |
3 |
return this.values.get( index ); |
54 |
|
} |
55 |
|
|
56 |
|
public String getLabel( int index ) { |
57 |
3 |
return this.labels.get( index ); |
58 |
|
} |
59 |
|
|
60 |
|
public String getValue( int index ) { |
61 |
3 |
return Integer.toString( index ); |
62 |
|
} |
63 |
|
|
64 |
|
public Object translateValue( String value ) { |
65 |
3 |
return this.values.get( Integer.parseInt( value ) ); |
66 |
|
} |
67 |
|
|
68 |
|
} |