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.File;
20import java.io.IOException;
21import java.io.PrintStream;
22import java.util.ResourceBundle;
23
24import org.tp23.antinstaller.InstallerContext;
25import org.tp23.antinstaller.input.DirectoryInput;
26import org.tp23.antinstaller.input.OutputField;
27
28public class DirectoryInputRenderer implements TextOutputFieldRenderer {
29
30    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.text.Res");
31
32    protected InstallerContext ctx;
33
34    public DirectoryInputRenderer() {
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        DirectoryInput iField = (DirectoryInput) field;
43        out.print(iField.getDisplayText());
44
45        out.print("   [");
46        out.print(res.getString("_default_"));
47        out.print(":");
48        out.print(iField.getDefaultValue(true));
49        out.print("]");
50
51        out.println();
52        String input = reader.readLine();
53        out.println();
54        if (input == null || input.equals("")) {
55            input = iField.getDefaultValue(true);
56            if("".equals(input)) { // if we have empty default and empty input getAbsolutePath() produces a temporary directory
57                iField.setInputResult("");
58            }
59            else {
60                iField.setInputResult(new File(input).getAbsolutePath());
61            }
62        }
63        else {
64            iField.setInputResult(new File(input).getAbsolutePath());
65        }
66    }
67
68    public boolean isAbort() {
69        return false;
70    }
71
72    /**
73     * renderError
74     * 
75     * @param field
76     *            InputField
77     * @param in
78     *            InputStream
79     * @param out
80     *            PrintStream
81     */
82    public void renderError(OutputField field, BufferedReader reader, PrintStream out) throws IOException {
83        renderOutput(field, reader, out);
84    }
85
86}
87