| 1 | 5 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
package net.sf.echobinding.binding; |
| 30 | |
|
| 31 | |
import java.beans.PropertyChangeListener; |
| 32 | |
import java.beans.PropertyChangeSupport; |
| 33 | |
import java.io.Serializable; |
| 34 | |
import java.lang.reflect.Method; |
| 35 | |
import java.util.HashMap; |
| 36 | |
import java.util.Map.Entry; |
| 37 | |
|
| 38 | |
import net.sf.echobinding.model.PresentationModel; |
| 39 | |
import net.sf.echobinding.validation.ValidationHandler; |
| 40 | |
import net.sf.echobinding.validation.ValidationReport; |
| 41 | |
import ognl.Ognl; |
| 42 | |
import ognl.OgnlContext; |
| 43 | |
|
| 44 | |
import org.apache.log4j.Logger; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class OgnlBindingContext extends AbstractBindingContext { |
| 50 | |
|
| 51 | |
private static final long serialVersionUID = -3799783525654201978L; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static final String OGNL_NAMESPACE_PMOD = "pm"; |
| 57 | |
|
| 58 | |
@SuppressWarnings("unused") |
| 59 | 33 | private final Logger _logger = Logger.getLogger(OgnlBindingContext.class); |
| 60 | |
|
| 61 | |
private Object _model; |
| 62 | |
|
| 63 | 33 | private OgnlContext _ognlContext = new OgnlContext(); |
| 64 | |
|
| 65 | 33 | private HashMap<String, Object> _contextMap = new HashMap<String, Object>(); |
| 66 | |
|
| 67 | |
private ValidationHandler _validationHandler; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public OgnlBindingContext(Object bean) { |
| 77 | 28 | this(); |
| 78 | 28 | setModel(bean); |
| 79 | 28 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public OgnlBindingContext() { |
| 86 | 33 | super(); |
| 87 | 33 | } |
| 88 | |
|
| 89 | |
public BindingContext add(String id, PropertyAdapter adapter) { |
| 90 | |
|
| 91 | 67 | if (!(adapter instanceof OgnlPropertyAdapter)) |
| 92 | |
throw new ClassCastException("Adapter has to be of type OgnlPropertyAdapter!"); |
| 93 | |
|
| 94 | 67 | super.add(id, adapter); |
| 95 | |
|
| 96 | 67 | return this; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
public Object getValue(String id) throws BindingException { |
| 101 | 165 | OgnlPropertyAdapter adapter = getAdapter(id); |
| 102 | |
|
| 103 | 329 | Object value = (_ognlContext == null) ? adapter.getValue(_model) |
| 104 | 165 | : adapter.getValue(_ognlContext, _model); |
| 105 | |
|
| 106 | 164 | return value; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public void setValue(String id, Object value) throws BindingException { |
| 118 | |
|
| 119 | 14 | OgnlPropertyAdapter adapter = getAdapter(id); |
| 120 | |
|
| 121 | 14 | Object oldValue = this.getValue(id); |
| 122 | |
|
| 123 | 14 | if (_ognlContext == null) |
| 124 | |
adapter.setValue( _model, value ); |
| 125 | |
else |
| 126 | 14 | adapter.setValue( _ognlContext, _model, value ); |
| 127 | |
|
| 128 | 14 | firePropertyChange(id, oldValue, value); |
| 129 | 14 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
@SuppressWarnings("unchecked") |
| 138 | |
public ValidationReport validate(String id, Object value) { |
| 139 | 42 | Object bean = getModel(); |
| 140 | 42 | OgnlPropertyAdapter adapter = getAdapter(id); |
| 141 | 42 | String ognlNameSpace = adapter.getOgnlContextName(); |
| 142 | 42 | if(ognlNameSpace != null && getOgnlContext() != null) { |
| 143 | 5 | bean = getOgnlContext().get( ognlNameSpace ); |
| 144 | |
} |
| 145 | |
|
| 146 | 42 | return adapter.validate(bean, value); |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public Object getModel() { |
| 156 | 51 | return _model; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public OgnlBindingContext setModel(Object bean) { |
| 165 | 31 | _model = bean; |
| 166 | |
|
| 167 | 31 | registerSource( bean ); |
| 168 | |
|
| 169 | 31 | return this; |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
@Override |
| 175 | |
public void setPresentationModel(PresentationModel presentationModel) { |
| 176 | 3 | super.setPresentationModel( presentationModel ); |
| 177 | 3 | addModel( OGNL_NAMESPACE_PMOD, presentationModel ); |
| 178 | 3 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
private void registerSource(Object contextRoot) { |
| 187 | 31 | if(contextRoot==null) |
| 188 | |
return; |
| 189 | |
|
| 190 | 31 | if ( contextRoot instanceof PropertyChangeSupport ) { |
| 191 | |
PropertyChangeSupport propertyChangeSupport = (PropertyChangeSupport) contextRoot; |
| 192 | |
propertyChangeSupport.addPropertyChangeListener(this); |
| 193 | |
} |
| 194 | |
else { |
| 195 | |
|
| 196 | 31 | Class contextClass = contextRoot.getClass(); |
| 197 | 31 | Class[] parameterTypes = new Class[] {PropertyChangeListener.class}; |
| 198 | 31 | Object[] arguments = new Object[] {this}; |
| 199 | |
Method addPropertyChangeListenerMethod; |
| 200 | |
try { |
| 201 | 31 | addPropertyChangeListenerMethod = contextClass.getMethod("addPropertyChangeListener", parameterTypes); |
| 202 | 2 | addPropertyChangeListenerMethod.invoke(contextRoot, arguments); |
| 203 | 29 | } catch (Exception e) { |
| 204 | |
|
| 205 | |
} |
| 206 | |
} |
| 207 | 31 | } |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public OgnlBindingContext addModel(String contextName, Object bean) { |
| 218 | |
|
| 219 | 10 | if (_ognlContext == null) |
| 220 | |
_ognlContext = (OgnlContext) Ognl |
| 221 | |
.createDefaultContext( _model ); |
| 222 | |
|
| 223 | 10 | _ognlContext.put(contextName, bean); |
| 224 | |
|
| 225 | 10 | _contextMap.put(contextName, bean); |
| 226 | |
|
| 227 | 10 | return this; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
public BindingContext createChild() { |
| 232 | |
|
| 233 | 1 | OgnlBindingContext childContext = new OgnlBindingContext(); |
| 234 | |
|
| 235 | 4 | for (Entry<String, PropertyAdapter> entry : _adapters.entrySet()) { |
| 236 | 2 | PropertyAdapter clonedAdapter = entry.getValue().newInstance(); |
| 237 | 2 | childContext.add(entry.getKey(), clonedAdapter); |
| 238 | |
} |
| 239 | |
|
| 240 | 1 | childContext.setContextMap(getContextMap()); |
| 241 | 1 | childContext.setParent(this); |
| 242 | |
|
| 243 | 1 | if(getPresentationModel()!=null) |
| 244 | |
childContext.setPresentationModel(getPresentationModel().cloneInstance()); |
| 245 | |
|
| 246 | 1 | addChild(childContext); |
| 247 | |
|
| 248 | 1 | childContext.setModel(getModel()); |
| 249 | |
|
| 250 | 1 | return childContext; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public HashMap<String, Object> getContextMap() { |
| 259 | 1 | return _contextMap; |
| 260 | |
} |
| 261 | |
|
| 262 | |
protected OgnlContext getOgnlContext() { |
| 263 | 10 | return _ognlContext; |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public void setContextMap(HashMap<String, Object> contextMap) { |
| 273 | 1 | _contextMap = contextMap; |
| 274 | 1 | if (contextMap != null) { |
| 275 | 2 | for (Entry<String, Object> entry : contextMap.entrySet()) { |
| 276 | |
_ognlContext.put(entry.getKey(), entry.getValue()); |
| 277 | |
} |
| 278 | |
} |
| 279 | 1 | } |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public ValidationHandler getValidationHandler(String id) { |
| 286 | 4 | ValidationHandler handler = getAdapter(id).getValidationHandler(); |
| 287 | 4 | return (handler==null) ? getValidationHandler() : handler; |
| 288 | |
} |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
public BindingContext setValidationHandler(ValidationHandler handler) { |
| 294 | |
_validationHandler = handler; |
| 295 | |
return this; |
| 296 | |
} |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public ValidationHandler getValidationHandler() { |
| 302 | 4 | return _validationHandler; |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public boolean isDirty(String id, Object value) throws BindingException { |
| 310 | 7 | Object oldValue = getValue( id ); |
| 311 | 7 | if(oldValue==null) { |
| 312 | |
if(value==null) |
| 313 | |
return false; |
| 314 | |
return true; |
| 315 | |
} |
| 316 | 7 | return ! oldValue.equals(value); |
| 317 | |
} |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
public OgnlPropertyAdapter getAdapter(String id) { |
| 330 | 229 | OgnlPropertyAdapter adapter = (OgnlPropertyAdapter) _adapters.get(id); |
| 331 | 229 | if (adapter == null) { |
| 332 | |
|
| 333 | 18 | adapter = new OgnlPropertyAdapter(id); |
| 334 | 18 | this.add(id, adapter); |
| 335 | |
} |
| 336 | 229 | return adapter; |
| 337 | |
} |
| 338 | |
|
| 339 | |
|
| 340 | |
} |