1 11 package org.eclipse.team.ui.mapping; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.mapping.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.jface.viewers.*; 24 import org.eclipse.team.core.diff.*; 25 import org.eclipse.team.core.mapping.ISynchronizationContext; 26 import org.eclipse.team.core.mapping.ISynchronizationScope; 27 import org.eclipse.team.internal.core.TeamPlugin; 28 import org.eclipse.team.internal.ui.Utils; 29 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration; 30 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 31 import org.eclipse.ui.IMemento; 32 import org.eclipse.ui.navigator.*; 33 34 39 public abstract class SynchronizationContentProvider implements ICommonContentProvider, IDiffChangeListener, IPropertyChangeListener { 40 41 private Viewer viewer; 42 private boolean empty; 43 private ICommonContentExtensionSite site; 44 45 48 public Object [] getChildren(Object parent) { 49 return internalGetChildren(parent, false); 50 } 51 52 55 public Object [] getElements(Object parent) { 56 return internalGetChildren(parent, true); 57 } 58 59 62 public Object getParent(Object element) { 63 element = internalGetElement(element); 64 if (element instanceof ModelProvider) 65 return null; 66 if (element == getModelRoot()) 67 return null; 68 Object parent = getDelegateContentProvider().getParent(element); 69 if (parent == getModelRoot()) 70 return getModelProvider(); 71 return parent; 72 } 73 74 77 public boolean hasChildren(Object element) { 78 return internalHasChildren(element); 79 } 80 81 private Object [] internalGetChildren(Object parent, boolean isElement) { 82 Object element = internalGetElement(parent); 83 if (element instanceof ISynchronizationScope) { 84 ISynchronizationScope rms = (ISynchronizationScope) element; 86 if (rms.getMappings(getModelProviderId()).length > 0) { 87 empty = false; 88 return new Object [] { getModelProvider() }; 89 } 90 empty = true; 91 return new Object [0]; 92 } else if (element instanceof ISynchronizationContext) { 93 ISynchronizationContext context = (ISynchronizationContext)element; 94 ISynchronizationContext sc = (ISynchronizationContext) element; 96 if (sc.getScope().getMappings(getModelProviderId()).length > 0) { 97 Object root = getModelRoot(); 98 boolean initialized = isInitialized(context); 99 if (!initialized || getChildrenInContext(sc, root, getDelegateChildren(root, isElement)).length > 0) { 100 if (!initialized) 101 requestInitialization(context); 102 empty = false; 103 return new Object [] { getModelProvider() }; 104 } 105 } 106 empty = true; 107 return new Object [0]; 108 } 109 if (element == getModelProvider()) { 110 ISynchronizationContext context = getContext(); 111 if (context != null && !isInitialized(context)) { 112 return new Object [0]; 113 } 114 element = getModelRoot(); 115 if (parent instanceof TreePath) { 116 parent = TreePath.EMPTY.createChildPath(element); 117 } else { 118 parent = element; 119 } 120 } 121 Object [] delegateChildren = getDelegateChildren(parent, isElement); 122 ISynchronizationContext context = getContext(); 123 if (context == null) { 124 ISynchronizationScope scope = getScope(); 125 if (scope == null) { 126 return delegateChildren; 127 } else { 128 return getChildrenInScope(scope, parent, delegateChildren); 129 } 130 } else { 131 return getChildrenInContext(context, parent, delegateChildren); 132 } 133 } 134 135 145 protected boolean isInitialized(ISynchronizationContext context) { 146 return true; 147 } 148 149 159 protected void requestInitialization(ISynchronizationContext context) { 160 } 162 163 170 protected Object [] getDelegateChildren(Object parent) { 171 return getDelegateContentProvider().getChildren(internalGetElement(parent)); 172 } 173 174 private Object [] getDelegateChildren(Object parent, boolean isElement) { 175 if (isElement) 176 return getDelegateContentProvider().getElements(parent); 177 return getDelegateChildren(parent); 178 } 179 180 private boolean internalHasChildren(Object elementOrPath) { 181 Object element = internalGetElement(elementOrPath); 183 if (element instanceof ModelProvider) { 184 element = getModelRoot(); 185 } 186 if (getDelegateContentProvider().hasChildren(element)) { 187 ISynchronizationContext sc = getContext(); 188 if (sc == null) { 189 ISynchronizationScope scope = getScope(); 190 if (scope == null) { 191 return true; 192 } else { 193 return hasChildrenInScope(scope, elementOrPath); 194 } 195 } else { 196 return hasChildrenInContext(sc, elementOrPath); 197 } 198 } else { 199 ISynchronizationContext sc = getContext(); 200 if (sc != null) 201 return hasChildrenInContext(sc, elementOrPath); 202 } 203 return false; 204 } 205 206 218 protected boolean hasChildrenInScope(ISynchronizationScope scope, Object element) { 219 ResourceMapping mapping = Utils.getResourceMapping(internalGetElement(element)); 220 if (mapping != null) { 221 ResourceMapping[] mappings = scope.getMappings(mapping.getModelProviderId()); 222 for (int i = 0; i < mappings.length; i++) { 223 ResourceMapping sm = mappings[i]; 224 if (mapping.contains(sm)) { 225 return true; 226 } 227 if (sm.contains(mapping)) { 228 return getDelegateChildren(element).length > 0; 229 } 230 } 231 } 232 return false; 233 } 234 235 245 protected boolean hasChildrenInContext(ISynchronizationContext context, Object element) { 246 ResourceTraversal[] traversals = getTraversals(context, element); 247 if (traversals == null) 248 return true; 249 return context.getDiffTree().getDiffs(traversals).length > 0; 250 } 251 252 255 public void dispose() { 256 ICommonContentExtensionSite extensionSite = getExtensionSite(); 257 if (extensionSite != null) { 258 extensionSite.getExtensionStateModel().removePropertyChangeListener(this); 259 } 260 ISynchronizationContext context = getContext(); 261 if (context != null) 262 context.getDiffTree().removeDiffChangeListener(this); 263 ISynchronizePageConfiguration configuration = getConfiguration(); 264 if (configuration != null) 265 configuration.removePropertyChangeListener(this); 266 } 267 268 271 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 272 this.viewer = viewer; 273 getDelegateContentProvider().inputChanged(viewer, oldInput, newInput); 274 } 275 276 279 public void init(ICommonContentExtensionSite site) { 280 this.site = site; 282 site.getExtensionStateModel().addPropertyChangeListener(this); 284 ISynchronizePageConfiguration configuration = getConfiguration(); 285 if (configuration != null) 286 configuration.addPropertyChangeListener(this); 287 ITreeContentProvider provider = getDelegateContentProvider(); 288 if (provider instanceof ICommonContentProvider) { 289 ((ICommonContentProvider) provider).init(site); 290 } 291 ISynchronizationContext context = getContext(); 292 if (context != null) 293 context.getDiffTree().addDiffChangeListener(this); 294 } 295 296 299 public void propertyChange(PropertyChangeEvent event) { 300 if (event.getProperty().equals(ISynchronizePageConfiguration.P_MODE)) { 302 refresh(); 303 } 304 } 305 306 322 protected boolean includeDirection(int direction) { 323 ISynchronizePageConfiguration configuration = getConfiguration(); 324 if (configuration != null) 325 return ((SynchronizePageConfiguration)configuration).includeDirection(direction); 326 return true; 327 } 328 329 335 protected ISynchronizationContext getContext() { 336 ICommonContentExtensionSite extensionSite = getExtensionSite(); 337 if (extensionSite != null) 338 return (ISynchronizationContext) extensionSite 339 .getExtensionStateModel() 340 .getProperty( 341 ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT); 342 return null; 343 } 344 345 351 protected ISynchronizationScope getScope() { 352 ICommonContentExtensionSite extensionSite = getExtensionSite(); 353 if (extensionSite != null) 354 return (ISynchronizationScope) extensionSite 355 .getExtensionStateModel() 356 .getProperty( 357 ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE); 358 return null; 359 } 360 361 367 protected ISynchronizePageConfiguration getConfiguration() { 368 ICommonContentExtensionSite extensionSite = getExtensionSite(); 369 if (extensionSite != null) 370 return (ISynchronizePageConfiguration) extensionSite 371 .getExtensionStateModel() 372 .getProperty( 373 ITeamContentProviderManager.P_SYNCHRONIZATION_PAGE_CONFIGURATION); 374 return null; 375 } 376 377 380 public void restoreState(IMemento aMemento) { 381 ITreeContentProvider provider = getDelegateContentProvider(); 382 if (provider instanceof ICommonContentProvider) { 383 ((ICommonContentProvider) provider).restoreState(aMemento); 384 } 385 } 386 387 390 public void saveState(IMemento aMemento) { 391 ITreeContentProvider provider = getDelegateContentProvider(); 392 if (provider instanceof ICommonContentProvider) { 393 ((ICommonContentProvider) provider).saveState(aMemento); 394 } 395 } 396 397 400 public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) { 401 refresh(); 402 } 403 404 407 public void propertyChanged(IDiffTree tree, int property, IPath[] paths) { 408 } 410 411 414 protected void refresh() { 415 Utils.syncExec(new Runnable () { 416 public void run() { 417 TreeViewer treeViewer = ((TreeViewer)getViewer()); 418 if (empty) 420 treeViewer.refresh(); 421 else 422 treeViewer.refresh(getModelProvider()); 423 } 424 425 }, getViewer().getControl()); 426 } 427 428 433 protected abstract ITreeContentProvider getDelegateContentProvider(); 434 435 439 protected final ModelProvider getModelProvider() { 440 try { 441 return ModelProvider.getModelProviderDescriptor(getModelProviderId()).getModelProvider(); 442 } catch (CoreException e) { 443 throw new IllegalStateException (); 445 } 446 } 447 448 452 protected abstract String getModelProviderId(); 453 454 459 protected abstract Object getModelRoot(); 460 461 465 protected final Viewer getViewer() { 466 return viewer; 467 } 468 469 478 protected Object [] getChildrenInScope(ISynchronizationScope scope, Object parent, Object [] children) { 479 List result = new ArrayList (); 480 for (int i = 0; i < children.length; i++) { 481 Object object = children[i]; 482 if (object != null && isInScope(scope, parent, object)) { 483 result.add(object); 484 } 485 } 486 return result.toArray(new Object [result.size()]); 487 } 488 489 501 protected Object [] getChildrenInContext(ISynchronizationContext context, Object parent, Object [] children) { 502 if (children.length != 0) 503 children = getChildrenInScope(context.getScope(), parent, children); 504 if (parent instanceof IResource) { 505 IResource resource = (IResource) parent; 506 children = getChildrenWithPhantoms(context, resource, children); 507 } 508 if (children.length == 0) 509 return children; 510 return internalGetChildren(context, parent, children); 511 } 512 513 private Object [] getChildrenWithPhantoms(ISynchronizationContext context, IResource resource, Object [] children) { 514 IResource[] setChildren = context.getDiffTree().members(resource); 515 if (setChildren.length == 0) 516 return children; 517 if (children.length == 0) 518 return setChildren; 519 Set result = new HashSet (children.length); 520 for (int i = 0; i < children.length; i++) { 521 result.add(children[i]); 522 } 523 for (int i = 0; i < setChildren.length; i++) { 524 result.add(setChildren[i]); 525 } 526 return result.toArray(); 527 } 528 529 private Object [] internalGetChildren(ISynchronizationContext context, Object parent, Object [] children) { 530 List result = new ArrayList (children.length); 531 for (int i = 0; i < children.length; i++) { 532 Object object = children[i]; 533 if (parent instanceof TreePath) { 537 TreePath tp = (TreePath) parent; 538 object = tp.createChildPath(object); 539 } 540 if (isVisible(context, object)) 541 result.add(internalGetElement(object)); 542 } 543 return result.toArray(new Object [result.size()]); 544 } 545 546 561 protected boolean isVisible(ISynchronizationContext context, Object object) { 562 ResourceTraversal[] traversals = getTraversals(context, object); 563 IDiff[] deltas = context.getDiffTree().getDiffs(traversals); 564 boolean visible = false; 565 if (isVisible(deltas)) { 566 visible = true; 567 } 568 return visible; 569 } 570 571 private boolean isVisible(IDiff[] diffs) { 572 if (diffs.length > 0) { 573 for (int j = 0; j < diffs.length; j++) { 574 IDiff diff = diffs[j]; 575 if (isVisible(diff)) { 576 return true; 577 } 578 } 579 } 580 return false; 581 } 582 583 594 protected boolean isVisible(IDiff diff) { 595 if (diff instanceof IThreeWayDiff) { 596 IThreeWayDiff twd = (IThreeWayDiff) diff; 597 return includeDirection(twd.getDirection()); 598 } 599 return diff.getKind() != IDiff.NO_CHANGE; 600 } 601 602 612 protected abstract ResourceTraversal[] getTraversals(ISynchronizationContext context, Object object); 613 614 619 protected void handleException(CoreException e) { 620 TeamPlugin.log(e); 621 } 622 623 638 protected boolean isInScope(ISynchronizationScope scope, Object parent, Object element) { 639 ResourceMapping mapping = Utils.getResourceMapping(internalGetElement(element)); 640 if (mapping != null) { 641 ResourceMapping[] mappings = ((ISynchronizationScope)scope).getMappings(mapping.getModelProviderId()); 642 for (int i = 0; i < mappings.length; i++) { 643 ResourceMapping sm = mappings[i]; 644 if (mapping.contains(sm)) { 645 return true; 646 } 647 if (sm.contains(mapping)) { 648 return true; 649 } 650 } 651 } 652 return false; 653 } 654 655 661 public ICommonContentExtensionSite getExtensionSite() { 662 return site; 663 } 664 665 private Object internalGetElement(Object elementOrPath) { 666 if (elementOrPath instanceof TreePath) { 667 TreePath tp = (TreePath) elementOrPath; 668 return tp.getLastSegment(); 669 } 670 return elementOrPath; 671 } 672 673 678 protected final boolean isFlatLayout() { 679 ISynchronizePageConfiguration c = getConfiguration(); 680 if (c != null) { 681 String p = (String )c.getProperty(ITeamContentProviderManager.PROP_PAGE_LAYOUT); 682 return p != null && p.equals(ITeamContentProviderManager.FLAT_LAYOUT); 683 } 684 return false; 685 } 686 } 687 | Popular Tags |