View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.property.editors;
21  
22  import java.awt.event.ItemEvent;
23  import java.awt.event.ItemListener;
24  
25  import javax.swing.JCheckBox;
26  
27  
28  
29  /**
30   * JBooleanCheckBox is a component which serves as an editor for boolean
31   * values. It displays a checkBox and a string representation of the value,
32   * i.e. either "true" or "false". It can also display an arbitrary user text
33   * which labels the checkBox.
34   *
35   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
36   * @version $id$
37   */
38  public class BooleanEditor extends JCheckBox implements PropertyEditor
39  {
40  	private String description=null;
41  	/**
42  	 * Constructor for JBooleanCheckBox. Constructs a check box with default
43  	 * value set to false.
44  	 */
45  	public BooleanEditor()
46  	{
47  		this(false);
48  	}
49  
50  	/**
51  	 * Constructor for JBooleanCheckBox. Constructs a check box with default
52  	 * value set to <code>value</code>.
53  	 *
54  	 * @param value the value to set.
55  	 */
56  	public BooleanEditor(boolean value)
57  	{
58  		super();
59  		setSelected(value);
60  		super.setText(Boolean.toString(value));
61  		init();
62  	}
63  
64  	/**
65  	 * Constructor for JBooleanCheckBox. Constructs a check box with default
66  	 * value set to <code>value</code>.
67  	 *
68  	 * @param description desription of edited value
69  	 * @param value the value to set.
70  	 */
71  	public BooleanEditor(String description, boolean value)
72  	{
73  		this(value);
74  		setDescription(description);
75  	}
76  
77  	/**
78  	 * returns a <code>Boolean</code> object with the value set. To retrieve a
79  	 * primitive <code>boolean</code>, use isSelected() method
80  	 *
81  	 * @return Object a Boolean containing the selected value.
82  	 */
83  	public Object getPropertyValue()
84  	{
85  		return new Boolean(isSelected());
86  	}
87  
88  	/**
89  	 * If and only if the text is "true" this checkbox is selected.
90  	 *
91  	 * @param text the text to be set as boolean
92  	 *
93  	 * @see Boolean#getBoolean(java.lang.String)
94  	 * @see #setValue(boolean)
95  	 */
96  	public void setText(String text)
97  	{
98  		if ("true".equalsIgnoreCase(text)) {
99  			setSelected(true);
100 		} else if ("false".equalsIgnoreCase(text)) {
101 			setSelected(false);
102 		}
103 		super.setText(text);
104 	}
105 
106 	/**
107 	 * DOCUMENT ME!
108 	 *
109 	 * @param sel the value to be set
110 	 *
111 	 * @see setValue
112 	 */
113 	public void setSelected(boolean sel)
114 	{
115 		boolean oldValue = isSelected();
116 
117 		if (oldValue != sel) {
118 			super.setSelected(sel);
119 			//super.setText(Boolean.toString(sel));
120 			firePropertyChange(PROPERTY_VALUE_NAME, oldValue, sel);
121 			firePropertyChange("value", oldValue, sel);
122 		}
123 	}
124 
125 
126 	private void init()
127 	{
128 		addItemListener(new ItemListener() {
129 			public void itemStateChanged(ItemEvent e) {
130 				if (description==null) {
131 					BooleanEditor.super.setText(Boolean.toString(isSelected()));
132 				}
133 				firePropertyChange(PROPERTY_VALUE_NAME,!isSelected(),isSelected());
134 			}
135 		});
136 	}
137 
138 	/**
139 	 * Sets the displayed value. Should be java.lang.Boolean
140 	 *
141 	 * @param value the value to be set.
142 	 *
143 	 * @return DOCUMENT ME!
144 	 *
145 	 * @see ValueEditor#setValue(Object)
146 	 */
147 	public boolean setPropertyValue(Object value)
148 	{
149 		if (value == null) {
150 			setSelected(false);
151 		} else if (value instanceof Boolean) {
152 			setSelected(((Boolean)value).booleanValue());
153 
154 			return true;
155 		}
156 
157 		return false;
158 	}
159 
160 	/**
161 	 * @see com.cosylab.gui.property.editors.PropertyEditor#getDescription()
162 	 */
163 	public String getDescription()
164 	{
165 		return description;
166 	}
167 
168 	/**
169 	 * @see com.cosylab.gui.property.editors.PropertyEditor#getEditor()
170 	 */
171 	public PropertyEditor getEditor()
172 	{
173 		return this;
174 	}
175 
176 	/**
177 	 * @see com.cosylab.gui.property.editors.PropertyEditor#setDescription(String)
178 	 */
179 	public void setDescription(String description)
180 	{
181 		this.description= description; 
182 		setText(description);
183 	}
184 }
185 
186 /* __oOo__ */