Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8a493aead9b168d14067fab0b15cddabd2c2f527 (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
/*******************************************************************************
 * Copyright (c) 2012 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.ide.ui.internal.handler;

import java.util.Iterator;

import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareUI;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class CompareInNewEditor extends AbstractCompareHandler {

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		AdapterFactory adapterFactory = new ComposedAdapterFactory(
				ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

		final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
		final ISelection currentSelection = HandlerUtil.getCurrentSelection(event);

		if (currentSelection instanceof IStructuredSelection) {
			Iterator<?> iterator = ((IStructuredSelection)currentSelection).iterator();
			Notifier left = null;
			Notifier right = null;
			Notifier origin = null;

			left = (Notifier)iterator.next();
			if (iterator.hasNext()) {
				right = (Notifier)iterator.next();
			}
			if (iterator.hasNext()) {
				origin = (Notifier)iterator.next();
			}

			if (origin != null) {
				Shell shell = HandlerUtil.getActiveShell(event);
				SelectAncestorDialog dialog = new SelectAncestorDialog(shell, adapterFactory, new Notifier[] {
						left, right, origin });
				if (dialog.open() == Window.CANCEL) {
					return null;
				} else {
					left = dialog.leftNotifier;
					right = dialog.rightNotifier;
					origin = dialog.originNotifier;
				}
			}

			if (left instanceof EObject
					&& right instanceof EObject
					&& (EcoreUtil.isAncestor((EObject)left, (EObject)right) || EcoreUtil.isAncestor(
							(EObject)right, (EObject)left))) {
				MessageDialog.openInformation(activePart.getSite().getShell(), "EMF Compare",
						"Can't run a comparison between an object and one of its ancestor.");
			} else {
				final CompareEditorInput input = createCompareEditorInput(activePart, adapterFactory, left,
						right, origin);
				CompareUI.openCompareEditor(input);
			}
		}

		return null; // Reserved for future use, MUST be null.
	}
}

Back to the top