1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 */
16package org.tp23.antinstaller.renderer.text;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.PrintStream;
21import java.util.ResourceBundle;
22
23import org.tp23.antinstaller.InstallerContext;
24import org.tp23.antinstaller.input.LargeSelectInput;
25import org.tp23.antinstaller.input.OutputField;
26
27
28public class LargeSelectInputRenderer
29    implements TextOutputFieldRenderer {
30
31    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
32    private static final String nextChar = res.getString("nextChar");
33
34    protected InstallerContext ctx;
35    public LargeSelectInputRenderer() {
36    }
37
38    public void setContext(InstallerContext ctx) {
39        this.ctx = ctx;
40    }
41
42    public void renderOutput(OutputField field, BufferedReader reader, PrintStream out) throws IOException {
43        LargeSelectInput iField = (LargeSelectInput) field;
44
45        printText(iField,out,reader);
46        String input = reader.readLine();
47        out.println();
48        if(input == null || input.equals("")) {
49            input = iField.getDefaultValue();
50        }
51        else {
52            try {
53                int idx = Integer.parseInt(input.trim());
54                input = iField.getOptions()[idx - 1].value;
55            } catch(Exception numFormatOrIndexOutOfBounds) {
56                return;
57            }
58        }
59        iField.setInputResult(input);
60    }
61    
62    public boolean isAbort(){
63        return false;
64    }
65    private void printText(LargeSelectInput iField, PrintStream out,BufferedReader reader) throws IOException{
66        out.println(iField.getDisplayText());
67        LargeSelectInput.Option[] options = iField.getOptions();
68        out.print("  ");
69        out.println(res.getString("availableOptions"));
70        StringBuffer optionsData = new StringBuffer();
71        for (int i = 0; i < options.length; i++) {
72            optionsData.append("  ");
73            optionsData.append(i+1);
74            optionsData.append(") ");
75            optionsData.append(options[i].getText());
76            if(iField.getDefaultValue().equals(options[i].value)){
77                optionsData.append(" [");
78                optionsData.append(res.getString("_default_"));
79                optionsData.append("]");
80            }
81            optionsData.append("\n");
82        }
83        optionsData.append("  ");
84        optionsData.append(res.getString("enterNumber")).append("\n");
85        Pager pager = new Pager(optionsData.toString());
86        String command = null;
87        do {
88            if (!pager.next(out)) {
89                break;
90            }
91            out.println();
92            out.println(getNextInstructions());
93            command = reader.readLine();
94        }
95        while (command.toUpperCase().startsWith( nextChar ));
96        pager.rest(out);
97
98    }
99
00
01
02    /**
03     * renderError
04     *
05     * @param field InputField
06     * @param out PrintStream
07     */
08    public void renderError(OutputField field, BufferedReader reader, PrintStream out) throws IOException {
09        ctx.getMessageRenderer().printMessage("Not a valid selection");
10        renderOutput(field, reader, out);
11    }
12    private String getNextInstructions() {
13        return res.getString("large_select_next");
14    }
15}
16