1 11 package org.eclipse.team.internal.core.subscribers; 12 13 import java.util.*; 14 15 import org.eclipse.core.runtime.*; 16 17 20 public abstract class ChangeSetManager { 21 22 private ListenerList listeners = new ListenerList(ListenerList.IDENTITY); 23 private Set sets; 24 private boolean initializing; 25 26 30 protected Object [] getListeners() { 31 return listeners.getListeners(); 32 } 33 34 39 protected void fireNameChangedEvent(final ChangeSet set) { 40 if (initializing) 41 return; 42 if (contains(set)) { 43 Object [] listeners = getListeners(); 44 for (int i = 0; i < listeners.length; i++) { 45 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i]; 46 SafeRunner.run(new ISafeRunnable() { 47 public void handleException(Throwable exception) { 48 } 50 public void run() throws Exception { 51 listener.nameChanged(set); 52 } 53 }); 54 } 55 } 56 } 57 58 64 protected void fireDefaultChangedEvent(final ChangeSet oldSet, final ChangeSet defaultSet) { 65 if (initializing) 66 return; 67 Object [] listeners = getListeners(); 68 for (int i = 0; i < listeners.length; i++) { 69 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i]; 70 SafeRunner.run(new ISafeRunnable() { 71 public void handleException(Throwable exception) { 72 } 74 public void run() throws Exception { 75 listener.defaultSetChanged(oldSet, defaultSet); 76 } 77 }); 78 } 79 } 80 81 85 public void add(final ChangeSet set) { 86 if (!contains(set)) { 87 internalGetSets().add(set); 88 handleSetAdded(set); 89 } 90 } 91 92 96 protected void handleSetAdded(final ChangeSet set) { 97 if (initializing) 98 return; 99 Object [] listeners = getListeners(); 100 for (int i = 0; i < listeners.length; i++) { 101 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i]; 102 SafeRunner.run(new ISafeRunnable() { 103 public void handleException(Throwable exception) { 104 } 106 public void run() throws Exception { 107 listener.setAdded(set); 108 } 109 }); 110 } 111 } 112 113 117 public void remove(final ChangeSet set) { 118 if (contains(set)) { 119 internalGetSets().remove(set); 120 handleSetRemoved(set); 121 } 122 } 123 124 128 protected void handleSetRemoved(final ChangeSet set) { 129 if (initializing) 130 return; 131 Object [] listeners = getListeners(); 132 for (int i = 0; i < listeners.length; i++) { 133 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i]; 134 SafeRunner.run(new ISafeRunnable() { 135 public void handleException(Throwable exception) { 136 } 138 public void run() throws Exception { 139 listener.setRemoved(set); 140 } 141 }); 142 } 143 } 144 145 150 public boolean contains(ChangeSet set) { 151 return internalGetSets().contains(set); 152 } 153 154 158 public void addListener(IChangeSetChangeListener listener) { 159 listeners.add(listener); 160 } 161 162 166 public void removeListener(IChangeSetChangeListener listener) { 167 listeners.remove(listener); 168 } 169 170 174 public ChangeSet[] getSortedSets() { 175 Set sets = internalGetSets(); 176 ChangeSet[] array = (ChangeSet[]) sets.toArray(new ChangeSet[sets.size()]); 177 List list = Arrays.asList(array); 178 Collections.sort(list, new Comparator(){ 179 public int compare(Object a, Object b) { 180 ChangeSet ca = (ChangeSet) a; 181 ChangeSet cb = (ChangeSet) b; 182 return ca.getName().compareTo(cb.getName()); 183 } 184 }); 185 return (ChangeSet[]) list.toArray(new ChangeSet[]{}); 186 } 187 188 192 public ChangeSet[] getSets() { 193 Set sets = internalGetSets(); 194 return (ChangeSet[]) sets.toArray(new ChangeSet[sets.size()]); 195 } 196 197 200 public void dispose() { 201 } 203 204 209 protected void fireResourcesChangedEvent(final ChangeSet changeSet, final IPath[] allAffectedResources) { 210 if (initializing) 211 return; 212 Object [] listeners = getListeners(); 213 for (int i = 0; i < listeners.length; i++) { 214 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i]; 215 SafeRunner.run(new ISafeRunnable() { 216 public void handleException(Throwable exception) { 217 } 219 public void run() throws Exception { 220 listener.resourcesChanged(changeSet, allAffectedResources); 221 } 222 }); 223 } 224 } 225 226 private Set internalGetSets() { 227 if (sets == null) { 228 sets = Collections.synchronizedSet(new HashSet()); 229 try { 230 initializing = true; 231 initializeSets(); 232 } finally { 233 initializing = false; 234 } 235 } 236 return sets; 237 } 238 239 243 protected abstract void initializeSets(); 244 245 public boolean isInitialized() { 246 return sets != null; 247 } 248 } 249 | Popular Tags |