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.runtime.exe;
017:
018: import java.io.IOException;
019: import java.util.StringTokenizer;
020:
021: import org.tp23.antinstaller.InstallException;
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.renderer.swing.plaf.LookAndFeelFactory;
024: import org.tp23.antinstaller.runtime.AutoSwingRunner;
025: import org.tp23.antinstaller.runtime.AutoTextRunner;
026: import org.tp23.antinstaller.runtime.Runner;
027: import org.tp23.antinstaller.runtime.SwingRunner;
028: import org.tp23.antinstaller.runtime.TextRunner;
029:
030: /**
031: * Creates the Runner instance for the execution UI and sets up an appropriate
032: * message renderer.
033: * @author Paul Hinds
034: * @version $Id: CreateUIFilter.java,v 1.7 2007/01/28 17:49:15 teknopaul Exp $
035: */
036: public class CreateUIFilter implements ExecuteFilter {
037:
038: /**
039: * @see org.tp23.antinstaller.runtime.exe.ExecuteFilter#exec(org.tp23.antinstaller.InstallerContext)
040: */
041: public void exec(InstallerContext ctx) throws InstallException {
042: try {
043: if (ctx.getInstaller().isVerbose()) {
044: ctx.log("Creating UI classes");
045: }
046: ctx.setRunner(getRunner(ctx));
047: ctx.log("Created UI classes");
048: } catch (IOException e) {
049: throw new InstallException(
050: "Unable to create the user interface", e);
051: } catch (InstallException e) {
052: throw new InstallException(e.getMessage(), e);
053: }
054: }
055:
056: /**
057: * Determines which Runner to use text or swing or "auto" UIs which skip past the properties sreens.
058: * @param override String if this paramter is not null it will be used. If
059: * swing and there is no graphics environment the install will fail, if it is left
060: * as null a check is made to see if there is a Graphics Environment and swing is used
061: * if there are no errors, if there are errors the system falls back to the text console
062: *
063: * @throws IOException
064: * @return Runner
065: */
066: protected Runner getRunner(InstallerContext ctx)
067: throws IOException, InstallException {
068:
069: if (ctx.getUIOverride() != null) {
070: if (ctx.getUIOverride().equalsIgnoreCase("swing")) {
071: if (isUi("swing", ctx.getInstaller().getUi())) {
072: new LookAndFeelFactory(ctx).setLAF();
073: ctx.setGui(true);
074: return new SwingRunner(ctx);
075: } else {
076: throw new InstallException(
077: "Not a permited UI override, swing is not allowed");
078: }
079: }
080:
081: if (ctx.getUIOverride().equalsIgnoreCase("text")) {
082: if (isUi("text", ctx.getInstaller().getUi())) {
083: ctx.setGui(false);
084: return new TextRunner(ctx);
085: } else {
086: throw new InstallException(
087: "Not a permited UI override, text is not allowed");
088: }
089: }
090:
091: if (ctx.getUIOverride().equalsIgnoreCase("swing-auto")) {
092: if (isUi("swing-auto", ctx.getInstaller().getUi())) {
093: new LookAndFeelFactory(ctx).setLAF();
094: ctx.setGui(true);
095: return new AutoSwingRunner(ctx);
096: } else {
097: throw new InstallException(
098: "Not a permited UI override, swing-auto is not allowed");
099: }
100: }
101:
102: if (ctx.getUIOverride().equalsIgnoreCase("text-auto")) {
103: if (isUi("text-auto", ctx.getInstaller().getUi())) {
104: ctx.setGui(false);
105: return new AutoTextRunner(ctx);
106: } else {
107: throw new InstallException(
108: "Not a permited UI override, text-auto is not allowed");
109: }
110: }
111:
112: }
113: //else do stuff to work out if there is a graphics context
114: try {
115: java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
116: /*
117: * Above test is not enough to be sure that we can use the graphics env
118: * so do remaining setup within try/catch block
119: */
120: new LookAndFeelFactory(ctx).setLAF();
121: ctx.setGui(true);
122: return new SwingRunner(ctx);
123: } catch (Throwable e) {
124: System.out
125: .println("No graphics environment available, reverting to text");
126: System.out.println();
127: ctx.setGui(false);
128: return new TextRunner(ctx);
129: }
130: }
131:
132: public static boolean isUi(String ui, String commaSeparatedUiList) {
133: StringTokenizer st = new StringTokenizer(commaSeparatedUiList,
134: ",");
135: while (st.hasMoreTokens()) {
136: if (st.nextToken().equals(ui)) {
137: return true;
138: }
139: }
140: return false;
141: }
142:
143: }
|