01: /*
02: * Copyright 2005 Paul Hinds, Mark Anderson
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.input.OutputField;
23: import org.tp23.antinstaller.input.TargetSelectInput;
24: import org.tp23.antinstaller.renderer.AIResourceBundle;
25:
26: /**
27: *
28: * @author Paul Hinds, Mark Anderson
29: * @version $Id: TargetSelectInputRenderer.java,v 1.3 2007/01/09 22:41:40 teknopaul Exp $
30: */
31: public class TargetSelectInputRenderer extends SelectInputRenderer {
32:
33: private static final AIResourceBundle res = new AIResourceBundle();
34:
35: public TargetSelectInputRenderer() {
36: }
37:
38: public void renderOutput(OutputField field, BufferedReader reader,
39: PrintStream out) throws IOException {
40: TargetSelectInput iField = (TargetSelectInput) field;
41: printText(iField, out);
42:
43: String input = reader.readLine();
44: out.println();
45: if (input == null || input.equals(""))
46: input = iField.getDefaultValue();
47: else {
48: try {
49: int idx = Integer.parseInt(input.trim());
50: input = iField.getOptions()[idx - 1].value;
51: } catch (Exception numFormatOrIndexOutOfBounds) {
52: return;
53: }
54: }
55: ctx.getCurrentPage().removeTarget(iField.getIdx());
56: ctx.getCurrentPage().addTarget(iField.getIdx(), input);
57: iField.setInputResult(input);
58: }
59:
60: private void printText(TargetSelectInput iField, PrintStream out)
61: throws IOException {
62: out.println(iField.getDisplayText());
63: TargetSelectInput.Option[] options = iField.getOptions();
64: out.print(" ");
65: out.println(res.getString("enter.number"));
66: for (int i = 0; i < options.length; i++) {
67: out.print(" ");
68: out.print(i + 1);
69: out.print(") ");
70: out.print(options[i].getText());
71: if (iField.getDefaultValue().equals(options[i].value)) {
72: out.print(" [");
73: out.print(res.getString("selection.default"));
74: out.print("]");
75: }
76: out.println();
77: }
78: }
79:
80: }
|