01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.File;
20: import java.io.IOException;
21: import java.io.PrintStream;
22:
23: import org.tp23.antinstaller.InstallerContext;
24: import org.tp23.antinstaller.input.DirectoryInput;
25: import org.tp23.antinstaller.input.OutputField;
26: import org.tp23.antinstaller.renderer.AIResourceBundle;
27:
28: public class DirectoryInputRenderer implements TextOutputFieldRenderer {
29:
30: private static final AIResourceBundle res = new AIResourceBundle();
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,
42: PrintStream out) throws IOException {
43: DirectoryInput iField = (DirectoryInput) field;
44: out.print(iField.getDisplayText());
45:
46: out.print(" [");
47: out.print(res.getString("selection.default"));
48: out.print(":");
49: out.print(iField.getDefaultValue(true));
50: out.print("]");
51:
52: out.println();
53: String input = reader.readLine();
54: out.println();
55: if (input == null || input.equals("")) {
56: input = iField.getDefaultValue(true);
57: if ("".equals(input)) { // if we have empty default and empty input getAbsolutePath() produces a temporary directory
58: iField.setInputResult("");
59: } else {
60: iField
61: .setInputResult(new File(input)
62: .getAbsolutePath());
63: }
64: } else {
65: iField.setInputResult(new File(input).getAbsolutePath());
66: }
67: }
68:
69: public boolean isAbort() {
70: return false;
71: }
72:
73: /**
74: * renderError
75: *
76: * @param field
77: * InputField
78: * @param in
79: * InputStream
80: * @param out
81: * PrintStream
82: */
83: public void renderError(OutputField field, BufferedReader reader,
84: PrintStream out) throws IOException {
85: renderOutput(field, reader, out);
86: }
87:
88: }
|