KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.ResourceMapping;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.ListenerList;
20 import org.eclipse.jface.util.*;
21 import org.eclipse.team.internal.ui.TeamUIPlugin;
22 import org.eclipse.ui.IMemento;
23
24 /**
25  * Abstract superclass of resource scopes for <code>SubscriberParticipant</code>
26  * instances.
27  * <p>
28  * Clients are not expected to subclass this class.
29  * </p>
30  * @see SubscriberParticipant
31  * @since 3.0
32  */

33 public abstract class AbstractSynchronizeScope implements ISynchronizeScope {
34     
35     /*
36      * Key for scope in memento
37      */

38     private static final String JavaDoc CTX_SUBSCRIBER_SCOPE_TYPE = TeamUIPlugin.ID + ".SCOPE_TYPE"; //$NON-NLS-1$
39

40     /*
41      * Scope change listeners
42      */

43     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
44     
45     /**
46      * Save the scope to the given memento
47      *
48      * @param scope a scope
49      * @param settings a memento
50      */

51     protected static void saveScope(ISynchronizeScope scope, IMemento settings) {
52         settings.putString(CTX_SUBSCRIBER_SCOPE_TYPE, getType(scope));
53         ((AbstractSynchronizeScope)scope).saveState(settings);
54     }
55     
56     /**
57      * Restore a scope from the given memento
58      *
59      * @param settings a memento
60      * @return the scope restored from the given memento
61      */

62     protected static ISynchronizeScope createScope(IMemento settings) {
63         String JavaDoc type = settings.getString(CTX_SUBSCRIBER_SCOPE_TYPE);
64         if (type == null) {
65             return new WorkspaceScope();
66         }
67         if (type.equals("ResourceScope")) { //$NON-NLS-1$
68
return new ResourceScope(settings);
69         }
70         if (type.equals("WorkingSetScope")) { //$NON-NLS-1$
71
return new WorkingSetScope(settings);
72         }
73         return new WorkspaceScope();
74     }
75     
76     private static String JavaDoc getType(ISynchronizeScope scope) {
77         String JavaDoc name = scope.getClass().getName();
78         int lastDot = name.lastIndexOf("."); //$NON-NLS-1$
79
if (lastDot == -1) {
80             return name;
81         }
82         return name.substring(lastDot + 1);
83     }
84     
85     /**
86      * Constructor a scope from scratch
87      */

88     protected AbstractSynchronizeScope() {
89     }
90     
91     /**
92      * Constructor a scope from a previously saved state
93      */

94     protected AbstractSynchronizeScope(IMemento memento) {
95         init(memento);
96     }
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
100      */

101     public void addPropertyChangeListener(IPropertyChangeListener listener) {
102         synchronized(listeners) {
103             listeners.add(listener);
104         }
105     }
106     
107     /* (non-Javadoc)
108      * @see org.eclipse.team.internal.ui.synchronize.ScopableSubscriberParticipant.ISynchronizeScope#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
109      */

110     public void removePropertyChangeListener(IPropertyChangeListener listener) {
111         synchronized(listeners) {
112             listeners.remove(listeners);
113         }
114     }
115     
116     /* (non-Javadoc)
117      * @see org.eclipse.team.ui.synchronize.ISynchronizeScope#dispose()
118      */

119     public void dispose() {
120         // Do nothing by default
121
}
122     
123     /**
124      * Fires the given property change event to all registered listeners.
125      *
126      * @param event the property change event to be fired
127      */

128     protected void firePropertyChangedEvent(final PropertyChangeEvent event) {
129         Object JavaDoc[] allListeners;
130         synchronized(listeners) {
131             allListeners = listeners.getListeners();
132         }
133         for (int i = 0; i < allListeners.length; i++) {
134             final IPropertyChangeListener listener = (IPropertyChangeListener)allListeners[i];
135             SafeRunner.run(new SafeRunnable() {
136                 public void run() throws Exception JavaDoc {
137                     listener.propertyChange(event);
138                 }
139             });
140         }
141     }
142     /**
143      * Fires a change event for property <code>ISynchronizeScope.ROOTS</code>
144      * containing the new roots. The old roots are not provided in the event.
145      */

146     protected void fireRootsChanges() {
147         firePropertyChangedEvent(new PropertyChangeEvent(this, ROOTS, new IResource[0], getRoots()));
148     }
149     
150     /**
151      * Persist the state of this scope. Clients must persist enough additional
152      * state to know what type (i.e. subclass) of scope to be recreated.
153      *
154      * @param memento the memento into which the scope is to be saved
155      */

156     public void saveState(IMemento memento) {
157         // Do nothing by default
158
}
159     
160     /**
161      * Method invoked from the constructor which populates the fields of this scope
162      *
163      * @param memento the memento into which the scope was previously saved
164      */

165     protected void init(IMemento memento) {
166         // Do nothing by default
167
}
168     
169     /**
170      * Return whether the given resource is within this scope.
171      * By default, a resource is considered in the scope if
172      * it is a root or a descendant of a root.
173      * @param resource the resource
174      * @return whether the given resource is within this scope
175      * @since 3.2
176      */

177     public boolean contains(IResource resource) {
178         IResource[] roots = getRoots();
179         IPath resourcePath = resource.getFullPath();
180         for (int i = 0; i < roots.length; i++) {
181             IResource root = roots[i];
182             if (root.getFullPath().isPrefixOf(resourcePath)) {
183                 return true;
184             }
185         }
186         return false;
187     }
188     
189     /**
190      * Return the resource mappings that define this scope.
191      * By default, the mappings are just be deep traversals
192      * of the roots of the scope but subclasses may override.
193      * @return the resource mappings that define this scope
194      * @since 3.2
195      */

196     public ResourceMapping[] getMappings() {
197         List JavaDoc result = new ArrayList JavaDoc();
198         IResource[] roots = getRoots();
199         for (int i = 0; i < roots.length; i++) {
200             IResource resource = roots[i];
201             result.add(resource.getAdapter(ResourceMapping.class));
202         }
203         return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
204     }
205 }
206
Popular Tags