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 | */
|
---|
16 | package org.tp23.antinstaller.renderer.text;
|
---|
17 |
|
---|
18 | import java.io.BufferedReader;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.PrintStream;
|
---|
21 | import java.util.ResourceBundle;
|
---|
22 |
|
---|
23 | import org.tp23.antinstaller.InstallerContext;
|
---|
24 | import org.tp23.antinstaller.input.OutputField;
|
---|
25 | import org.tp23.antinstaller.input.SelectInput;
|
---|
26 |
|
---|
27 |
|
---|
28 | public class SelectInputRenderer
|
---|
29 | implements TextOutputFieldRenderer {
|
---|
30 |
|
---|
31 | private static final ResourceBundle res = ResourceBundle.getBundle("resources.LanguagePack");
|
---|
32 |
|
---|
33 | protected InstallerContext ctx;
|
---|
34 | public SelectInputRenderer() {
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void setContext(InstallerContext ctx) {
|
---|
38 | this.ctx = ctx;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void renderOutput(OutputField field, BufferedReader reader, 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 | }
|
---|
50 | else {
|
---|
51 | try {
|
---|
52 | int idx = Integer.parseInt(input.trim());
|
---|
53 | input = iField.getOptions()[idx - 1].value;
|
---|
54 | } catch(Exception numFormatOrIndexOutOfBounds){
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | iField.setInputResult(input);
|
---|
59 | }
|
---|
60 | public boolean isAbort(){
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | private void printText(SelectInput iField, PrintStream out) throws IOException{
|
---|
64 | out.println(iField.getDisplayText());
|
---|
65 | SelectInput.Option[] options = iField.getOptions();
|
---|
66 | out.print(" ");
|
---|
67 | out.println(res.getString("enterNumber"));
|
---|
68 | for (int i = 0; i < options.length; i++) {
|
---|
69 | out.print(" ");
|
---|
70 | out.print(i+1);
|
---|
71 | out.print(") ");
|
---|
72 | out.print(options[i].getText());
|
---|
73 | if(iField.getDefaultValue().equals(options[i].value)){
|
---|
74 | out.print(" [");
|
---|
75 | out.print(res.getString("_default_"));
|
---|
76 | out.print("]");
|
---|
77 | }
|
---|
78 | out.println();
|
---|
79 | }
|
---|
80 | }
|
---|
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, PrintStream out) throws IOException {
|
---|
92 | ctx.getMessageRenderer().printMessage("Not a valid selection");
|
---|
93 | renderOutput(field, reader, out);
|
---|
94 | }
|
---|
95 | }
|
---|