KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > dialogs > ResourceMappingResourceDisplayArea


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.ui.dialogs;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.resources.mapping.*;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.team.internal.core.Policy;
31 import org.eclipse.team.internal.ui.TeamUIPlugin;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.model.*;
34 import org.eclipse.ui.views.navigator.ResourceSorter;
35
36 /**
37  * Dialog area which displays the resources for a resource mapping
38  */

39 public class ResourceMappingResourceDisplayArea extends DialogArea {
40
41     private ResourceMapping mapping;
42     private ResourceMappingContext context = ResourceMappingContext.LOCAL_CONTEXT;
43     private TreeViewer viewer;
44     private Label label;
45     private IResourceMappingResourceFilter filter;
46     private Map JavaDoc cachedFiltering = new HashMap JavaDoc(); // String(mapping)-> Map: Resource -> List(IResource)
47
private String JavaDoc message;
48     
49     private static IWorkbenchAdapter getWorkbenchAdapter(IAdaptable o) {
50         return (IWorkbenchAdapter)o.getAdapter(IWorkbenchAdapter.class);
51     }
52     
53     /**
54      * Return the label that should be used for the given mapping
55      * as determined using the IWorkbnchAdaptable for the mapping
56      * or it's model object.
57      * @param mapping the mappings
58      * @return it's label
59      */

60     public static String JavaDoc getLabel(ResourceMapping mapping) {
61         Object JavaDoc o = mapping;
62         IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter((IAdaptable)o);
63         if (workbenchAdapter == null) {
64             Object JavaDoc modelObject = mapping.getModelObject();
65             if (modelObject instanceof IAdaptable) {
66                 workbenchAdapter = getWorkbenchAdapter((IAdaptable)modelObject);
67                 o = modelObject;
68             }
69         }
70         if (workbenchAdapter == null) {
71             return mapping.toString();
72         }
73         return workbenchAdapter.getLabel(o);
74     }
75     
76     public class ResourceMappingElement implements IWorkbenchAdapter, IAdaptable {
77         private ResourceMapping mapping;
78         private ResourceMappingContext context;
79
80         public ResourceMappingElement(ResourceMapping mapping, ResourceMappingContext context) {
81             this.mapping = mapping;
82             this.context = context;
83         }
84         
85         /* (non-Javadoc)
86          * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
87          */

88         public Object JavaDoc[] getChildren(Object JavaDoc o) {
89             ResourceTraversal[] traversals = getTraversals();
90             List JavaDoc result = new ArrayList JavaDoc();
91             for (int i = 0; i < traversals.length; i++) {
92                 ResourceTraversal traversal = traversals[i];
93                 IResource[] resources = traversal.getResources();
94                 for (int j = 0; j < resources.length; j++) {
95                     IResource resource = resources[j];
96                     if (isIncludedInFilter(resource, traversal))
97                         result.add(new ResourceTraversalElement(this, traversal, resource, context));
98                 }
99             }
100             return result.toArray(new Object JavaDoc[result.size()]);
101         }
102
103         private ResourceTraversal[] getTraversals() {
104             return ResourceMappingResourceDisplayArea.getTraversals(mapping, context);
105         }
106
107         /* (non-Javadoc)
108          * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
109          */

110         public ImageDescriptor getImageDescriptor(Object JavaDoc o) {
111             o = mapping;
112             IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter((IAdaptable)o);
113             if (workbenchAdapter == null) {
114                 Object JavaDoc modelObject = mapping.getModelObject();
115                 if (modelObject instanceof IAdaptable) {
116                     workbenchAdapter = getWorkbenchAdapter((IAdaptable)modelObject);
117                     o = modelObject;
118                 }
119             }
120             if (workbenchAdapter == null) {
121                 return null;
122             }
123             return workbenchAdapter.getImageDescriptor(o);
124         }
125
126         /* (non-Javadoc)
127          * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
128          */

129         public String JavaDoc getLabel(Object JavaDoc o) {
130             return ResourceMappingResourceDisplayArea.getLabel(mapping);
131         }
132
133         /* (non-Javadoc)
134          * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
135          */

136         public Object JavaDoc getParent(Object JavaDoc o) {
137             return null;
138         }
139
140         /* (non-Javadoc)
141          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
142          */

143         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
144             if (adapter == IWorkbenchAdapter.class)
145                 return this;
146             return null;
147         }
148     }
149     
150     /**
151      * The model element for resources that are obtained from a traversal.
152      */

153     public class ResourceTraversalElement implements IWorkbenchAdapter, IAdaptable {
154         private ResourceTraversal traversal;
155         private ResourceMappingContext context;
156         private IResource resource;
157         private Object JavaDoc parent;
158         
159         public ResourceTraversalElement(Object JavaDoc parent, ResourceTraversal traversal, IResource resource, ResourceMappingContext context) {
160             this.parent = parent;
161             this.traversal = traversal;
162             this.resource = resource;
163             this.context = context;
164         }
165
166         /* (non-Javadoc)
167          * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
168          */

169         public Object JavaDoc[] getChildren(Object JavaDoc o) {
170             if (traversal.getDepth() == IResource.DEPTH_INFINITE) {
171                 return getChildren(true);
172             } else if (traversal.getDepth() == IResource.DEPTH_ONE && isTraversalRoot(resource)) {
173                 return getChildren(false);
174             }
175             return new Object JavaDoc[0];
176         }
177
178         private Object JavaDoc[] getChildren(boolean includeFolders) {
179             try {
180                 if (resource.getType() != IResource.FILE) {
181                     IResource[] members = members(((IContainer)resource));
182                     List JavaDoc result = new ArrayList JavaDoc();
183                     for (int i = 0; i < members.length; i++) {
184                         IResource child = members[i];
185                         if ((includeFolders || child.getType() == IResource.FILE)
186                                 && isIncludedInFilter(child, traversal))
187                             result.add(new ResourceTraversalElement(this, traversal, child, context));
188                     }
189                     return result.toArray(new Object JavaDoc[result.size()]);
190                 }
191             } catch (CoreException e) {
192                 TeamUIPlugin.log(IStatus.ERROR, "An error occurred fetching the members of " + resource.getFullPath(), e); //$NON-NLS-1$
193
}
194             return new Object JavaDoc[0];
195         }
196
197         private IResource[] members(IContainer container) throws CoreException {
198             if (context instanceof RemoteResourceMappingContext) {
199                 RemoteResourceMappingContext remoteContext = (RemoteResourceMappingContext) context;
200                 return ResourceMappingResourceDisplayArea.members(container, remoteContext);
201             }
202             return container.members();
203         }
204         
205         /* (non-Javadoc)
206          * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
207          */

208         public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
209             IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter(resource);
210             if (workbenchAdapter == null)
211                 return null;
212             return workbenchAdapter.getImageDescriptor(resource);
213         }
214
215         /* (non-Javadoc)
216          * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
217          */

218         public String JavaDoc getLabel(Object JavaDoc o) {
219             if (resource.getType() != IResource.PROJECT && isTraversalRoot(resource))
220                 return resource.getFullPath().toString();
221             IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter(resource);
222             if (workbenchAdapter == null)
223                 return resource.getName();
224             return workbenchAdapter.getLabel(resource);
225         }
226         
227         private boolean isTraversalRoot(IResource resource) {
228             return ResourceMappingResourceDisplayArea.isTraversalRoot(traversal, resource);
229         }
230
231         /* (non-Javadoc)
232          * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
233          */

234         public Object JavaDoc getParent(Object JavaDoc o) {
235             return parent;
236         }
237         
238         /* (non-Javadoc)
239          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
240          */

241         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
242             if (adapter == IWorkbenchAdapter.class)
243                 return this;
244             return null;
245         }
246         public IResource getResource() {
247             return resource;
248         }
249     }
250     
251     /**
252      * Create a dialog area tht will display the resources contained in the
253      * given mapping
254      * @param string
255      * @param filter
256      */

257     public ResourceMappingResourceDisplayArea(ResourceMapping mapping, String JavaDoc string, IResourceMappingResourceFilter filter) {
258         this.mapping = mapping;
259         this.filter = filter;
260         this.message = string;
261     }
262     
263     /* (non-Javadoc)
264      * @see org.eclipse.team.internal.ui.dialogs.DialogArea#createArea(org.eclipse.swt.widgets.Composite)
265      */

266     public void createArea(Composite parent) {
267         Composite composite = createComposite(parent, 1, true);
268         
269         label = createWrappingLabel(composite, message, 1);
270         viewer = new TreeViewer(composite);
271         GridData gridData = new GridData(GridData.FILL_BOTH);
272         gridData.heightHint = 100;
273         viewer.getControl().setLayoutData(gridData);
274         viewer.setContentProvider(new WorkbenchContentProvider());
275         viewer.setLabelProvider(new WorkbenchLabelProvider());
276         viewer.setSorter(new ResourceSorter(ResourceSorter.NAME) {
277             public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
278                 if (o1 instanceof ResourceTraversalElement && o2 instanceof ResourceTraversalElement) {
279                     ResourceTraversalElement e1 = (ResourceTraversalElement) o1;
280                     ResourceTraversalElement e2 = (ResourceTraversalElement) o2;
281                     return super.compare(viewer, e1.getResource(), e2.getResource());
282                 }
283                 return super.compare(viewer, o1, o2);
284             }
285         });
286         setInput(message);
287         Dialog.applyDialogFont(parent);
288     }
289
290     private void setInput(String JavaDoc labelText) {
291         if (viewer != null) {
292             Object JavaDoc o = null;
293             if (mapping != null)
294                 o = new ResourceMappingElement(mapping, context);
295             viewer.setInput(o);
296         }
297         if (label != null) {
298             this.message = labelText;
299             label.setText(labelText);
300         }
301     }
302
303     public void setMapping(ResourceMapping mapping, String JavaDoc labelText) {
304         this.mapping = mapping;
305         setInput(labelText);
306     }
307     
308     private boolean isIncludedInFilter(IResource resource, ResourceTraversal traversal) {
309         if (filter == null)
310             return true;
311         Map JavaDoc mappingResources = (Map JavaDoc)cachedFiltering.get(mapping);
312         if (mappingResources == null) {
313             mappingResources = buildFilteredResourceMap(mapping, context);
314             cachedFiltering.put(mapping, mappingResources);
315         }
316         return mappingResources.containsKey(resource);
317     }
318
319     private Map JavaDoc buildFilteredResourceMap(final ResourceMapping mapping, final ResourceMappingContext context) {
320         final Map JavaDoc result = new HashMap JavaDoc();
321         try {
322             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
323                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
324                     try {
325                         monitor.beginTask(null, IProgressMonitor.UNKNOWN);
326                         ResourceTraversal[] traversals = mapping.getTraversals(context, Policy.subMonitorFor(monitor, IProgressMonitor.UNKNOWN));
327                         for (int i = 0; i < traversals.length; i++) {
328                             ResourceTraversal traversal = traversals[i];
329                             buildFilteredResourceMap(mapping, traversal, Policy.subMonitorFor(monitor, IProgressMonitor.UNKNOWN), result);
330                         }
331                     } catch (CoreException e) {
332                         throw new InvocationTargetException JavaDoc(e);
333                     } finally {
334                         monitor.done();
335                     }
336                 }
337
338                 private void buildFilteredResourceMap(final ResourceMapping mapping, final ResourceTraversal traversal, IProgressMonitor monitor, final Map JavaDoc result) throws CoreException {
339                     traversal.accept(new IResourceVisitor() {
340                         public boolean visit(IResource resource) throws CoreException {
341                             if (filter.select(resource, mapping, traversal)) {
342                                 // Add the resource to the result
343
result.put(resource, new ArrayList JavaDoc());
344                                 // Make sure that there are parent folders for the resource up to the traversal root
345
IResource child = resource;
346                                 while (!isTraversalRoot(traversal, child)) {
347                                     IContainer parent = child.getParent();
348                                     List JavaDoc children = (List JavaDoc)result.get(parent);
349                                     if (children == null) {
350                                         children = new ArrayList JavaDoc();
351                                         result.put(parent, children);
352                                     }
353                                     children.add(child);
354                                     child = parent;
355                                 }
356                             }
357                             return true;
358                         }
359                     });
360                     
361                 }
362             });
363         } catch (InvocationTargetException JavaDoc e) {
364             TeamUIPlugin.log(IStatus.ERROR, "An error occurred while filtering " + getLabel(mapping), e); //$NON-NLS-1$
365
} catch (InterruptedException JavaDoc e) {
366             // Ignore
367
}
368         return result;
369     }
370     
371     /* private */ static ResourceTraversal[] getTraversals(final ResourceMapping mapping, final ResourceMappingContext context) {
372         final List JavaDoc traversals = new ArrayList JavaDoc();
373         try {
374             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
375                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
376                     try {
377                         traversals.add(mapping.getTraversals(context, monitor));
378                     } catch (CoreException e) {
379                         throw new InvocationTargetException JavaDoc(e);
380                     }
381                 }
382             });
383             return (ResourceTraversal[])traversals.get(0);
384         } catch (InvocationTargetException JavaDoc e) {
385             TeamUIPlugin.log(IStatus.ERROR, "An error occurred while traversing " + getLabel(mapping), e); //$NON-NLS-1$
386
} catch (InterruptedException JavaDoc e) {
387             // Ignore
388
}
389         return new ResourceTraversal[0];
390     }
391     
392     /* private */ static IResource[] members(final IContainer container, final RemoteResourceMappingContext context) {
393         final List JavaDoc members = new ArrayList JavaDoc();
394         try {
395             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
396                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
397                     try {
398                         members.add(context.fetchMembers(container, monitor));
399                     } catch (CoreException e) {
400                         throw new InvocationTargetException JavaDoc(e);
401                     }
402                 }
403             });
404             return (IResource[])members.get(0);
405         } catch (InvocationTargetException JavaDoc e) {
406             TeamUIPlugin.log(IStatus.ERROR, "An error occurred while fetching the members of" + container.getFullPath(), e); //$NON-NLS-1$
407
} catch (InterruptedException JavaDoc e) {
408             // Ignore
409
}
410         return new IResource[0];
411     }
412     
413     /* private */ static boolean isTraversalRoot(ResourceTraversal traversal, IResource resource) {
414         IResource[] resources = traversal.getResources();
415         for (int i = 0; i < resources.length; i++) {
416             IResource root = resources[i];
417             if (root.equals(resource)) {
418                 return true;
419             }
420         }
421         return false;
422     }
423 }
424
Popular Tags