KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.mapping.ResourceMapping;
17 import org.eclipse.core.resources.mapping.ResourceTraversal;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.team.core.mapping.ISynchronizationScope;
20 import org.eclipse.team.core.mapping.ISynchronizationScopeChangeListener;
21
22 /**
23  * An abstract implementation of an {@link ISynchronizationScope}.
24  *
25  * @since 3.2
26  */

27 public abstract class AbstractSynchronizationScope implements ISynchronizationScope {
28
29     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
30
31     /* (non-Javadoc)
32      * @see org.eclipse.team.core.mapping.ISynchronizationScope#getRoots()
33      */

34     public IResource[] getRoots() {
35         List result = new ArrayList();
36         ResourceTraversal[] traversals = getTraversals();
37         for (int i = 0; i < traversals.length; i++) {
38             ResourceTraversal traversal = traversals[i];
39             IResource[] resources = traversal.getResources();
40             for (int j = 0; j < resources.length; j++) {
41                 IResource resource = resources[j];
42                 accumulateRoots(result, resource);
43             }
44         }
45         return (IResource[]) result.toArray(new IResource[result.size()]);
46     }
47     
48     /* (non-Javadoc)
49      * @see org.eclipse.team.core.mapping.ISynchronizationScope#contains(org.eclipse.core.resources.IResource)
50      */

51     public boolean contains(IResource resource) {
52         ResourceTraversal[] traversals = getTraversals();
53         for (int i = 0; i < traversals.length; i++) {
54             ResourceTraversal traversal = traversals[i];
55             if (traversal.contains(resource))
56                 return true;
57         }
58         return false;
59     }
60     
61     /*
62      * Add the resource to the list if it isn't there already
63      * or is not a child of an existing resource.
64      */

65     private void accumulateRoots(List roots, IResource resource) {
66         IPath resourcePath = resource.getFullPath();
67         for (Iterator iter = roots.iterator(); iter.hasNext();) {
68             IResource root = (IResource) iter.next();
69             IPath rootPath = root.getFullPath();
70             // If there is a higher resource in the collection, skip this one
71
if (rootPath.isPrefixOf(resourcePath))
72                 return;
73             // If there are lower resources, remove them
74
if (resourcePath.isPrefixOf(rootPath))
75                 iter.remove();
76         }
77         // There were no higher resources, so add this one
78
roots.add(resource);
79     }
80     
81     /**
82      * Fire the scope change event
83      * @param newTraversals the new traversals (may be empty)
84      * @param newMappings the new mappings (may be empty)
85      */

86     public void fireTraversalsChangedEvent(final ResourceTraversal[] newTraversals, final ResourceMapping[] newMappings) {
87         Object JavaDoc[] allListeners = listeners.getListeners();
88         for (int i = 0; i < allListeners.length; i++) {
89             final Object JavaDoc listener = allListeners[i];
90             SafeRunner.run(new ISafeRunnable() {
91                 public void run() throws Exception JavaDoc {
92                     ((ISynchronizationScopeChangeListener)listener).scopeChanged(AbstractSynchronizationScope.this, newMappings, newTraversals);
93                 }
94                 public void handleException(Throwable JavaDoc exception) {
95                     // Logged by Platform
96
}
97             });
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.team.core.mapping.ISynchronizationScope#addPropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener)
103      */

104     public void addScopeChangeListener(ISynchronizationScopeChangeListener listener) {
105         listeners.add(listener);
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.team.core.mapping.ISynchronizationScope#removePropertyChangeListener(org.eclipse.core.runtime.Preferences.IPropertyChangeListener)
110      */

111     public void removeScopeChangeListener(ISynchronizationScopeChangeListener listener) {
112         listeners.remove(listener);
113     }
114
115 }
116
Popular Tags