KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.runtime.*;
16
17 /**
18  * An abstract class that managers a collection of change sets.
19  */

20 public abstract class ChangeSetManager {
21
22     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
23     private Set sets;
24     private boolean initializing;
25     
26     /**
27      * Return the list of listeners registered with this change set manager.
28      * @return the list of listeners registered with this change set manager
29      */

30     protected Object JavaDoc[] getListeners() {
31         return listeners.getListeners();
32     }
33     
34     /**
35      * Method that can be invoked by subclasses when the name of
36      * a managed change set changes.
37      * @param set the set whose title has changed
38      */

39     protected void fireNameChangedEvent(final ChangeSet set) {
40         if (initializing)
41             return;
42         if (contains(set)) {
43             Object JavaDoc[] 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 JavaDoc exception) {
48                         // Exceptions are logged by the platform
49
}
50                     public void run() throws Exception JavaDoc {
51                         listener.nameChanged(set);
52                     }
53                 });
54             }
55         }
56     }
57     
58     /**
59      * Method which allows subclasses to notify listeners that the default
60      * set has changed.
61      * @param oldSet the previous default
62      * @param defaultSet the new default
63      */

64     protected void fireDefaultChangedEvent(final ChangeSet oldSet, final ChangeSet defaultSet) {
65         if (initializing)
66             return;
67         Object JavaDoc[] 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 JavaDoc exception) {
72                     // Exceptions are logged by the platform
73
}
74                 public void run() throws Exception JavaDoc {
75                     listener.defaultSetChanged(oldSet, defaultSet);
76                 }
77             });
78         }
79     }
80     
81     /**
82      * Add the set to the list of active sets.
83      * @param set the set to be added
84      */

85     public void add(final ChangeSet set) {
86         if (!contains(set)) {
87             internalGetSets().add(set);
88             handleSetAdded(set);
89         }
90     }
91
92     /**
93      * Handle the set addition by notifying listeners.
94      * @param set the added set
95      */

96     protected void handleSetAdded(final ChangeSet set) {
97         if (initializing)
98             return;
99         Object JavaDoc[] 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 JavaDoc exception) {
104                     // Exceptions are logged by the platform
105
}
106                 public void run() throws Exception JavaDoc {
107                     listener.setAdded(set);
108                 }
109             });
110         }
111     }
112
113     /**
114      * Remove the set from the list of active sets.
115      * @param set the set to be removed
116      */

117     public void remove(final ChangeSet set) {
118         if (contains(set)) {
119             internalGetSets().remove(set);
120             handleSetRemoved(set);
121         }
122     }
123
124     /**
125      * Handle the set removal by notifying listeners.
126      * @param set the removed set
127      */

128     protected void handleSetRemoved(final ChangeSet set) {
129         if (initializing)
130             return;
131         Object JavaDoc[] 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 JavaDoc exception) {
136                     // Exceptions are logged by the platform
137
}
138                 public void run() throws Exception JavaDoc {
139                     listener.setRemoved(set);
140                 }
141             });
142         }
143     }
144
145     /**
146      * Return whether the manager contains the given commit set
147      * @param set the commit set being tested
148      * @return whether the set is contained in the manager's list of active sets
149      */

150     public boolean contains(ChangeSet set) {
151         return internalGetSets().contains(set);
152     }
153
154     /**
155      * Add the listener to the set of registered listeners.
156      * @param listener the listener to be added
157      */

158     public void addListener(IChangeSetChangeListener listener) {
159         listeners.add(listener);
160     }
161
162     /**
163      * Remove the listener from the set of registered listeners.
164      * @param listener the listener to remove
165      */

166     public void removeListener(IChangeSetChangeListener listener) {
167         listeners.remove(listener);
168     }
169
170     /**
171      * Return the sorted list of active commit sets.
172      * @return the sorted list of active commit sets
173      */

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 JavaDoc a, Object JavaDoc 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     /**
189      * Return the list of active commit sets.
190      * @return the list of active commit sets
191      */

192     public ChangeSet[] getSets() {
193         Set sets = internalGetSets();
194         return (ChangeSet[]) sets.toArray(new ChangeSet[sets.size()]);
195     }
196     
197     /**
198      * Dispose of any resources maintained by the manager
199      */

200     public void dispose() {
201         // Nothing to do
202
}
203
204     /**
205      * Fire resource change notifications to the listeners.
206      * @param changeSet
207      * @param allAffectedResources
208      */

209     protected void fireResourcesChangedEvent(final ChangeSet changeSet, final IPath[] allAffectedResources) {
210         if (initializing)
211             return;
212         Object JavaDoc[] 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 JavaDoc exception) {
217                     // Exceptions are logged by the platform
218
}
219                 public void run() throws Exception JavaDoc {
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     /**
240      * Initialize the sets contained in this manager.
241      * This method is called the first time the sets are accessed.
242      */

243     protected abstract void initializeSets();
244     
245     public boolean isInitialized() {
246         return sets != null;
247     }
248 }
249
Popular Tags