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 | 0 | { |
44 | 0 | Defense.notNull(term, "term"); |
45 | |
|
46 | 0 | int equalx = term.indexOf('='); |
47 | |
|
48 | 0 | if (equalx < 0) |
49 | |
{ |
50 | 0 | _label = term.trim(); |
51 | 0 | _value = _label; |
52 | |
} |
53 | |
else |
54 | |
{ |
55 | 0 | _label = term.substring(0, equalx).trim(); |
56 | 0 | _value = term.substring(equalx + 1).trim(); |
57 | |
} |
58 | 0 | } |
59 | |
} |
60 | |
|
61 | |
private final List _entries; |
62 | |
|
63 | |
public StringConvertedPropertySelectionModel(String[] terms) |
64 | 0 | { |
65 | 0 | Defense.notNull(terms, "terms"); |
66 | |
|
67 | 0 | _entries = new ArrayList(terms.length); |
68 | |
|
69 | 0 | for (int i = 0; i < terms.length; i++) |
70 | |
{ |
71 | 0 | _entries.add(new Entry(terms[i])); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
public int getOptionCount() |
76 | |
{ |
77 | 0 | return _entries.size(); |
78 | |
} |
79 | |
|
80 | |
private Entry getEntry(int index) |
81 | |
{ |
82 | 0 | return (Entry) _entries.get(index); |
83 | |
} |
84 | |
|
85 | |
public Object getOption(int index) |
86 | |
{ |
87 | 0 | return getValue(index); |
88 | |
} |
89 | |
|
90 | |
public String getLabel(int index) |
91 | |
{ |
92 | 0 | return getEntry(index)._label; |
93 | |
} |
94 | |
|
95 | |
public String getValue(int index) |
96 | |
{ |
97 | 0 | 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 | |
} |