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.page;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.MissingResourceException;
022: import java.util.Set;
023: import java.util.StringTokenizer;
024: import java.util.TreeSet;
025:
026: import org.tp23.antinstaller.InstallException;
027: import org.tp23.antinstaller.InstallerContext;
028: import org.tp23.antinstaller.input.OutputField;
029: import org.tp23.antinstaller.input.TargetInput;
030: import org.tp23.antinstaller.renderer.AILanguagePack;
031: import org.tp23.antinstaller.runtime.IfPropertyHelper;
032:
033: /**
034: *
035: * <p>Represents a page in the installer. </p>
036: * <p>This object maintians an ordered list of targets that have been selected
037: * so that when ant is run the targets are run in the correct order. If
038: * Targets exist in multiple pages they are run in the order they appear in the config file. </p>
039: * <p>Copyright: Copyright (c) 2004</p>
040: * <p>Company: tp23</p>
041: * @author Paul Hinds
042: * @version $Id: Page.java,v 1.10 2007/01/19 00:24:36 teknopaul Exp $
043: */
044:
045: public abstract class Page {
046:
047: // i18n support
048: private static final AILanguagePack langPack = new AILanguagePack();
049:
050: //private static final int MAX_TARGETS = 10;
051: private String name;
052: private String displayText;
053: private String imageResource;
054: private OutputField[] outputField;
055: private boolean abort;
056: /**
057: * target to be called as the installer is running
058: */
059: private String postDisplayTarget;
060: private Set targets = new TreeSet();
061:
062: public Page() {
063: }
064:
065: public String getName() {
066: return name;
067: }
068:
069: public void setName(String name) {
070: this .name = name;
071: }
072:
073: public String getDisplayText() {
074: if (langPack.isI18n()) {
075: try {
076: return langPack.getString("page." + getName()
077: + ".displayText");
078: } catch (MissingResourceException e) {
079: // ignore, signifies no lang packs installed
080: }
081: }
082: return displayText;
083: }
084:
085: public void setDisplayText(String displayText) {
086: this .displayText = displayText;
087: }
088:
089: public String getImageResource() {
090: return imageResource;
091: }
092:
093: public void setImageResource(String imageResource) {
094: this .imageResource = imageResource;
095: }
096:
097: public OutputField[] getOutputField() {
098: return outputField;
099: }
100:
101: public void setOutputField(OutputField[] outputField) {
102: this .outputField = outputField;
103: }
104:
105: public String getPostDisplayTarget() {
106: return postDisplayTarget;
107: }
108:
109: public void setPostDisplayTarget(String runtimeTarget) {
110: this .postDisplayTarget = runtimeTarget;
111: }
112:
113: /**
114: * These are the ant targets that will be run, this is decided after
115: * the Page has been displayed. For example if the user chooses not
116: * to enter a field that may signify that a target should not be run
117: *
118: * @param ctx installer context
119: * @return A sorted List a list of Ant targets as Strings;
120: */
121: public List getTargets(InstallerContext ctx) {
122: List results = new ArrayList(targets.size());
123: try {
124: Iterator iter = targets.iterator();
125: IfPropertyHelper helper = new IfPropertyHelper(ctx);
126: while (iter.hasNext()) {
127: IndexedTarget idxTarget = (IndexedTarget) iter.next();
128: if (IndexedTarget.PAGE
129: .equals(idxTarget.getTargetType())) {
130: if (helper.ifProperty(this )
131: || helper.ifTarget(this , ctx.getInstaller()
132: .getPages())) {
133: results.add(idxTarget.target);
134: }
135: } else {
136: results.add(idxTarget.target);
137: }
138: }
139: } catch (InstallException e) {
140: // should not happen at runtime if ifProperty has been validated
141: e.printStackTrace();
142: ctx.log(ctx.getInstaller().isVerbose(), e);
143: throw new RuntimeException();
144: }
145: return results;
146: }
147:
148: /**
149: * get input targets that are selected and all page targets, independent of
150: * the ifProperty value
151: * @return list of targets
152: */
153: public List getAllTargets() {
154: List results = new ArrayList(targets.size());
155: Iterator iter = targets.iterator();
156: while (iter.hasNext()) {
157: IndexedTarget idxTarget = (IndexedTarget) iter.next();
158: results.add(idxTarget.target);
159: }
160: return results;
161: }
162:
163: /**
164: * Comma separated list of targets for this page, called when parsing the
165: * config file
166: * @param targetList list of targets for this page
167: */
168: public void setTarget(String targetList) {
169: StringTokenizer st = new StringTokenizer(targetList, ",");
170: while (st.hasMoreTokens()) {
171: targets.add(new IndexedTarget(TargetInput.getGlobalIdx(),
172: st.nextToken(), IndexedTarget.PAGE));
173: }
174: }
175:
176: /**
177: * Adds an INPUT target to the Page config
178: * @param idx index
179: * @param target target to be added
180: */
181: public void addTarget(int idx, String target) {
182: this .targets.add(new IndexedTarget(idx, target,
183: IndexedTarget.INPUT));
184: }
185:
186: public void removeTarget(int idx) {
187: this .targets.remove(new IndexedTarget(idx, null));
188: }
189:
190: /**
191: * returns true if the page has the current target set
192: * @param target String
193: * @return boolean
194: */
195: public boolean isTarget(String target) {
196: if (target == null) {
197: return false;
198: }
199: Iterator iter = targets.iterator();
200: while (iter.hasNext()) {
201: IndexedTarget idxTarget = (IndexedTarget) iter.next();
202: if (idxTarget.target.equals(target)) {
203: return true;
204: }
205: }
206: return false;
207: }
208:
209: /**
210: * @return a List of IndexedTarget objects
211: */
212: public List getPageTargets() {
213: List toReturn = new ArrayList(targets.size());
214: Iterator iter = targets.iterator();
215: while (iter.hasNext()) {
216: IndexedTarget idxTarget = (IndexedTarget) iter.next();
217: if (IndexedTarget.PAGE.equals(idxTarget.targetType)) {
218: toReturn.add(idxTarget);
219: }
220: }
221: return toReturn;
222: }
223:
224: /**
225: * @return a List of IndexedTarget objects
226: */
227: public List getElementTargets() {
228: List toReturn = new ArrayList(targets.size());
229: Iterator iter = targets.iterator();
230: while (iter.hasNext()) {
231: IndexedTarget idxTarget = (IndexedTarget) iter.next();
232: if (IndexedTarget.INPUT.equals(idxTarget.targetType)) {
233: toReturn.add(idxTarget);
234: }
235: }
236: return toReturn;
237: }
238:
239: /**
240: * This is called after the page is displayed, a page can return false to indicate
241: * that the installation should abort. Should be false if the cancel button is pressed.
242: * System.exit is not called to allow the installer to clean up temporary files.
243: * @return boolean
244: */
245: public boolean isAbort() {
246: return abort;
247: }
248:
249: public void setAbort(boolean abort) {
250: this .abort = abort;
251: }
252:
253: public static class IndexedTarget implements Comparable {
254:
255: private static final String PAGE = "page";
256: private static final String INPUT = "input";
257:
258: int idx;
259: String target;
260: String targetType = INPUT;
261:
262: IndexedTarget(int idx, String target) {
263: this .idx = idx;
264: this .target = target;
265: }
266:
267: IndexedTarget(int idx, String target, String targetType) {
268: this .idx = idx;
269: this .target = target;
270: this .targetType = targetType;
271: }
272:
273: public boolean equals(Object target) {
274: IndexedTarget test = (IndexedTarget) target;
275: return test.idx == idx;
276: }
277:
278: // FindBugs - part of equals() contract
279: public int hashCode() {
280: return this .idx;
281: }
282:
283: public int compareTo(Object o) {
284: IndexedTarget test = (IndexedTarget) o;
285: return idx - test.idx;
286: }
287:
288: public String getTarget() {
289: return target;
290: }
291:
292: public String getTargetType() {
293: return targetType;
294: }
295:
296: }
297:
298: }
|