01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.antinstaller.renderer.text;
17:
18: import java.io.BufferedReader;
19: import java.io.IOException;
20: import java.io.PrintStream;
21:
22: import org.tp23.antinstaller.InstallerContext;
23: import org.tp23.antinstaller.input.OutputField;
24: import org.tp23.antinstaller.input.SelectInput;
25: import org.tp23.antinstaller.renderer.AIResourceBundle;
26:
27: public class SelectInputRenderer implements TextOutputFieldRenderer {
28:
29: private static final AIResourceBundle res = new AIResourceBundle();
30:
31: protected InstallerContext ctx;
32:
33: public SelectInputRenderer() {
34: }
35:
36: public void setContext(InstallerContext ctx) {
37: this .ctx = ctx;
38: }
39:
40: public void renderOutput(OutputField field, BufferedReader reader,
41: PrintStream out) throws IOException {
42: SelectInput iField = (SelectInput) field;
43: printText(iField, out);
44:
45: String input = reader.readLine();
46: out.println();
47: if (input == null || input.equals("")) {
48: input = iField.getDefaultValue();
49: } else {
50: try {
51: int idx = Integer.parseInt(input.trim());
52: input = iField.getOptions()[idx - 1].value;
53: } catch (Exception numFormatOrIndexOutOfBounds) {
54: return;
55: }
56: }
57: iField.setInputResult(input);
58: }
59:
60: public boolean isAbort() {
61: return false;
62: }
63:
64: private void printText(SelectInput iField, PrintStream out)
65: throws IOException {
66: out.println(iField.getDisplayText());
67: SelectInput.Option[] options = iField.getOptions();
68: out.print(" ");
69: out.println(res.getString("enter.number"));
70: for (int i = 0; i < options.length; i++) {
71: out.print(" ");
72: out.print(i + 1);
73: out.print(") ");
74: out.print(options[i].getText());
75: if (iField.getDefaultValue().equals(options[i].value)) {
76: out.print(" [");
77: out.print(res.getString("selection.default"));
78: out.print("]");
79: }
80: out.println();
81: }
82: }
83:
84: /**
85: * renderError
86: *
87: * @param field InputField
88: * @param in InputStream
89: * @param out PrintStream
90: */
91: public void renderError(OutputField field, BufferedReader reader,
92: PrintStream out) throws IOException {
93: ctx.getMessageRenderer().printMessage("Not a valid selection");
94: renderOutput(field, reader, out);
95: }
96: }
|