source: release-kits/lirk3/bin/ant-installer/src/org/tp23/antinstaller/page/Page.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 7.5 KB
Line 
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;
100 }
101
102 public void setOutputField(OutputField[] outputField) {
103 this.outputField = outputField;
104 }
105
106 public String getPostDisplayTarget() {
107 return postDisplayTarget;
108 }
109
110 public void setPostDisplayTarget(String runtimeTarget) {
111 this.postDisplayTarget = runtimeTarget;
112 }
113
114
115 /**
116 * These are the ant targets that will be run, this is decided after
117 * the Page has been displayed. For example if the user chooses not
118 * to enter a field that may signify that a target should not be run
119 * @return A sorted List a list of Ant targets as Strings;
120 * @throws InstallException
121 */
122 public List getTargets(InstallerContext ctx) {
123 List results = new ArrayList(targets.size());
124 try {
125 Iterator iter = targets.iterator();
126 IfPropertyHelper helper = new IfPropertyHelper(ctx);
127 while(iter.hasNext()){
128 IndexedTarget idxTarget = (IndexedTarget)iter.next();
129 if( IndexedTarget.PAGE.equals(idxTarget.getTargetType()) ){
130 if( helper.ifProperty(this) || helper.ifTarget(this, ctx.getInstaller().getPages() ) ){
131 results.add(idxTarget.target);
132 }
133 }
134 else{
135 results.add(idxTarget.target);
136 }
137 }
138 } catch (InstallException e) {
139 // should not happen at runtime if ifProperty has been validated
140 e.printStackTrace();
141 ctx.log(ctx.getInstaller().isVerbose(), e);
142 throw new RuntimeException();
143 }
144 return results;
145 }
146 /**
147 * get input targets that are selected and all page targets, independent of
148 * the ifProperty value
149 * @return
150 */
151 public List getAllTargets() {
152 List results = new ArrayList(targets.size());
153 Iterator iter = targets.iterator();
154 while(iter.hasNext()){
155 IndexedTarget idxTarget = (IndexedTarget)iter.next();
156 results.add(idxTarget.target);
157 }
158 return results;
159 }
160 /**
161 * Comma separated list of targets for this page, called when parsing the
162 * config file
163 * @param target String
164 */
165 public void setTarget(String targetList) {
166 StringTokenizer st = new StringTokenizer(targetList, ",");
167 while (st.hasMoreTokens()) {
168 targets.add(new IndexedTarget(TargetInput.getGlobalIdx(),
169 st.nextToken(),
170 IndexedTarget.PAGE));
171 }
172 }
173 /**
174 * Adds an INPUT target to the Page config
175 * @param idx
176 * @param target
177 */
178 public void addTarget(int idx, String target) {
179 this.targets.add(new IndexedTarget(idx, target, IndexedTarget.INPUT));
180 }
181 public void removeTarget(int idx) {
182 this.targets.remove(new IndexedTarget(idx, null));
183 }
184 /**
185 * returns true if the page has the current target set
186 * @param target String
187 * @return boolean
188 */
189 public boolean isTarget(String target) {
190 if(target == null){
191 return false;
192 }
193 Iterator iter = targets.iterator();
194 while(iter.hasNext()) {
195 IndexedTarget idxTarget = (IndexedTarget)iter.next();
196 if(idxTarget.target.equals(target)){
197 return true;
198 }
199 }
200 return false;
201 }
202
203 /**
204 * @return a List of IndexedTarget objects
205 */
206 public List getPageTargets(){
207 List toReturn = new ArrayList(targets.size());
208 Iterator iter = targets.iterator();
209 while(iter.hasNext()) {
210 IndexedTarget idxTarget = (IndexedTarget)iter.next();
211 if( IndexedTarget.PAGE.equals(idxTarget.targetType) ){
212 toReturn.add(idxTarget);
213 }
214 }
215 return toReturn;
216 }
217
218 /**
219 * @return a List of IndexedTarget objects
220 */
221 public List getElementTargets(){
222 List toReturn = new ArrayList(targets.size());
223 Iterator iter = targets.iterator();
224 while(iter.hasNext()) {
225 IndexedTarget idxTarget = (IndexedTarget)iter.next();
226 if( IndexedTarget.INPUT.equals(idxTarget.targetType) ){
227 toReturn.add(idxTarget);
228 }
229 }
230 return toReturn;
231 }
232 /**
233 * This is called after the page is displayed, a page can return false to indicate
234 * that the installation should abort. Should be false if the cancel button is pressed.
235 * System.exit is not called to allow the installer to clean up temporary files.
236 * @return boolean
237 */
238 public boolean isAbort() {
239 return abort;
240 }
241
242 public void setAbort(boolean abort) {
243 this.abort = abort;
244 }
245 public static class IndexedTarget implements Comparable{
246
247 private static final String PAGE = "page";
248 private static final String INPUT = "input";
249
250 int idx;
251 String target;
252 String targetType = INPUT;
253
254 IndexedTarget(int idx, String target){
255 this.idx = idx;
256 this.target = target;
257 }
258 IndexedTarget(int idx, String target, String targetType){
259 this.idx = idx;
260 this.target = target;
261 this.targetType = targetType;
262 }
263
264 public boolean equals(Object target){
265 IndexedTarget test = (IndexedTarget)target;
266 return test.idx == idx;
267 }
268 // FindBugs - part of equals() contract
269 public int hashCode(){
270 return this.idx;
271 }
272 public int compareTo(Object o) {
273 IndexedTarget test = (IndexedTarget)o;
274 return idx - test.idx;
275 }
276 public String getTarget() {
277 return target;
278 }
279 public String getTargetType() {
280 return targetType;
281 }
282
283 }
284}
Note: See TracBrowser for help on using the repository browser.