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;
17
18import java.util.List;
19import java.util.MissingResourceException;
20import java.util.ResourceBundle;
21import java.util.Vector;
22
23import org.tp23.antinstaller.input.OutputField;
24import org.tp23.antinstaller.input.ResultContainer;
25import org.tp23.antinstaller.page.Page;
26import org.tp23.antinstaller.renderer.swing.SizeConstants;
27import org.tp23.antinstaller.runtime.IfPropertyHelper;
28import org.tp23.antinstaller.runtime.VersionHelper;
29import org.tp23.antinstaller.runtime.exe.ExecuteRunnerFilter.AbortException;
30
31
32
33/**
34 * <p>Object representation of the Installer element in the config. </p>
35 * <p>This object holds the reference to all the Pages which in tern hold references
36 * to the InputFields,  All the properties in the Installer element are held at this level
37 * as is the ResultContainer</p>
38 * @author Paul Hinds
39 * @version $Id: Installer.java,v 1.9 2007/01/28 08:44:41 teknopaul Exp $
40 */
41public class Installer {
42
43
44    // i18n support
45    private static ResourceBundle langPack = null;
46    static{
47        try {
48            langPack = ResourceBundle.getBundle("resources.LanguagePack");
49        } catch (MissingResourceException e) {
50            // ignore, signifies no lang packs installed  
51        }
52    }
53
54    private String name;
55    private String minJavaVersion = "1.4";
56    private String ui;// permitted UI override values
57    private boolean verbose;
58    private boolean debug;
59    private String lookAndFeel;
60    private String wide;
61    private String windowIcon;
62    private String defaultImageResource;
63    private String finishButtonText = "Install";
64    private String antialiased;
65    private String loadDefaults;
66    private String version = "0.0";
67    
68    private Page[] pages;
69    private ResultContainer resultContainer = new ResultContainer();
70
71    public Installer() {
72    }
73
74    public String getName() {
75        return name;
76    }
77
78    public void setName(String name) {
79        this.name = name;
80    }
81
82
83    public String getMinJavaVersion() {
84        return minJavaVersion;
85    }
86    
87    public void setMinJavaVersion(String minJavaVersion) throws AbortException {
88        if(minJavaVersion != null && ! "".equals(minJavaVersion) ) {
89            VersionHelper helper = new VersionHelper();
90            if( ! helper.equalOrHigher(System.getProperty("java.version"), minJavaVersion, true) ) {
91                throw new AbortException("Incorrect Java version, installer requires: " + minJavaVersion);
92            }
93        }
94        this.minJavaVersion = minJavaVersion;
95    }
96
97    public Page[] getPages() {
98        return pages;
99    }
00
01    public void setPages(Page[] pages) {
02        this.pages = pages;
03    }
04
05    /**
06     * returns full ui attribute with spaces removed (the string is not parsed into tokens).
07     * e.g.  "text,swint,swing-auto"
08     * @return
09     */
10    public String getUi() {
11        return ui;
12    }
13
14    /**
15     * Sets the ui attrribute removing any whitespace
16     * @param ui
17     */
18    public void setUi(String ui) {
19        this.ui = ui.replaceAll("\\s", "");
20    }
21
22    public boolean supportsAutoBuild(){
23        return ui != null && ui.indexOf("-auto") > -1;
24    }
25
26    public boolean isVerbose() {
27        return verbose;
28    }
29    public void setVerbose(boolean verbose) {
30        this.verbose = verbose;
31    }
32    public void setVerbose(String strVerbose) {
33        this.verbose = OutputField.isTrue(strVerbose);
34    }
35
36    public boolean isDebug() {
37        return debug;
38    }
39    public void setDebug(boolean debug) {
40        this.debug = debug;
41    }
42    public void setDebug(String strDebug) {
43        this.debug = OutputField.isTrue(strDebug);
44    }
45
46
47    public String getLookAndFeel() {
48        return lookAndFeel;
49    }
50
51    public void setLookAndFeel(String lookAndFeel) {
52        this.lookAndFeel = lookAndFeel;
53    }
54
55    public String getWindowIcon() {
56        return windowIcon;
57    }
58
59    public void setWindowIcon(String windowIcon) {
60        this.windowIcon = windowIcon;
61    }
62
63    public String getDefaultImageResource() {
64        return defaultImageResource;
65    }
66
67    public void setDefaultImageResource(String defaultImageResource) {
68        this.defaultImageResource = defaultImageResource;
69    }
70
71    public String getFinishButtonText() {
72        if(langPack != null){
73            return langPack.getString("finishButtonText");
74        }
75        return finishButtonText;
76    }
77
78    public void setFinishButtonText(String finishButtonText) {
79        this.finishButtonText = finishButtonText;
80    }
81
82    public ResultContainer getResultContainer() {
83        return resultContainer;
84    }
85
86    public String getAntialiased() {
87        return antialiased;
88    }
89
90    public void setAntialiased(String antialiased) {
91        this.antialiased = antialiased;
92    }
93
94    public String getWide() {
95        return wide;
96    }
97
98    public void setWide(String wide) {
99        try {
00            this.wide = wide;
01            parseWideValue(wide);
02        } catch (Exception e) {
03            // ignore use defaults
04        }
05    }
06    public void parseWideValue(String wide) throws NumberFormatException, StringIndexOutOfBoundsException{
07        int pageWidth = Integer.parseInt(wide.substring(0, wide.indexOf(':')));
08        int labelWidth = Integer.parseInt(wide.substring(wide.indexOf(':') + 1, wide.length()));
09        SizeConstants.PAGE_WIDTH = pageWidth;
10        SizeConstants.LABEL_WIDTH = labelWidth;
11    }
12
13    public String getLoadDefaults() {
14        return loadDefaults;
15    }
16
17    public void setLoadDefaults(String loadDefaults) {
18        this.loadDefaults = loadDefaults;
19    }
20
21    /**
22     * Returns the list of selected targets from the installer obeying
23     * page order. This method is
24     * probably only usefull after the UI screens have finished.  Using prior to that
25     * bear in mind that the user in the Swing GUI can go back and reselect
26     * targets that were not selected previously.
27     * Page targets for pages that were not shown are not returned in the list 
28     * @return Returns a Vector since Ant requires this for the Project class (Should be a List)
29     * @throws InstallException 
30     */
31    public Vector getTargets(InstallerContext ctx){
32        Vector argsList = new Vector();
33        for (int i = 0; i < getPages().length; i++) {
34            Page page = getPages()[i];
35            List pageTargets = page.getPageTargets();
36            boolean shown = conditionalDisplay(page, ctx);
37            for (int pt = 0; pt < pageTargets.size(); pt++) {
38                Page.IndexedTarget target = (Page.IndexedTarget)pageTargets.get(pt);
39                if( ! argsList.contains(target.getTarget()) &&
40                    shown){
41                    argsList.add(target.getTarget());
42                }
43            }
44            List elementTargets = page.getElementTargets();
45            for (int pt = 0; pt < elementTargets.size(); pt++) {
46                Page.IndexedTarget target = (Page.IndexedTarget)elementTargets.get(pt);
47                if( ! argsList.contains(target.getTarget()) ){
48                    argsList.add(target.getTarget());
49                }
50            }
51        }
52        return argsList;
53    }
54
55    /**
56     * @return boolean true if the page was to be shown
57     * @throws InstallException 
58     */
59    private boolean conditionalDisplay(Page page, InstallerContext ctx){
60        try {
61            IfPropertyHelper helper = new IfPropertyHelper(ctx);
62            return helper.ifProperty(page) && helper.ifTarget(page, ctx.getInstaller().getPages());
63        } catch (InstallException e) {
64            throw new RuntimeException("ifProperty runtime exception");
65        }
66    }
67
68    public String getVersion() {
69        return version;
70    }
71
72    public void setVersion(String version) {
73        this.version = version;
74    }
75
76
77}
78