KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > actions > WorkspaceTraversalAction


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.ccvs.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.team.core.RepositoryProvider;
23 import org.eclipse.team.core.TeamException;
24 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
25 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager;
26 import org.eclipse.team.core.subscribers.Subscriber;
27 import org.eclipse.team.core.subscribers.SubscriberResourceMappingContext;
28 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
29 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
30 import org.eclipse.team.internal.ccvs.ui.Policy;
31 import org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation;
32 import org.eclipse.team.internal.ui.Utils;
33 import org.eclipse.team.internal.ui.mapping.BuildScopeOperation;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.PlatformUI;
36
37 /**
38  * A specialized workspace actions that operates on resource traversals
39  * instead of resources/
40  */

41 public abstract class WorkspaceTraversalAction extends WorkspaceAction {
42
43     /**
44      * Return the selected mappings that contain resources
45      * within a CVS managed project.
46      * @return the selected mappings that contain resources
47      * within a CVS managed project
48      */

49     protected ResourceMapping[] getCVSResourceMappings() {
50         return getSelectedResourceMappings(CVSProviderPlugin.getTypeId());
51     }
52
53     private static ResourceTraversal[] getTraversals(IWorkbenchPart part, ISynchronizationScopeManager manager, IProgressMonitor monitor) throws CoreException {
54         try {
55             BuildScopeOperation op = new BuildScopeOperation(part, manager);
56             op.run(monitor);
57             return manager.getScope().getTraversals();
58         } catch (InvocationTargetException JavaDoc e) {
59             throw TeamException.asTeamException(e);
60         } catch (InterruptedException JavaDoc e) {
61             throw new OperationCanceledException();
62         }
63     }
64     
65     private static IResource[] getRootTraversalResources(ISynchronizationScopeManager manager, IProgressMonitor monitor) throws CoreException {
66         Set JavaDoc result = new HashSet JavaDoc();
67         ResourceTraversal[] traversals = getTraversals(null, manager, monitor);
68         for (int i = 0; i < traversals.length; i++) {
69             ResourceTraversal traversal = traversals[i];
70             IResource[] resources = traversal.getResources();
71             for (int k = 0; k < resources.length; k++) {
72                 IResource resource = resources[k];
73                 if (RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) != null) {
74                     result.add(resource);
75                 }
76             }
77         }
78         return (IResource[]) result.toArray(new IResource[result.size()]);
79     }
80
81     protected Subscriber getWorkspaceSubscriber() {
82         return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber();
83     }
84     
85     public static IResource[] getResourcesToCompare(ResourceMapping[] mappings, Subscriber subscriber) throws InvocationTargetException JavaDoc {
86         ISynchronizationScopeManager manager = new SynchronizationScopeManager("", //$NON-NLS-1$
87
mappings, SubscriberResourceMappingContext.createContext(subscriber), true);
88         try {
89             return getResourcesToCompare(manager);
90         } finally {
91             manager.dispose();
92         }
93     }
94     
95     protected IResource[] getResourcesToCompare(final Subscriber subscriber) throws InvocationTargetException JavaDoc {
96         ISynchronizationScopeManager manager = new SynchronizationScopeManager("", //$NON-NLS-1$
97
getCVSResourceMappings(), SubscriberResourceMappingContext.createContext(subscriber), true);
98         try {
99             return getResourcesToCompare(manager);
100         } finally {
101             manager.dispose();
102         }
103     }
104     
105     protected ResourceMappingContext getResourceMappingContext() {
106         return SubscriberResourceMappingContext.createContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber());
107     }
108
109     public static IResource[] getResourcesToCompare(final ISynchronizationScopeManager manager) throws InvocationTargetException JavaDoc {
110         // Determine what resources need to be synchronized.
111
// Use a resource mapping context to include any relevant remote resources
112
final IResource[][] resources = new IResource[][] { null };
113         try {
114             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
115                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
116                     try {
117                         resources[0] = getRootTraversalResources(
118                                 manager,
119                                 monitor);
120                     } catch (CoreException e) {
121                         throw new InvocationTargetException JavaDoc(e);
122                     }
123                 }
124             
125             });
126         } catch (InterruptedException JavaDoc e) {
127             // Canceled
128
return null;
129         }
130         return resources[0];
131     }
132     
133     public static IResource[] getProjects(IResource[] resources) {
134         Set JavaDoc projects = new HashSet JavaDoc();
135         for (int i = 0; i < resources.length; i++) {
136             IResource resource = resources[i];
137             projects.add(resource.getProject());
138         }
139         return (IResource[]) projects.toArray(new IResource[projects.size()]);
140     }
141     
142     /**
143      *
144      * @param mappings
145      * @return
146      *
147      * @deprecated need to find a better way to do this
148      */

149     public static boolean isLogicalModel(ResourceMapping[] mappings) {
150         for (int i = 0; i < mappings.length; i++) {
151             ResourceMapping mapping = mappings[i];
152             if (! (mapping.getModelObject() instanceof IResource) ) {
153                 return true;
154             }
155         }
156         return false;
157     }
158     
159     protected IFile getSelectedFile() {
160         ResourceMapping[] mappings = getCVSResourceMappings();
161         if (mappings.length == 1) {
162             IResource resource = Utils.getResource(mappings[0].getModelObject());
163             if (resource != null && resource.getType() == IResource.FILE)
164                 return (IFile)resource;
165         }
166         return null;
167     }
168     
169     protected boolean hasOutgoingChanges(final RepositoryProviderOperation operation) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
170         final boolean[] hasChange = new boolean[] { false };
171         PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
172             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
173                     InterruptedException JavaDoc {
174                 try {
175                     monitor.beginTask(CVSUIMessages.WorkspaceTraversalAction_0, 100);
176                     operation.buildScope(Policy.subMonitorFor(monitor, 50));
177                     hasChange[0] = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().hasLocalChanges(
178                             operation.getScope().getTraversals(),
179                             Policy.subMonitorFor(monitor, 50));
180                 } catch (CoreException e) {
181                     throw new InvocationTargetException JavaDoc(e);
182                 } finally {
183                     monitor.done();
184                 }
185             }
186         });
187         return hasChange[0];
188     }
189     
190     /**
191      * Return the complete set of traversals to be targeted by the action
192      * including those that are included by consulting the models.
193      *
194      * @param monitor
195      * a progress monitor
196      * @return the complete set of traversals to be targeted by the action
197      * @throws CoreException
198      */

199     protected ResourceTraversal[] getTraversals(IProgressMonitor monitor) throws CoreException {
200         SynchronizationScopeManager scopeManager = getScopeManager();
201         try {
202             return getTraversals(getTargetPart(), scopeManager, monitor);
203         } finally {
204             scopeManager.dispose();
205         }
206     }
207     
208     /**
209      * Return a scope manager that provides the scope for the action.
210      * @return a scope manager that provides the scope for the action
211      */

212     protected SynchronizationScopeManager getScopeManager() {
213         return new SynchronizationScopeManager(
214                 "", //$NON-NLS-1$
215
getCVSResourceMappings(),
216                 getResourceMappingContext(), true);
217     }
218 }
219
Popular Tags