1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems.config.tools.console;
19
20 import java.io.IOException;
21 import java.text.MessageFormat;
22 import java.util.ResourceBundle;
23
24 import com.gridsystems.config.ConsoleConfiguratorView;
25 import com.gridsystems.config.app.UI;
26
27
28
29
30
31
32 public class SelectionField extends Field implements ConsoleViewAction {
33
34
35
36
37 protected String linePattern;
38
39
40
41
42 protected String valuePattern;
43
44
45
46
47 protected Object value;
48
49
50
51
52 protected MessageFormat valueFormatter = null;
53
54
55
56
57 private String[] values = null;
58
59
60
61
62
63
64
65
66
67 public SelectionField(ResourceBundle bundle, String linePattern,
68 String valuePattern, String[] values) {
69 super(bundle);
70 this.linePattern = linePattern;
71 this.valuePattern = valuePattern;
72
73 if (valuePattern != null) {
74 valueFormatter = new MessageFormat(valuePattern);
75 }
76 if (values != null) {
77 this.values = values;
78 } else {
79 this.values = new String[0];
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public boolean execute(ConsoleConfiguratorView view) {
98 while (true) {
99 try {
100 String line = readLine();
101 if (line.equals("")) {
102 return true;
103 }
104
105 line = line.trim();
106 if (line.length() == 1) {
107 int index = line.charAt(0) - 'a';
108 if ((index >= 0) && (index < this.values.length)) {
109 setStringValue(this.values[index]);
110 return true;
111 }
112 }
113 } catch (Exception e) {
114 e.printStackTrace();
115 }
116 }
117 }
118
119
120
121
122
123
124
125 protected String readLine() throws IOException {
126 if (value != null) {
127 String currentValuePattern =
128 UI.getString("console.readLine.currentValue");
129 String currentValue = MessageFormat.format(
130 currentValuePattern, new Object[]{getStringValue()});
131
132 System.out.println(currentValue);
133 }
134
135 char index = 'a';
136 for (int i = 0; i < this.values.length; i++) {
137 System.out.println(index + ") " + this.values[i]);
138 index++;
139 }
140
141 String line =
142 ConsoleTools.readLine(UI.getString("console.readLine.newValue"));
143 return line;
144 }
145
146
147
148
149 public String getKeyMapping() {
150 return getString(this.linePattern + ".keyMap");
151 }
152
153
154
155
156 public String[] getContents() {
157 String[] contents = new String[1];
158
159
160 StringBuffer pattern = new StringBuffer();
161 String key = getKeyMapping();
162 if (key != null) {
163 pattern.append(" ").append(key).append(". ");
164 }
165
166 pattern.append(getString(linePattern));
167
168 String s = getStringValue();
169 contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
170 return contents;
171 }
172
173
174
175
176
177
178 public Object getValue() {
179 return value;
180 }
181
182
183
184
185
186
187 public void setValue(Object value) {
188 this.value = value;
189 }
190
191
192
193
194
195
196 protected String getStringValue() {
197 if (value == null) {
198 return "";
199 } else if (valueFormatter == null) {
200 return value.toString();
201 } else {
202 return valueFormatter.format(new Object[] { value });
203 }
204 }
205
206
207
208
209
210
211
212 protected void setStringValue(String newValue) {
213 if (valueFormatter == null) {
214 value = newValue;
215 } else {
216 try {
217 value = valueFormatter.parse(newValue)[0];
218 } catch (Exception e) {
219 e.printStackTrace();
220 }
221 }
222 }
223 }