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.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.ResourceBundle;
31 import java.util.MissingResourceException;
32 import java.text.MessageFormat;
33
34 import com.gridsystems.config.Configurator;
35 import com.gridsystems.config.ConsoleConfiguratorView;
36 import com.gridsystems.config.ErrorListener;
37 import com.gridsystems.config.app.UI;
38
39
40
41
42
43
44
45 public abstract class AbstractConsoleView implements ConsoleConfiguratorView,
46 ErrorListener {
47
48
49
50 private static final int DEFAULT_CONSOLE_WIDTH = 70;
51
52
53
54
55 protected ResourceBundle bundle = null;
56
57
58
59
60 protected int width;
61
62
63
64
65 private Object[] form = null;
66
67
68
69
70 private Map<String, ConsoleViewAction> keyMappings
71 = new HashMap<String, ConsoleViewAction>();
72
73
74
75
76 private MessageField messages = new MessageField();
77
78
79
80
81 private Configurator config;
82
83
84
85
86
87
88 public AbstractConsoleView(Configurator config) {
89 this(config, DEFAULT_CONSOLE_WIDTH);
90 }
91
92
93
94
95
96
97
98 public AbstractConsoleView(Configurator config, int width) {
99 this.bundle = config.getBundle();
100 this.width = width;
101 this.config = config;
102 this.config.setErrorListener(this);
103
104 setApplyDiscardMappings(config);
105 }
106
107
108
109
110
111
112
113 public void setApplyDiscardMappings(Configurator config) {
114 addKeyMapping(new ApplyAction(config));
115 addKeyMapping(new DiscardAction(config));
116 }
117
118
119
120
121
122
123 public Configurator getConfigurator() {
124 return config;
125 }
126
127
128
129
130 public void execute() {
131 Object[] localForm = buildForm();
132 String sPrompt = UI.getString("console.menu.prompt");
133
134 boolean alive = true;
135 while (alive) {
136 try {
137 paintForm(localForm);
138 System.out.println();
139 String line = ConsoleTools.readLine(sPrompt).toUpperCase();
140 ConsoleViewAction action = (ConsoleViewAction)keyMappings.get(line);
141 if (action != null) {
142 alive = action.execute(this);
143 }
144 } catch (Exception e) {
145 e.printStackTrace();
146 }
147 }
148 }
149
150
151
152
153
154
155 protected void paintForm(Object[] form) {
156 ConsoleTools.clear();
157 ConsoleTools.paintBox(getTitle(), form, width);
158
159
160 messages.reset();
161 }
162
163
164
165
166
167
168
169
170 protected Object[] buildForm() {
171 if (form == null) {
172 String sApplyText = UI.getString("console.apply.text");
173 String sDiscardText = UI.getString("console.discard.text");
174
175 ArrayList<Object> items = new ArrayList<Object>();
176 getContents(items);
177 items.add("-");
178 items.add(" [" + sApplyText + "] [" + sDiscardText + "]");
179
180
181 items.add(null);
182 items.add(messages);
183
184 form = new Object[items.size()];
185 items.toArray(form);
186 }
187 return form;
188 }
189
190
191
192
193
194
195
196
197 public void addKeyMapping(String key, ConsoleViewAction action) {
198 keyMappings.put(key.toUpperCase(), action);
199 }
200
201
202
203
204
205
206 public void addKeyMapping(ConsoleViewAction action) {
207 addKeyMapping(action.getKeyMapping(), action);
208 }
209
210
211
212
213
214
215 public void addKeyMapping(GroupField group) {
216 ConsoleViewAction[] actions = group.getActions();
217 for (int i = 0; i < actions.length; i++) {
218 addKeyMapping(actions[i]);
219 }
220 }
221
222
223
224
225
226
227 public void removeKeyMapping(ConsoleViewAction action) {
228 keyMappings.remove(action.getKeyMapping().toUpperCase());
229 }
230
231
232
233
234
235
236 public void removeKeyMapping(GroupField group) {
237 ConsoleViewAction[] actions = group.getActions();
238 for (int i = 0; i < actions.length; i++) {
239 removeKeyMapping(actions[i]);
240 }
241 }
242
243
244
245
246
247
248 protected abstract void getContents(List<Object> list);
249
250
251
252
253
254
255
256
257 protected ValueField createValueField(String linePattern, String valuePattern) {
258 return new ValueField(bundle, linePattern, valuePattern);
259 }
260
261
262
263
264
265
266
267
268
269
270 protected SelectionField createSelectionField(
271 String linePattern, String valuePattern, String[] values) {
272 return new SelectionField(bundle, linePattern, valuePattern, values);
273 }
274
275
276
277
278
279
280
281 protected BoolField createBoolField(String linePattern) {
282 return new BoolField(bundle, linePattern);
283 }
284
285
286
287
288
289
290
291 protected PasswordField createPasswordField(String linePattern) {
292 return new PasswordField(bundle, linePattern);
293 }
294
295
296
297
298
299
300
301
302 protected ConstantField createConstantField(String linePattern, String value) {
303 return new ConstantField(bundle, linePattern, value);
304 }
305
306
307
308
309
310
311
312
313
314 public String getString(String key, Object[] params) {
315 if (bundle == null) {
316 throw new NullPointerException("Bundle not set");
317 }
318
319 try {
320 String pattern = bundle.getString(key);
321 return MessageFormat.format(pattern, params);
322 } catch (MissingResourceException e) {
323 return key + "#not found";
324 }
325 }
326
327
328
329
330
331
332
333
334
335 public String getString(String key) {
336 return getString(key, null);
337 }
338
339
340
341
342
343
344
345
346 public void showError(String[] message) {
347 int count = (message == null) ? 0 : message.length;
348 if (count == 0) {
349
350 messages.reset();
351 } else {
352 String title = UI.getString("messages.error");
353 StringBuffer sb = new StringBuffer();
354
355 messages.addLine("-");
356 for (int i = 0; i < count; i++) {
357 sb.setLength(0);
358 if (i == 0) {
359 sb.append(title).append(": ");
360 } else {
361 ConsoleTools.fill(sb, ' ', title.length() + 2);
362 }
363 sb.append(message[i]);
364 messages.addLine(sb.toString());
365 }
366 }
367 }
368
369
370
371
372 Map<String, String> errors = new HashMap<String, String>();
373
374
375
376
377 public void updateError(String id, String msg, boolean add) {
378 if (add) {
379 System.err.println("ERROR: " + msg);
380 errors.put(id, msg);
381 } else {
382 errors.remove(id);
383 }
384 }
385
386
387
388
389 public void clearErrors() {
390 errors.clear();
391 }
392
393
394
395
396 public boolean hasErrors() {
397 return this.errors.size() == 0;
398 }
399 }