source: other-projects/trunk/realistic-books/packages/AntInstaller/src/org/tp23/antinstaller/input/OutputField.java@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 4.3 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.input;
17
18import java.util.MissingResourceException;
19import java.util.ResourceBundle;
20
21import org.tp23.antinstaller.InstallerContext;
22import org.tp23.antinstaller.ValidationException;
23/**
24 * This is the super class of all "Input types". It is called OutputField since
25 * it handles the base features of "Input types" for outputing text for
26 * the user to read. It also encapsulates some convenience methods for
27 * interpreting boolean values from the command line and in configuration files.
28 */
29public abstract class OutputField {
30
31 // i18n support
32 private static ResourceBundle langPack = null;
33 private static int commentIdx = 0;
34 static{
35 try {
36 langPack = ResourceBundle.getBundle("resources.LanguagePack");
37 } catch (MissingResourceException e) {
38 // ignore, signifies no lang packs installed
39 }
40 }
41
42 /* This is redundant unless language packs are used
43 */
44 private String name = "comment." + ++commentIdx;
45
46 protected String displayText;
47 protected String explanatoryText;
48 protected ResultContainer resultContainer;
49
50 public OutputField() {
51
52 }
53
54 /* This is redundant unless language packs are used
55 */
56 public String getName() {
57 return name;
58 }
59
60 /* This is redundant unless language packs are used
61 */
62 public void setName(String name) {
63 this.name = name;
64 }
65
66 public String getDisplayText() {
67 if(langPack != null){
68 return langPack.getString(getName() + ".displayText");
69 }
70 return displayText;
71 }
72
73 public void setDisplayText(String displayText) {
74 this.displayText = displayText;
75 }
76
77 public String getExplanatoryText() {
78 if(langPack != null){
79 try {
80 return langPack.getString(getName() + ".explanatoryText");
81 } catch (MissingResourceException e) {
82 // ignore and return null explanatoryText is optional
83 }
84 }
85 return explanatoryText;
86 }
87
88 public void setExplanatoryText(String explanatoryText) {
89 this.explanatoryText = explanatoryText;
90 }
91
92 public void setResultContainer(ResultContainer resultContainer) {
93 this.resultContainer = resultContainer;
94 }
95
96 /**
97 * Validate the user input (or lack of it)
98 * This method should return false if the validation fails an throw an exception
99 * if it is not possible to validate or there is an error.
100 *
101 * @param cxt InstallerContext
102 * @throws ValidationException thrown in error conditions not validation failure
103 * @return boolean
104 */
105 public abstract boolean validate(InstallerContext cxt) throws ValidationException;
106
107 /**
108 * Used to validate the configuration, this can be run prior to distributing the
109 * installer to check that the config is valid. Will not be used at runtime.
110 * @throws ValidationException
111 * @return boolean
112 */
113 public abstract boolean validateObject();
114
115 //////////////////////Static convenience methods
116
117 /** true if specified and true or yes.
118 * N.B it is possible for X, isTrue(X) == isFalse(X);
119 * This occurs if the value is null.
120 */
121 public static boolean isTrue(String value){
122 if(value == null)return false;
123 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
124 }
125 /** same as isTrue() but default is false if not specified */
126 public static boolean isFalse(String value){
127 if(value == null)return false;
128 return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no");
129 }
130 /**
131 * Return true if the value is set to true or false, returns false if the value is null
132 * @param value String
133 * @return boolean
134 */
135 public static boolean requiredBoolean(String value){
136 return isTrue(value) || isFalse(value);
137 }
138 /**
139 * Return true if the value is set to true or false, returns false if the value is null
140 * @param value String
141 * @return boolean
142 */
143 public static boolean optionalBoolean(String value){
144 return value == null || isTrue(value) || isFalse(value);
145 }
146}
Note: See TracBrowser for help on using the repository browser.