KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > TeamStateChangeEvent


1 /*******************************************************************************
2  * Copyright (c) 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.ui.synchronize;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.team.core.subscribers.ISubscriberChangeEvent;
18 import org.eclipse.team.ui.mapping.ITeamStateChangeEvent;
19
20 /**
21  * An implementation of {@link ITeamStateChangeEvent}.
22  * <p>
23  * This class is not intended to be subclassed by clients.
24  *
25  * @since 3.2
26  */

27 public class TeamStateChangeEvent implements ITeamStateChangeEvent {
28
29     private Set JavaDoc changes = new HashSet JavaDoc();
30     private Set JavaDoc addedRoots = new HashSet JavaDoc();
31     private Set JavaDoc removedRoots = new HashSet JavaDoc();
32     
33     public TeamStateChangeEvent() {
34         super();
35     }
36     
37     /**
38      * Convenience constructor for creating an event from a subscriber change.
39      * @param deltas the set of subscriber changes
40      */

41     public TeamStateChangeEvent(ISubscriberChangeEvent[] deltas) {
42         for (int i = 0; i < deltas.length; i++) {
43             ISubscriberChangeEvent event = deltas[i];
44             IResource resource = event.getResource();
45             if ((event.getFlags() & ISubscriberChangeEvent.ROOT_ADDED) != 0)
46                 rootAdded(resource);
47             if ((event.getFlags() & ISubscriberChangeEvent.ROOT_REMOVED) != 0)
48                 rootRemoved(resource);
49             if ((event.getFlags() & ISubscriberChangeEvent.SYNC_CHANGED) != 0)
50                 changed(resource);
51             // Indicate that the ancestors may have changed as well
52
while (resource.getType() != IResource.PROJECT) {
53                 resource = resource.getParent();
54                 changed(resource);
55             }
56         }
57     }
58
59     /**
60      * The given resource has changed state.
61      * @param resource the resource whose state has changed
62      */

63     public void changed(IResource resource) {
64         changes.add(resource);
65     }
66
67     /**
68      * The given root resource has been removed.
69      * @param resource the resource
70      */

71     public void rootRemoved(IResource resource) {
72         removedRoots.add(resource);
73     }
74
75     /**
76      * The given root resource has been added.
77      * @param resource the resource
78      */

79     public void rootAdded(IResource resource) {
80         addedRoots.add(resource);
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.team.ui.mapping.IDecoratedStateChangeEvent#getAddedRoots()
85      */

86     public IResource[] getAddedRoots() {
87         return (IResource[]) addedRoots.toArray(new IResource[addedRoots.size()]);
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.team.ui.mapping.IDecoratedStateChangeEvent#getRemovedRoots()
92      */

93     public IResource[] getRemovedRoots() {
94         return (IResource[]) removedRoots.toArray(new IResource[removedRoots.size()]);
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.team.ui.mapping.IDecoratedStateChangeEvent#getChangedResources()
99      */

100     public IResource[] getChangedResources() {
101         return (IResource[]) changes.toArray(new IResource[changes.size()]);
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.team.ui.mapping.IDecoratedStateChangeEvent#hasChange(org.eclipse.core.resources.IResource)
106      */

107     public boolean hasChange(IResource resource) {
108         if (changes.contains(resource))
109             return true;
110         if (isChildOfChangedRoot(resource)) {
111             return true;
112         }
113         return false;
114     }
115
116     private boolean isChildOfChangedRoot(IResource resource) {
117         if (resource == null || resource.getType() == IResource.ROOT)
118             return false;
119         if (addedRoots.contains(resource) || removedRoots.contains(resource))
120             return true;
121         return isChildOfChangedRoot(resource.getParent());
122     }
123
124 }
125
Popular Tags