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.util.Util;
36  import net.sf.echobinding.validation.*;
37  
38  /***
39   *
40   */
41  public class TextField extends nextapp.echo2.app.TextField implements BoundControl {
42  
43  	private static final long serialVersionUID = -3569388161415086297L;
44  
45  	private String _id;
46  	private BindingContext _ctx;
47  	private ValidationHandler _validationHandler;
48  	
49  	/***
50  	 * creates a data bound text field
51  	 * 
52  	 * @param adapterId
53  	 * @param context
54  	 */
55  	public TextField(String adapterId, BindingContext context) {
56  		super();
57  		_id = adapterId;
58  		setActionCommand( adapterId );
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  		update();
71  	}
72  
73  
74  	/* (non-Javadoc)
75  	 * @see echobinding.BoundControl#setBindingId(java.lang.String)
76  	 */
77  	public void setAdapterId(String id) {
78  		_id = id;
79  	}
80  
81  	/* (non-Javadoc)
82  	 * @see echobinding.BoundControl#saveValues()
83  	 */
84  	public void save() {
85  		if(!isDirty())
86  			return;
87  		validateInput();
88  		if(isValid()) 
89  			_ctx.setValue(_id, getText());
90  	}
91  
92  	/* (non-Javadoc)
93  	 * @see echobinding.BoundControl#loadValues()
94  	 */
95  	public void update() {
96  		assert(_ctx != null);
97  		Object value = _ctx.getValue(_id);
98  		if( getText().equals( value ))
99  			return;
100 		setText(Util.valueToString(value));
101 	}
102 	
103 	/* (non-Javadoc)
104 	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
105 	 */
106 	public void propertyChange(PropertyChangeEvent event) {
107 		update();	// reload values
108 	}
109 	
110 	/* (non-Javadoc)
111 	 * @see echobinding.BoundControl#isValid()
112 	 */
113 	public boolean isValid() {
114 		ValidationReport validationReport = _ctx.validate(_id, Util.stringToValue( getText() ));
115 		// FIXME: find a solution for reporting validation errors
116 		setToolTipText(validationReport.getMessage()); 
117 		return validationReport.isValid();
118 	}
119 
120 	private void initValidationHandler() {
121 		if( _validationHandler == null ) {
122 			_validationHandler = _ctx.getValidationHandler(_id);
123 			if( _validationHandler == null )
124 				_validationHandler = new DefaultValidationHandler(this);
125 		}
126 	}
127 
128 	/* (non-Javadoc)
129 	 * @see echobinding.BoundControl#validateInput()
130 	 */
131 	public void validateInput() {
132 		initValidationHandler();
133 		if(isValid())
134 			_validationHandler.markValid(this);
135 		else
136 			_validationHandler.markInvalid(this);
137 	}
138 
139 	/* (non-Javadoc)
140 	 * @see echobinding.BoundControl#isDirty()
141 	 */
142 	public boolean isDirty() {
143 		return _ctx.isDirty(_id, Util.stringToValue( getText() ));
144 	}
145 
146 	/* (non-Javadoc)
147 	 * @see echobinding.BoundControl#getValue()
148 	 */
149 	public Object getValue() {
150 		return getDocument().getText();
151 	}
152 
153 	/* (non-Javadoc)
154 	 * @see echobinding.BoundControl#setValue(java.lang.Object)
155 	 */
156 	public void setValue(Object value) {
157 		setText(Util.valueToString(value));
158 	}
159 	
160 }