Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2795419df5eac412e301001aeed127cd8b19f09b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.examples.filesystem.ui;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.synchronize.SyncInfoSet;
import org.eclipse.team.examples.filesystem.FileSystemPlugin;
import org.eclipse.team.examples.filesystem.FileSystemProvider;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.team.ui.synchronize.SynchronizeModelOperation;

/**
 * Override SynchronizeModelOperation in order to delegate the operation to each file system 
 * provider instance (i.e. each project). Also, prompt to prune conflicts from the set of
 * selected resources.
 */
public abstract class FileSystemSynchronizeOperation extends SynchronizeModelOperation {

	protected FileSystemSynchronizeOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
		super(configuration, elements);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		// First, ask the user if they want to include conflicts
		SyncInfoSet syncSet = getSyncInfoSet();
		if (!promptForConflictHandling(getShell(), syncSet)) return;
		// Divide the sync info by project
		final Map projectSyncInfos = getProjectSyncInfoSetMap(syncSet);
		monitor.beginTask(null, projectSyncInfos.size() * 100);
		for (Iterator iter = projectSyncInfos.keySet().iterator(); iter.hasNext(); ) {
			final IProject project = (IProject) iter.next();
			try {
				// Pass the scheduling rule to the synchronizer so that sync change events
				// and cache commits to disk are batched
				FileSystemProvider provider = (FileSystemProvider)RepositoryProvider.getProvider(project, FileSystemPlugin.PROVIDER_ID);
				if (provider != null) {
					run(provider, (SyncInfoSet)projectSyncInfos.get(project), monitor);
				}
			} catch (TeamException e) {
				throw new InvocationTargetException(e);
			}
		}
		monitor.done();
	}

	/**
	 * Prompt the user to include conflicts. If the user choses not to include
	 * conflicts, they will be removed from the passed set. If the user cancels,
	 * <code>false</code> is returned.
	 * @param shell a shell
	 * @param syncSet the set of selected resources
	 * @return whether the operation should proceed.
	 */
	protected abstract boolean promptForConflictHandling(Shell shell, SyncInfoSet syncSet);

	/*
	 * Divide the sync info for the operation by project
	 */
	private Map getProjectSyncInfoSetMap(SyncInfoSet syncSet) {
		Map map = new HashMap();
		SyncInfo[] infos = syncSet.getSyncInfos();
		for (int i = 0; i < infos.length; i++) {
			SyncInfo info = infos[i];
			IProject project = info.getLocal().getProject();
			SyncInfoSet set = (SyncInfoSet)map.get(project);
			if (set == null) {
				set = new SyncInfoSet();
				map.put(project, set);
			}
			set.add(info);
		}
		return map;
	}
	
	/**
	 * Run the operation on the sync info in the given set. The sync info will be all
	 * from the same project.
	 * @param provider
	 * @param set the sync info set
	 * @param monitor a progress monitor
	 */
	protected abstract void run(FileSystemProvider provider, SyncInfoSet set, IProgressMonitor monitor) throws TeamException;
}

Back to the top