1 11 package org.eclipse.team.internal.ui.dialogs; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 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 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 cachedFiltering = new HashMap (); private String message; 48 49 private static IWorkbenchAdapter getWorkbenchAdapter(IAdaptable o) { 50 return (IWorkbenchAdapter)o.getAdapter(IWorkbenchAdapter.class); 51 } 52 53 60 public static String getLabel(ResourceMapping mapping) { 61 Object o = mapping; 62 IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter((IAdaptable)o); 63 if (workbenchAdapter == null) { 64 Object 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 88 public Object [] getChildren(Object o) { 89 ResourceTraversal[] traversals = getTraversals(); 90 List result = new ArrayList (); 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 [result.size()]); 101 } 102 103 private ResourceTraversal[] getTraversals() { 104 return ResourceMappingResourceDisplayArea.getTraversals(mapping, context); 105 } 106 107 110 public ImageDescriptor getImageDescriptor(Object o) { 111 o = mapping; 112 IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter((IAdaptable)o); 113 if (workbenchAdapter == null) { 114 Object 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 129 public String getLabel(Object o) { 130 return ResourceMappingResourceDisplayArea.getLabel(mapping); 131 } 132 133 136 public Object getParent(Object o) { 137 return null; 138 } 139 140 143 public Object getAdapter(Class adapter) { 144 if (adapter == IWorkbenchAdapter.class) 145 return this; 146 return null; 147 } 148 } 149 150 153 public class ResourceTraversalElement implements IWorkbenchAdapter, IAdaptable { 154 private ResourceTraversal traversal; 155 private ResourceMappingContext context; 156 private IResource resource; 157 private Object parent; 158 159 public ResourceTraversalElement(Object 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 169 public Object [] getChildren(Object 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 [0]; 176 } 177 178 private Object [] getChildren(boolean includeFolders) { 179 try { 180 if (resource.getType() != IResource.FILE) { 181 IResource[] members = members(((IContainer)resource)); 182 List result = new ArrayList (); 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 [result.size()]); 190 } 191 } catch (CoreException e) { 192 TeamUIPlugin.log(IStatus.ERROR, "An error occurred fetching the members of " + resource.getFullPath(), e); } 194 return new Object [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 208 public ImageDescriptor getImageDescriptor(Object object) { 209 IWorkbenchAdapter workbenchAdapter = getWorkbenchAdapter(resource); 210 if (workbenchAdapter == null) 211 return null; 212 return workbenchAdapter.getImageDescriptor(resource); 213 } 214 215 218 public String getLabel(Object 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 234 public Object getParent(Object o) { 235 return parent; 236 } 237 238 241 public Object getAdapter(Class adapter) { 242 if (adapter == IWorkbenchAdapter.class) 243 return this; 244 return null; 245 } 246 public IResource getResource() { 247 return resource; 248 } 249 } 250 251 257 public ResourceMappingResourceDisplayArea(ResourceMapping mapping, String string, IResourceMappingResourceFilter filter) { 258 this.mapping = mapping; 259 this.filter = filter; 260 this.message = string; 261 } 262 263 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 o1, Object 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 labelText) { 291 if (viewer != null) { 292 Object 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 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 mappingResources = (Map )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 buildFilteredResourceMap(final ResourceMapping mapping, final ResourceMappingContext context) { 320 final Map result = new HashMap (); 321 try { 322 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 323 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 (e); 333 } finally { 334 monitor.done(); 335 } 336 } 337 338 private void buildFilteredResourceMap(final ResourceMapping mapping, final ResourceTraversal traversal, IProgressMonitor monitor, final Map result) throws CoreException { 339 traversal.accept(new IResourceVisitor() { 340 public boolean visit(IResource resource) throws CoreException { 341 if (filter.select(resource, mapping, traversal)) { 342 result.put(resource, new ArrayList ()); 344 IResource child = resource; 346 while (!isTraversalRoot(traversal, child)) { 347 IContainer parent = child.getParent(); 348 List children = (List )result.get(parent); 349 if (children == null) { 350 children = new ArrayList (); 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 e) { 364 TeamUIPlugin.log(IStatus.ERROR, "An error occurred while filtering " + getLabel(mapping), e); } catch (InterruptedException e) { 366 } 368 return result; 369 } 370 371 static ResourceTraversal[] getTraversals(final ResourceMapping mapping, final ResourceMappingContext context) { 372 final List traversals = new ArrayList (); 373 try { 374 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 375 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 376 try { 377 traversals.add(mapping.getTraversals(context, monitor)); 378 } catch (CoreException e) { 379 throw new InvocationTargetException (e); 380 } 381 } 382 }); 383 return (ResourceTraversal[])traversals.get(0); 384 } catch (InvocationTargetException e) { 385 TeamUIPlugin.log(IStatus.ERROR, "An error occurred while traversing " + getLabel(mapping), e); } catch (InterruptedException e) { 387 } 389 return new ResourceTraversal[0]; 390 } 391 392 static IResource[] members(final IContainer container, final RemoteResourceMappingContext context) { 393 final List members = new ArrayList (); 394 try { 395 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 396 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 397 try { 398 members.add(context.fetchMembers(container, monitor)); 399 } catch (CoreException e) { 400 throw new InvocationTargetException (e); 401 } 402 } 403 }); 404 return (IResource[])members.get(0); 405 } catch (InvocationTargetException e) { 406 TeamUIPlugin.log(IStatus.ERROR, "An error occurred while fetching the members of" + container.getFullPath(), e); } catch (InterruptedException e) { 408 } 410 return new IResource[0]; 411 } 412 413 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 |