1 19 20 package org.openide.src; 21 22 import java.beans.PropertyChangeEvent ; 23 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 28 56 public class MultiPropertyChangeEvent extends PropertyChangeEvent { 57 60 public static final int TYPE_ADD = 1; 61 62 65 public static final int TYPE_REMOVE = 2; 66 67 69 public static final int TYPE_MODIFY = 3; 70 71 74 public static final int TYPE_REORDER = 4; 75 76 78 public static final int TYPE_COMPOUND = 5; 79 80 83 private Collection affected; 84 86 87 private Collection newItems; 88 91 private int[] indices; 92 95 private int eventType; 96 97 public MultiPropertyChangeEvent(Object source, String propName, 98 Object oldVal, Object newVal) { 99 super(source, propName, oldVal, newVal); 100 } 101 102 public void makeInsertion(Collection items, int[] indices) { 103 checkUninitialized(); 104 this.affected = items; 105 this.indices = indices; 106 this.eventType = TYPE_ADD; 107 } 108 109 public void makeRemoval(Collection items, int[] indices) { 110 checkUninitialized(); 111 this.affected = items; 112 this.indices = indices; 113 this.eventType = TYPE_REMOVE; 114 } 115 116 public void makeRemoval(Collection items) { 117 checkUninitialized(); 118 this.affected = items; 119 this.eventType = TYPE_REMOVE; 120 } 121 122 public void makeReorder(int[] permutation) { 123 checkUninitialized(); 124 this.indices = permutation; 125 this.eventType = TYPE_REORDER; 126 } 127 128 public void makeReplacement(Collection old, Collection n, int[] indices) { 129 checkUninitialized(); 130 this.indices = indices; 131 this.affected = old; 132 this.newItems = n; 133 this.eventType = TYPE_MODIFY; 134 } 135 136 public void makeCompound(Collection partialChanges, int[] offsets) { 137 checkUninitialized(); 138 this.affected = partialChanges; 139 this.eventType = TYPE_COMPOUND; 140 this.indices = offsets; 141 } 142 143 protected void checkUninitialized() { 144 if (this.eventType != 0) { 145 throw new IllegalStateException ("Event object is already initialized."); } 147 } 148 149 154 public Collection getAffectedItems() { 155 return this.affected; 156 } 157 158 public Collection getComponents() { 159 if (eventType != TYPE_COMPOUND) 160 throw new IllegalStateException ("Not a compound event"); return affected; 162 } 163 164 168 public Collection getReplacement() throws IllegalStateException { 169 if (eventType != TYPE_MODIFY) 170 throw new IllegalStateException ("No replacement"); return this.newItems; 172 } 173 174 179 public int[] getPermutation() throws IllegalStateException { 180 if (eventType != TYPE_REORDER) 181 throw new IllegalStateException ("Not reordered"); return this.indices; 183 } 184 185 194 public int[] getIndices() { 195 return this.indices; 196 } 197 198 public final int getEventType() { 199 return this.eventType; 200 } 201 202 206 public Iterator getIterator() { 207 if (getEventType() == TYPE_COMPOUND) 208 return getComponents().iterator(); 209 else 210 return Collections.singleton(this).iterator(); 211 } 212 } 213 | Popular Tags |