Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0375cda6a6483d15fd98b9294681c1aff80ebcef (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 * 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:
 *  Ernest Wozniak (CEA LIST) ernest.wozniak@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr modification
 *****************************************************************************/
package org.eclipse.papyrus.dsml.validation.generation.ui;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.dsml.validation.model.elements.impl.ConstraintManagerImpl;
import org.eclipse.papyrus.dsml.validation.model.elements.interfaces.IConstraintProvider;
import org.eclipse.papyrus.dsml.validation.model.elements.interfaces.IConstraintsCategory;
import org.eclipse.papyrus.dsml.validation.model.elements.interfaces.IConstraintsManager;
import org.eclipse.papyrus.dsml.validation.model.elements.interfaces.IValidationRule;
import org.eclipse.papyrus.dsml.validation.model.profilenames.Utils;
import org.eclipse.papyrus.dsml.validation.wizard.CreateEMFValidationProject;
import org.eclipse.papyrus.dsml.validation.wizard.JavaContentGenerator;
import org.eclipse.papyrus.dsml.validation.wizard.ValidationPluginGenerator;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.builders.NotificationBuilder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Profile;


/**
 * this handler launch the creation of the plugin to validate contraints of the profile
 *
 */
public class CreateJavaValidationPluginHandler extends AbstractHandler {

	private IConstraintsManager constraintsManager;


	/**
	 * <pre>
	 * Get the selected element, the first selected element if several are selected or null
	 * if no selection or the selection is not an {@link EObject}.
	 * 
	 * @return selected {@link EObject} or null
	 * </pre>
	 *
	 */
	protected EObject getSelectedElement() {
		EObject eObject = null;
		Object selection = null;

		// Get current selection
		selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();

		// Get first element if the selection is an IStructuredSelection
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			selection = structuredSelection.getFirstElement();
		}

		// Treat non-null selected object (try to adapt and return EObject)
		if (selection != null) {

			eObject = EMFHelper.getEObject(selection);
		}
		return eObject;
	}


	/**
	 *
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 *
	 * @param event
	 * @return null
	 * @throws ExecutionException
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		EObject selection = getSelectedElement();

		if (selection instanceof Profile) {
			Profile profileSelection = (Profile) selection;

			constraintsManager = new ConstraintManagerImpl(profileSelection);
			boolean isOCLConstraint = false;
			for (IConstraintProvider constraintProvider : constraintsManager.getConstraintsProviders()) {
				for (IConstraintsCategory constraintCategory : constraintProvider.getConstraintsCategories()) {
					for (IValidationRule constraint : constraintCategory.getConstraints()) {
						// this is an OCL constraint?
						if (Utils.hasSpecificationForOCL(constraint.getConstraint())) {
							isOCLConstraint = true;
						}
					}
				}
			}
			EPackage definition = null;
			if (isOCLConstraint) {
				definition = profileSelection.getDefinition();
				if (definition == null) {
					NotificationBuilder errorDialog = NotificationBuilder.createErrorPopup("The profile must be defined in order to generate OCL Constraints");
					errorDialog.run();
					// finish by displaying a message for the user to inform that it need to define it before to launch it.
					return null;
				}
			}
			IProject existingProject = null;
			URI uri = profileSelection.eResource().getURI();

			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			if (uri.segmentCount() >= 2) {
				existingProject = root.getProject(uri.segment(1));
			}
			int question = 0;
			Shell shell = Display.getDefault().getActiveShell();
			if ((existingProject != null) && existingProject.exists()) {
				MessageDialog dialog = new MessageDialog(shell,
						"Choose plugin generation", null,
						"How should the plugin be generated?", MessageDialog.QUESTION,
						new String[] { "Create a new plugin", "write to plugin hosting the model or profile" }, 1);
				question = dialog.open();
			}

			if (question == 1) {

				// generate java code
				JavaContentGenerator generateAllJava = new JavaContentGenerator(existingProject, profileSelection);
				generateAllJava.run();
				// generate plugin + extension point
				try {
					ValidationPluginGenerator.instance.generate(existingProject, constraintsManager, definition);
				} catch (Exception e) {
					MessageDialog.openInformation(shell, "Exception occured during plugin generation", e.getMessage());
				}
			}
			else {
				CreateEMFValidationProject wizard = new CreateEMFValidationProject(profileSelection, constraintsManager, definition);
				wizard.openDialog();
			}
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		EObject eObject = getSelectedElement();
		if (eObject instanceof Profile) {
			return true;
		}
		return false;
	}

}

Back to the top