View Javadoc

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  package com.gridsystems.config.tools.console;
19  
20  import java.io.IOException;
21  import java.text.MessageFormat;
22  import java.util.ResourceBundle;
23  
24  import com.gridsystems.config.ConsoleConfiguratorView;
25  import com.gridsystems.config.app.UI;
26  
27  /**
28   * Asks the user to chose between a list of valid values.
29   *
30   * @author SJM.
31   */
32  public class SelectionField extends Field implements ConsoleViewAction {
33  
34    /**
35     * Pattern to use for field display.
36     */
37    protected String linePattern;
38  
39    /**
40     * Pattern to use for value display and parsing.
41     */
42    protected String valuePattern;
43  
44    /**
45     * The value.
46     */
47    protected Object value;
48  
49    /**
50     * A cached instance of the value formatter.
51     */
52    protected MessageFormat valueFormatter = null;
53  
54    /**
55     * The list of allowed values.
56     */
57    private String[] values = null;
58  
59    /**
60     * Creates a new instance.
61     *
62     * @param bundle       The resource bundle
63     * @param linePattern  The pattern for field display
64     * @param valuePattern The pattern for value display and parsing
65     * @param values a String[] with the list of allowed values.
66     */
67    public SelectionField(ResourceBundle bundle, String linePattern,
68      String valuePattern, String[] values) {
69      super(bundle);
70      this.linePattern = linePattern;
71      this.valuePattern = valuePattern;
72  
73      if (valuePattern != null) {
74        valueFormatter = new MessageFormat(valuePattern);
75      }
76      if (values != null) {
77        this.values = values;
78      } else {
79        this.values = new String[0];
80      }
81    }
82  
83    /**
84     * Asks the user for the new value for this field. The empty string means to
85     * maintain the current value.
86     * <p>
87     * To specify an empty value, in trimmed fields a blank space can be used
88     * <p>
89     * This method does not return until the default value or a valid one is
90     * specified.
91     *
92     * @param view The view where this field is located
93     * @return     It always returns true, as the edition does not imply an
94     *             exit from the view execution loop.
95     * @see com.gridsystems.config.tools.console.ConsoleViewAction
96     */
97    public boolean execute(ConsoleConfiguratorView view) {
98      while (true) {
99        try {
100         String line = readLine();
101         if (line.equals("")) {
102           return true;
103         }
104 
105         line = line.trim();
106         if (line.length() == 1) {
107           int index = line.charAt(0) - 'a';
108           if ((index >= 0) && (index < this.values.length)) {
109             setStringValue(this.values[index]);
110             return true;
111           }
112         }
113       } catch (Exception e) {
114         e.printStackTrace();
115       }
116     }
117   }
118 
119   /**
120    * Reads a line of text from the console.
121    *
122    * @return The read text
123    * @throws IOException If an error occurs while reading from the console
124    */
125   protected String readLine() throws IOException {
126     if (value != null) {
127       String currentValuePattern =
128         UI.getString("console.readLine.currentValue");
129       String currentValue = MessageFormat.format(
130         currentValuePattern, new Object[]{getStringValue()});
131 
132       System.out.println(currentValue);
133     }
134 
135     char index = 'a';
136     for (int i = 0; i < this.values.length; i++) {
137       System.out.println(index + ") " + this.values[i]);
138       index++;
139     }
140 
141     String line =
142       ConsoleTools.readLine(UI.getString("console.readLine.newValue"));
143     return line;
144   }
145 
146   /**
147    * {@inheritDoc}
148    */
149   public String getKeyMapping() {
150     return getString(this.linePattern + ".keyMap");
151   }
152 
153   /**
154    * {@inheritDoc}
155    */
156   public String[] getContents() {
157     String[] contents = new String[1];
158 
159     // Constructs the actual line pattern
160     StringBuffer pattern = new StringBuffer();
161     String key = getKeyMapping();
162     if (key != null) {
163       pattern.append(" ").append(key).append(". ");
164     }
165 
166     pattern.append(getString(linePattern));
167 
168     String s = getStringValue();
169     contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
170     return contents;
171   }
172 
173   /**
174    * Gets this field value.
175    *
176    * @return The value
177    */
178   public Object getValue() {
179     return value;
180   }
181 
182   /**
183    * Sets the value of this field.
184    *
185    * @param value The new value
186    */
187   public void setValue(Object value) {
188     this.value = value;
189   }
190 
191   /**
192    * Gets the field value as a string.
193    *
194    * @return A string containing this field value
195    */
196   protected String getStringValue() {
197     if (value == null) {
198       return "";
199     } else if (valueFormatter == null) {
200       return value.toString();
201     } else {
202       return valueFormatter.format(new Object[] { value });
203     }
204   }
205 
206   /**
207    * Sets the field value from the specified string. If not null, the value pattern
208    * will be used for parsing the text
209    *
210    * @param newValue The text containing the new value
211    */
212   protected void setStringValue(String newValue) {
213     if (valueFormatter == null) {
214       value = newValue;
215     } else {
216       try {
217         value = valueFormatter.parse(newValue)[0];
218       } catch (Exception e) {
219         e.printStackTrace();
220       }
221     }
222   }
223 }