001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.text;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.PrintStream;
021:
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.input.LargeSelectInput;
024: import org.tp23.antinstaller.input.OutputField;
025: import org.tp23.antinstaller.renderer.AIResourceBundle;
026:
027: public class LargeSelectInputRenderer implements
028: TextOutputFieldRenderer {
029:
030: private static final AIResourceBundle res = new AIResourceBundle();
031: private static final String nextChar = res.getString("next.char");
032:
033: protected InstallerContext ctx;
034:
035: public LargeSelectInputRenderer() {
036: }
037:
038: public void setContext(InstallerContext ctx) {
039: this .ctx = ctx;
040: }
041:
042: public void renderOutput(OutputField field, BufferedReader reader,
043: PrintStream out) throws IOException {
044: LargeSelectInput iField = (LargeSelectInput) field;
045:
046: printText(iField, out, reader);
047: String input = reader.readLine();
048: out.println();
049: if (input == null || input.equals("")) {
050: input = iField.getDefaultValue();
051: } else {
052: try {
053: int idx = Integer.parseInt(input.trim());
054: input = iField.getOptions()[idx - 1].value;
055: } catch (Exception numFormatOrIndexOutOfBounds) {
056: return;
057: }
058: }
059: iField.setInputResult(input);
060: }
061:
062: public boolean isAbort() {
063: return false;
064: }
065:
066: private void printText(LargeSelectInput iField, PrintStream out,
067: BufferedReader reader) throws IOException {
068: out.println(iField.getDisplayText());
069: LargeSelectInput.Option[] options = iField.getOptions();
070: out.print(" ");
071: out.println(res.getString("available.options"));
072: StringBuffer optionsData = new StringBuffer();
073: for (int i = 0; i < options.length; i++) {
074: optionsData.append(" ");
075: optionsData.append(i + 1);
076: optionsData.append(") ");
077: optionsData.append(options[i].getText());
078: if (iField.getDefaultValue().equals(options[i].value)) {
079: optionsData.append(" [");
080: optionsData.append(res.getString("selection.default"));
081: optionsData.append("]");
082: }
083: optionsData.append("\n");
084: }
085: optionsData.append(" ");
086: optionsData.append(res.getString("enter.number")).append("\n");
087: Pager pager = new Pager(optionsData.toString());
088: String command = null;
089: do {
090: if (!pager.next(out)) {
091: break;
092: }
093: out.println();
094: out.println(getNextInstructions());
095: command = reader.readLine();
096: } while (command.toUpperCase().startsWith(nextChar));
097: pager.rest(out);
098:
099: }
100:
101: /**
102: * renderError
103: *
104: * @param field InputField
105: * @param out PrintStream
106: */
107: public void renderError(OutputField field, BufferedReader reader,
108: PrintStream out) throws IOException {
109: ctx.getMessageRenderer().printMessage("Not a valid selection");
110: renderOutput(field, reader, out);
111: }
112:
113: private String getNextInstructions() {
114: return res.getString("large.select.next");
115: }
116: }
|