Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f800d62329a0ba30aa882665e34caa5a81b155ff (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************************
 * 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.examples.model.ui.mapping;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.team.core.diff.FastDiffFilter;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.diff.IThreeWayDiff;
import org.eclipse.team.core.mapping.IMergeContext;
import org.eclipse.team.examples.model.ModelObjectDefinitionFile;
import org.eclipse.team.examples.model.ModelObjectElementFile;
import org.eclipse.team.internal.ui.mapping.ResourceModelProviderOperation;
import org.eclipse.team.ui.mapping.MergeActionHandler;
import org.eclipse.team.ui.mapping.SynchronizationOperation;
import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;

public class ModelMergeActionHandler extends MergeActionHandler {

	/*
	 * Operation to merge model elements. We're using an internal superclass to save on copying
	 * code.
	 */
	private final class ModelSynchronizeOperation extends ResourceModelProviderOperation {
		public ModelSynchronizeOperation(ISynchronizePageConfiguration configuration, IStructuredSelection selection) {
			super(configuration, selection);
		}

		@Override
		protected void execute(IProgressMonitor monitor) throws InvocationTargetException {
			// We need to perform special handling for any MOE file whose parent MOD is not included in the merge
			try {
				IMergeContext context = (IMergeContext)getContext();
				IDiff[] diffs = getTargetDiffs();
				ModelObjectElementFile[] moeMerges = getMoeOnlyMerges();
				IStatus status = context.merge(diffs, overwrite, monitor);
				if (!status.isOK())
					throw new CoreException(status);
				// For now, just cycle through each lonely MOE and update the parent
				for (ModelObjectElementFile file : moeMerges) {
					ModelObjectDefinitionFile modFile = (ModelObjectDefinitionFile)file.getParent();
					if (file.getResource().exists() && !modFile.hasMoe((IFile)file.getResource()))
						modFile.addMoe((IFile)file.getResource());
					else
						modFile.remove(file);
				}
			} catch (CoreException e) {
				throw new InvocationTargetException(e);
			}
		}

		private ModelObjectElementFile[] getMoeOnlyMerges() {
			List<ModelObjectElementFile> result = new ArrayList<>();
			Object[] elements = getElements();
			for (Object object : elements) {
				if (object instanceof ModelObjectElementFile) {
					ModelObjectElementFile moeFile = (ModelObjectElementFile) object;
					result.add(moeFile);
				}
			}
			return result.toArray(new ModelObjectElementFile[result.size()]);
		}

		@Override
		protected FastDiffFilter getDiffFilter() {
			return new FastDiffFilter() {
				@Override
				public boolean select(IDiff node) {
					if (node instanceof IThreeWayDiff) {
						IThreeWayDiff twd = (IThreeWayDiff) node;
						if ((twd.getDirection() == IThreeWayDiff.OUTGOING && overwrite) || twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) {
							return true;
						}
						return false;
					}
					// Overwrite should always be available for two-way diffs
					return overwrite;
				}
			};
		}
	}

	final boolean overwrite;
	private SynchronizationOperation operation;

	public ModelMergeActionHandler(ISynchronizePageConfiguration configuration, boolean overwrite) {
		super(configuration);
		this.overwrite = overwrite;
	}

	@Override
	protected SynchronizationOperation getOperation() {
		if (operation == null) {
			operation = new ModelSynchronizeOperation(getConfiguration(), getStructuredSelection());
		}
		return operation;
	}

	@Override
	protected void updateEnablement(IStructuredSelection selection) {
		synchronized (this) {
			operation = null;
		}
		super.updateEnablement(selection);
	}

}

Back to the top