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  /*
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  
33  /**
34   * Constant text field.
35   * <p>
36   *
37   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
38   * @version 1.0
39   */
40  public class ConstantField extends Field implements ConsoleViewAction {
41    /**
42     * Pattern to use for field display.
43     */
44    protected String linePattern;
45  
46    /**
47     * The value.
48     */
49    protected String value;
50  
51    /**
52     * Creates a new instance.
53     *
54     * @param bundle       The resource bundle
55     * @param linePattern  The pattern for field display
56     * @param value        The value to display
57     */
58    public ConstantField(ResourceBundle bundle, String linePattern, String value) {
59      super(bundle);
60      this.linePattern = linePattern;
61      this.value = value;
62    }
63  
64    /**
65     * {@inheritDoc}
66     */
67    public String getKeyMapping() {
68      return getString(this.linePattern + ".keyMap");
69    }
70  
71    /**
72     * {@inheritDoc}
73     */
74    public String[] getContents() {
75      String[] contents = new String[1];
76  
77      // Constructs the actual line pattern
78      StringBuffer pattern = new StringBuffer();
79      String key = getKeyMapping();
80      if (key != null) {
81        pattern.append(" ").append(key).append(". ");
82      }
83  
84      pattern.append(getString(linePattern));
85  
86      String s = getStringValue();
87      contents[0] = MessageFormat.format(pattern.toString(), new Object[] { s });
88      return contents;
89    }
90  
91    /**
92     * Gets this field value.
93     *
94     * @return The value
95     */
96    public Object getValue() {
97      return value;
98    }
99  
100   /**
101    * Sets the value of this field.
102    *
103    * @param value The new value
104    */
105   public void setValue(String value) {
106     this.value = value;
107   }
108 
109 
110   /**
111    * Gets the field value as a string.
112    *
113    * @return A string containing this field value
114    */
115   protected String getStringValue() {
116     if (value == null) {
117       return "";
118     } else {
119       return value;
120     }
121   }
122 
123   /**
124    * Asks the user for the new value for this field. The empty string means to
125    * maintain the current value.
126    * <p>
127    * To specify an empty value, in trimmed fields a blank space can be used
128    * <p>
129    * This method does not return until the default value or a valid one is
130    * specified.
131    *
132    * @param view The view where this field is located
133    * @return     It always returns true, as the edition does not imply an
134    *             exit from the view execution loop.
135    * @see com.gridsystems.config.tools.console.ConsoleViewAction
136    */
137   public boolean execute(ConsoleConfiguratorView view) {
138     try {
139       String line = readLine();
140       if (line.equals("")) {
141         return true;
142       }
143     } catch (Exception e) {
144       e.printStackTrace();
145     }
146     return true;
147   }
148 
149   /**
150    * Reads a line of text from the console.
151    *
152    * @return The read text
153    * @throws IOException If an error occurs while reading from the console
154    */
155   protected String readLine() throws IOException {
156     String line =
157       ConsoleTools.readLine(UI.getString("console.readLine.constantValue"));
158     return line;
159   }
160 }