KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceModelProviderOperation


1 /*******************************************************************************
2  * Copyright (c) 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.ui.mapping;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.*;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.viewers.*;
22 import org.eclipse.team.core.diff.FastDiffFilter;
23 import org.eclipse.team.core.diff.IDiff;
24 import org.eclipse.team.core.mapping.IResourceDiffTree;
25 import org.eclipse.team.core.mapping.ISynchronizationContext;
26 import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
27 import org.eclipse.team.internal.ui.TeamUIPlugin;
28 import org.eclipse.team.internal.ui.Utils;
29 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
30 import org.eclipse.team.ui.mapping.SynchronizationOperation;
31 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
32
33 public abstract class ResourceModelProviderOperation extends SynchronizationOperation {
34
35     private final IStructuredSelection selection;
36
37     protected ResourceModelProviderOperation(ISynchronizePageConfiguration configuration, IStructuredSelection selection) {
38         super(configuration, getElements(selection));
39         this.selection = selection;
40     }
41
42     private static Object JavaDoc[] getElements(IStructuredSelection selection) {
43         return selection.toArray();
44     }
45
46     /**
47      * Return the file deltas that are either contained in the selection
48      * or are children of the selection and visible given the current
49      * mode of the page configuration.
50      * @param elements the selected elements
51      * @return the file deltas contained in or descended from the selection
52      */

53     private IDiff[] getFileDeltas(Object JavaDoc[] pathOrElements) {
54         Set JavaDoc result = new HashSet JavaDoc();
55         for (int i = 0; i < pathOrElements.length; i++) {
56             Object JavaDoc pathOrElement = pathOrElements[i];
57             IDiff[] diffs = getFileDeltas(pathOrElement);
58             for (int j = 0; j < diffs.length; j++) {
59                 IDiff node = diffs[j];
60                 result.add(node);
61             }
62         }
63         return (IDiff[]) result.toArray(new IDiff[result.size()]);
64     }
65     
66     private IDiff[] getFileDeltas(Object JavaDoc pathOrElement) {
67         List JavaDoc result = new ArrayList JavaDoc();
68         ResourceTraversal[] traversals = getTraversals(pathOrElement);
69         if (traversals.length > 0) {
70             ISynchronizationContext context = getContext();
71             final IResourceDiffTree diffTree = context.getDiffTree();
72             IDiff[] diffs = diffTree.getDiffs(traversals);
73             // Now filter the by the mode of the configuration
74
for (int i = 0; i < diffs.length; i++) {
75                 IDiff node = diffs[i];
76                 if (isVisible(node) && getDiffFilter().select(node))
77                     result.add(node);
78             }
79         }
80         return (IDiff[]) result.toArray(new IDiff[result.size()]);
81     }
82     
83     /**
84      * Return whether the given node is visible in the page based
85      * on the mode in the configuration.
86      * @param node a diff node
87      * @return whether the given node is visible in the page
88      */

89     protected boolean isVisible(IDiff node) {
90         return ((SynchronizePageConfiguration)getConfiguration()).isVisible(node);
91     }
92
93     private ResourceTraversal[] getTraversals(Object JavaDoc pathOrElement) {
94         // First check to see if the element is a tree path
95
Object JavaDoc element;
96         if (pathOrElement instanceof TreePath) {
97             TreePath tp = (TreePath) pathOrElement;
98             Object JavaDoc o = tp.getFirstSegment();
99             if (o instanceof DiffChangeSet) {
100                 // Special handling for change sets
101
DiffChangeSet dcs = (DiffChangeSet) o;
102                 return getTraversalCalculator().getTraversals(dcs, tp);
103             }
104             element = tp.getLastSegment();
105         } else {
106             element = pathOrElement;
107         }
108         
109         // Check for resources and adjust the depth to match the provider depth
110
if (isResourcePath(pathOrElement)) {
111             IResource resource = (IResource) element;
112             return getTraversalCalculator().getTraversals(resource, (TreePath)pathOrElement);
113         }
114         
115         // Finally, just get the traversals from the mapping.
116
ResourceMapping mapping = Utils.getResourceMapping(element);
117         if (mapping != null) {
118             // First, check if we have already calculated the traversal
119
ResourceTraversal[] traversals = getContext().getScope().getTraversals(mapping);
120             if (traversals != null)
121                 return traversals;
122             // We need to determine the traversals from the mapping.
123
// By default, use the local context. Models will need to provide
124
// custom handlers if this doesn't work for them
125
try {
126                 return mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null);
127             } catch (CoreException e) {
128                 TeamUIPlugin.log(e);
129             }
130         }
131         return new ResourceTraversal[0];
132     }
133     
134     private boolean isResourcePath(Object JavaDoc pathOrElement) {
135         if (pathOrElement instanceof TreePath) {
136             TreePath tp = (TreePath) pathOrElement;
137             return getTraversalCalculator().isResourcePath(tp);
138         }
139         return false;
140     }
141
142     /**
143      * Return the filter used to match diffs to which this action applies.
144      * @return the filter used to match diffs to which this action applies
145      */

146     protected abstract FastDiffFilter getDiffFilter();
147     
148     /* (non-Javadoc)
149      * @see org.eclipse.team.ui.mapping.ModelProviderOperation#shouldRun()
150      */

151     public boolean shouldRun() {
152         Object JavaDoc[] elements = getElements();
153         for (int i = 0; i < elements.length; i++) {
154             Object JavaDoc object = elements[i];
155             if (Utils.getResourceMapping(internalGetElement(object)) != null) {
156                 return true;
157             }
158         }
159         return false;
160     }
161     
162     protected IDiff[] getTargetDiffs() {
163         return getFileDeltas(getTreePathsOrElements());
164     }
165
166     private Object JavaDoc[] getTreePathsOrElements() {
167         if (selection instanceof ITreeSelection) {
168             ITreeSelection ts = (ITreeSelection) selection;
169             return ts.getPaths();
170         }
171         return getElements();
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.team.ui.TeamOperation#canRunAsJob()
176      */

177     protected boolean canRunAsJob() {
178         return true;
179     }
180     
181     protected ResourceModelTraversalCalculator getTraversalCalculator() {
182         return ResourceModelTraversalCalculator.getTraversalCalculator(getConfiguration());
183     }
184     
185     private Object JavaDoc internalGetElement(Object JavaDoc elementOrPath) {
186         if (elementOrPath instanceof TreePath) {
187             TreePath tp = (TreePath) elementOrPath;
188             return tp.getLastSegment();
189         }
190         return elementOrPath;
191     }
192     
193     public boolean belongsTo(Object JavaDoc family) {
194         if (family == getContext()) {
195             return true;
196         }
197         return super.belongsTo(family);
198     }
199
200 }
201
Popular Tags