1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.HashSet ; 15 import java.util.Set ; 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 41 public abstract class WorkspaceTraversalAction extends WorkspaceAction { 42 43 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 e) { 59 throw TeamException.asTeamException(e); 60 } catch (InterruptedException e) { 61 throw new OperationCanceledException(); 62 } 63 } 64 65 private static IResource[] getRootTraversalResources(ISynchronizationScopeManager manager, IProgressMonitor monitor) throws CoreException { 66 Set result = new HashSet (); 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 { 86 ISynchronizationScopeManager manager = new SynchronizationScopeManager("", 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 { 96 ISynchronizationScopeManager manager = new SynchronizationScopeManager("", 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 { 110 final IResource[][] resources = new IResource[][] { null }; 113 try { 114 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 115 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 116 try { 117 resources[0] = getRootTraversalResources( 118 manager, 119 monitor); 120 } catch (CoreException e) { 121 throw new InvocationTargetException (e); 122 } 123 } 124 125 }); 126 } catch (InterruptedException e) { 127 return null; 129 } 130 return resources[0]; 131 } 132 133 public static IResource[] getProjects(IResource[] resources) { 134 Set projects = new HashSet (); 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 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 , InterruptedException { 170 final boolean[] hasChange = new boolean[] { false }; 171 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { 172 public void run(IProgressMonitor monitor) throws InvocationTargetException , 173 InterruptedException { 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 (e); 182 } finally { 183 monitor.done(); 184 } 185 } 186 }); 187 return hasChange[0]; 188 } 189 190 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 212 protected SynchronizationScopeManager getScopeManager() { 213 return new SynchronizationScopeManager( 214 "", getCVSResourceMappings(), 216 getResourceMappingContext(), true); 217 } 218 } 219 | Popular Tags |