| 1 | |
|
| 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 | |
package net.sf.echobinding.binding; |
| 29 | |
|
| 30 | |
import java.beans.*; |
| 31 | |
import java.io.Serializable; |
| 32 | |
import java.util.*; |
| 33 | |
|
| 34 | |
import net.sf.echobinding.BoundControl; |
| 35 | |
import net.sf.echobinding.decorator.Decorator; |
| 36 | |
import net.sf.echobinding.format.Format; |
| 37 | |
import net.sf.echobinding.model.PresentationModel; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 66 | public abstract class AbstractBindingContext implements BindingContext, Serializable { |
| 43 | |
|
| 44 | |
private BindingContext _parent; |
| 45 | |
private Set<BindingContext> _childs; |
| 46 | 33 | protected HashMap<String, PropertyAdapter> _adapters = new HashMap<String, PropertyAdapter>(); |
| 47 | |
private HashMap<String, BoundControl> _controls; |
| 48 | 33 | protected PropertyChangeSupport _changes = new PropertyChangeSupport(this); |
| 49 | |
|
| 50 | |
private PresentationModel _presentationModel; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public BindingContext add(String id, PropertyAdapter binding) { |
| 59 | 67 | binding.setId(id); |
| 60 | 67 | _adapters.put(id, binding); |
| 61 | 67 | return this; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public PropertyAdapter remove(String id) { |
| 70 | 3 | return _adapters.remove(id); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void addChild(BindingContext child) { |
| 80 | 1 | getChilds().add(child); |
| 81 | 1 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public Set<BindingContext> getChilds() { |
| 87 | 33 | if( _childs == null) |
| 88 | 15 | _childs = new HashSet<BindingContext>(); |
| 89 | 33 | return _childs; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public void setChilds(Set<BindingContext> childs) { |
| 98 | |
_childs = childs; |
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public BindingContext getParent() { |
| 105 | 21 | return _parent; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public void setParent(BindingContext parent) { |
| 114 | 1 | _parent = parent; |
| 115 | 1 | } |
| 116 | |
|
| 117 | |
public List<PropertyAdapter> getPropertyAdapters() { |
| 118 | 2 | ArrayList<PropertyAdapter> list = new ArrayList<PropertyAdapter>(); |
| 119 | 5 | for (String bindingId : _adapters.keySet()) { |
| 120 | 1 | list.add(_adapters.get(bindingId)); |
| 121 | |
} |
| 122 | 2 | return list; |
| 123 | |
} |
| 124 | |
|
| 125 | |
public Decorator getDecorator(String id) { |
| 126 | 4 | return getAdapter(id).getDecorator(); |
| 127 | |
} |
| 128 | |
|
| 129 | |
public Format getFormat(String id) { |
| 130 | |
return getAdapter(id).getFormat(); |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public void registerControl(String id, BoundControl control) { |
| 138 | 32 | if( _controls == null ) |
| 139 | 20 | _controls = new HashMap<String,BoundControl>(); |
| 140 | |
|
| 141 | 32 | addPropertyChangeListener(id, control); |
| 142 | |
|
| 143 | 32 | _controls.put(id, control); |
| 144 | 32 | } |
| 145 | |
|
| 146 | |
public void removeControl(BoundControl control) { |
| 147 | |
if( _controls != null ) |
| 148 | |
_controls.remove(control); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
public BoundControl getControl(String id) { |
| 153 | 6 | if( _controls == null ) |
| 154 | |
return null; |
| 155 | 6 | return _controls.get(id); |
| 156 | |
} |
| 157 | |
|
| 158 | |
public Set<BoundControl> getControls() { |
| 159 | 17 | if(_controls == null) |
| 160 | 2 | return new HashSet<BoundControl>(); |
| 161 | 15 | return new HashSet<BoundControl>( _controls.values() ); |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
protected void firePropertyChange(String bindingId, Object oldValue, Object newValue) throws BindingException { |
| 174 | |
|
| 175 | 28 | PropertyChangeEvent event = new PropertyChangeEvent(this, bindingId, |
| 176 | 14 | getValue(bindingId), newValue); |
| 177 | |
|
| 178 | |
|
| 179 | 14 | firePropertyChange(event); |
| 180 | 14 | } |
| 181 | |
|
| 182 | |
protected void firePropertyChange(PropertyChangeEvent event) throws BindingException { |
| 183 | |
|
| 184 | 14 | notifyPropertyChangeListener(event); |
| 185 | |
|
| 186 | 14 | notifyParentContext( event ); |
| 187 | |
|
| 188 | 14 | notifiyChildContexts( event ); |
| 189 | 14 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private void notifiyChildContexts(PropertyChangeEvent event) { |
| 199 | 21 | Object source = event.getSource(); |
| 200 | |
|
| 201 | 21 | PropertyChangeEvent newEvent = replaceSender( event ); |
| 202 | |
|
| 203 | |
|
| 204 | 42 | for( BindingContext child : getChilds() ) { |
| 205 | |
if( ! (source==null || source.equals(child))) |
| 206 | |
child.propertyChange(newEvent); |
| 207 | |
} |
| 208 | 21 | } |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
private void notifyParentContext(PropertyChangeEvent event) { |
| 216 | |
|
| 217 | 21 | Object source = event.getSource(); |
| 218 | |
|
| 219 | 21 | PropertyChangeEvent newEvent = replaceSender( event ); |
| 220 | |
|
| 221 | |
|
| 222 | 21 | if( ! ( getParent()==null || source.equals(getParent())) ) |
| 223 | |
getParent().propertyChange(newEvent); |
| 224 | 21 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
private PropertyChangeEvent replaceSender(PropertyChangeEvent event) { |
| 231 | 63 | if(event.getSource().equals(this)) |
| 232 | 42 | return event; |
| 233 | 21 | PropertyChangeEvent newEvent = new PropertyChangeEvent(this, event.getPropertyName(), event.getOldValue(), event.getNewValue()); |
| 234 | 21 | return newEvent; |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public void propertyChange(PropertyChangeEvent event) { |
| 242 | |
|
| 243 | 7 | notifyPropertyChangeListener( event ); |
| 244 | |
|
| 245 | 7 | notifiyChildContexts(event); |
| 246 | |
|
| 247 | 7 | notifyParentContext(event); |
| 248 | 7 | } |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
private void notifyPropertyChangeListener(PropertyChangeEvent event) { |
| 254 | 21 | PropertyChangeEvent newEvent = replaceSender(event); |
| 255 | |
|
| 256 | |
|
| 257 | 34 | for (PropertyChangeListener listener : _changes.getPropertyChangeListeners(event.getPropertyName()) ) { |
| 258 | 13 | if (!listener.equals(event.getSource())) |
| 259 | 9 | listener.propertyChange(newEvent); |
| 260 | |
} |
| 261 | |
|
| 262 | 60 | for (PropertyChangeListener listener : _changes.getPropertyChangeListeners() ) { |
| 263 | 39 | if (!listener.equals(event.getSource())) |
| 264 | 38 | listener.propertyChange(newEvent); |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | 21 | } |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
| 276 | 2 | _changes.addPropertyChangeListener(listener); |
| 277 | 2 | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { |
| 286 | 34 | _changes.addPropertyChangeListener(propertyName, listener); |
| 287 | 34 | } |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
| 293 | 2 | _changes.removePropertyChangeListener(listener); |
| 294 | 2 | } |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { |
| 303 | |
_changes.removePropertyChangeListener(propertyName, listener); |
| 304 | |
} |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public void synchronize() { |
| 310 | |
|
| 311 | 3 | for(BoundControl widget: getControls()) { |
| 312 | 1 | widget.save(); |
| 313 | |
} |
| 314 | |
|
| 315 | 2 | for(BindingContext child: getChilds()) { |
| 316 | |
child.synchronize(); |
| 317 | |
} |
| 318 | 1 | } |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
public boolean isValid() { |
| 324 | |
|
| 325 | |
|
| 326 | 43 | for(BoundControl widget: getControls()) { |
| 327 | 18 | if( ! widget.isValid() ) |
| 328 | 5 | return false; |
| 329 | |
} |
| 330 | |
|
| 331 | 21 | for(BindingContext child: getChilds()) { |
| 332 | 2 | if( ! child.isValid() ) |
| 333 | 1 | return false; |
| 334 | |
} |
| 335 | 9 | return true; |
| 336 | |
} |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
public boolean isDirty() { |
| 343 | |
|
| 344 | |
for(BoundControl widget: getControls()) { |
| 345 | |
if( widget.isDirty() ) |
| 346 | |
return true; |
| 347 | |
} |
| 348 | |
|
| 349 | |
for(BindingContext child: getChilds()) { |
| 350 | |
if( child.isDirty() ) |
| 351 | |
return true; |
| 352 | |
} |
| 353 | |
return false; |
| 354 | |
} |
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
public void update() { |
| 360 | |
|
| 361 | |
for(BoundControl widget: getControls()) { |
| 362 | |
widget.update(); |
| 363 | |
} |
| 364 | |
|
| 365 | |
for(BindingContext child: getChilds()) { |
| 366 | |
child.update(); |
| 367 | |
} |
| 368 | |
} |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
public void validate() { |
| 376 | |
|
| 377 | |
for(BoundControl widget: getControls()) { |
| 378 | |
widget.validateInput(); |
| 379 | |
} |
| 380 | |
|
| 381 | |
for(BindingContext child: getChilds()) { |
| 382 | |
child.validate(); |
| 383 | |
} |
| 384 | |
} |
| 385 | |
|
| 386 | |
public boolean removeChild(BindingContext context) { |
| 387 | |
return _childs.remove(context); |
| 388 | |
} |
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
public PresentationModel getPresentationModel() { |
| 394 | 1 | return _presentationModel; |
| 395 | |
} |
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
public void setPresentationModel(PresentationModel presentationModel) { |
| 401 | 3 | _presentationModel = presentationModel; |
| 402 | 3 | _presentationModel.setContext(this); |
| 403 | 3 | } |
| 404 | |
|
| 405 | |
|
| 406 | |
} |