1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SRadioButton.java,v 1.9 2002/09/13 17:04:41 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.beans.Beans;
44 import javax.swing.JRadioButton;
45 import javax.swing.JToolTip;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48 import org.scopemvc.core.Control;
49 import org.scopemvc.core.Controller;
50 import org.scopemvc.core.PropertyView;
51 import org.scopemvc.core.Selector;
52 import org.scopemvc.view.util.ModelBindable;
53
54 /***
55 * <P>
56 *
57 * A JRadioButton linked to a Boolean or boolean property of a bound model
58 * object. Updates to the radiobutton result in changes to the model property
59 * immediately. </P> <P>
60 *
61 * Note that the Selector specified for a SRadioButton must select a single
62 * Boolean or boolean property. </P> <P>
63 *
64 * SRadioButton responds to the bound model or the particular bound property
65 * becoming read-only by disabling itself. A SRadioButton is also disabled if it
66 * has no bound model or property, or the property is a null Boolean. </P> <P>
67 *
68 * SRadioButton can issue a Control when it changes state. </P>
69 *
70 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
71 * @created 05 September 2002
72 * @version $Revision: 1.9 $ $Date: 2002/09/13 17:04:41 $
73 */
74 public class SRadioButton extends JRadioButton implements PropertyView, ItemListener, ModelBindable, Refreshable {
75
76 private static final Log LOG = LogFactory.getLog(SCheckBox.class);
77
78 /***
79 * Helper to manage model to view binding.
80 */
81 private SwingBoundModel boundModel = new SwingBoundModel(this);
82
83 /***
84 * Helper to manage validation state.
85 */
86 private ValidationHelper validationHelper = new ValidationHelper(this);
87
88 /***
89 * ID of the Control issued when checkbox changes state.
90 */
91 private String controlID;
92
93 /***
94 * SCheckbox can "hold" a null when bound to a Boolean property that happens
95 * to be null.
96 */
97 private boolean valueIsNull = false;
98
99
100 /***
101 * Constructor for the SRadioButton object
102 */
103 public SRadioButton() {
104 setEnabled(Beans.isDesignTime());
105 addItemListener(this);
106 }
107
108
109 // ------------------- Delegate to BoundModel -------------------
110
111 /***
112 * Gets the bound model
113 *
114 * @return The boundModel value
115 */
116 public final Object getBoundModel() {
117 return boundModel.getBoundModel();
118 }
119
120
121 /***
122 * Gets the selector
123 *
124 * @return The selector value
125 */
126 public final Selector getSelector() {
127 return boundModel.getSelector();
128 }
129
130
131 // ---------- Issue a Control on change of state --------------
132
133 /***
134 * Gets the control ID
135 *
136 * @return The controlID value
137 */
138 public final String getControlID() {
139 return controlID;
140 }
141
142
143 /***
144 * Get the current value (what would be set as a property of the bound model
145 * object) being presented on the View.
146 *
147 * @return a Boolean or null when (! isEnabled())
148 */
149 public Object getViewValue() {
150 /*
151 * throws IllegalArgumentException
152 */
153 if (valueIsNull) {
154 return null;
155 }
156
157 if (isSelected()) {
158 return Boolean.TRUE;
159 } else {
160 return Boolean.FALSE;
161 }
162 }
163
164
165 /***
166 * Don't assign a Controller to SRadioButton, instead delegate to the
167 * containing SwingView that has a parent Controller.
168 *
169 * @return The controller value
170 */
171 public Controller getController() {
172 return null;
173 }
174
175
176 /***
177 * Don't assign a Controller to SRadioButton, instead delegate to the
178 * containing SwingView that has a parent Controller.
179 *
180 * @param inControl TODO: Describe the Parameter
181 */
182 public void issueControl(Control inControl) {
183 SwingUtil.issueControl(this, inControl);
184 }
185
186
187 /***
188 * Sets the bound model
189 *
190 * @param inModel The new boundModel value
191 */
192 public final void setBoundModel(Object inModel) {
193 boundModel.setBoundModel(inModel);
194 }
195
196
197 /***
198 * Sets the selector
199 *
200 * @param inSelector The new selector value
201 */
202 public final void setSelector(Selector inSelector) {
203 boundModel.setSelector(inSelector);
204 }
205
206
207 /***
208 * Sets the selector string
209 *
210 * @param inSelectorString The new selectorString value
211 */
212 public final void setSelectorString(String inSelectorString) {
213 boundModel.setSelectorString(inSelectorString);
214 }
215
216 /***
217 * Set the ID of the Control that will be issued when state changes. If null
218 * no Control will be issued.
219 *
220 * @param inControlID The new controlID value
221 */
222 public final void setControlID(String inControlID) {
223 controlID = inControlID;
224 }
225
226
227 /***
228 * Sets the selected
229 *
230 * @param b The new selected value
231 */
232 public void setSelected(boolean b) {
233 if (b && !isSelected()) {
234 super.setSelected(true);
235 } else if (!b && isSelected()) {
236 super.setSelected(false);
237 }
238 }
239
240 /***
241 * Don't assign a Controller to this component, instead delegate to the
242 * containing SwingView that has a parent Controller.
243 *
244 * @param inController The new controller value
245 */
246 public void setController(Controller inController) {
247 throw new UnsupportedOperationException("Can't assign a Controller to a " + getClass());
248 }
249
250 // --------------------- Implement ModelBindable ----------------------
251
252 /***
253 * Incoming value is a Boolean or null.
254 *
255 * @param inValue TODO: Describe the Parameter
256 * @param inReadOnly TODO: Describe the Parameter
257 */
258 public void updateFromProperty(Object inValue, boolean inReadOnly) {
259 if (LOG.isDebugEnabled()) {
260 LOG.debug("updateFromProperty: " + inValue + ", " + inReadOnly);
261 }
262
263 if (inValue == null || !(inValue instanceof Boolean)) {
264 valueIsNull = true;
265 setEnabled(false);
266 // setSelected(false);
267 return;
268 }
269
270 valueIsNull = false;
271 setEnabled(!inReadOnly);
272 if (((Boolean) inValue).booleanValue()) {
273 setSelected(true);
274 } else {
275 setSelected(false);
276 }
277 }
278
279
280 /***
281 * TODO: document the method
282 *
283 * @param inException TODO: Describe the Parameter
284 */
285 public void validationFailed(Exception inException) {
286 validationHelper.validationFailed(inException);
287 }
288
289
290 /***
291 * TODO: document the method
292 */
293 public void validationSuccess() {
294 validationHelper.validationSuccess();
295 }
296
297
298 /***
299 * TODO: document the method
300 *
301 * @return TODO: Describe the Return Value
302 */
303 public JToolTip createToolTip() {
304 return validationHelper.createToolTip(super.createToolTip());
305 }
306
307
308 // ---------------------- View to model ----------------------
309
310 /***
311 * TODO: document the method
312 *
313 * @param inEvent TODO: Describe the Parameter
314 */
315 public void itemStateChanged(ItemEvent inEvent) {
316 valueIsNull = false;
317 if (isEnabled()) {
318 boundModel.updateModel();
319 }
320 if (controlID != null) {
321 Control control = new Control(controlID);
322 issueControl(control);
323 }
324 }
325
326
327 // ------------------ Refreshable -------------------------
328
329 /***
330 * TODO: document the method
331 */
332 public void refresh() {
333 Object propertyValue = boundModel.getPropertyValue();
334 boolean propertyReadOnly = boundModel.getPropertyReadOnly();
335 updateFromProperty(propertyValue, propertyReadOnly);
336 }
337 }
This page was automatically generated by Maven