001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.text;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.PrintStream;
021:
022: import org.tp23.antinstaller.InstallException;
023: import org.tp23.antinstaller.InstallerContext;
024: import org.tp23.antinstaller.ValidationException;
025: import org.tp23.antinstaller.input.CommentOutput;
026: import org.tp23.antinstaller.input.OutputField;
027: import org.tp23.antinstaller.page.Page;
028: import org.tp23.antinstaller.page.SimpleInputPage;
029: import org.tp23.antinstaller.renderer.AIResourceBundle;
030: import org.tp23.antinstaller.renderer.RendererFactory;
031:
032: public class SimpleInputPageRenderer extends AbstractTextPageRenderer {
033:
034: private static final AIResourceBundle res = new AIResourceBundle();
035:
036: public SimpleInputPageRenderer() {
037: }
038:
039: public boolean renderPage(Page page) throws InstallException {
040: if (page instanceof SimpleInputPage) {
041: try {
042: return renderSimpleInputPage((SimpleInputPage) page);
043: } catch (ClassNotFoundException ex) {
044: // this would be a code error
045: throw new InstallException(
046: "Cant find acceptable TextField renderer in SimpleInputPageRenderer.renderPage:"
047: + ex.getMessage(), ex);
048: }
049: } else {
050: //this would be a code error
051: throw new InstallException(
052: "Wrong Renderer in SimpleInputPageRenderer.renderPage");
053: }
054: }
055:
056: private boolean renderSimpleInputPage(SimpleInputPage page)
057: throws InstallException, ClassNotFoundException,
058: ValidationException {
059:
060: try {
061: printHeader(page);
062: OutputField[] fields = page.getOutputField();
063: return renderFields(getContext(), fields, reader, out);
064: } catch (IOException ex) {
065: // If you cant write to the console there is not much you can do
066: throw new InstallException("IOException", ex);
067: }
068: }
069:
070: public static boolean renderFields(InstallerContext context,
071: OutputField[] fields, BufferedReader reader, PrintStream out)
072: throws ClassNotFoundException, IOException,
073: ValidationException, InstallException {
074:
075: boolean onlyComments = true;
076: for (int f = 0; f < fields.length; f++) {
077: String text = fields[f].getExplanatoryText();
078: if (text != null) {
079: out.println(CommentOutputRenderer.stripHtml(text));
080: out.println();
081: }
082:
083: onlyComments &= fields[f] instanceof CommentOutput;
084:
085: TextOutputFieldRenderer frenderer = RendererFactory
086: .getTextRenderer(fields[f]);
087: frenderer.setContext(context);
088: frenderer.renderOutput(fields[f], reader, out);
089: if (frenderer.isAbort()) {
090: return false;
091: }
092: while (!fields[f].validate(context)) {
093: frenderer.renderError(fields[f], reader, out);
094: }
095: ;
096: }
097: if (onlyComments) {
098: out.println();
099: out.println(res.getString("click.view.text"));
100: reader.readLine();
101: }
102:
103: return true;
104: }
105: }
|