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.util.ResourceBundle;
27  
28  import com.gridsystems.config.ConsoleConfiguratorView;
29  
30  /**
31   * Specialized field for boolean values. Its execute method switches its value
32   * without extra edition.
33   *
34   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
35   * @version 1.0
36   */
37  public class BoolField extends ValueField {
38  
39    /** String representation of true value. */
40    public static final String TRUE = "X";
41  
42    /** String representation of true value. */
43    public static final String FALSE = " ";
44  
45    /**
46     * Creates a new instance with the specified bundle and pattern.
47     *
48     * @param bundle      The resource bundle
49     * @param linePattern The pattern for field display
50     */
51    public BoolField(ResourceBundle bundle, String linePattern) {
52      super(bundle, linePattern, null);
53    }
54  
55    /**
56     * {@inheritDoc}
57     */
58    public void setValue(Object value) {
59      if (value == null) {
60        this.value = Boolean.FALSE;
61      } else {
62        boolean b = (value instanceof Boolean) ? ((Boolean)value).booleanValue()
63                                               : "true".equalsIgnoreCase(value.toString());
64  
65        this.value = b ? Boolean.TRUE : Boolean.FALSE;
66      }
67    }
68  
69    /**
70     * Sets the value of this field.
71     *
72     * @param b The new value
73     */
74    public void setValue(boolean b) {
75      this.value = b ? Boolean.TRUE : Boolean.FALSE;
76    }
77  
78    /**
79     * {@inheritDoc}
80     */
81    public String getStringValue() {
82      return getBooleanValue() ? TRUE : FALSE;
83    }
84  
85    /**
86     * Obtains boolean value of this Field.
87     * @return boolean value of this field.
88     */
89    public boolean getBooleanValue() {
90      //  null is equivalent to false
91      if (value == null) {
92        return false;
93      } else {
94        return ((Boolean)value).booleanValue();
95      }
96    }
97  
98  
99    /**
100    * Switches the boolean value to its negative.
101    *{@inheritDoc}
102    */
103   public boolean execute(ConsoleConfiguratorView view) {
104     if (value == null) {
105       value = Boolean.TRUE;
106     } else {
107       Boolean b = (Boolean)value;
108       value = b.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
109     }
110     return true;
111   }
112 }