1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.tools.console;
25
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.ResourceBundle;
29
30 import com.gridsystems.config.ConsoleConfiguratorView;
31 import com.gridsystems.config.app.UI;
32 import com.gridsystems.config.tools.TextVerifier;
33
34
35
36
37
38
39
40
41 public class ValueField extends Field implements ConsoleViewAction {
42
43
44
45 protected String linePattern;
46
47
48
49
50 protected String valuePattern;
51
52
53
54
55 protected Object value;
56
57
58
59
60 protected TextVerifier verifier;
61
62
63
64
65 protected MessageFormat valueFormater = null;
66
67
68
69
70 protected boolean trimmed = true;
71
72
73
74
75 private String keyMapping;
76
77
78
79
80
81
82
83
84 public ValueField(ResourceBundle bundle, String linePattern, String valuePattern) {
85 super(bundle);
86 this.linePattern = linePattern;
87 this.valuePattern = valuePattern;
88
89 if (valuePattern != null) {
90 valueFormater = new MessageFormat(valuePattern);
91 }
92 }
93
94
95
96
97 public String getKeyMapping() {
98 if (keyMapping == null) {
99 keyMapping = getString(this.linePattern + ".keyMap");
100 }
101 return keyMapping;
102 }
103
104
105
106
107
108
109 public void setKeyMapping(String mapping) {
110 this.keyMapping = mapping;
111 }
112
113
114
115
116 public String[] getContents() {
117 String[] contents = new String[1];
118
119
120 StringBuffer pattern = new StringBuffer();
121 String key = getKeyMapping();
122 if (key != null) {
123 pattern.append(" ").append(key).append(". ");
124 }
125
126 pattern.append(getString(linePattern));
127
128 String s = getStringValue();
129 contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
130 return contents;
131 }
132
133
134
135
136
137
138 public Object getValue() {
139 return value;
140 }
141
142
143
144
145
146
147 public void setValue(Object value) {
148 this.value = value;
149 }
150
151
152
153
154
155
156 public void setVerifier(TextVerifier verifier) {
157 this.verifier = verifier;
158 }
159
160
161
162
163
164
165 public TextVerifier getVerifier() {
166 return this.verifier;
167 }
168
169
170
171
172
173
174 public void setTrimmed(boolean trimmed) {
175 this.trimmed = trimmed;
176 }
177
178
179
180
181
182
183 public boolean isTrimmed() {
184 return this.trimmed;
185 }
186
187
188
189
190
191
192 protected String getStringValue() {
193 if (value == null) {
194 return "";
195 } else if (valueFormater == null) {
196 return value.toString();
197 } else {
198 return valueFormater.format(new Object[] { value });
199 }
200 }
201
202
203
204
205
206
207
208 protected void setStringValue(String newValue) {
209 if (valueFormater == null) {
210 value = newValue;
211 } else {
212 try {
213 value = valueFormater.parse(newValue)[0];
214 } catch (Exception e) {
215 e.printStackTrace();
216 }
217 }
218 }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 public boolean execute(ConsoleConfiguratorView view) {
235 while (true) {
236 try {
237 String line = readLine();
238 if (line.equals("")) {
239 return true;
240 }
241
242
243 if (trimmed) {
244 line = line.trim();
245 }
246
247
248 boolean valid = (verifier == null) ? true : verifier.verify(line);
249 if (valid) {
250 setStringValue(line);
251 return true;
252 }
253 } catch (Exception e) {
254 e.printStackTrace();
255 }
256 }
257 }
258
259
260
261
262
263
264
265 protected String readLine() throws IOException {
266 if (value != null) {
267 String currentValuePattern =
268 UI.getString("console.readLine.currentValue");
269 String currentValue = MessageFormat.format(
270 currentValuePattern, new Object[]{getStringValue()});
271
272 System.out.println(currentValue);
273 }
274
275 String line =
276 ConsoleTools.readLine(UI.getString("console.readLine.newValue"));
277 return line;
278 }
279 }