Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3e8bc5aefed5974e60ee0693dae810542c907bc9 (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
/***************************************************
 * Copyright (c) 2010 Atos Origin.

 *
 * 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:
 * Atos Origin - Initial API and implementation
 *
 ****************************************************/
package org.eclipse.papyrus.views.modelexplorer.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.widgets.toolbox.dialog.InformationDialog;
import org.eclipse.papyrus.views.modelexplorer.Activator;
import org.eclipse.papyrus.views.modelexplorer.preferences.INavigatorPreferenceConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;

/**
 * GenericTransformAction is an action which transforms and EObject by changing
 * its eclass.
 */
public class GenericTransformAction extends Action {

	/** title of error dialog */
	private static final String ERROR_TITLE = "Impossible to continue transformation.";

	/** message in error dialog */
	private static final String ERROR_MESSAGE = "The transformation can not continue.\n"
			+ "Some objects referencing your selection could not be able to reference the result of the transformation.\n"
			+ "For UML and SysML, applied stereotypes could not be applicable on the result of the transformation.\n"
			+ "Before performing the transformation please delete or unapply the elements listed bellow.";

	/** title of error dialog */
	private static final String WARNING_TITLE = "Warning: transformation command";

	/** WARNING_MESSAGE for transform command execution */
	private static final String WARNING_MESSAGE = "You are trying to transform an element typed %s into %s.\nThis operation will copy all the common elements between the two eclasses.\nDo you want to continue ?";

	/** The EClass to transform into. */
	private EClass targetEClass = null;

	/** The element to transform. */
	private EObject element;

	/**
	 * Constructor for a new action.
	 *
	 * @param transformationEClass
	 *            the eclass element must be transformed into
	 * @param adapterFactory
	 *            the adapter factory for providing label image
	 * @param elementToTransform
	 *            the element to transform
	 */
	public GenericTransformAction(EClass transformationEClass,
			AdapterFactory adapterFactory, EObject elementToTransform) {
		super(transformationEClass.getName());
		targetEClass = transformationEClass;
		element = elementToTransform;

		if (adapterFactory != null) {
			EObject tmpEobject = transformationEClass.getEPackage()
					.getEFactoryInstance().create(transformationEClass);
			IItemLabelProvider provider = (IItemLabelProvider) adapterFactory
					.adapt(tmpEobject, IItemLabelProvider.class);
			setImageDescriptor(ExtendedImageRegistry.INSTANCE
					.getImageDescriptor(provider.getImage(tmpEobject)));
		}
	}

	/**
	 * Transform the element and update referencing diagrams.
	 *
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		GenericTransformer transformer = new GenericTransformer(element);
		MultiStatus messages = transformer
				.isTransformationPossible(targetEClass);
		if (messages != null && messages.getChildren().length == 0) {
			String message = String.format(WARNING_MESSAGE, this.element
					.eClass().getName(), targetEClass.getName());
			InformationDialog dialog = new InformationDialog(
					Display.getDefault().getActiveShell(),
					WARNING_TITLE,
					message,
					Activator.getDefault().getPreferenceStore(),
					INavigatorPreferenceConstants.PREF_NAVIGATOR_TRANSFORM_INTO_SHOW_POPUP,
					SWT.YES | SWT.NO, MessageDialog.INFORMATION, new String[] {
							IDialogConstants.YES_LABEL,
							IDialogConstants.NO_LABEL });
			int result = dialog.open();
			if (result == SWT.YES || result == Window.OK) {
				transformer.transform(targetEClass);
			}
		} else {
			ErrorDialog errorDialog = new ErrorDialog(Display.getDefault()
					.getActiveShell(), ERROR_TITLE, ERROR_MESSAGE, messages,
					IStatus.WARNING);
			errorDialog.open();
		}
	}

}

Back to the top