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.datacontrol;
30  
31  import net.sf.echobinding.BoundControl;
32  import nextapp.echo2.app.Component;
33  
34  /***
35   * Utility class for validating and inspecting forms.
36   * 
37   * @deprecated
38   */
39  public class FormHelper {
40  
41  	/***
42  	 * Recursively loads the values of all widgets within this component and all
43  	 * its child components thus loading the values from the model into the
44  	 * widgets.
45  	 * 
46  	 * @param component
47  	 */
48  	public static void invokePropertyGetters(Component component) {
49  
50  		Component subComponent;
51  		for (int i = 0; i < component.getComponentCount(); i++) {
52  			subComponent = component.getComponent(i);
53  			if (subComponent instanceof BoundControl) {
54  				((BoundControl) subComponent).update();
55  			}
56  			invokePropertyGetters(subComponent);
57  		}
58  
59  	}
60  
61  	/***
62  	 * Recursively saves the values of all widgets within this component and all
63  	 * its child components thus writing the values from the widgets into the
64  	 * model.
65  	 * 
66  	 * @param component
67  	 */
68  	public static void invokePropertySetters(Component component) {
69  
70  		Component subComponent;
71  		for (int i = 0; i < component.getComponentCount(); i++) {
72  			subComponent = component.getComponent(i);
73  			if (subComponent instanceof BoundControl) {
74  				((BoundControl) subComponent).save();
75  			}
76  			invokePropertySetters(subComponent);
77  		}
78  
79  	}
80  
81  	/***
82  	 * Recursively validates the values of all widgets within this component and
83  	 * all its child components. Invalid inputs will be reported by the widget's 
84  	 * <code>ValidationHandler</code>.
85  	 * 
86  	 * @param component
87  	 */
88  	public static void invokeValidationHandlers(Component component) {
89  
90  		Component subComponent;
91  		for (int i = 0; i < component.getComponentCount(); i++) {
92  			subComponent = component.getComponent(i);
93  			if (subComponent instanceof BoundControl) {
94  				((BoundControl) subComponent).validateInput();
95  			}
96  			invokeValidationHandlers(subComponent);
97  		}
98  
99  	}
100 	
101 	/***
102 	 * Recursively checks the values of all widgets within this component and
103 	 * all its child components.
104 	 * 
105 	 * @param component
106 	 */
107 	public static boolean isValid(Component component) {
108 
109 		boolean isValid = true;
110 		Component subComponent;
111 		for (int i = 0; i < component.getComponentCount(); i++) {
112 			subComponent = component.getComponent(i);
113 			if (subComponent instanceof BoundControl) {
114 				isValid = ((BoundControl) subComponent).isValid();
115 			}
116 			isValid &= isValid(subComponent);
117 		}
118 		
119 		return isValid;
120 	}
121 
122 	/***
123 	 * Recursively tests if the values in the widgets have changed.
124 	 * 
125 	 * @param component
126 	 */
127 	public static boolean isDirty(Component component) {
128 		
129 		boolean isDirty = false;
130 		Component subComponent;
131 		for (int i = 0; i < component.getComponentCount(); i++) {
132 			subComponent = component.getComponent(i);
133 			if (subComponent instanceof BoundControl) {
134 				isDirty = ((BoundControl) subComponent).isValid();
135 			}
136 			isDirty &= isDirty(subComponent);
137 		}
138 		
139 		return isDirty;
140 	}
141 }