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.runtime.exe;
17
18import java.io.IOException;
19import java.util.StringTokenizer;
20
21import org.tp23.antinstaller.InstallException;
22import org.tp23.antinstaller.InstallerContext;
23import org.tp23.antinstaller.renderer.swing.plaf.LookAndFeelFactory;
24import org.tp23.antinstaller.runtime.AutoSwingRunner;
25import org.tp23.antinstaller.runtime.AutoTextRunner;
26import org.tp23.antinstaller.runtime.Runner;
27import org.tp23.antinstaller.runtime.SwingRunner;
28import org.tp23.antinstaller.runtime.TextRunner;
29
30
31/**
32 * Creates the Runner instance for the execution UI and sets up an appropriate
33 * message renderer.
34 * @author Paul Hinds
35 * @version $Id: CreateUIFilter.java,v 1.7 2007/01/28 17:49:15 teknopaul Exp $
36 */
37public class CreateUIFilter implements ExecuteFilter {
38
39    /**
40     * @see org.tp23.antinstaller.runtime.exe.ExecuteFilter#exec(org.tp23.antinstaller.InstallerContext)
41     */
42    public void exec(InstallerContext ctx) throws InstallException {
43        try {
44            if(ctx.getInstaller().isVerbose()){
45                ctx.log("Creating UI classes");
46            }
47            ctx.setRunner(getRunner(ctx));
48            ctx.log("Created UI classes");
49        }
50        catch (IOException e) {
51            throw new InstallException("Unable to create the user interface", e);
52        }
53        catch (InstallException e) {
54            throw new InstallException(e.getMessage(), e);
55        }
56    }
57    /**
58     * Determines which Runner to use text or swing or "auto" UIs which skip past the properties sreens.
59     * @param override String if this paramter is not null it will be used. If
60     * swing and there is no graphics environment the install will fail, if it is left
61     * as null a check is made to see if there is a Graphics Environment and swing is used
62     * if there are no errors, if there are errors the system falls back to the text console
63     *
64     * @throws IOException
65     * @return Runner
66     */
67    private Runner getRunner(InstallerContext ctx) throws IOException, InstallException {
68
69        if(ctx.getUIOverride() != null){
70            if (ctx.getUIOverride().equalsIgnoreCase("swing")){
71                if(isUi("swing", ctx.getInstaller().getUi())){
72                    new LookAndFeelFactory(ctx).setLAF();
73                    return new SwingRunner(ctx);
74                }else{
75                    throw new InstallException("Not a permited UI override, swing is not allowed");
76                }
77            }
78
79            if (ctx.getUIOverride().equalsIgnoreCase("text")){
80                if(isUi("text", ctx.getInstaller().getUi())){
81                    return new TextRunner(ctx);
82                }else{
83                    throw new InstallException("Not a permited UI override, text is not allowed");
84                }
85            }
86            
87            if (ctx.getUIOverride().equalsIgnoreCase("swing-auto")){
88                if(isUi("swing-auto", ctx.getInstaller().getUi())){
89                    new LookAndFeelFactory(ctx).setLAF();
90                    return new AutoSwingRunner(ctx);
91                }else{
92                    throw new InstallException("Not a permited UI override, swing-auto is not allowed");
93                }
94            }
95            
96            if (ctx.getUIOverride().equalsIgnoreCase("text-auto")){
97                if(isUi("text-auto", ctx.getInstaller().getUi())){
98                    return new AutoTextRunner(ctx);
99                }else{
00                    throw new InstallException("Not a permited UI override, text-auto is not allowed");
01                }
02            }
03
04        }
05        //else do stuff to work out if there is a graphics context
06        try{
07            java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
08            /*
09             * Above test is not enough to be sure that we can use the graphics env
10             * so do remaining setup within try/catch block
11             */
12            new LookAndFeelFactory(ctx).setLAF();
13            return new SwingRunner(ctx);
14        } catch (Throwable e){
15            System.out.println("No graphics environment available, reverting to text");
16            System.out.println();
17            return new TextRunner(ctx);
18        }
19    }
20    
21    
22    private boolean isUi(String ui, String commaSeparatedUiList){
23        StringTokenizer st = new StringTokenizer(commaSeparatedUiList, ",");
24        while(st.hasMoreTokens()){
25            if(st.nextToken().equals(ui)){
26                return true;
27            }
28        }
29        return false;
30    }
31
32}
33