1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.coerce; |
16 |
|
|
17 |
|
import java.util.ArrayList; |
18 |
|
import java.util.List; |
19 |
|
|
20 |
|
import org.apache.hivemind.util.Defense; |
21 |
|
import org.apache.tapestry.form.IPropertySelectionModel; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
public final class StringConvertedPropertySelectionModel implements IPropertySelectionModel |
31 |
|
{ |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
private static class Entry |
37 |
|
{ |
38 |
|
String _label; |
39 |
|
|
40 |
|
String _value; |
41 |
|
|
42 |
|
Entry(String term) |
43 |
14 |
{ |
44 |
14 |
Defense.notNull(term, "term"); |
45 |
|
|
46 |
14 |
int equalx = term.indexOf('='); |
47 |
|
|
48 |
14 |
if (equalx < 0) |
49 |
|
{ |
50 |
3 |
_label = term.trim(); |
51 |
3 |
_value = _label; |
52 |
|
} |
53 |
|
else |
54 |
|
{ |
55 |
11 |
_label = term.substring(0, equalx).trim(); |
56 |
11 |
_value = term.substring(equalx + 1).trim(); |
57 |
|
} |
58 |
14 |
} |
59 |
|
} |
60 |
|
|
61 |
|
private final List _entries; |
62 |
|
|
63 |
|
public StringConvertedPropertySelectionModel(String[] terms) |
64 |
4 |
{ |
65 |
4 |
Defense.notNull(terms, "terms"); |
66 |
|
|
67 |
4 |
_entries = new ArrayList(terms.length); |
68 |
|
|
69 |
18 |
for (int i = 0; i < terms.length; i++) |
70 |
|
{ |
71 |
14 |
_entries.add(new Entry(terms[i])); |
72 |
|
} |
73 |
4 |
} |
74 |
|
|
75 |
|
public int getOptionCount() |
76 |
|
{ |
77 |
8 |
return _entries.size(); |
78 |
|
} |
79 |
|
|
80 |
|
private Entry getEntry(int index) |
81 |
|
{ |
82 |
42 |
return (Entry) _entries.get(index); |
83 |
|
} |
84 |
|
|
85 |
|
public Object getOption(int index) |
86 |
|
{ |
87 |
14 |
return getValue(index); |
88 |
|
} |
89 |
|
|
90 |
|
public String getLabel(int index) |
91 |
|
{ |
92 |
14 |
return getEntry(index)._label; |
93 |
|
} |
94 |
|
|
95 |
|
public String getValue(int index) |
96 |
|
{ |
97 |
28 |
return getEntry(index)._value; |
98 |
|
} |
99 |
|
|
100 |
|
public boolean isDisabled(int index) |
101 |
|
{ |
102 |
0 |
return false; |
103 |
|
} |
104 |
|
|
105 |
|
public Object translateValue(String value) |
106 |
|
{ |
107 |
|
|
108 |
0 |
return value; |
109 |
|
} |
110 |
|
|
111 |
|
} |