1 29 30 package nextapp.echo2.app.update; 31 32 import java.io.Serializable ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 import nextapp.echo2.app.Component; 39 40 50 public class ServerComponentUpdate 51 implements Serializable { 52 53 private static final Component[] EMPTY_COMPONENT_ARRAY = new Component[0]; 54 private static final String [] EMPTY_STRING_ARRAY = new String [0]; 55 56 59 private Set addedChildren; 60 61 64 private Component parent; 65 66 70 private Map propertyUpdates; 71 72 75 private Set removedChildren; 76 77 81 private Set removedDescendants; 82 83 87 private Set updatedLayoutDataChildren; 88 89 93 public ServerComponentUpdate(Component parent) { 94 this.parent = parent; 95 } 96 97 103 public void addChild(Component child) { 104 if (addedChildren == null) { 105 addedChildren = new HashSet (); 106 } 107 addedChildren.add(child); 108 } 109 110 117 public void cancelUpdateProperty(String propertyName) { 118 if (propertyUpdates == null) { 119 return; 120 } 121 propertyUpdates.remove(propertyName); 122 if (propertyUpdates.size() == 0) { 123 propertyUpdates = null; 124 } 125 } 126 127 141 public void appendRemovedDescendants(ServerComponentUpdate update) { 142 if (update.removedDescendants != null) { 144 if (removedDescendants == null) { 145 removedDescendants = new HashSet (); 146 } 147 removedDescendants.addAll(update.removedDescendants); 148 } 149 if (update.removedChildren != null) { 151 if (removedDescendants == null) { 152 removedDescendants = new HashSet (); 153 } 154 removedDescendants.addAll(update.removedChildren); 155 } 156 } 157 158 163 public Component[] getAddedChildren() { 164 if (addedChildren == null) { 165 return EMPTY_COMPONENT_ARRAY; 166 } else { 167 return (Component[]) addedChildren.toArray(new Component[addedChildren.size()]); 168 } 169 } 170 171 176 public Component getParent() { 177 return parent; 178 } 179 180 190 public Component[] getRemovedChildren() { 191 if (removedChildren == null) { 192 return EMPTY_COMPONENT_ARRAY; 193 } else { 194 return (Component[]) removedChildren.toArray(new Component[removedChildren.size()]); 195 } 196 } 197 198 210 public Component[] getRemovedDescendants() { 211 if (removedDescendants == null) { 212 return EMPTY_COMPONENT_ARRAY; 213 } else { 214 return (Component[]) removedDescendants.toArray(new Component[removedDescendants.size()]); 215 } 216 } 217 218 224 public Component[] getUpdatedLayoutDataChildren() { 225 if (updatedLayoutDataChildren == null) { 226 return EMPTY_COMPONENT_ARRAY; 227 } else { 228 return (Component[]) updatedLayoutDataChildren.toArray(new Component[updatedLayoutDataChildren.size()]); 229 } 230 } 231 232 240 public PropertyUpdate getUpdatedProperty(String name) { 241 return propertyUpdates == null ? null : (PropertyUpdate) propertyUpdates.get(name); 242 } 243 244 250 public String [] getUpdatedPropertyNames() { 251 if (propertyUpdates == null) { 252 return EMPTY_STRING_ARRAY; 253 } else { 254 return (String []) propertyUpdates.keySet().toArray(new String [propertyUpdates.size()]); 255 } 256 } 257 258 265 public boolean hasAddedChild(Component component) { 266 return addedChildren != null && addedChildren.contains(component); 267 } 268 269 274 public boolean hasAddedChildren() { 275 return addedChildren != null; 276 } 277 278 284 public boolean hasRemovedChild(Component component) { 285 return removedChildren != null && removedChildren.contains(component); 286 } 287 288 294 public boolean hasRemovedChildren() { 295 return removedChildren != null; 296 } 297 298 307 public boolean hasRemovedDescendants() { 308 return removedDescendants != null; 309 } 310 311 317 public boolean hasUpdatedLayoutDataChildren() { 318 return updatedLayoutDataChildren != null; 319 } 320 321 326 public boolean hasUpdatedProperties() { 327 return propertyUpdates != null; 328 } 329 330 336 public void removeChild(Component child) { 337 if (addedChildren != null && addedChildren.contains(child)) { 338 addedChildren.remove(child); 340 } 341 if (updatedLayoutDataChildren != null && updatedLayoutDataChildren.contains(child)) { 342 updatedLayoutDataChildren.remove(child); 344 } 345 if (removedChildren == null) { 346 removedChildren = new HashSet (); 347 } 348 removedChildren.add(child); 349 350 Component[] descendants = child.getComponents(); 351 for (int i = 0; i < descendants.length; ++i) { 352 removeDescendant(descendants[i]); 353 } 354 } 355 356 362 public void removeDescendant(Component descendant) { 363 if (removedDescendants == null) { 364 removedDescendants = new HashSet (); 365 } 366 removedDescendants.add(descendant); 367 Component[] descendants = descendant.getComponents(); 368 for (int i = 0; i < descendants.length; ++i) { 369 removeDescendant(descendants[i]); 370 } 371 } 372 373 378 public String toString() { 379 StringBuffer out = new StringBuffer (); 380 out.append(ServerComponentUpdate.class.getName() + "\n"); 381 out.append("- Parent: " + getParent() + "\n"); 382 out.append("- Adds: " + addedChildren + "\n"); 383 out.append("- Removes: " + removedChildren + "\n"); 384 out.append("- DescendantRemoves: " + removedDescendants + "\n"); 385 out.append("- ChildLayoutDataUpdates: " + updatedLayoutDataChildren + "\n"); 386 out.append("- PropertyUpdates: " + propertyUpdates + "\n"); 387 return out.toString(); 388 } 389 390 397 public void updateLayoutData(Component child) { 398 if (updatedLayoutDataChildren == null) { 399 updatedLayoutDataChildren = new HashSet (); 400 } 401 updatedLayoutDataChildren.add(child); 402 } 403 404 412 public void updateProperty(String propertyName, Object oldValue, Object newValue) { 413 if (propertyUpdates == null) { 414 propertyUpdates = new HashMap (); 415 } 416 PropertyUpdate propertyUpdate = new PropertyUpdate(oldValue, newValue); 417 propertyUpdates.put(propertyName, propertyUpdate); 418 } 419 } 420 | Popular Tags |