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 net.sf.echobinding.binding.BindingContext;
32  
33  /***
34   * @param <T> 
35   *
36   */
37  public class BoundButtonGroup<T> extends nextapp.echo2.app.button.ButtonGroup {
38  
39  	private static final long serialVersionUID = -6501961132887549165L;
40  
41  	private String _adapterId;
42  	private BindingContext _ctx;
43  	
44  	/***
45  	 * Creates a bound button group. 
46  	 * 
47  	 * @param adapterId
48  	 * @param context
49  	 */
50  	public BoundButtonGroup(String adapterId, BindingContext context) {
51  		super();
52  		_adapterId = adapterId;
53  		_ctx = context;
54  	}
55  	
56  	/***
57  	 * Creates a bound radio button and adds it to this group.
58  	 * 
59  	 * @param adapterId The adapter id for the radio button option
60  	 * @param label The label of the radio button
61  	 * @return
62  	 */
63  	public RadioButton<T> createButton(String adapterId, String label) {
64  		
65  		RadioButton<T> button = new RadioButton<T>(adapterId, _ctx, label);
66  		addButton( button );
67  		button.setGroup( this );
68  		button.update();
69  		return button;
70  	}
71  
72  	/***
73  	 * Returns the value saved in the model.
74  	 * 
75  	 * @return
76  	 */
77  	public Object load() {
78  		return _ctx.getValue(_adapterId);
79  	}
80  	
81  	/***
82  	 * Update the model's value with the value of the selected button.
83  	 * 
84  	 * @param value
85  	 */
86  	public void update(Object value) {
87  		_ctx.setValue(_adapterId, value);
88  	}
89  
90  	/***
91  	 * Checks if the selected value is the sames as the value in the model.
92  	 * 
93  	 * @param value
94  	 * @return
95  	 */
96  	public boolean isDirty(Object value) {
97  		return _ctx.isDirty(_adapterId, value);
98  	}
99  
100 	/***
101 	 * Returns the value of the selected button.
102 	 * 
103 	 * @return the value of the selected button
104 	 */
105 	public Object getValue() {
106 		for(Object rButton : getButtons() ) {
107 			RadioButton button = (RadioButton) rButton;
108 			if(button.isSelected()) 
109 				return button.getOptionValue();
110 		}
111 		return null;
112 	}
113 	
114 	/***
115 	 * Selects the button whose option equals the given value. 
116 	 * @param value
117 	 */
118 	public void setValue(Object value) {
119 		if(value == null) return;
120 		for(Object rButton : getButtons() ) {
121 			RadioButton button = (RadioButton) rButton;
122 			button.setSelected(button.getOptionValue().equals( value )); 
123 		}
124 	}
125 }