Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 433344eba354be3eca2964ce5b5d354fc46d4229 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 *
 */
package org.eclipse.papyrus.java.reverse.ui;

import java.util.Arrays;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.java.reverse.ui.dialog.ReverseCodeDialog;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.uml2.uml.Package;


/**
 * @author dumoulin
 *
 */
public class ReverseCodeHandler extends AbstractHandler implements IHandler {

	private static String DefaultGenerationModeleName = "generated";

	/**
	 * Method called when button is pressed.
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// Try to find uml resource
		final Resource umlResource;
		try {
			umlResource = getUmlResource();
		} catch (NullPointerException e) {
			// No uml resource available. User must open a model. We open an error dialog with an explicit message to advice user.
			Shell shell = HandlerUtil.getActiveShell(event);
			Status errorStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ReverseCodeHandler_NoModelError_Title);
			ErrorDialog.openError(shell, "", Messages.ReverseCodeHandler_NoModelError_Message, errorStatus);

			// Stop the reverse execution.
			return null;
		}
		;

		String modelUid = getModelUid(umlResource);
		System.out.println("Model uid :" + modelUid);

		// Get reverse parameters from a dialog
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().getShell();
		// ReverseCodeDialog dialog = new ReverseCodeDialog(shell, DefaultGenerationPackageName, Arrays.asList("generated") );
		final ReverseCodeDialog dialog = getDialog(shell, modelUid);

		int res = dialog.open();
		// System.out.println("dialog result =" + res);
		if (res == Window.CANCEL) {
			System.out.println("Canceled by user.");
			return null;
		}

		// Execute the reverse with provided parameters
		TransactionalEditingDomain editingDomain;
		try {
			editingDomain = getEditingDomain();
		} catch (ServiceException e) {
			// Can't get editing domain
			e.printStackTrace();
			throw new ExecutionException(e.getMessage());
		}

		RecordingCommand command = new RecordingCommand(editingDomain, "Reverse Java Code") {

			@Override
			protected void doExecute() {
				ReverseCodeHandler.this.doExecute(dialog);
			}

		};

		editingDomain.getCommandStack().execute(command);


		return null;
	}

	/**
	 * Find the modelUid name contains into umlResource taken in parameter
	 *
	 * @param umlResource
	 * @return the modelUid name
	 */
	private String getModelUid(Resource umlResource) {
		// Try to compute a uid identifying the model. Used to store user settings.
		String modelUid = umlResource.getURI().toPlatformString(true);
		if (modelUid == null) {
			System.err.println("Can't compute relatif model uid. Use absolute one");
			modelUid = umlResource.getURI().path();
		}
		return modelUid;
	}

	/**
	 * Command ran in a RecordingCommand, after the dialog
	 * Run the @link{JavaCodeReverse.executeCodeReverse}
	 * Shall be override to change command behavior
	 */
	protected void doExecute(ReverseCodeDialog dialog) {
		// Create searchpaths. Add the rootmodelname as prefix.
		final List<String> searchPaths = Arrays.asList(dialog.getSearchPath());
		Resource umlResource = getUmlResource();
		String packageName = getPackageName(dialog);
		JavaCodeReverse reverse = new JavaCodeReverse(getRootPackage(umlResource), packageName, searchPaths);
		reverse.executeCodeReverse(umlResource, packageName, searchPaths);
	}

	/**
	 * The dialog used for user.
	 *
	 * @param shell
	 * @param modelUid
	 * @return the dialog to show to user
	 */
	protected ReverseCodeDialog getDialog(Shell shell, String modelUid) {
		return new ReverseCodeDialog(shell, modelUid, null, null);
	}

	/**
	 * Find the name of the model provided by the dialog
	 *
	 * @param dialog
	 *            opened dialog to user
	 * @return the name of the model. If the user has changed this name, return the name provided by the user; return the default model name
	 *         otherwise.
	 */
	protected String getPackageName(ReverseCodeDialog dialog) {
		String generationPackageName = dialog.getValue();
		if (generationPackageName == null || generationPackageName.length() == 0) {
			generationPackageName = DefaultGenerationModeleName;
		}
		return generationPackageName;
	}

	/**
	 * Get the uml resource used by the model.
	 *
	 * @return the Uml Resource
	 */
	protected Resource getUmlResource() {
		Resource umlResource = UmlUtils.getUmlModel().getResource();
		return umlResource;
	}

	/**
	 * Get the name of the root model.
	 *
	 * @return
	 */
	protected Package getRootPackage(Resource umlResource) {
		Package rootPackage = (Package) umlResource.getContents().get(0);
		return rootPackage;
	}

	/**
	 * Get the current MultiDiagramEditor.
	 *
	 * @return
	 */
	protected IMultiDiagramEditor getMultiDiagramEditor() {
		return EditorUtils.getMultiDiagramEditor();
	}

	/**
	 * Get the main editing doamin.
	 *
	 * @return
	 * @throws ServiceException
	 */
	protected TransactionalEditingDomain getEditingDomain() throws ServiceException {
		return ServiceUtilsForActionHandlers.getInstance().getTransactionalEditingDomain();
	}

}

Back to the top