1 /* 
2  * Copyright 2005 Paul Hinds, Mark Anderson
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.input.OutputField;
24import org.tp23.antinstaller.input.TargetSelectInput;
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 */
31public class TargetSelectInputRenderer
32    extends SelectInputRenderer {
33
34    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
35
36    public TargetSelectInputRenderer() {
37    }
38
39    public void renderOutput(OutputField field, BufferedReader reader, 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) throws IOException{
61        out.println(iField.getDisplayText());
62        TargetSelectInput.Option[] options = iField.getOptions();
63        out.print("  ");
64        out.println(res.getString("enterNumber"));
65        for (int i = 0; i < options.length; i++) {
66            out.print("  ");
67            out.print(i+1);
68            out.print(") ");
69            out.print(options[i].getText());
70            if(iField.getDefaultValue().equals(options[i].value)){
71                out.print(" [");
72                out.print(res.getString("_default_"));
73                out.print("]");
74            }
75            out.println();
76        }
77    }
78
79}
80