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 | */
|
---|
16 | package org.tp23.antinstaller.runtime.exe;
|
---|
17 |
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.util.StringTokenizer;
|
---|
20 |
|
---|
21 | import org.tp23.antinstaller.InstallException;
|
---|
22 | import org.tp23.antinstaller.InstallerContext;
|
---|
23 | import org.tp23.antinstaller.renderer.swing.plaf.LookAndFeelFactory;
|
---|
24 | import org.tp23.antinstaller.runtime.AutoSwingRunner;
|
---|
25 | import org.tp23.antinstaller.runtime.AutoTextRunner;
|
---|
26 | import org.tp23.antinstaller.runtime.Runner;
|
---|
27 | import org.tp23.antinstaller.runtime.SwingRunner;
|
---|
28 | import 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 | */
|
---|
37 | public 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{
|
---|
100 | throw new InstallException("Not a permited UI override, text-auto is not allowed");
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 | //else do stuff to work out if there is a graphics context
|
---|
106 | try{
|
---|
107 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
|
---|
108 | /*
|
---|
109 | * Above test is not enough to be sure that we can use the graphics env
|
---|
110 | * so do remaining setup within try/catch block
|
---|
111 | */
|
---|
112 | new LookAndFeelFactory(ctx).setLAF();
|
---|
113 | return new SwingRunner(ctx);
|
---|
114 | } catch (Throwable e){
|
---|
115 | System.out.println("No graphics environment available, reverting to text");
|
---|
116 | System.out.println();
|
---|
117 | return new TextRunner(ctx);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | private boolean isUi(String ui, String commaSeparatedUiList){
|
---|
123 | StringTokenizer st = new StringTokenizer(commaSeparatedUiList, ",");
|
---|
124 | while(st.hasMoreTokens()){
|
---|
125 | if(st.nextToken().equals(ui)){
|
---|
126 | return true;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | return false;
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|