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  import net.sf.echobinding.validation.DefaultValidationHandler;
36  import net.sf.echobinding.validation.ValidationHandler;
37  import nextapp.echo2.app.ImageReference;
38  import nextapp.echo2.app.event.ActionEvent;
39  import nextapp.echo2.app.event.ActionListener;
40  
41  /***
42   * @author Philipp Mpalampanis 
43   *
44   */
45  public class CheckBox extends nextapp.echo2.app.CheckBox implements
46  		BoundControl, ActionListener {
47  
48  	private static final long serialVersionUID = 192196546013312251L;
49  
50  	/***
51  	 * 
52  	 */
53  	public CheckBox() {
54  		super();
55  
56  	}
57  
58  	/***
59  	 * @param text
60  	 */
61  	public CheckBox(String text) {
62  		super(text);
63  
64  	}
65  
66  	/***
67  	 * @param icon
68  	 */
69  	public CheckBox(ImageReference icon) {
70  		super(icon);
71  
72  	}
73  
74  	/***
75  	 * @param text
76  	 * @param icon
77  	 */
78  	public CheckBox(String text, ImageReference icon) {
79  		super(text, icon);
80  
81  	}
82  	
83  	private String _id;
84  	private BindingContext _ctx;
85  	private ValidationHandler _validationHandler;
86  	
87  	/***
88  	 * Creates a data bound check box.
89  	 * 
90  	 * @param adapterId
91  	 * @param context
92  	 */
93  	public CheckBox(String adapterId, BindingContext context ) {
94  		_id = adapterId;
95  		setBindingConext(context);
96  		addActionListener(this);
97  		setActionCommand( adapterId );
98  	}
99  
100 	/* (non-Javadoc)
101 	 * @see echobinding.BoundControl#setBindingConext(echobinding.BindingContext)
102 	 */
103 	public void setBindingConext(BindingContext context) {
104 		if(context == null)	return;
105 		if( _ctx != null) _ctx.removeControl(this);
106 		context.registerControl( _id, this);
107 		_ctx = context;
108 		update();
109 	}
110 
111 
112 	/* (non-Javadoc)
113 	 * @see echobinding.BoundControl#setBindingId(java.lang.String)
114 	 */
115 	public void setAdapterId(String bindingId) {
116 		_id = bindingId;
117 	}
118 
119 	/* (non-Javadoc)
120 	 * @see echobinding.BoundControl#loadValues()
121 	 */
122 	public void update() {
123 		Boolean checked = (Boolean) _ctx.getValue(_id);
124 		setSelected(checked);	
125 	}
126 
127 	/* (non-Javadoc)
128 	 * @see echobinding.BoundControl#saveValues()
129 	 */
130 	public void save() {
131 		if(!isDirty())
132 			return;
133 		validateInput();
134 		if(isValid())
135 			_ctx.setValue(_id, isSelected());
136 	}
137 
138 	/* (non-Javadoc)
139 	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
140 	 */
141 	public void propertyChange(PropertyChangeEvent event) {
142 		update();	// reload values
143 	}
144 	
145 	/* (non-Javadoc)
146 	 * @see echobinding.BoundControl#isValid()
147 	 */
148 	public boolean isValid() {
149 		return _ctx.validate(_id, isSelected()).isValid();
150 	}
151 
152 		
153 	/* (non-Javadoc)
154 	 * @see echobinding.BoundControl#validateInput()
155 	 */
156 	public void validateInput() {
157 		initValidationHandler();
158 		
159 		if(isValid())
160 			_validationHandler.markValid(this);
161 		else
162 			_validationHandler.markInvalid(this);
163 	}
164 
165 	private void initValidationHandler() {
166 		if( _validationHandler == null ) {
167 			_validationHandler = _ctx.getValidationHandler(_id);
168 			if( _validationHandler == null )
169 				_validationHandler = new DefaultValidationHandler(this);
170 		}
171 	}
172 
173 	/* (non-Javadoc)
174 	 * @see echobinding.BoundControl#isDirty()
175 	 */
176 	public boolean isDirty() {
177 		return _ctx.isDirty(_id, isSelected() );
178 	}
179 
180 	/* (non-Javadoc)
181 	 * @see echobinding.BoundControl#getValue()
182 	 */
183 	public Object getValue() {
184 		return new Boolean(isSelected());
185 	}
186 
187 	/* (non-Javadoc)
188 	 * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
189 	 */
190 	public void actionPerformed(ActionEvent arg0) {
191 		firePropertyChange( ACTION_LISTENERS_CHANGED_PROPERTY, !isSelected(), isSelected() );
192 	}
193 
194 	/* (non-Javadoc)
195 	 * @see echobinding.BoundControl#setValue(java.lang.Object)
196 	 */
197 	public void setValue(Object value) {
198 		if(value==null) return;
199 		setSelected( (Boolean) value );
200 	}
201 	
202 }