1 11 package org.eclipse.team.internal.ccvs.core.util; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.team.internal.ccvs.core.IResourceStateChangeListener; 21 import org.eclipse.team.internal.ccvs.core.Policy; 22 23 26 public class ResourceStateChangeListeners { 27 28 private static List listeners = new ArrayList (); 29 30 private static ResourceStateChangeListeners instance; 31 32 public static synchronized ResourceStateChangeListeners getListener() { 33 if (instance == null) { 34 instance = new ResourceStateChangeListeners(); 35 } 36 return instance; 37 } 38 39 44 private abstract class Notification implements ISafeRunnable { 45 private IResourceStateChangeListener listener; 46 public void handleException(Throwable exception) { 47 } 49 public void run(IResourceStateChangeListener listener) { 50 this.listener = listener; 51 Platform.run(this); 52 } 53 public void run() throws Exception { 54 notify(listener); 55 } 56 60 protected abstract void notify(IResourceStateChangeListener listener); 61 } 62 63 private IResourceStateChangeListener[] getListeners() { 64 synchronized(listeners) { 65 return (IResourceStateChangeListener[]) listeners.toArray(new IResourceStateChangeListener[listeners.size()]); 66 } 67 } 68 69 private void fireNotification(Notification notification) { 70 IResourceStateChangeListener[] listeners = getListeners(); 72 for (int i = 0; i < listeners.length; i++) { 74 IResourceStateChangeListener listener = listeners[i]; 75 notification.run(listener); 76 } 77 } 78 79 public void addResourceStateChangeListener(IResourceStateChangeListener listener) { 80 synchronized(listeners) { 81 listeners.add(listener); 82 } 83 } 84 85 public void removeResourceStateChangeListener(IResourceStateChangeListener listener) { 86 synchronized(listeners) { 87 listeners.remove(listener); 88 } 89 } 90 91 public void resourceSyncInfoChanged(final IResource[] resources) { 92 if (Policy.DEBUG_SYNC_CHANGE_EVENTS) { 93 printDebugInfo("Sync info change event ", resources); } 95 fireNotification(new Notification() { 96 public void notify(IResourceStateChangeListener listener) { 97 listener.resourceSyncInfoChanged(resources); 98 } 99 }); 100 } 101 102 public void externalSyncInfoChange(final IResource[] resources) { 103 if (Policy.DEBUG_SYNC_CHANGE_EVENTS) { 104 printDebugInfo("External sync info change event ", resources); } 106 fireNotification(new Notification() { 107 public void notify(IResourceStateChangeListener listener) { 108 listener.externalSyncInfoChange(resources); 109 } 110 }); 111 } 112 113 public void resourceModified(final IResource[] resources) { 114 if (Policy.DEBUG_SYNC_CHANGE_EVENTS) { 115 printDebugInfo("Resource modified change event ", resources); } 117 fireNotification(new Notification() { 118 public void notify(IResourceStateChangeListener listener) { 119 listener.resourceModified(resources); 120 } 121 }); 122 } 123 public void projectConfigured(final IProject project) { 124 if (Policy.DEBUG_SYNC_CHANGE_EVENTS) { 125 printDebugInfo("Project configured change event ", new IResource[] { project }); } 127 fireNotification(new Notification() { 128 public void notify(IResourceStateChangeListener listener) { 129 listener.projectConfigured(project); 130 } 131 }); 132 } 133 public void projectDeconfigured(final IProject project) { 134 if (Policy.DEBUG_SYNC_CHANGE_EVENTS) { 135 printDebugInfo("Project deconfigured change event ", new IResource[] { project }); } 137 fireNotification(new Notification() { 138 public void notify(IResourceStateChangeListener listener) { 139 listener.projectDeconfigured(project); 140 } 141 }); 142 } 143 144 private void printDebugInfo(String prefix, IResource[] resources) { 145 System.out.print(prefix); 146 System.out.print(" from thread " + Thread.currentThread().getName()); System.out.print(" for the following " + resources.length + " resources"); System.out.println(":"); for (int i = 0; i < resources.length; i++) { 150 IResource resource = resources[i]; 151 System.out.println(resource.getFullPath().toString()); 152 } 153 } 154 155 } 156 | Popular Tags |