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.InputField;
25import org.tp23.antinstaller.input.OutputField;
26import org.tp23.antinstaller.input.TargetInput;
27
28
29public class TargetInputRenderer
30    implements TextOutputFieldRenderer {
31
32    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
33
34    protected InstallerContext ctx;
35    public TargetInputRenderer() {
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        TargetInput iField = (TargetInput) field;
44        out.println("Install the following component?");
45        out.print(iField.getDisplayText());
46
47        out.print("   [");
48        out.print(res.getString("_default_"));
49        out.print(":");
50        out.print(iField.getDefaultValue());
51        out.print("]");
52
53        if (InputField.isTrue(iField.getForce())) {
54            out.print("   [");
55            out.print(res.getString("_required_"));
56            out.println("]");
57            ctx.getCurrentPage().addTarget(iField.getIdx(), iField.getTarget());
58            iField.setInputResult("true");
59            out.println();
60            return;
61        }
62
63
64
65        out.println();
66        String input = reader.readLine();
67        out.println();
68        if (input == null || input.trim().equals("")){
69            input = iField.getDefaultValue();
70        }
71        if(InputField.isTrue(input)){
72            ctx.getCurrentPage().addTarget(iField.getIdx(), iField.getTarget());
73            iField.setInputResult("true");
74        }
75        else{
76            ctx.getCurrentPage().removeTarget(iField.getIdx());
77            iField.setInputResult("false");
78        }
79        
80    }
81
82    public boolean isAbort() {
83        return false;
84    }
85
86
87
88    /**
89     * renderError
90     *
91     * @param field InputField
92     * @param out PrintStream
93     */
94    public void renderError(OutputField field, BufferedReader reader, PrintStream out) {
95    }
96}
97