View Javadoc

1   package com.silvermindsoftware.hitch.config;
2   
3   /**
4    * Copyright 2007 Brandon Goodin
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import com.silvermindsoftware.hitch.Binder;
20  import com.silvermindsoftware.hitch.BinderManager;
21  import com.silvermindsoftware.hitch.ReadOnly;
22  import com.silvermindsoftware.hitch.meta.FormMeta;
23  import com.silvermindsoftware.hitch.reflect.ClassManager;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import java.lang.reflect.Field;
28  import java.lang.reflect.Method;
29  import java.util.Arrays;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/com/silvermindsoftware/hitch/config/BinderConfig.java $
35   */
36  public class BinderConfig {
37  
38  	private static final Logger log = LoggerFactory.getLogger(BinderConfig.class);
39  	private FormMeta formMeta;
40  	private FormConfig formConfig;
41  
42  	public BinderConfig(Class componentClass, Binder binder) {
43  		this(componentClass, binder, new FormConfig());
44  	}
45  
46  	public BinderConfig(Class componentClass, Binder binder, FormConfig formConfig) {
47  		this.formMeta = BinderManager.getFormMetaData(componentClass);
48  		this.formConfig = formConfig;
49  	}
50  
51  	public BinderConfig bindModel(ModelObjectConfig modelObjectConfig) {
52  
53  		bindCompleteModel(
54  				modelObjectConfig.getModelField(),
55  				modelObjectConfig.getModelField(),
56  				modelObjectConfig.isDefault(),
57  				modelObjectConfig.isAutoBind(),
58  				modelObjectConfig.getIgnoreFields());
59  
60  		return this;
61  	}
62  
63  	/**
64  	 * @param modelField
65  	 * @param ignoreFields
66  	 */
67  	public BinderConfig bindDefaultModel(String modelField, String... ignoreFields) {
68  		return bindCompleteModel("[default]", modelField, true, true, ignoreFields);
69  	}
70  
71  
72  	/**
73  	 * @param modelField
74  	 * @param ignoreFields
75  	 */
76  	public BinderConfig bindModel(String modelField, String... ignoreFields) {
77  		return bindCompleteModel(modelField, modelField, false, true, ignoreFields);
78  	}
79  
80  	/**
81  	 * @param modelField
82  	 */
83  	public BinderConfig bindDefaultModel(String modelField) {
84  		return bindCompleteModel("[default]", modelField, true, true);
85  	}
86  
87  
88  	/**
89  	 * @param modelField
90  	 */
91  	public BinderConfig bindModel(String modelField) {
92  		return bindCompleteModel(modelField, modelField, false, true);
93  	}
94  
95  	private BinderConfig bindCompleteModel(String modelFieldName, String modelField, boolean defaultModel, boolean autoBindModelObject, String... ignoreFields) {
96  		formMeta.putModelMeta(modelFieldName, modelField);
97  		autoBind(modelField, defaultModel, autoBindModelObject, ignoreFields);
98  		return this;
99  	}
100 
101 
102 	private void autoBind(String modelField, boolean defaultModel, boolean autoBindModelObject, String... ignoreFields) {
103 
104 		List ignoreFieldsList = Arrays.asList(ignoreFields);
105 
106 		if (formConfig.isAutoBind() && autoBindModelObject) {
107 
108 			Class formClass = formMeta.getComponentClass();
109 
110 			Field field = null;
111 			try {
112 				field = ClassManager.getClassInfo(formClass).getField(modelField);
113 			} catch (NoSuchFieldException e) {
114 				log.error("Error while getting field " + modelField);
115 				return;
116 			}
117 
118 			// iterate modelObject fields
119 			Class modelObjectType = field.getType();
120 
121 			if (!Map.class.isAssignableFrom(modelObjectType)) { // can't autobind maps
122 				for (Method moMethod : ClassManager.getClassInfo(modelObjectType).getSetters()) {
123 					String componentFieldName =
124 							moMethod.getName().substring(3, 4).toLowerCase() +
125 									moMethod.getName().substring(4);
126 
127 					Field formClassField = null;
128 
129 					try {
130 						formClassField = ClassManager.getClassInfo(formClass).getField(componentFieldName);
131 
132 						if (!ignoreFieldsList.contains(formClassField.getName())) {
133 
134 							formMeta.addComponentMeta(
135 									defaultModel ? "[default]" : field.getName(),
136 									componentFieldName, formClassField, void.class,
137 									new String[]{}, true, ReadOnly.DEFAULT, moMethod.getParameterTypes()[0]);
138 
139 						}
140 
141 					} catch (NoSuchFieldException e) {
142 						// ignored
143 					}
144 				}
145 			}
146 		}
147 	}
148 
149 	/**
150 	 * @param boundComponentConfig
151 	 */
152 	public BinderConfig bindComponentToDefault(BoundComponentConfig boundComponentConfig) {
153 		formMeta.addComponentMeta("[default]", boundComponentConfig, false);
154 		return this;
155 	}
156 
157 	/**
158 	 * @param boundComponentConfigs
159 	 */
160 	public BinderConfig bindComponentToDefault(BoundComponentConfig... boundComponentConfigs) {
161 		for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
162 			formMeta.addComponentMeta("[default]", boundComponentConfig, false);
163 		}
164 		return this;
165 	}
166 
167 	/**
168 	 * @param modelId
169 	 * @param boundComponentConfig
170 	 */
171 	public BinderConfig bindComponent(String modelId, BoundComponentConfig boundComponentConfig) {
172 		formMeta.addComponentMeta(modelId, boundComponentConfig, false);
173 		return this;
174 	}
175 
176 	/**
177 	 * @param modelId
178 	 * @param boundComponentConfigs
179 	 */
180 	public BinderConfig bindComponent(String modelId, BoundComponentConfig... boundComponentConfigs) {
181 		for (BoundComponentConfig boundComponentConfig : boundComponentConfigs) {
182 			formMeta.addComponentMeta(modelId, boundComponentConfig, false);
183 		}
184 		return this;
185 	}
186 
187 }