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.page;
17
18import java.util.ArrayList;
19import java.util.Iterator;
20import java.util.List;
21import java.util.MissingResourceException;
22import java.util.ResourceBundle;
23import java.util.Set;
24import java.util.StringTokenizer;
25import java.util.TreeSet;
26
27import org.tp23.antinstaller.InstallException;
28import org.tp23.antinstaller.InstallerContext;
29import org.tp23.antinstaller.input.OutputField;
30import org.tp23.antinstaller.input.TargetInput;
31import org.tp23.antinstaller.runtime.IfPropertyHelper;
32/**
33 *
34 * <p>Represents a page in the installer. </p>
35 * <p>This object maintians an ordered list of targets that have been selected
36* so that when ant is run the targets are run in the correct order.  If
37* Targets exist in multiple pages they are run in the order they appear in the config file.  </p>
38 * <p>Copyright: Copyright (c) 2004</p>
39 * <p>Company: tp23</p>
40 * @author Paul Hinds
41 * @version $Id: Page.java,v 1.10 2007/01/19 00:24:36 teknopaul Exp $
42 */
43
44public abstract class Page {
45
46//   i18n support
47    private static ResourceBundle langPack = null;
48    static{
49        try {
50            langPack = ResourceBundle.getBundle("LanguagePack");
51        } catch (MissingResourceException e) {
52            // ignore, signifies no lang packs installed  
53        }
54    }
55    
56    //private static final int MAX_TARGETS = 10;
57    private String name;
58    private String displayText;
59    private String imageResource;
60    private OutputField[] outputField;
61    private boolean abort;
62    /**
63     *  target to be called as the installer is running
64     */
65    private String postDisplayTarget;
66    private Set targets = new TreeSet();
67
68    public Page() {
69    }
70
71    public String getName() {
72        return name;
73    }
74
75    public void setName(String name) {
76        this.name = name;
77    }
78
79    public String getDisplayText() {
80        if(langPack != null){
81            return langPack.getString("page." + getName() + ".displayText");
82        }
83        return displayText;
84    }
85
86    public void setDisplayText(String displayText) {
87        this.displayText = displayText;
88    }
89
90    public String getImageResource() {
91        return imageResource;
92    }
93
94    public void setImageResource(String imageResource) {
95        this.imageResource = imageResource;
96    }
97
98    public OutputField[] getOutputField() {
99        return outputField;
00    }
01
02    public void setOutputField(OutputField[] outputField) {
03        this.outputField = outputField;
04    }
05
06    public String getPostDisplayTarget() {
07        return postDisplayTarget;
08    }
09
10    public void setPostDisplayTarget(String runtimeTarget) {
11        this.postDisplayTarget = runtimeTarget;
12    }
13
14
15    /**
16     * These are the ant targets that will be run, this is decided after
17     * the Page has been displayed.  For example if the user chooses not
18     * to enter a field that may signify that a target should not be run
19     * @return A sorted List a list of Ant targets as Strings;
20     * @throws InstallException 
21     */
22    public List getTargets(InstallerContext ctx) {
23        List results = new ArrayList(targets.size());
24        try {
25            Iterator iter = targets.iterator();
26            IfPropertyHelper helper = new IfPropertyHelper(ctx);
27            while(iter.hasNext()){
28                IndexedTarget idxTarget = (IndexedTarget)iter.next();
29                if( IndexedTarget.PAGE.equals(idxTarget.getTargetType()) ){
30                    if( helper.ifProperty(this) || helper.ifTarget(this, ctx.getInstaller().getPages() ) ){
31                        results.add(idxTarget.target);
32                    }
33                }
34                else{
35                    results.add(idxTarget.target);
36                }
37            }
38        } catch (InstallException e) {
39            // should not happen at runtime if ifProperty has been validated
40            e.printStackTrace();
41            ctx.log(ctx.getInstaller().isVerbose(), e);
42            throw new RuntimeException();
43        }
44        return results;
45    }
46    /**
47     * get input targets that are selected and all page targets, independent of
48     * the ifProperty value 
49     * @return
50     */
51    public List getAllTargets() {
52        List results = new ArrayList(targets.size());
53        Iterator iter = targets.iterator();
54        while(iter.hasNext()){
55            IndexedTarget idxTarget = (IndexedTarget)iter.next();
56            results.add(idxTarget.target);
57        }
58        return results;
59    }
60    /**
61     * Comma separated list of targets for this page, called when parsing the
62     * config file
63     * @param target String
64     */
65    public void setTarget(String targetList) {
66        StringTokenizer st = new StringTokenizer(targetList, ",");
67        while (st.hasMoreTokens()) {
68            targets.add(new IndexedTarget(TargetInput.getGlobalIdx(), 
69                    st.nextToken(),
70                    IndexedTarget.PAGE));
71        }
72    }
73    /**
74     * Adds an INPUT target to the Page config
75     * @param idx
76     * @param target
77     */
78    public void addTarget(int idx, String target) {
79        this.targets.add(new IndexedTarget(idx, target, IndexedTarget.INPUT));
80    }
81    public void removeTarget(int idx) {
82        this.targets.remove(new IndexedTarget(idx, null));
83    }
84    /**
85     * returns true if the page has the current target set
86     * @param target String
87     * @return boolean
88     */
89    public boolean isTarget(String target) {
90        if(target == null){
91            return false;
92        }
93        Iterator iter = targets.iterator();
94        while(iter.hasNext()) {
95            IndexedTarget idxTarget = (IndexedTarget)iter.next();
96            if(idxTarget.target.equals(target)){
97                return true;
98            }
99        }
00        return false;
01    }
02
03    /**
04     * @return a List of IndexedTarget objects
05     */
06    public List getPageTargets(){
07        List toReturn = new ArrayList(targets.size());
08        Iterator iter = targets.iterator();
09        while(iter.hasNext()) {
10            IndexedTarget idxTarget = (IndexedTarget)iter.next();
11            if( IndexedTarget.PAGE.equals(idxTarget.targetType) ){
12                toReturn.add(idxTarget);
13            }
14        }
15        return toReturn;
16    }
17
18    /**
19     * @return a List of IndexedTarget objects
20     */
21    public List getElementTargets(){
22        List toReturn = new ArrayList(targets.size());
23        Iterator iter = targets.iterator();
24        while(iter.hasNext()) {
25            IndexedTarget idxTarget = (IndexedTarget)iter.next();
26            if( IndexedTarget.INPUT.equals(idxTarget.targetType) ){
27                toReturn.add(idxTarget);
28            }
29        }
30        return toReturn;
31    }
32    /**
33     * This is called after the page is displayed, a page can return false to indicate
34     * that the installation should abort.  Should be false if the cancel button is pressed.
35     * System.exit is not called to allow the installer to clean up temporary files.
36     * @return boolean
37     */
38    public boolean isAbort() {
39        return abort;
40    }
41
42    public void setAbort(boolean abort) {
43        this.abort = abort;
44    }
45    public static class IndexedTarget implements Comparable{
46        
47        private static final String PAGE = "page";
48        private static final String INPUT = "input";
49        
50        int idx;
51        String target;
52        String targetType = INPUT;
53        
54        IndexedTarget(int idx, String target){
55            this.idx = idx;
56            this.target = target;
57        }
58        IndexedTarget(int idx, String target, String targetType){
59            this.idx = idx;
60            this.target = target;
61            this.targetType = targetType;
62        }
63        
64        public boolean equals(Object target){
65            IndexedTarget test = (IndexedTarget)target;
66            return test.idx == idx;
67        }
68        // FindBugs - part of equals() contract
69        public int hashCode(){
70            return this.idx;
71        }
72        public int compareTo(Object o) {
73            IndexedTarget test = (IndexedTarget)o;
74            return idx - test.idx;
75        }
76        public String getTarget() {
77            return target;
78        }
79        public String getTargetType() {
80            return targetType;
81        }
82
83    }
84}
85