source: release-kits/shared/ant-installer/src/org/tp23/antinstaller/page/Page.java@ 15210

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

Lots of changes to the installer. Now only look in LanguagePack resource bundle for strings.

File size: 7.6 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 //private static ResourceBundle otherLangPack = null;
49 static{
50 try {
51 langPack = ResourceBundle.getBundle("resources.LanguagePack");
52 } catch (MissingResourceException e) {
53 // ignore, signifies no lang packs installed
54 }
55 }
56
57 //private static final int MAX_TARGETS = 10;
58 private String name;
59 private String displayText;
60 private String imageResource;
61 private OutputField[] outputField;
62 private boolean abort;
63 /**
64 * target to be called as the installer is running
65 */
66 private String postDisplayTarget;
67 private Set targets = new TreeSet();
68
69 public Page() {
70 }
71
72 public String getName() {
73 return name;
74 }
75
76 public void setName(String name) {
77 this.name = name;
78 }
79
80 public String getDisplayText() {
81 if( langPack != null){
82 return langPack.getString("page." + getName() + ".displayText") + "r";
83 }
84 return displayText + "c";
85 }
86
87 public void setDisplayText(String displayText) {
88 this.displayText = displayText;
89 }
90
91 public String getImageResource() {
92 return imageResource;
93 }
94
95 public void setImageResource(String imageResource) {
96 this.imageResource = imageResource;
97 }
98
99 public OutputField[] getOutputField() {
100 return outputField;
101 }
102
103 public void setOutputField(OutputField[] outputField) {
104 this.outputField = outputField;
105 }
106
107 public String getPostDisplayTarget() {
108 return postDisplayTarget;
109 }
110
111 public void setPostDisplayTarget(String runtimeTarget) {
112 this.postDisplayTarget = runtimeTarget;
113 }
114
115
116 /**
117 * These are the ant targets that will be run, this is decided after
118 * the Page has been displayed. For example if the user chooses not
119 * to enter a field that may signify that a target should not be run
120 * @return A sorted List a list of Ant targets as Strings;
121 * @throws InstallException
122 */
123 public List getTargets(InstallerContext ctx) {
124 List results = new ArrayList(targets.size());
125 try {
126 Iterator iter = targets.iterator();
127 IfPropertyHelper helper = new IfPropertyHelper(ctx);
128 while(iter.hasNext()){
129 IndexedTarget idxTarget = (IndexedTarget)iter.next();
130 if( IndexedTarget.PAGE.equals(idxTarget.getTargetType()) ){
131 if( helper.ifProperty(this) || helper.ifTarget(this, ctx.getInstaller().getPages() ) ){
132 results.add(idxTarget.target);
133 }
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 * get input targets that are selected and all page targets, independent of
149 * the ifProperty value
150 * @return
151 */
152 public List getAllTargets() {
153 List results = new ArrayList(targets.size());
154 Iterator iter = targets.iterator();
155 while(iter.hasNext()){
156 IndexedTarget idxTarget = (IndexedTarget)iter.next();
157 results.add(idxTarget.target);
158 }
159 return results;
160 }
161 /**
162 * Comma separated list of targets for this page, called when parsing the
163 * config file
164 * @param target String
165 */
166 public void setTarget(String targetList) {
167 StringTokenizer st = new StringTokenizer(targetList, ",");
168 while (st.hasMoreTokens()) {
169 targets.add(new IndexedTarget(TargetInput.getGlobalIdx(),
170 st.nextToken(),
171 IndexedTarget.PAGE));
172 }
173 }
174 /**
175 * Adds an INPUT target to the Page config
176 * @param idx
177 * @param target
178 */
179 public void addTarget(int idx, String target) {
180 this.targets.add(new IndexedTarget(idx, target, IndexedTarget.INPUT));
181 }
182 public void removeTarget(int idx) {
183 this.targets.remove(new IndexedTarget(idx, null));
184 }
185 /**
186 * returns true if the page has the current target set
187 * @param target String
188 * @return boolean
189 */
190 public boolean isTarget(String target) {
191 if(target == null){
192 return false;
193 }
194 Iterator iter = targets.iterator();
195 while(iter.hasNext()) {
196 IndexedTarget idxTarget = (IndexedTarget)iter.next();
197 if(idxTarget.target.equals(target)){
198 return true;
199 }
200 }
201 return false;
202 }
203
204 /**
205 * @return a List of IndexedTarget objects
206 */
207 public List getPageTargets(){
208 List toReturn = new ArrayList(targets.size());
209 Iterator iter = targets.iterator();
210 while(iter.hasNext()) {
211 IndexedTarget idxTarget = (IndexedTarget)iter.next();
212 if( IndexedTarget.PAGE.equals(idxTarget.targetType) ){
213 toReturn.add(idxTarget);
214 }
215 }
216 return toReturn;
217 }
218
219 /**
220 * @return a List of IndexedTarget objects
221 */
222 public List getElementTargets(){
223 List toReturn = new ArrayList(targets.size());
224 Iterator iter = targets.iterator();
225 while(iter.hasNext()) {
226 IndexedTarget idxTarget = (IndexedTarget)iter.next();
227 if( IndexedTarget.INPUT.equals(idxTarget.targetType) ){
228 toReturn.add(idxTarget);
229 }
230 }
231 return toReturn;
232 }
233 /**
234 * This is called after the page is displayed, a page can return false to indicate
235 * that the installation should abort. Should be false if the cancel button is pressed.
236 * System.exit is not called to allow the installer to clean up temporary files.
237 * @return boolean
238 */
239 public boolean isAbort() {
240 return abort;
241 }
242
243 public void setAbort(boolean abort) {
244 this.abort = abort;
245 }
246 public static class IndexedTarget implements Comparable{
247
248 private static final String PAGE = "page";
249 private static final String INPUT = "input";
250
251 int idx;
252 String target;
253 String targetType = INPUT;
254
255 IndexedTarget(int idx, String target){
256 this.idx = idx;
257 this.target = target;
258 }
259 IndexedTarget(int idx, String target, String targetType){
260 this.idx = idx;
261 this.target = target;
262 this.targetType = targetType;
263 }
264
265 public boolean equals(Object target){
266 IndexedTarget test = (IndexedTarget)target;
267 return test.idx == idx;
268 }
269 // FindBugs - part of equals() contract
270 public int hashCode(){
271 return this.idx;
272 }
273 public int compareTo(Object o) {
274 IndexedTarget test = (IndexedTarget)o;
275 return idx - test.idx;
276 }
277 public String getTarget() {
278 return target;
279 }
280 public String getTargetType() {
281 return targetType;
282 }
283
284 }
285}
Note: See TracBrowser for help on using the repository browser.