Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8cfbcd909544b2d387e02acccb4b4aee10910747 (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
/*******************************************************************************
 * Copyright (c) 2006, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.mappings;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.mapping.ResourceTraversal;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.diff.IThreeWayDiff;
import org.eclipse.team.core.mapping.IResourceDiffTree;
import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.wizards.CommitWizard;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
import org.eclipse.ui.PlatformUI;

public abstract class AbstractCommitAction extends CVSModelProviderAction {

	public AbstractCommitAction(ISynchronizePageConfiguration configuration) {
		super(configuration);
	}
	
	@Override
	public void execute() {
    	final List resources = new ArrayList();
		try {
			final IStructuredSelection selection = getActualSelection();
			PlatformUI.getWorkbench().getProgressService().busyCursorWhile(monitor -> {
				try {
					ResourceTraversal[] traversals = getCommitTraversals(selection, monitor);
					resources.add(getOutgoingChanges(getSynchronizationContext().getDiffTree(), traversals, monitor));
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				}
			});
		} catch (InvocationTargetException e) {
			Utils.handleError(getConfiguration().getSite().getShell(), e, null, null);
		} catch (InterruptedException e) {
			// Ignore
		} catch (CVSException e) {
			Utils.handleError(getConfiguration().getSite().getShell(), e, null, null);
		}
		if (!resources.isEmpty() && ((IResource[])resources.get(0)).length > 0) {
	        Shell shell= getConfiguration().getSite().getShell();
	        try {
	            CommitWizard.run(getConfiguration().getSite().getPart(), shell, ((IResource[])resources.get(0)));
	        } catch (CVSException e) {
	            CVSUIPlugin.log(e);
	        }
		}
	}
	
	protected IStructuredSelection getActualSelection() throws CVSException {
		return getStructuredSelection();
	}

	protected abstract ResourceTraversal[] getCommitTraversals(IStructuredSelection selection, IProgressMonitor monitor) throws CoreException;
	
    public static IResource[] getOutgoingChanges(final IResourceDiffTree tree, ResourceTraversal[] traversals, IProgressMonitor monitor) {
		final List<IResource> resources = new ArrayList<>();
		IDiff[] diffs = tree.getDiffs(traversals);
		for (int i = 0; i < diffs.length; i++) {
			IDiff diff = diffs[i];
			if (hasLocalChange(diff)) {
				IResource resource = ResourceDiffTree.getResourceFor(diff);
				if (resource != null)
					resources.add(resource);
			}
		}
		return resources.toArray(new IResource[resources.size()]);
    }
    
	public static boolean hasLocalChange(IDiff diff) {
		if (diff instanceof IThreeWayDiff) {
			IThreeWayDiff twd = (IThreeWayDiff) diff;
			return twd.getDirection() == IThreeWayDiff.OUTGOING 
				|| twd.getDirection() ==  IThreeWayDiff.CONFLICTING;
		}
		return false;
	}

}

Back to the top