KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > ChangeSetCollector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.core.internal.runtime.ListenerList;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.ISafeRunnable;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.team.core.synchronize.ISyncInfoSetChangeListener;
20 import org.eclipse.team.core.synchronize.SyncInfoSet;
21
22 /**
23  * This class defines the common aspects of collecting a set of change
24  * sets, including event notification.
25  *
26  * @since 3.1
27  */

28 public abstract class ChangeSetCollector {
29
30     private ListenerList listeners = new ListenerList();
31     private Set sets = new HashSet();
32     
33     private Object JavaDoc[] getListeners() {
34         return listeners.getListeners();
35     }
36     
37     /**
38      * Method that can be invoked by subclasses when the name of
39      * a managed change set changes.
40      * @param set the set whose title has changed
41      */

42     protected void fireNameChangedEvent(final ChangeSet set) {
43         if (contains(set)) {
44             Object JavaDoc[] listeners = getListeners();
45             for (int i = 0; i < listeners.length; i++) {
46                 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
47                 Platform.run(new ISafeRunnable() {
48                     public void handleException(Throwable JavaDoc exception) {
49                         // Exceptions are logged by the platform
50
}
51                     public void run() throws Exception JavaDoc {
52                         listener.nameChanged(set);
53                     }
54                 });
55             }
56         }
57     }
58     
59     /**
60      * Method which allows subclasses to notify listeners that the default
61      * set has changed.
62      * @param oldSet the previous default
63      * @param defaultSet the new default
64      */

65     protected void fireDefaultChangedEvent(final ChangeSet oldSet, final ChangeSet defaultSet) {
66         Object JavaDoc[] listeners = getListeners();
67         for (int i = 0; i < listeners.length; i++) {
68             final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
69             Platform.run(new ISafeRunnable() {
70                 public void handleException(Throwable JavaDoc exception) {
71                     // Exceptions are logged by the platform
72
}
73                 public void run() throws Exception JavaDoc {
74                     listener.defaultSetChanged(oldSet, defaultSet);
75                 }
76             });
77         }
78     }
79     
80     /**
81      * Add the set to the list of active sets.
82      * @param set the set to be added
83      */

84     public void add(final ChangeSet set) {
85         if (!contains(set)) {
86             sets.add(set);
87             set.getSyncInfoSet().addSyncSetChangedListener(getChangeSetChangeListener());
88             Object JavaDoc[] listeners = getListeners();
89             for (int i = 0; i < listeners.length; i++) {
90                 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
91                 Platform.run(new ISafeRunnable() {
92                     public void handleException(Throwable JavaDoc exception) {
93                         // Exceptions are logged by the platform
94
}
95                     public void run() throws Exception JavaDoc {
96                         listener.setAdded(set);
97                     }
98                 });
99             }
100         }
101     }
102
103     /**
104      * Remove the set from the list of active sets.
105      * @param set the set to be removed
106      */

107     public void remove(final ChangeSet set) {
108         if (contains(set)) {
109             set.getSyncInfoSet().removeSyncSetChangedListener(getChangeSetChangeListener());
110             sets.remove(set);
111             Object JavaDoc[] listeners = getListeners();
112             for (int i = 0; i < listeners.length; i++) {
113                 final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
114                 Platform.run(new ISafeRunnable() {
115                     public void handleException(Throwable JavaDoc exception) {
116                         // Exceptions are logged by the platform
117
}
118                     public void run() throws Exception JavaDoc {
119                         listener.setRemoved(set);
120                     }
121                 });
122             }
123         }
124     }
125     
126     /**
127      * Return the change listener that will be registered with each
128      * <code>SyncInfoSet</code> associated with the <code>ChangeSets</code>
129      * added to this collector.
130      * @return the change listener that will be registered with each
131      * <code>SyncInfoSet</code> associated with the <code>ChangeSets</code>
132      * added to this collector
133      */

134     protected abstract ISyncInfoSetChangeListener getChangeSetChangeListener();
135
136     /**
137      * Return whether the manager contains the given commit set
138      * @param set the commit set being tested
139      * @return whether the set is contained in the manager's list of active sets
140      */

141     public boolean contains(ChangeSet set) {
142         return sets.contains(set);
143     }
144
145     /**
146      * Add the listener to the set of registered listeners.
147      * @param listener the listener to be added
148      */

149     public void addListener(IChangeSetChangeListener listener) {
150         listeners.add(listener);
151     }
152
153     /**
154      * Remove the listener from the set of registered listeners.
155      * @param listener the listener to remove
156      */

157     public void removeListener(IChangeSetChangeListener listener) {
158         listeners.remove(listener);
159     }
160
161     /**
162      * Return the list of active commit sets.
163      * @return the list of active commit sets
164      */

165     public ChangeSet[] getSets() {
166         return (ChangeSet[]) sets.toArray(new ChangeSet[sets.size()]);
167     }
168     
169     /**
170      * Dispose of any resources maintained by the manager
171      */

172     public void dispose() {
173         // Nothing to do
174
}
175
176     /**
177      * @param changeSet
178      * @param allAffectedResources
179      */

180     protected void fireResourcesChangedEvent(final ChangeSet changeSet, final IResource[] allAffectedResources) {
181         Object JavaDoc[] listeners = getListeners();
182         for (int i = 0; i < listeners.length; i++) {
183             final IChangeSetChangeListener listener = (IChangeSetChangeListener)listeners[i];
184             Platform.run(new ISafeRunnable() {
185                 public void handleException(Throwable JavaDoc exception) {
186                     // Exceptions are logged by the platform
187
}
188                 public void run() throws Exception JavaDoc {
189                     listener.resourcesChanged(changeSet, allAffectedResources);
190                 }
191             });
192         }
193     }
194     
195     /**
196      * Return the Change Set whose sync info set is the
197      * one given.
198      * @param set a sync info set
199      * @return the change set for the given sync info set
200      */

201     protected ChangeSet getChangeSet(SyncInfoSet set) {
202         for (Iterator iter = sets.iterator(); iter.hasNext();) {
203             ChangeSet changeSet = (ChangeSet) iter.next();
204             if (changeSet.getSyncInfoSet() == set) {
205                 return changeSet;
206             }
207         }
208         return null;
209     }
210 }
211
Popular Tags