1 /*
2 Copyright (C) 2000 - 2007 Grid Systems, S.A.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /*
19 * Project: KernelConfigurator
20 * Created on 03-mar-2004
21 *
22 * Copyright (c)2003 Grid Systems
23 */
24 package com.gridsystems.config.tools.console;
25
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.ResourceBundle;
29
30 import com.gridsystems.config.ConsoleConfiguratorView;
31 import com.gridsystems.config.app.UI;
32 import com.gridsystems.config.tools.TextVerifier;
33
34 /**
35 * Text value edition field.
36 * <p>
37 * It can be optionally validated through a TextVerifier instance
38 *
39 * @author Rodrigo Ruiz
40 */
41 public class ValueField extends Field implements ConsoleViewAction {
42 /**
43 * Pattern to use for field display.
44 */
45 protected String linePattern;
46
47 /**
48 * Pattern to use for value display and parsing.
49 */
50 protected String valuePattern;
51
52 /**
53 * The value.
54 */
55 protected Object value;
56
57 /**
58 * The text verifier.
59 */
60 protected TextVerifier verifier;
61
62 /**
63 * A cached instance of the value formatter.
64 */
65 protected MessageFormat valueFormater = null;
66
67 /**
68 * If <code>true</code> text spaces will be trimmed.
69 */
70 protected boolean trimmed = true;
71
72 /**
73 * Actual key-mapping for this field.
74 */
75 private String keyMapping;
76
77 /**
78 * Creates a new instance.
79 *
80 * @param bundle The resource bundle
81 * @param linePattern The pattern for field display
82 * @param valuePattern The pattern for value display and parsing
83 */
84 public ValueField(ResourceBundle bundle, String linePattern, String valuePattern) {
85 super(bundle);
86 this.linePattern = linePattern;
87 this.valuePattern = valuePattern;
88
89 if (valuePattern != null) {
90 valueFormater = new MessageFormat(valuePattern);
91 }
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 public String getKeyMapping() {
98 if (keyMapping == null) {
99 keyMapping = getString(this.linePattern + ".keyMap");
100 }
101 return keyMapping;
102 }
103
104 /**
105 * Sets the key-mapping to use for this field.
106 *
107 * @param mapping The key-mapping to set
108 */
109 public void setKeyMapping(String mapping) {
110 this.keyMapping = mapping;
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public String[] getContents() {
117 String[] contents = new String[1];
118
119 // Constructs the actual line pattern
120 StringBuffer pattern = new StringBuffer();
121 String key = getKeyMapping();
122 if (key != null) {
123 pattern.append(" ").append(key).append(". ");
124 }
125
126 pattern.append(getString(linePattern));
127
128 String s = getStringValue();
129 contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
130 return contents;
131 }
132
133 /**
134 * Gets this field value.
135 *
136 * @return The value
137 */
138 public Object getValue() {
139 return value;
140 }
141
142 /**
143 * Sets the value of this field.
144 *
145 * @param value The new value
146 */
147 public void setValue(Object value) {
148 this.value = value;
149 }
150
151 /**
152 * Sets the verifier for this field.
153 *
154 * @param verifier The verifier
155 */
156 public void setVerifier(TextVerifier verifier) {
157 this.verifier = verifier;
158 }
159
160 /**
161 * Gets the current verifier for this field.
162 *
163 * @return The verifier
164 */
165 public TextVerifier getVerifier() {
166 return this.verifier;
167 }
168
169 /**
170 * Sets if the value should be trimmed on storing it.
171 *
172 * @param trimmed true if the value should be trimmed, false otherwise
173 */
174 public void setTrimmed(boolean trimmed) {
175 this.trimmed = trimmed;
176 }
177
178 /**
179 * Gets if the value should be trimmed.
180 *
181 * @return true if the value should be trimmed, false otherwise
182 */
183 public boolean isTrimmed() {
184 return this.trimmed;
185 }
186
187 /**
188 * Gets the field value as a string.
189 *
190 * @return A string containing this field value
191 */
192 protected String getStringValue() {
193 if (value == null) {
194 return "";
195 } else if (valueFormater == null) {
196 return value.toString();
197 } else {
198 return valueFormater.format(new Object[] { value });
199 }
200 }
201
202 /**
203 * Sets the field value from the specified string. If not null, the value pattern
204 * will be used for parsing the text
205 *
206 * @param newValue The text containing the new value
207 */
208 protected void setStringValue(String newValue) {
209 if (valueFormater == null) {
210 value = newValue;
211 } else {
212 try {
213 value = valueFormater.parse(newValue)[0];
214 } catch (Exception e) {
215 e.printStackTrace();
216 }
217 }
218 }
219
220 /**
221 * Asks the user for the new value for this field. The empty string means to
222 * maintain the current value.
223 * <p>
224 * To specify an empty value, in trimmed fields a blank space can be used
225 * <p>
226 * This method does not return until the default value or a valid one is
227 * specified.
228 *
229 * @param view The view where this field is located
230 * @return It always returns true, as the edition does not imply an
231 * exit from the view execution loop.
232 * @see com.gridsystems.config.tools.console.ConsoleViewAction
233 */
234 public boolean execute(ConsoleConfiguratorView view) {
235 while (true) {
236 try {
237 String line = readLine();
238 if (line.equals("")) {
239 return true;
240 }
241
242 // Trims the string if configured to do so
243 if (trimmed) {
244 line = line.trim();
245 }
246
247 // Verifies the new value before setting it
248 boolean valid = (verifier == null) ? true : verifier.verify(line);
249 if (valid) {
250 setStringValue(line);
251 return true;
252 }
253 } catch (Exception e) {
254 e.printStackTrace();
255 }
256 }
257 }
258
259 /**
260 * Reads a line of text from the console.
261 *
262 * @return The read text
263 * @throws IOException If an error occurs while reading from the console
264 */
265 protected String readLine() throws IOException {
266 if (value != null) {
267 String currentValuePattern =
268 UI.getString("console.readLine.currentValue");
269 String currentValue = MessageFormat.format(
270 currentValuePattern, new Object[]{getStringValue()});
271
272 System.out.println(currentValue);
273 }
274
275 String line =
276 ConsoleTools.readLine(UI.getString("console.readLine.newValue"));
277 return line;
278 }
279 }