KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.mapping.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.team.core.diff.IDiff;
21 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
22 import org.eclipse.team.internal.core.mapping.CompoundResourceTraversal;
23 import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
24 import org.eclipse.team.internal.ui.Policy;
25 import org.eclipse.team.internal.ui.Utils;
26 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
27 import org.eclipse.team.ui.synchronize.ModelParticipantAction;
28
29 public abstract class ResourceModelParticipantAction extends ModelParticipantAction {
30
31     public ResourceModelParticipantAction(String JavaDoc text, ISynchronizePageConfiguration configuration) {
32         super(text, configuration);
33     }
34
35     /**
36      * Return the set of traversals that cover the resources in the current selection
37      * @param selection the selection
38      * @param monitor a progress monitor
39      * @return the traversals that cover the resources in the current selection
40      * @throws CoreException
41      */

42     protected ResourceTraversal[] getResourceTraversals(IStructuredSelection selection, IProgressMonitor monitor) throws CoreException {
43         try {
44             monitor.beginTask(null, selection.size() * 100);
45             CompoundResourceTraversal traversal = new CompoundResourceTraversal();
46             if (selection instanceof ITreeSelection) {
47                 ITreeSelection ts = (ITreeSelection) selection;
48                 TreePath[] paths = ts.getPaths();
49                 for (int i = 0; i < paths.length; i++) {
50                     TreePath path = paths[i];
51                     ResourceTraversal[] traversals = getTraversals(path, Policy.subMonitorFor(monitor, 100));
52                     traversal.addTraversals(traversals);
53                 }
54             } else {
55                 for (Iterator iter = selection.iterator(); iter.hasNext();) {
56                     Object JavaDoc element = (Object JavaDoc) iter.next();
57                     ResourceTraversal[] traversals = getTraversals(element, Policy.subMonitorFor(monitor, 100));
58                     traversal.addTraversals(traversals);
59                 }
60             }
61             return traversal.asTraversals();
62         } finally {
63             monitor.done();
64         }
65     }
66     
67     /**
68      * Return a traversal that includes the resources that are visible
69      * in the sync view.
70      * @param element the selected element
71      * @return a set of traversals that cover the visible resources.
72      */

73     private ResourceTraversal[] getTraversals(Object JavaDoc element, IProgressMonitor monitor) throws CoreException {
74         ResourceMapping mapping = Utils.getResourceMapping(element);
75         if (mapping != null)
76             return mapping.getTraversals(getResourceMappingContext(), monitor);
77         return null;
78     }
79
80     protected ResourceMappingContext getResourceMappingContext() {
81         return new SynchronizationResourceMappingContext(getSynchronizationContext());
82     }
83     
84     protected ResourceModelTraversalCalculator getTraversalCalculator() {
85         return ResourceModelTraversalCalculator.getTraversalCalculator(getConfiguration());
86     }
87
88     /**
89      * Return a traversal that includes the resources that are visible
90      * in the sync view.
91      * @param element the selected element
92      * @return a set of traversals that cover the visible resources.
93      */

94     private ResourceTraversal[] getTraversals(TreePath path, IProgressMonitor monitor) throws CoreException {
95         if (path.getSegmentCount() > 0) {
96             DiffChangeSet set = getChangeSet(path);
97             Object JavaDoc o = path.getLastSegment();
98             if (set != null) {
99                 if (path.getSegmentCount() == 1) {
100                     return new ResourceTraversal[] { new ResourceTraversal(set.getResources(), IResource.DEPTH_ZERO, IResource.NONE) };
101                 }
102                 if (o instanceof IResource) {
103                     IResource resource = (IResource) o;
104                     int depth = getTraversalCalculator().getLayoutDepth(resource, path);
105                     IDiff[] diffs = set.getDiffTree().getDiffs(resource, depth);
106                     Set resources = new HashSet();
107                     for (int i = 0; i < diffs.length; i++) {
108                         IDiff diff = diffs[i];
109                         IResource r = ResourceDiffTree.getResourceFor(diff);
110                         if (r != null)
111                             resources.add(r);
112                     }
113                     return new ResourceTraversal[] { new ResourceTraversal((IResource[]) resources.toArray(new IResource[resources.size()]), IResource.DEPTH_ZERO, IResource.NONE) };
114                 }
115             }
116             if (getTraversalCalculator().isResourcePath(path)) {
117                 IResource resource = (IResource) o;
118                 return getTraversalCalculator().getTraversals(resource, path);
119             }
120             return getTraversals(o, monitor);
121         }
122         return null;
123     }
124     
125     private DiffChangeSet getChangeSet(TreePath path) {
126         Object JavaDoc o = path.getFirstSegment();
127         if (o instanceof DiffChangeSet) {
128             return (DiffChangeSet) o;
129         }
130         return null;
131     }
132
133 }
134
Popular Tags