View Javadoc

1   /***
2    * Copyright (C) 2006 Philipp Mpalampanis
3    *
4    * License: MPL 1.1/GPL 2.0/LGPL 2.1
5    *
6    * The contents of this file are subject to the Mozilla Public License Version
7    * 1.1 (the "License"); you may not use this file except in compliance with
8    * the License. You may obtain a copy of the License at
9    * http://www.mozilla.org/MPL/
10   *
11   * Software distributed under the License is distributed on an "AS IS" basis,
12   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13   * for the specific language governing rights and limitations under the
14   * License.
15   *
16   * Alternatively, the contents of this file may be used under the terms of
17   * either the GNU General Public License Version 2 or later (the "GPL"), or
18   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19   * in which case the provisions of the GPL or the LGPL are applicable instead
20   * of those above. If you wish to allow use of your version of this file only
21   * under the terms of either the GPL or the LGPL, and not to allow others to
22   * use your version of this file under the terms of the MPL, indicate your
23   * decision by deleting the provisions above and replace them with the notice
24   * and other provisions required by the GPL or the LGPL. If you do not delete
25   * the provisions above, a recipient may use your version of this file under
26   * the terms of any one of the MPL, the GPL or the LGPL.
27   */
28  
29  package net.sf.echobinding.controls;
30  
31  import java.beans.PropertyChangeEvent;
32  
33  import net.sf.echobinding.BoundControl;
34  import net.sf.echobinding.binding.BindingContext;
35  
36  /***
37   * @param <T> 
38   *
39   */
40  public class RadioButton <T> extends nextapp.echo2.app.RadioButton implements
41  		BoundControl {
42  
43  	private static final long serialVersionUID = -7529879447752346712L;
44  
45  	private String _id;
46  	private BindingContext _ctx;
47  	private Object _option;
48  	
49  	/***
50  	 * Creates a data bound radio button.
51  	 * 
52  	 * @param bindingId
53  	 * @param context
54  	 * @param text
55  	 */
56  	public RadioButton(String bindingId, BindingContext context, String text) {
57  		super(text);
58  		_id = bindingId;
59  		setBindingConext(context);
60  	}
61  
62  	/* (non-Javadoc)
63  	 * @see echobinding.BoundControl#setBindingConext(echobinding.BindingContext)
64  	 */
65  	public void setBindingConext(BindingContext context) {
66  		if(context == null)	return;
67  		if( _ctx != null) _ctx.removeControl(this);
68  		context.registerControl( _id, this);
69  		_ctx = context;
70  	}
71  
72  	/* (non-Javadoc)
73  	 * @see echobinding.BoundControl#setBindingId(java.lang.String)
74  	 */
75  	public void setAdapterId(String bindingId) {
76  		_id = bindingId;
77  	}
78  
79  	/* (non-Javadoc)
80  	 * @see echobinding.BoundControl#loadValues()
81  	 */
82  	public void update() {
83  		Object value = ((BoundButtonGroup)getGroup()).load();
84  		setSelected( getOptionValue().equals( value ) );
85  	}
86  
87  	/* (non-Javadoc)
88  	 * @see echobinding.BoundControl#saveValues()
89  	 */
90  	public void save() {
91  		if(isSelected())
92  			((BoundButtonGroup)getGroup()).update(getOptionValue());
93  	}
94  
95  	/* (non-Javadoc)
96  	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
97  	 */
98  	public void propertyChange(PropertyChangeEvent event) {
99  		update();	// reload values
100 	}
101 	
102 	/* (non-Javadoc)
103 	 * @see echobinding.BoundControl#isValid()
104 	 */
105 	public boolean isValid() {
106 		if(isSelected())
107 			return ((BoundButtonGroup)getGroup()).isDirty(getOptionValue());
108 		return false;
109 	}
110 
111 	
112 	/* (non-Javadoc)
113 	 * @see echobinding.BoundControl#validateInput()
114 	 */
115 	public void validateInput() {
116 		// do nada
117 	}
118 
119 	/* (non-Javadoc)
120 	 * @see echobinding.BoundControl#isDirty()
121 	 */
122 	public boolean isDirty() {
123 		Object value = ((BoundButtonGroup)getGroup()).load();
124 		boolean shouldBeSelected = getOptionValue().equals( value );
125 		return isSelected() != shouldBeSelected;
126 	}
127 
128 	/*** 
129 	 * Returns the value of the selected radio button within the button group.
130 	 *  
131 	 * @see net.sf.echobinding.BoundControl#getValue()
132 	 */
133 	public Object getValue() {
134 		return ((BoundButtonGroup)getGroup()).getValue();
135 	}
136 
137 	/* (non-Javadoc)
138 	 * @see echobinding.BoundControl#setValue(java.lang.Object)
139 	 */
140 	public void setValue(Object value) {
141 		((BoundButtonGroup)getGroup()).setValue( value );
142 	}
143 
144 	/***
145 	 * @return
146 	 */
147 	public Object getOptionValue() {
148 		if( _option == null )
149 			_option = _ctx.getValue(_id);
150 		return _option;
151 	}
152 
153 	
154 }