KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceModelScopeParticipant


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.internal.ui.mapping;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.resources.mapping.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.team.core.mapping.*;
23 import org.eclipse.team.internal.ui.TeamUIPlugin;
24 import org.eclipse.team.internal.ui.Utils;
25 import org.eclipse.ui.*;
26
27 public class ResourceModelScopeParticipant implements
28         ISynchronizationScopeParticipant, IResourceChangeListener, IPropertyChangeListener {
29
30     private final ModelProvider provider;
31     private final ISynchronizationScope scope;
32
33     public ResourceModelScopeParticipant(ModelProvider provider, ISynchronizationScope scope) {
34         this.provider = provider;
35         this.scope = scope;
36         if (hasWorkspaceMapping()) {
37             ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
38         }
39         if (hasWorkingSetMappings()) {
40             PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(this);
41         }
42     }
43
44     private boolean hasWorkingSetMappings() {
45         ResourceMapping[] mappings = scope.getMappings(provider.getDescriptor().getId());
46         for (int i = 0; i < mappings.length; i++) {
47             ResourceMapping mapping = mappings[i];
48             Object JavaDoc modelObject = mapping.getModelObject();
49             if (modelObject instanceof IWorkingSet) {
50                 return true;
51             }
52         }
53         return false;
54     }
55
56     private boolean hasWorkspaceMapping() {
57         ResourceMapping[] mappings = scope.getMappings(provider.getDescriptor().getId());
58         for (int i = 0; i < mappings.length; i++) {
59             ResourceMapping mapping = mappings[i];
60             Object JavaDoc modelObject = mapping.getModelObject();
61             if (modelObject instanceof IResource) {
62                 IResource resource = (IResource) modelObject;
63                 if (resource.getType() == IResource.ROOT) {
64                     return true;
65                 }
66             } else if (modelObject instanceof ModelProvider) {
67                 ModelProvider provider = (ModelProvider) modelObject;
68                 if (provider.getId().equals(ModelProvider.RESOURCE_MODEL_PROVIDER_ID)) {
69                     return true;
70                 }
71             }
72         }
73         return false;
74     }
75
76     public ResourceMapping[] handleContextChange(
77             ISynchronizationScope scope, IResource[] resources,
78             IProject[] projects) {
79         Set JavaDoc result = new HashSet JavaDoc();
80         for (int i = 0; i < projects.length; i++) {
81             IProject project = projects[i];
82             collectMappings(project, result);
83         }
84         return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
85     }
86
87     private void collectMappings(IProject project, Set JavaDoc result) {
88         ResourceMapping[] mappings = scope.getMappings(provider.getDescriptor().getId());
89         for (int i = 0; i < mappings.length; i++) {
90             ResourceMapping mapping = mappings[i];
91             boolean refresh = false;
92             Object JavaDoc modelObject = mapping.getModelObject();
93             if (modelObject instanceof IWorkingSet) {
94                 IWorkingSet set = (IWorkingSet)modelObject;
95                 IAdaptable[] elements = set.getElements();
96                 for (int j = 0; j < elements.length; j++) {
97                     IAdaptable adaptable = elements[j];
98                     ResourceMapping m = (ResourceMapping)Utils.getAdapter(adaptable, ResourceMapping.class);
99                     if (m != null) {
100                         IProject[] p = m.getProjects();
101                         for (int k = 0; k < p.length; k++) {
102                             IProject mp = p[k];
103                             if (mp.equals(project)) {
104                                 refresh = true;
105                                 break;
106                             }
107                         }
108                     }
109                     if (refresh)
110                         break;
111                 }
112             } else if (modelObject instanceof IResource) {
113                 IResource resource = (IResource) modelObject;
114                 if (resource.getType() == IResource.ROOT) {
115                     refresh = true;
116                 }
117             } else if (modelObject instanceof ModelProvider) {
118                 ModelProvider mp = (ModelProvider) modelObject;
119                 try {
120                     ResourceMapping[] list = mp.getMappings(project, ResourceMappingContext.LOCAL_CONTEXT, null);
121                     if (list.length > 0) {
122                         refresh = true;
123                     }
124                 } catch (CoreException e) {
125                     TeamUIPlugin.log(e);
126                 }
127             }
128             if (refresh) {
129                 result.add(mapping);
130             }
131         }
132     }
133
134     public void dispose() {
135         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
136         if (PlatformUI.isWorkbenchRunning())
137             PlatformUI.getWorkbench().getWorkingSetManager().removePropertyChangeListener(this);
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
142      */

143     public void resourceChanged(IResourceChangeEvent event) {
144         // Only interested in project additions and removals
145
Set JavaDoc result = new HashSet JavaDoc();
146         IResourceDelta[] children = event.getDelta().getAffectedChildren();
147         for (int i = 0; i < children.length; i++) {
148             IResourceDelta delta = children[i];
149             IResource resource = delta.getResource();
150             if (resource.getType() == IResource.PROJECT
151                     && ((delta.getKind() & (IResourceDelta.ADDED | IResourceDelta.REMOVED)) != 0
152                         || (delta.getFlags() & IResourceDelta.OPEN) != 0)) {
153                 if (isInContext(resource))
154                     collectMappings((IProject)resource, result);
155             }
156         }
157         if (!result.isEmpty())
158             fireChange((ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]));
159             
160             
161     }
162
163     private boolean isInContext(IResource resource) {
164         IProject[] projects = scope.getProjects();
165         for (int i = 0; i < projects.length; i++) {
166             IProject project = projects[i];
167             if (project.equals(resource.getProject())) {
168                 return true;
169             }
170         }
171         return false;
172     }
173
174     private void fireChange(ResourceMapping[] mappings) {
175         scope.refresh(mappings);
176     }
177
178     public void propertyChange(PropertyChangeEvent event) {
179         if (event.getProperty() == IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE) {
180             IWorkingSet newSet = (IWorkingSet) event.getNewValue();
181             ResourceMapping[] mappings = scope.getMappings(provider.getDescriptor().getId());
182             for (int i = 0; i < mappings.length; i++) {
183                 ResourceMapping mapping = mappings[i];
184                 if (newSet == mapping.getModelObject()) {
185                     fireChange(new ResourceMapping[] { mapping });
186                 }
187             }
188         } else if(event.getProperty() == IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE) {
189             // TODO: Need to update the participant description somehow
190
//firePropertyChangedEvent(new PropertyChangeEvent(this, NAME, null, event.getNewValue()));
191
}
192     }
193
194 }
195
Popular Tags