1 11 package org.eclipse.team.internal.ccvs.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.*; 15 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.resources.mapping.*; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.action.IAction; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.team.core.RepositoryProvider; 24 import org.eclipse.team.core.TeamException; 25 import org.eclipse.team.core.mapping.ISynchronizationContext; 26 import org.eclipse.team.core.subscribers.Subscriber; 27 import org.eclipse.team.core.subscribers.SubscriberScopeManager; 28 import org.eclipse.team.core.synchronize.SyncInfo; 29 import org.eclipse.team.internal.ccvs.core.*; 30 import org.eclipse.team.internal.ccvs.ui.*; 31 import org.eclipse.team.internal.ccvs.ui.mappings.WorkspaceModelParticipant; 32 import org.eclipse.team.internal.ccvs.ui.mappings.WorkspaceSubscriberContext; 33 import org.eclipse.team.internal.ccvs.ui.subscriber.WorkspaceSynchronizeParticipant; 34 import org.eclipse.team.internal.ui.Utils; 35 import org.eclipse.team.internal.ui.synchronize.actions.OpenInCompareAction; 36 import org.eclipse.team.ui.TeamUI; 37 import org.eclipse.team.ui.synchronize.*; 38 import org.eclipse.ui.*; 39 40 43 public class SyncAction extends WorkspaceTraversalAction { 44 45 public void execute(IAction action) throws InvocationTargetException { 46 if (isOpenEditorForSingleFile()) { 48 IFile file = getSelectedFile(); 49 if (file != null && isOKToShowSingleFile(file)) { 50 showSingleFileComparison(getShell(), CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(), file, getTargetPage()); 51 return; 52 } 53 } 54 if (isShowModelSync()) { 55 ResourceMapping[] mappings = getCVSResourceMappings(); 56 if (mappings.length == 0) 57 return; 58 SubscriberScopeManager manager = WorkspaceSubscriberContext.createWorkspaceScopeManager(mappings, true, CommitAction.isIncludeChangeSets(getShell(), CVSUIMessages.SyncAction_1)); 59 WorkspaceSubscriberContext context = WorkspaceSubscriberContext.createContext(manager, ISynchronizationContext.THREE_WAY); 60 WorkspaceModelParticipant participant = new WorkspaceModelParticipant(context); 61 TeamUI.getSynchronizeManager().addSynchronizeParticipants(new ISynchronizeParticipant[] {participant}); 62 participant.run(getTargetPart()); 63 } else { 64 IResource[] resources = getResourcesToCompare(getWorkspaceSubscriber()); 65 if (resources == null || resources.length == 0) return; 66 WorkspaceSynchronizeParticipant participant = (WorkspaceSynchronizeParticipant)SubscriberParticipant.getMatchingParticipant(WorkspaceSynchronizeParticipant.ID, resources); 68 if (participant == null) { 70 ISynchronizeScope scope; 71 if (includesAllCVSProjects(resources)) { 72 scope = new WorkspaceScope(); 73 } else { 74 IWorkingSet[] sets = getSelectedWorkingSets(); 75 if (sets != null) { 76 scope = new WorkingSetScope(sets); 77 } else { 78 scope = new ResourceScope(resources); 79 } 80 } 81 participant = new WorkspaceSynchronizeParticipant(scope); 82 TeamUI.getSynchronizeManager().addSynchronizeParticipants(new ISynchronizeParticipant[] {participant}); 83 } 84 participant.refresh(resources, getTargetPart().getSite()); 85 } 86 } 87 88 private static boolean isShowModelSync() { 89 return CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_ENABLE_MODEL_SYNC); 90 } 91 92 private static boolean isOpenEditorForSingleFile() { 93 return CVSUIPlugin.getPlugin().getPreferenceStore().getBoolean(ICVSUIConstants.PREF_OPEN_COMPARE_EDITOR_FOR_SINGLE_FILE); 94 } 95 96 private IWorkingSet[] getSelectedWorkingSets() { 97 ResourceMapping[] mappings = getCVSResourceMappings(); 98 List sets = new ArrayList(); 99 for (int i = 0; i < mappings.length; i++) { 100 ResourceMapping mapping = mappings[i]; 101 if (mapping.getModelObject() instanceof IWorkingSet) { 102 IWorkingSet set = (IWorkingSet) mapping.getModelObject(); 103 sets.add(set); 104 } else { 105 return null; 106 } 107 } 108 if (sets.isEmpty()) 109 return null; 110 return (IWorkingSet[]) sets.toArray(new IWorkingSet[sets.size()]); 111 } 112 113 private boolean includesAllCVSProjects(IResource[] resources) { 114 for (int i = 0; i < resources.length; i++) { 116 IResource resource = resources[i]; 117 if (resource.getType() != IResource.PROJECT) 118 return false; 119 } 120 IProject[] cvsProjects = getAllCVSProjects(); 121 return cvsProjects.length == resources.length; 122 } 123 124 private IProject[] getAllCVSProjects() { 125 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 126 Set cvsProjects = new HashSet(); 127 for (int i = 0; i < projects.length; i++) { 128 IProject project = projects[i]; 129 if (RepositoryProvider.isShared(project) && RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()) != null) { 130 cvsProjects.add(project); 131 } 132 } 133 return (IProject[]) cvsProjects.toArray(new IProject[cvsProjects.size()]); 134 } 135 136 143 public static boolean isOKToShowSingleFile(IFile file) { 144 if (!isShowModelSync()) 145 return true; 146 IModelProviderDescriptor[] descriptors = ModelProvider.getModelProviderDescriptors(); 147 for (int i = 0; i < descriptors.length; i++) { 148 IModelProviderDescriptor descriptor = descriptors[i]; 149 try { 150 IResource[] resources = descriptor.getMatchingResources(new IResource[] { file }); 151 if (resources.length > 0) { 152 ModelProvider provider = descriptor.getModelProvider(); 153 ResourceMapping[] mappings = provider.getMappings(file, ResourceMappingContext.LOCAL_CONTEXT, null); 157 for (int j = 0; j < mappings.length; j++) { 158 ResourceMapping mapping = mappings[j]; 159 ResourceTraversal[] traversals = mapping.getTraversals(ResourceMappingContext.LOCAL_CONTEXT, null); 160 for (int k = 0; k < traversals.length; k++) { 161 ResourceTraversal traversal = traversals[k]; 162 IResource[] tResources = traversal.getResources(); 163 for (int index = 0; index < tResources.length; index++) { 164 IResource tr = tResources[index]; 165 if (!tr.equals(file)) 166 return false; 167 } 168 } 169 } 170 } 171 } catch (CoreException e) { 172 CVSUIPlugin.log(e); 173 } 174 } 175 return true; 176 } 177 178 184 public static void showSingleFileComparison(final Shell shell, final Subscriber subscriber, final IResource resource, final IWorkbenchPage page) { 185 try { 186 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { 187 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 188 try { 189 subscriber.refresh(new IResource[]{resource}, IResource.DEPTH_ZERO, monitor); 190 } catch (TeamException e) { 191 throw new InvocationTargetException (e); 192 } 193 } 194 }); 195 final SyncInfo info = subscriber.getSyncInfo(resource); 196 if (info == null) return; 197 shell.getDisplay().syncExec(new Runnable () { 198 public void run() { 199 if (info.getKind() == SyncInfo.IN_SYNC) { 200 MessageDialog.openInformation(shell, CVSUIMessages.SyncAction_noChangesTitle, CVSUIMessages.SyncAction_noChangesMessage); } else { 202 SyncInfoCompareInput input = new SyncInfoCompareInput(subscriber.getName(), info); 203 OpenInCompareAction.openCompareEditor(input, page); 204 } 205 } 206 }); 207 } catch (InvocationTargetException e) { 208 Utils.handle(e); 209 } catch (InterruptedException e) { 210 } catch (TeamException e) { 211 Utils.handle(e); 212 } 213 } 214 215 public static boolean isSingleFile(IResource[] resources) { 216 return resources.length == 1 && resources[0].getType() == IResource.FILE; 217 } 218 219 225 protected boolean isEnabledForCVSResource(ICVSResource cvsResource) throws CVSException { 226 return (super.isEnabledForCVSResource(cvsResource) || (cvsResource.getParent().isCVSFolder() && !cvsResource.isIgnored())); 227 } 228 229 public String getId() { 230 return ICVSUIConstants.CMD_SYNCHRONIZE; 231 } 232 233 234 public boolean isEnabled() { 235 if(super.isEnabled()){ 236 return true; 237 } 238 IWorkingSet[] sets = getSelectedWorkingSets(); 239 if(sets == null || sets.length == 0){ 241 return false; 242 } 243 244 Set projects = getProjects(sets); 245 246 boolean existsProjectToSynchronize = false; 247 for (Iterator it = projects.iterator(); it.hasNext();) { 248 IProject project = (IProject) it.next(); 249 RepositoryProvider provider = RepositoryProvider.getProvider(project); 250 if(provider != null){ 251 existsProjectToSynchronize = true; 252 if(!CVSProviderPlugin.getTypeId().equals(provider.getID())){ 254 return false; 255 } 256 } 257 } 258 259 return existsProjectToSynchronize; 260 } 261 262 private Set getProjects(IWorkingSet[] sets) { 263 Set projects = new HashSet(); 264 265 if(sets == null) 266 return projects; 267 268 for (int i = 0; i < sets.length; i++) { 269 IAdaptable ad[] = sets[i].getElements(); 270 if (ad != null) { 271 for (int j = 0; j < ad.length; j++) { 272 IResource resource = (IResource) ad[j] 273 .getAdapter(IResource.class); 274 if (resource != null) { 275 projects.add(resource.getProject()); 276 } 277 } 278 } 279 } 280 281 return projects; 282 } 283 } 284 | Popular Tags |