KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > ResourceStateChangeListeners


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 /**
24  * Class that manages the listeners of CVS sync change notification
25  */

26 public class ResourceStateChangeListeners {
27
28     private static List JavaDoc listeners = new ArrayList JavaDoc();
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     /*
40      * Private class used to safely notify listeners of resouce sync info changes.
41      * Subclass override the notify(IResourceStateChangeListener) method to
42      * fire specific events inside an ISafeRunnable.
43      */

44     private abstract class Notification implements ISafeRunnable {
45         private IResourceStateChangeListener listener;
46         public void handleException(Throwable JavaDoc exception) {
47             // don't log the exception....it is already being logged in Platform#run
48
}
49         public void run(IResourceStateChangeListener listener) {
50             this.listener = listener;
51             Platform.run(this);
52         }
53         public void run() throws Exception JavaDoc {
54             notify(listener);
55         }
56         /**
57          * Subsclasses overide this method to send an event safely to a lsistener
58          * @param listener
59          */

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         // Get a snapshot of the listeners so the list doesn't change while we're firing
71
IResourceStateChangeListener[] listeners = getListeners();
72         // Notify each listener in a safe manner (i.e. so their exceptions don't kill us)
73
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); //$NON-NLS-1$
94
}
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); //$NON-NLS-1$
105
}
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); //$NON-NLS-1$
116
}
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 }); //$NON-NLS-1$
126
}
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 }); //$NON-NLS-1$
136
}
137         fireNotification(new Notification() {
138             public void notify(IResourceStateChangeListener listener) {
139                 listener.projectDeconfigured(project);
140             }
141         });
142     }
143     
144     private void printDebugInfo(String JavaDoc prefix, IResource[] resources) {
145         System.out.print(prefix);
146         System.out.print(" from thread " + Thread.currentThread().getName()); //$NON-NLS-1$
147
System.out.print(" for the following " + resources.length + " resources"); //$NON-NLS-1$ //$NON-NLS-2$
148
System.out.println(":"); //$NON-NLS-1$
149
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