KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > mapping > ScopeChangeEvent


1 /*******************************************************************************
2  * Copyright (c) 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.core.mapping;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.mapping.ResourceMapping;
16 import org.eclipse.core.resources.mapping.ResourceTraversal;
17 import org.eclipse.team.core.mapping.ISynchronizationScope;
18
19 public class ScopeChangeEvent {
20
21     private final ISynchronizationScope scope;
22     private final ResourceMapping[] originalMappings;
23     private final ResourceTraversal[] originalTraversals;
24     private boolean expanded;
25     private boolean contracted;
26
27     public ScopeChangeEvent(ISynchronizationScope scope) {
28         this.scope = scope;
29         originalMappings = scope.getMappings();
30         originalTraversals = scope.getTraversals();
31     }
32     
33     public boolean hasAdditionalMappings() {
34         return scope.getMappings().length > originalMappings.length;
35     }
36     
37     public ResourceTraversal[] getUncoveredTraversals(CompoundResourceTraversal traversal) {
38         CompoundResourceTraversal originals = new CompoundResourceTraversal();
39         originals.addTraversals(originalTraversals);
40         return originals.getUncoveredTraversals(traversal);
41     }
42
43     public void setExpanded(boolean expanded) {
44         this.expanded = expanded;
45     }
46
47     public boolean isExpanded() {
48         return expanded;
49     }
50
51     public void setContracted(boolean contracted) {
52         this.contracted = contracted;
53     }
54
55     public boolean isContracted() {
56         return contracted;
57     }
58     
59     public ResourceMapping[] getChangedMappings() {
60         ResourceMapping[] currentMappings = scope.getMappings();
61         ResourceMapping[] changedMappings;
62         if (currentMappings.length > originalMappings.length) {
63             // The number of mappings has increased so we should report the new mappings
64
Set originalSet = new HashSet();
65             List result = new ArrayList();
66             for (int i = 0; i < originalMappings.length; i++) {
67                 ResourceMapping mapping = originalMappings[i];
68                 originalSet.add(mapping);
69             }
70             for (int i = 0; i < currentMappings.length; i++) {
71                 ResourceMapping mapping = currentMappings[i];
72                 if (!originalSet.contains(mapping)) {
73                     result.add(mapping);
74                 }
75             }
76             changedMappings = (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
77         } else if (isContracted()) {
78             // The number of mappings may be smaller so report the removed mappings
79
Set finalSet = new HashSet();
80             List result = new ArrayList();
81             for (int i = 0; i < currentMappings.length; i++) {
82                 ResourceMapping mapping = currentMappings[i];
83                 finalSet.add(mapping);
84             }
85             for (int i = 0; i < originalMappings.length; i++) {
86                 ResourceMapping mapping = originalMappings[i];
87                 if (!finalSet.contains(mapping)) {
88                     result.add(mapping);
89                 }
90             }
91             changedMappings = (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
92         } else {
93             changedMappings = new ResourceMapping[0];
94         }
95         return changedMappings;
96     }
97
98     public ResourceTraversal[] getChangedTraversals(CompoundResourceTraversal refreshTraversals) {
99         ResourceTraversal[] changesTraversals;
100         if (isExpanded()) {
101             changesTraversals = getUncoveredTraversals(refreshTraversals);
102         } else if (isContracted()) {
103             CompoundResourceTraversal finalTraversals = new CompoundResourceTraversal();
104             finalTraversals.addTraversals(scope.getTraversals());
105             changesTraversals = finalTraversals.getUncoveredTraversals(originalTraversals);
106         } else {
107             changesTraversals = new ResourceTraversal[0];
108         }
109         return changesTraversals;
110     }
111
112     public boolean shouldFireChange() {
113         return isExpanded() || isContracted() || hasAdditionalMappings();
114     }
115 }
116
Popular Tags