1 package org.sapia.gumby.view; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import org.sapia.gumby.RenderContext; 9 import org.sapia.gumby.Scope; 10 11 76 public class View implements Scope { 77 78 private Map _items = new HashMap (); 79 private Map _bindingMap = new HashMap (); 80 private List _bindingList = new ArrayList (); 81 private boolean _created; 82 83 private Object _model; 84 private RenderContext _context; 85 86 public View(RenderContext context) { 87 _context = context; 88 } 89 90 93 public RenderContext getContext() { 94 return _context; 95 } 96 97 106 public synchronized Object acquire(String name) 107 throws IllegalArgumentException { 108 Object toReturn = _items.get(name); 109 if(toReturn == null) { 110 throw new IllegalArgumentException ("No item found for: " + name); 111 } 112 return toReturn; 113 } 114 115 118 public synchronized Object get(String name) { 119 return _items.get(name); 120 } 121 122 125 public synchronized void put(String name, Object o) { 126 _items.put(name, o); 127 } 128 129 133 public synchronized void setModel(Object model) { 134 if(_model == null) { 135 _model = model; 136 fireBind(); 137 } else { 138 _model = model; 139 fireChanged(); 140 } 141 } 142 143 146 public synchronized Object getModel() { 147 return _model; 148 } 149 150 154 public synchronized void addBinding(Binding binding) { 155 if(binding.getId() == null) { 156 throw new IllegalArgumentException ("Binding does not have an ID"); 157 } 158 if(_bindingMap.containsKey(binding.getId())) { 159 throw new IllegalArgumentException ("Binding already exists for: " 160 + binding.getId()); 161 } 162 _bindingMap.put(binding.getId(), binding); 163 _bindingList.add(binding); 164 if(_model != null) { 165 binding.onBound(this, _model); 166 } 167 } 168 169 176 public synchronized void removeBinding(String id) { 177 Binding b = (Binding) _bindingMap.remove(id); 178 if(b != null) 179 _bindingList.remove(b); 180 } 181 182 192 public synchronized void fireUpdated(String id) { 193 Binding b = (Binding) _bindingMap.get(id); 194 if(b != null && _model != null) { 195 b.onUpdated(this, _model); 196 } 197 } 198 199 205 public synchronized void fireUpdated() { 206 if(_model != null) { 207 for(int i = 0; i < _bindingList.size(); i++) { 208 ((Binding) _bindingList.get(i)).onUpdated(this, _model); 209 } 210 } 211 } 212 213 223 public synchronized void fireUpdateModel(String id) { 224 Binding b = (Binding) _bindingMap.get(id); 225 if(b != null && _model != null) { 226 b.updateModel(this, _model); 227 } 228 } 229 230 236 public synchronized void fireUpdateModel() { 237 if(_model != null) { 238 for(int i = 0; i < _bindingList.size(); i++) { 239 ((Binding) _bindingList.get(i)).updateModel(this, _model); 240 } 241 } 242 } 243 244 private synchronized void fireChanged() { 245 if(_model != null) { 246 for(int i = 0; i < _bindingList.size(); i++) { 247 ((Binding) _bindingList.get(i)).onChanged(this, _model); 248 } 249 } 250 } 251 252 private synchronized void fireBind() { 253 if(_model != null) { 254 for(int i = 0; i < _bindingList.size(); i++) { 255 ((Binding) _bindingList.get(i)).onBound(this, _model); 256 } 257 } 258 } 259 260 } 261 | Popular Tags |