KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > ChangeSetContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.internal.ccvs.ui.mappings;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.resources.mapping.ResourceTraversal;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.widgets.Tree;
21 import org.eclipse.swt.widgets.TreeItem;
22 import org.eclipse.team.core.diff.*;
23 import org.eclipse.team.core.mapping.IResourceDiffTree;
24 import org.eclipse.team.core.mapping.ISynchronizationContext;
25 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
26 import org.eclipse.team.internal.ccvs.core.mapping.*;
27 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
28 import org.eclipse.team.internal.ccvs.ui.subscriber.CVSChangeSetCollector;
29 import org.eclipse.team.internal.core.subscribers.*;
30 import org.eclipse.team.internal.core.subscribers.BatchingChangeSetManager.CollectorChangeEvent;
31 import org.eclipse.team.internal.ui.IPreferenceIds;
32 import org.eclipse.team.internal.ui.Utils;
33 import org.eclipse.team.internal.ui.mapping.ResourceModelContentProvider;
34 import org.eclipse.team.internal.ui.mapping.ResourceModelLabelProvider;
35 import org.eclipse.team.internal.ui.synchronize.ChangeSetCapability;
36 import org.eclipse.team.internal.ui.synchronize.IChangeSetProvider;
37 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
38 import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;
39 import org.eclipse.ui.navigator.*;
40
41 public class ChangeSetContentProvider extends ResourceModelContentProvider implements ITreePathContentProvider {
42
43     private final class CollectorListener implements IChangeSetChangeListener, BatchingChangeSetManager.IChangeSetCollectorChangeListener {
44         /* (non-Javadoc)
45          * @see org.eclipse.team.internal.core.subscribers.IChangeSetChangeListener#setAdded(org.eclipse.team.internal.core.subscribers.ChangeSet)
46          */

47         public void setAdded(final ChangeSet set) {
48             // We only react here for active change sets.
49
// Checked-in change set changes are batched
50
if (set instanceof ActiveChangeSet) {
51                 if (isVisibleInMode(set)) {
52                     Utils.syncExec(new Runnable JavaDoc() {
53                         public void run() {
54                             Object JavaDoc input = getViewer().getInput();
55                             ((AbstractTreeViewer)getViewer()).add(input, set);
56                         }
57                     }, (StructuredViewer)getViewer());
58                 }
59                 handleSetAddition(set);
60             }
61         }
62
63         private void handleSetAddition(final ChangeSet set) {
64             getUnassignedSet().remove(set.getResources());
65         }
66
67         /* (non-Javadoc)
68          * @see org.eclipse.team.internal.core.subscribers.IChangeSetChangeListener#defaultSetChanged(org.eclipse.team.internal.core.subscribers.ChangeSet, org.eclipse.team.internal.core.subscribers.ChangeSet)
69          */

70         public void defaultSetChanged(final ChangeSet previousDefault, final ChangeSet set) {
71             if (isVisibleInMode(set) || isVisibleInMode(previousDefault)) {
72                 Utils.asyncExec(new Runnable JavaDoc() {
73                     public void run() {
74                         ((AbstractTreeViewer)getViewer()).update(new Object JavaDoc[] {previousDefault, set}, null);
75                     }
76                 }, (StructuredViewer)getViewer());
77             }
78         }
79
80         /* (non-Javadoc)
81          * @see org.eclipse.team.internal.core.subscribers.IChangeSetChangeListener#setRemoved(org.eclipse.team.internal.core.subscribers.ChangeSet)
82          */

83         public void setRemoved(final ChangeSet set) {
84             // We only react here for active change sets.
85
// Checked-in change set changes are batched
86
if (set instanceof ActiveChangeSet) {
87                 if (isVisibleInMode(set)) {
88                     Utils.syncExec(new Runnable JavaDoc() {
89                         public void run() {
90                             ((AbstractTreeViewer)getViewer()).remove(TreePath.EMPTY.createChildPath(set));
91                         }
92                     }, (StructuredViewer)getViewer());
93                 }
94                 handleSetRemoval(set);
95             }
96         }
97
98         private void handleSetRemoval(final ChangeSet set) {
99             IResource[] resources = set.getResources();
100             List toAdd = new ArrayList();
101             for (int i = 0; i < resources.length; i++) {
102                 IResource resource = resources[i];
103                 IDiff diff = getContext().getDiffTree().getDiff(resource);
104                 if (diff != null && !isContainedInSet(diff))
105                     toAdd.add(diff);
106             }
107             getUnassignedSet().add((IDiff[]) toAdd.toArray(new IDiff[toAdd.size()]));
108         }
109
110         /* (non-Javadoc)
111          * @see org.eclipse.team.internal.core.subscribers.IChangeSetChangeListener#nameChanged(org.eclipse.team.internal.core.subscribers.ChangeSet)
112          */

113         public void nameChanged(final ChangeSet set) {
114             if (isVisibleInMode(set)) {
115                 Utils.asyncExec(new Runnable JavaDoc() {
116                     public void run() {
117                         ((AbstractTreeViewer)getViewer()).update(set, null);
118                     }
119                 }, (StructuredViewer)getViewer());
120             }
121         }
122
123         /* (non-Javadoc)
124          * @see org.eclipse.team.internal.core.subscribers.IChangeSetChangeListener#resourcesChanged(org.eclipse.team.internal.core.subscribers.ChangeSet, org.eclipse.core.runtime.IPath[])
125          */

126         public void resourcesChanged(final ChangeSet set, final IPath[] paths) {
127             // We only react here for active change sets.
128
// Checked-in change set changes are batched
129
if (set instanceof ActiveChangeSet) {
130                 if (isVisibleInMode(set)) {
131                     Utils.syncExec(new Runnable JavaDoc() {
132                         public void run() {
133                             if (hasChildrenInContext(set))
134                                 if (getVisibleSetsInViewer().contains(set))
135                                     ((AbstractTreeViewer)getViewer()).refresh(set, true);
136                                 else
137                                     ((AbstractTreeViewer)getViewer()).add(getViewer().getInput(), set);
138                             else
139                                 ((AbstractTreeViewer)getViewer()).remove(set);
140                         }
141                     }, (StructuredViewer)getViewer());
142                 }
143                 handleSetChange(set, paths);
144             }
145         }
146
147         private void handleSetChange(final ChangeSet set, final IPath[] paths) {
148             try {
149                 getTheRest().beginInput();
150                 for (int i = 0; i < paths.length; i++) {
151                     IPath path = paths[i];
152                     boolean isContained = ((DiffChangeSet)set).contains(path);
153                     if (isContained) {
154                         IDiff diff = ((DiffChangeSet)set).getDiffTree().getDiff(path);
155                         if (diff != null) {
156                             getTheRest().remove(ResourceDiffTree.getResourceFor(diff));
157                         }
158                     } else {
159                         IDiff diff = getContext().getDiffTree().getDiff(path);
160                         if (diff != null && !isContainedInSet(diff)) {
161                             getTheRest().add(diff);
162                         }
163                     }
164                 }
165             } finally {
166                 getTheRest().endInput(null);
167             }
168         }
169
170         public void changeSetChanges(final CollectorChangeEvent event, IProgressMonitor monitor) {
171             ChangeSet[] addedSets = event.getAddedSets();
172             final ChangeSet[] visibleAddedSets = getVisibleSets(addedSets);
173             ChangeSet[] removedSets = event.getRemovedSets();
174             final ChangeSet[] visibleRemovedSets = getVisibleSets(removedSets);
175             ChangeSet[] changedSets = event.getChangedSets();
176             final ChangeSet[] visibleChangedSets = getVisibleSets(changedSets);
177             if (visibleAddedSets.length > 0 || visibleRemovedSets.length > 0 || visibleChangedSets.length > 0) {
178                 Utils.syncExec(new Runnable JavaDoc() {
179                     public void run() {
180                         try {
181                             getViewer().getControl().setRedraw(false);
182                             if (visibleAddedSets.length > 0) {
183                                 Object JavaDoc input = getViewer().getInput();
184                                 ((AbstractTreeViewer)getViewer()).add(input, visibleAddedSets);
185                             }
186                             if (visibleRemovedSets.length > 0)
187                                 ((AbstractTreeViewer)getViewer()).remove(visibleRemovedSets);
188                             for (int i = 0; i < visibleChangedSets.length; i++) {
189                                 ChangeSet set = visibleChangedSets[i];
190                                 ((AbstractTreeViewer)getViewer()).refresh(set, true);
191                             }
192                         } finally {
193                             getViewer().getControl().setRedraw(true);
194                         }
195                     }
196                 }, (StructuredViewer)getViewer());
197             }
198             try {
199                 getTheRest().beginInput();
200                 for (int i = 0; i < addedSets.length; i++) {
201                     ChangeSet set = addedSets[i];
202                     handleSetAddition(set);
203                 }
204                 if (removedSets.length > 0) {
205                     // If sets were removed, we reset the unassigned set.
206
// We need to do this because it is possible that diffs were
207
// removed from the set before the set itself was removed.
208
// See bug 173138
209
addAllUnassignedToUnassignedSet();
210                 }
211                 for (int i = 0; i < changedSets.length; i++) {
212                     ChangeSet set = changedSets[i];
213                     IPath[] paths = event.getChangesFor(set);
214                     if (event.getSource().contains(set)) {
215                         handleSetChange(set, paths);
216                     } else {
217                         try {
218                             getTheRest().beginInput();
219                             for (int j = 0; j < paths.length; j++) {
220                                 IPath path = paths[j];
221                                 IDiff diff = getContext().getDiffTree().getDiff(path);
222                                 if (diff != null && !isContainedInSet(diff))
223                                     getTheRest().add(diff);
224                             }
225                         } finally {
226                             getTheRest().endInput(null);
227                         }
228                     }
229                 }
230             } finally {
231                 getTheRest().endInput(monitor);
232             }
233         }
234
235         private ChangeSet[] getVisibleSets(ChangeSet[] sets) {
236             List result = new ArrayList(sets.length);
237             for (int i = 0; i < sets.length; i++) {
238                 ChangeSet set = sets[i];
239                 if (isVisibleInMode(set)) {
240                     result.add(set);
241                 }
242             }
243             return (ChangeSet[]) result.toArray(new ChangeSet[result.size()]);
244         }
245     }
246
247     private DiffChangeSet unassignedDiffs;
248     private boolean firstDiffChange = true;
249     
250     /*
251      * Listener that reacts to changes made to the active change set collector
252      */

253     private IChangeSetChangeListener collectorListener = new CollectorListener();
254     
255     private IDiffChangeListener diffTreeListener = new IDiffChangeListener() {
256     
257         /* (non-Javadoc)
258          * @see org.eclipse.team.core.diff.IDiffChangeListener#propertyChanged(org.eclipse.team.core.diff.IDiffTree, int, org.eclipse.core.runtime.IPath[])
259          */

260         public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
261             // Ignore
262
}
263     
264         boolean isSetVisible(DiffChangeSet set) {
265             return getVisibleSetsInViewer().contains(set);
266         }
267         
268         /* (non-Javadoc)
269          * @see org.eclipse.team.core.diff.IDiffChangeListener#diffsChanged(org.eclipse.team.core.diff.IDiffChangeEvent, org.eclipse.core.runtime.IProgressMonitor)
270          */

271         public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
272             Object JavaDoc input = getViewer().getInput();
273             if (input instanceof ChangeSetModelProvider && unassignedDiffs != null && event.getTree() == unassignedDiffs.getDiffTree()) {
274                 Utils.asyncExec(new Runnable JavaDoc() {
275                     public void run() {
276                         if (unassignedDiffs.isEmpty() || !hasChildren(TreePath.EMPTY.createChildPath(getUnassignedSet()))) {
277                             ((AbstractTreeViewer)getViewer()).remove(unassignedDiffs);
278                         } else if (!isSetVisible(unassignedDiffs)) {
279                             Object JavaDoc input = getViewer().getInput();
280                             ((AbstractTreeViewer)getViewer()).add(input, unassignedDiffs);
281                         } else {
282                             ((AbstractTreeViewer)getViewer()).refresh(unassignedDiffs);
283                         }
284                     }
285                 }, (StructuredViewer)getViewer());
286             }
287         }
288     
289     };
290     private CheckedInChangeSetCollector checkedInCollector;
291     private boolean collectorInitialized;
292     
293     /* (non-Javadoc)
294      * @see org.eclipse.team.internal.ui.mapping.ResourceModelContentProvider#getModelProviderId()
295      */

296     protected String JavaDoc getModelProviderId() {
297         return ChangeSetModelProvider.ID;
298     }
299     
300     /* package */ boolean isVisibleInMode(ChangeSet set) {
301         final Object JavaDoc input = getViewer().getInput();
302         if (input instanceof ChangeSetModelProvider) {
303             if (set instanceof ActiveChangeSet) {
304                 return getConfiguration().getMode() != ISynchronizePageConfiguration.INCOMING_MODE;
305             }
306             if (set instanceof DiffChangeSet) {
307                 return getConfiguration().getMode() != ISynchronizePageConfiguration.OUTGOING_MODE;
308             }
309         }
310         return false;
311     }
312
313     protected boolean isEnabled() {
314         final Object JavaDoc input = getViewer().getInput();
315         return (input instanceof ChangeSetModelProvider);
316     }
317     
318     /* (non-Javadoc)
319      * @see org.eclipse.team.internal.ui.mapping.ResourceModelContentProvider#getElements(java.lang.Object)
320      */

321     public Object JavaDoc[] getElements(Object JavaDoc parent) {
322         if (parent instanceof ISynchronizationContext) {
323             // Do not show change sets when all models are visible because
324
// model providers that override the resource content may cause
325
// problems for the change set content provider
326
return new Object JavaDoc[0];
327         }
328         if (parent == getModelProvider()) {
329             return getRootElements();
330         }
331         return super.getElements(parent);
332     }
333
334     private Object JavaDoc[] getRootElements() {
335         if (!collectorInitialized) {
336             initializeCheckedInChangeSetCollector(getChangeSetCapability());
337             collectorInitialized = true;
338         }
339         List result = new ArrayList();
340         ChangeSet[] sets = getAllSets();
341         for (int i = 0; i < sets.length; i++) {
342             ChangeSet set = sets[i];
343             if (hasChildren(TreePath.EMPTY.createChildPath(set)))
344                 result.add(set);
345         }
346         if (!getUnassignedSet().isEmpty() && hasChildren(TreePath.EMPTY.createChildPath(getUnassignedSet()))) {
347             result.add(getUnassignedSet());
348         }
349         return result.toArray();
350     }
351
352     private synchronized DiffChangeSet getUnassignedSet() {
353         if (unassignedDiffs == null) {
354             unassignedDiffs = new UnassignedDiffChangeSet(CVSUIMessages.ChangeSetContentProvider_0);
355             unassignedDiffs.getDiffTree().addDiffChangeListener(diffTreeListener);
356             addAllUnassignedToUnassignedSet();
357         }
358         return unassignedDiffs;
359     }
360
361     private void addAllUnassignedToUnassignedSet() {
362         IResourceDiffTree allChanges = getContext().getDiffTree();
363         final List diffs = new ArrayList();
364         allChanges.accept(ResourcesPlugin.getWorkspace().getRoot().getFullPath(), new IDiffVisitor() {
365             public boolean visit(IDiff diff) {
366                 if (!isContainedInSet(diff))
367                     diffs.add(diff);
368                 return true;
369             }
370         }, IResource.DEPTH_INFINITE);
371         unassignedDiffs.add((IDiff[]) diffs.toArray(new IDiff[diffs.size()]));
372     }
373     
374     private ResourceDiffTree getTheRest() {
375         return (ResourceDiffTree)getUnassignedSet().getDiffTree();
376     }
377
378     /**
379      * Return whether the given diff is contained in a set other than
380      * the unassigned set.
381      * @param diff the diff
382      * @return whether the given diff is contained in a set other than
383      * the unassigned set
384      */

385     protected boolean isContainedInSet(IDiff diff) {
386         ChangeSet[] sets = getAllSets();
387         for (int i = 0; i < sets.length; i++) {
388             ChangeSet set = sets[i];
389             if (set.contains(ResourceDiffTree.getResourceFor(diff))) {
390                 return true;
391             }
392         }
393         return false;
394     }
395
396     /* (non-Javadoc)
397      * @see org.eclipse.team.internal.ui.mapping.ResourceModelContentProvider#getTraversals(org.eclipse.team.core.mapping.ISynchronizationContext, java.lang.Object)
398      */

399     protected ResourceTraversal[] getTraversals(
400             ISynchronizationContext context, Object JavaDoc object) {
401         if (object instanceof ChangeSet) {
402             ChangeSet set = (ChangeSet) object;
403             IResource[] resources = set.getResources();
404             return new ResourceTraversal[] { new ResourceTraversal(resources, IResource.DEPTH_ZERO, IResource.NONE) };
405         }
406         return super.getTraversals(context, object);
407     }
408
409     public Object JavaDoc[] getChildren(TreePath parentPath) {
410         if (!isEnabled())
411             return new Object JavaDoc[0];
412         if (parentPath.getSegmentCount() == 0)
413             return getRootElements();
414         Object JavaDoc first = parentPath.getFirstSegment();
415         if (!isVisibleInMode(first)) {
416             return new Object JavaDoc[0];
417         }
418         IResourceDiffTree diffTree;
419         Object JavaDoc parent = parentPath.getLastSegment();
420         if (first instanceof DiffChangeSet) {
421             DiffChangeSet set = (DiffChangeSet) first;
422             diffTree = set.getDiffTree();
423             if (parent instanceof DiffChangeSet) {
424                 parent = getModelRoot();
425             }
426         } else {
427             return new Object JavaDoc[0];
428         }
429         Object JavaDoc[] children = getChildren(parent);
430         Set result = new HashSet();
431         for (int i = 0; i < children.length; i++) {
432             Object JavaDoc child = children[i];
433             if (isVisible(child, diffTree)) {
434                 result.add(child);
435             }
436         }
437         return result.toArray();
438     }
439
440     private boolean isVisibleInMode(Object JavaDoc first) {
441         if (first instanceof ChangeSet) {
442             ChangeSet cs = (ChangeSet) first;
443             int mode = getConfiguration().getMode();
444             switch (mode) {
445             case ISynchronizePageConfiguration.BOTH_MODE:
446                 return true;
447             case ISynchronizePageConfiguration.CONFLICTING_MODE:
448                 return containsConflicts(cs);
449             case ISynchronizePageConfiguration.INCOMING_MODE:
450                 return cs instanceof CVSCheckedInChangeSet || (isUnassignedSet(cs) && hasIncomingChanges(cs));
451             case ISynchronizePageConfiguration.OUTGOING_MODE:
452                 return cs instanceof ActiveChangeSet || hasConflicts(cs) || (isUnassignedSet(cs) && hasOutgoingChanges(cs));
453             default:
454                 break;
455             }
456         }
457         return true;
458     }
459
460     private boolean hasIncomingChanges(ChangeSet cs) {
461         if (cs instanceof DiffChangeSet) {
462             DiffChangeSet dcs = (DiffChangeSet) cs;
463             return dcs.getDiffTree().countFor(IThreeWayDiff.INCOMING, IThreeWayDiff.DIRECTION_MASK) > 0;
464         }
465         return false;
466     }
467
468     private boolean hasOutgoingChanges(ChangeSet cs) {
469         if (cs instanceof DiffChangeSet) {
470             DiffChangeSet dcs = (DiffChangeSet) cs;
471             return dcs.getDiffTree().countFor(IThreeWayDiff.OUTGOING, IThreeWayDiff.DIRECTION_MASK) > 0;
472         }
473         return false;
474     }
475
476     private boolean isUnassignedSet(ChangeSet cs) {
477         return cs == unassignedDiffs;
478     }
479
480     private boolean hasConflicts(ChangeSet cs) {
481         if (cs instanceof DiffChangeSet) {
482             DiffChangeSet dcs = (DiffChangeSet) cs;
483             return dcs.getDiffTree().countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK) > 0;
484         }
485         return false;
486     }
487
488     private boolean containsConflicts(ChangeSet cs) {
489         if (cs instanceof DiffChangeSet) {
490             DiffChangeSet dcs = (DiffChangeSet) cs;
491             return dcs.getDiffTree().hasMatchingDiffs(ResourcesPlugin.getWorkspace().getRoot().getFullPath(), ResourceModelLabelProvider.CONFLICT_FILTER);
492         }
493         return false;
494     }
495
496     private boolean isVisible(Object JavaDoc object, IResourceDiffTree tree) {
497         if (object instanceof IResource) {
498             IResource resource = (IResource) object;
499             IDiff diff = tree.getDiff(resource);
500             if (diff != null && isVisible(diff))
501                 return true;
502             int depth = getTraversalCalculator().getLayoutDepth(resource, null);
503             IDiff[] diffs = tree.getDiffs(resource, depth);
504             for (int i = 0; i < diffs.length; i++) {
505                 IDiff child = diffs[i];
506                 if (isVisible(child)) {
507                     return true;
508                 }
509             }
510         }
511         return false;
512     }
513
514     public boolean hasChildren(TreePath path) {
515         if (path.getSegmentCount() == 1) {
516             Object JavaDoc first = path.getFirstSegment();
517             if (first instanceof ChangeSet) {
518                 return isVisibleInMode(first) && hasChildrenInContext((ChangeSet)first);
519             }
520         }
521         return getChildren(path).length > 0;
522     }
523
524     private boolean hasChildrenInContext(ChangeSet set) {
525         IResource[] resources = set.getResources();
526         for (int i = 0; i < resources.length; i++) {
527             IResource resource = resources[i];
528             if (getContext().getDiffTree().getDiff(resource) != null)
529                 return true;
530         }
531         return false;
532     }
533
534     public TreePath[] getParents(Object JavaDoc element) {
535         if (element instanceof ChangeSet) {
536             return new TreePath[] { TreePath.EMPTY };
537         }
538         if (element instanceof IResource) {
539             IResource resource = (IResource) element;
540             DiffChangeSet[] sets = getSetsContaining(resource);
541             if (sets.length > 0) {
542                 List result = new ArrayList();
543                 for (int i = 0; i < sets.length; i++) {
544                     DiffChangeSet set = sets[i];
545                     TreePath path = getPathForElement(set, resource.getParent());
546                     if (path != null)
547                         result.add(path);
548                 }
549                 return (TreePath[]) result.toArray(new TreePath[result.size()]);
550             } else {
551                 TreePath path = getPathForElement(getUnassignedSet(), resource.getParent());
552                 if (path != null)
553                     return new TreePath[] { path };
554             }
555         }
556         
557         return new TreePath[0];
558     }
559
560     private DiffChangeSet[] getSetsContaining(IResource resource) {
561         List result = new ArrayList();
562         DiffChangeSet[] allSets = getAllSets();
563         for (int i = 0; i < allSets.length; i++) {
564             DiffChangeSet set = allSets[i];
565             if (isVisible(resource, set.getDiffTree())) {
566                 result.add(set);
567             }
568         }
569         return (DiffChangeSet[]) result.toArray(new DiffChangeSet[result.size()]);
570     }
571
572     /**
573      * Return all the change sets (incoming and outgoing). This
574      * list must not include the unassigned set.
575      * @return all the change sets (incoming and outgoing)
576      */

577     private DiffChangeSet[] getAllSets() {
578         List result = new ArrayList();
579         ChangeSetCapability csc = getChangeSetCapability();
580         if (csc.supportsActiveChangeSets()) {
581             ActiveChangeSetManager collector = csc.getActiveChangeSetManager();
582             ChangeSet[] sets = collector.getSets();
583             for (int i = 0; i < sets.length; i++) {
584                 ChangeSet set = sets[i];
585                 result.add(set);
586             }
587         }
588         if (checkedInCollector != null) {
589             ChangeSet[] sets = checkedInCollector.getSets();
590             for (int i = 0; i < sets.length; i++) {
591                 ChangeSet set = sets[i];
592                 result.add(set);
593             }
594         }
595         return (DiffChangeSet[]) result.toArray(new DiffChangeSet[result.size()]);
596     }
597
598     private TreePath getPathForElement(DiffChangeSet set, IResource resource) {
599         List pathList = getPath(set.getDiffTree(), resource);
600         if (pathList != null) {
601             pathList.add(0, set);
602             TreePath path = new TreePath(pathList.toArray());
603             return path;
604         }
605         return null;
606     }
607     
608     private List getPath(IResourceDiffTree tree, IResource resource) {
609         if (resource == null)
610             return null;
611         boolean hasDiff = tree.getDiff(resource) == null;
612         if (hasDiff && tree.members(resource).length == 0)
613             return null;
614         if (resource.getType() == IResource.ROOT) {
615             return null;
616         }
617         List result = new ArrayList();
618         result.add(resource.getProject());
619         if (resource.getType() != IResource.PROJECT) {
620             String JavaDoc layout = getTraversalCalculator().getLayout();
621             if (layout.equals(IPreferenceIds.FLAT_LAYOUT)) {
622                 result.add(resource);
623             } else if (layout.equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.FOLDER) {
624                 result.add(resource);
625             } else if (layout.equals(IPreferenceIds.COMPRESSED_LAYOUT) && resource.getType() == IResource.FILE) {
626                 IContainer parent = resource.getParent();
627                 if (parent.getType() != IResource.PROJECT)
628                     result.add(parent);
629                 result.add(resource);
630             } else {
631                 List resourcePath = new ArrayList();
632                 IResource next = resource;
633                 while (next.getType() != IResource.PROJECT) {
634                     resourcePath.add(next);
635                     next = next.getParent();
636                 }
637                 for (int i = resourcePath.size() - 1; i >=0; i--) {
638                     result.add(resourcePath.get(i));
639                 }
640             }
641         }
642         return result;
643     }
644
645     public void init(ICommonContentExtensionSite site) {
646         super.init(site);
647         ChangeSetCapability csc = getChangeSetCapability();
648         if (csc.supportsActiveChangeSets()) {
649             ActiveChangeSetManager collector = csc.getActiveChangeSetManager();
650             collector.addListener(collectorListener);
651         }
652         ChangeSetSorter sorter = getSorter();
653         if (sorter != null) {
654             sorter.setConfiguration(getConfiguration());
655         }
656     }
657
658     private ChangeSetSorter getSorter() {
659         INavigatorContentService contentService = getExtensionSite().getService();
660         INavigatorSorterService sortingService = contentService.getSorterService();
661         INavigatorContentExtension extension = getExtensionSite().getExtension();
662         if (extension != null) {
663             ViewerSorter sorter = sortingService.findSorter(extension.getDescriptor(), getModelProvider(), new DiffChangeSet(), new DiffChangeSet());
664             if (sorter instanceof ChangeSetSorter) {
665                 return (ChangeSetSorter) sorter;
666             }
667         }
668         return null;
669     }
670     
671     private void initializeCheckedInChangeSetCollector(ChangeSetCapability csc) {
672         if (csc.supportsCheckedInChangeSets()) {
673             checkedInCollector = ((ModelParticipantChangeSetCapability)csc).createCheckedInChangeSetCollector(getConfiguration());
674             getConfiguration().setProperty(CVSChangeSetCollector.CVS_CHECKED_IN_COLLECTOR, checkedInCollector);
675             checkedInCollector.addListener(collectorListener);
676             checkedInCollector.add(((ResourceDiffTree)getContext().getDiffTree()).getDiffs());
677         }
678     }
679     
680     public void dispose() {
681         ChangeSetCapability csc = getChangeSetCapability();
682         if (csc.supportsActiveChangeSets()) {
683             csc.getActiveChangeSetManager().removeListener(collectorListener);
684         }
685         if (checkedInCollector != null) {
686             checkedInCollector.removeListener(collectorListener);
687             checkedInCollector.dispose();
688         }
689         if (unassignedDiffs != null) {
690             unassignedDiffs.getDiffTree().removeDiffChangeListener(diffTreeListener);
691         }
692         super.dispose();
693     }
694     
695     public boolean isVisible(IDiff diff) {
696         return super.isVisible(diff);
697     }
698
699     public IResourceDiffTree getDiffTree(TreePath path) {
700         if (path.getSegmentCount() > 0) {
701             Object JavaDoc first = path.getFirstSegment();
702             if (first instanceof DiffChangeSet) {
703                 DiffChangeSet set = (DiffChangeSet) first;
704                 return set.getDiffTree();
705             }
706         }
707         return getTheRest();
708     }
709     
710     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
711         // Override inherited method to reconcile sub-trees
712
IPath[] removed = event.getRemovals();
713         IDiff[] added = event.getAdditions();
714         IDiff[] changed = event.getChanges();
715         // Only adjust the set of the rest. The others will be handled by the collectors
716
try {
717             getTheRest().beginInput();
718             for (int i = 0; i < removed.length; i++) {
719                 IPath path = removed[i];
720                 getTheRest().remove(path);
721             }
722             for (int i = 0; i < added.length; i++) {
723                 IDiff diff = added[i];
724                 // Only add the diff if it is not already in another set
725
if (!isContainedInSet(diff)) {
726                     getTheRest().add(diff);
727                 }
728             }
729             for (int i = 0; i < changed.length; i++) {
730                 IDiff diff = changed[i];
731                 // Only add the diff if it is already contained in the free set
732
if (getTheRest().getDiff(diff.getPath()) != null) {
733                     getTheRest().add(diff);
734                 }
735             }
736         } finally {
737             getTheRest().endInput(monitor);
738         }
739         if (checkedInCollector != null)
740             checkedInCollector.handleChange(event);
741         if (firstDiffChange) {
742             // One the first diff event, refresh the viewer to ensure outgoing change sets appear
743
firstDiffChange = false;
744             Utils.asyncExec(new Runnable JavaDoc() {
745                 public void run() {
746                     ((AbstractTreeViewer)getViewer()).refresh();
747                 }
748             }, (StructuredViewer)getViewer());
749         }
750     }
751     
752     protected void updateLabels(ISynchronizationContext context, IPath[] paths) {
753         super.updateLabels(context, paths);
754         ChangeSet[] sets = getSetsShowingPropogatedStateFrom(paths);
755         if (sets.length > 0)
756             ((AbstractTreeViewer)getViewer()).update(sets, null);
757     }
758     
759     
760     private ChangeSet[] getSetsShowingPropogatedStateFrom(IPath[] paths) {
761         Set result = new HashSet();
762         for (int i = 0; i < paths.length; i++) {
763             IPath path = paths[i];
764             ChangeSet[] sets = getSetsShowingPropogatedStateFrom(path);
765             for (int j = 0; j < sets.length; j++) {
766                 ChangeSet set = sets[j];
767                 result.add(set);
768             }
769         }
770         return (ChangeSet[]) result.toArray(new ChangeSet[result.size()]);
771     }
772     
773     protected DiffChangeSet[] getSetsShowingPropogatedStateFrom(IPath path) {
774         List result = new ArrayList();
775         DiffChangeSet[] allSets = getAllSets();
776         for (int i = 0; i < allSets.length; i++) {
777             DiffChangeSet set = allSets[i];
778             if (set.getDiffTree().getDiff(path) != null || set.getDiffTree().getChildren(path).length > 0) {
779                 result.add(set);
780             }
781         }
782         return (DiffChangeSet[]) result.toArray(new DiffChangeSet[result.size()]);
783     }
784
785     public ChangeSetCapability getChangeSetCapability() {
786         ISynchronizeParticipant participant = getConfiguration().getParticipant();
787         if (participant instanceof IChangeSetProvider) {
788             IChangeSetProvider provider = (IChangeSetProvider) participant;
789             return provider.getChangeSetCapability();
790         }
791         return null;
792     }
793     
794     private Set getVisibleSetsInViewer() {
795         TreeViewer viewer = (TreeViewer)getViewer();
796         Tree tree = viewer.getTree();
797         TreeItem[] children = tree.getItems();
798         Set result = new HashSet();
799         for (int i = 0; i < children.length; i++) {
800             TreeItem control = children[i];
801             Object JavaDoc data = control.getData();
802             if (data instanceof ChangeSet) {
803                 ChangeSet set = (ChangeSet) data;
804                 result.add(set);
805             }
806         }
807         return result;
808     }
809
810 }
811
Popular Tags