KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > SyncInfoTreeChangeEvent


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.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent;
17 import org.eclipse.team.core.synchronize.SyncInfoSet;
18
19 public class SyncInfoTreeChangeEvent extends SyncSetChangedEvent implements ISyncInfoTreeChangeEvent {
20
21     private Set removedSubtrees = new HashSet();
22     private Set addedSubtrees = new HashSet();
23
24     public SyncInfoTreeChangeEvent(SyncInfoSet set) {
25         super(set);
26     }
27
28     public void removedSubtreeRoot(IResource root) {
29         if (addedSubtrees.contains(root)) {
30             // The root was added and removed which is a no-op
31
addedSubtrees.remove(root);
32         } else if (isDescendantOfAddedRoot(root)) {
33             // Nothing needs to be done since no listeners ever knew about the root
34
} else {
35             // check if the root is a child of an existing root
36
// (in which case it need not be added).
37
// Also, remove any exisiting roots that are children
38
// of the new root
39
for (Iterator iter = removedSubtrees.iterator(); iter.hasNext();) {
40                 IResource element = (IResource) iter.next();
41                 // check if the root is already in the list
42
if (root.equals(element)) return;
43                 if (isParent(root, element)) {
44                     // the root invalidates the current element
45
iter.remove();
46                 } else if (isParent(element, root)) {
47                     // the root is a child of an existing element
48
return;
49                 }
50             }
51             removedSubtrees.add(root);
52         }
53     }
54
55     private boolean isParent(IResource root, IResource element) {
56         return root.getFullPath().isPrefixOf(element.getFullPath());
57     }
58
59     public void addedSubtreeRoot(IResource parent) {
60         if (removedSubtrees.contains(parent)) {
61             // The root was re-added. Just removing the removedRoot
62
// may not give the proper event.
63
// Since we can't be sure, just force a reset.
64
reset();
65         } else {
66             // only add the root if their isn't a higher root in the list already
67
if (!isDescendantOfAddedRoot(parent)) {
68                 addedSubtrees.add(parent);
69             }
70         }
71     }
72
73     private boolean isDescendantOfAddedRoot(IResource resource) {
74         for (Iterator iter = addedSubtrees.iterator(); iter.hasNext();) {
75             IResource root = (IResource) iter.next();
76             if (isParent(root, resource)) {
77                 // There is a higher added root already in the list
78
return true;
79             }
80         }
81         return false;
82     }
83
84     public IResource[] getAddedSubtreeRoots() {
85         return (IResource[]) addedSubtrees.toArray(new IResource[addedSubtrees.size()]);
86     }
87
88     public IResource[] getRemovedSubtreeRoots() {
89         return (IResource[]) removedSubtrees.toArray(new IResource[removedSubtrees.size()]);
90     }
91     
92     public boolean isEmpty() {
93         return super.isEmpty() && removedSubtrees.isEmpty() && addedSubtrees.isEmpty();
94     }
95 }
96
Popular Tags