Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 848f5ea6cbc9b00ada067ce35ca706e9330ec2b8 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 *
 */
package org.eclipse.papyrus.java.reverse.ui.handlers;

import java.util.ArrayList;
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.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.java.reverse.utils.TypeOperationsEnhanced;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;


/**
 * An handler getting the selected element and reporting them in console.
 *
 * @author Cedric dumoulin
 *
 */
public class CreateAssociationFromPropertyHandler extends AbstractHandler implements IHandler {


	/**
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 * @param event
	 * @return
	 * @throws ExecutionException
	 *
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		List<Object> objects = lookupSelectedElements();
		List<Property> selectedProperties = getAllSelectionOfType(event, Property.class);
		if (objects == null) {

			showErrorDialog(event, "No object selected", "Can't create association. Please select a Property First.");
			return null;
		}

		// Try creation
		try {
			doExecute(selectedProperties);
		} catch (ServiceException e) {
			showErrorDialog(event, "Can't get TransactionalEditingDomain", e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			showErrorDialog(event, "Can't Create Association", e.getMessage());
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * Get all selected element of the specified type.
	 *
	 * @param expectedType
	 * @return
	 * @throws ExecutionException
	 */
	private <T> List<T> getAllSelectionOfType(ExecutionEvent event, Class<T> expectedType) throws ExecutionException {

		ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
		// IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		// ISelection selection = page.getSelection();
		System.out.println("check " + selection);
		if (selection instanceof IStructuredSelection)
		{
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			return getAllElementOfType(structuredSelection.toList(), expectedType);
		}
		else if (selection instanceof TreeSelection) {
			TreeSelection treeSelection = (TreeSelection) selection;
			return getAllElementOfType(treeSelection.toList(), expectedType);

		}
		return null;
	}

	/**
	 * Filter the list, and only retain objects that can be adapted to the specified type
	 *
	 * @param objects
	 * @param class1
	 * @return
	 */
	private <T> List<T> getAllElementOfType(List<Object> list, Class<T> expectedClassType) {

		List<T> res = new ArrayList<T>();

		for (Object cur : list) {

			T adapted = adapt(cur, expectedClassType);
			if (adapted != null) {
				res.add(adapted);
			}
		}
		return res;
	}

	/**
	 * Adapt the specified object to the requested type, if possible.
	 * Return null if the object can't be adapted.
	 *
	 * @param object
	 * @param expectedClassType
	 * @return The adapted object, or null.
	 */
	private <T> T adapt(Object object, Class<T> expectedClassType) {

		if (object instanceof IAdaptable) {
			@SuppressWarnings("unchecked")
			T ele = (T) ((IAdaptable) object).getAdapter(expectedClassType);
			if (ele != null) {
				return ele;
			}
			// Try as EObject if the expectedClasType is sub-type of EObject.
			if (EObject.class.isAssignableFrom(expectedClassType)) {
				// to EObject
				EObject eobject = (EObject) ((IAdaptable) object).getAdapter(EObject.class);

				if (eobject != null && expectedClassType.isInstance(eobject)) {
					return (T) eobject;
				}
			}
		}

		// Try global mechanism
		{
			@SuppressWarnings("unchecked")
			T ele = (T) Platform.getAdapterManager().getAdapter(object, expectedClassType);
			if (ele != null) {
				return ele;
			}
			// Try as EObject if the expectedClasType is sub-type of EObject.
			if (EObject.class.isAssignableFrom(expectedClassType)) {
				// to EObject
				EObject eobject = (EObject) Platform.getAdapterManager().getAdapter(object, EObject.class);

				if (eobject != null && expectedClassType.isInstance(eobject)) {

					return (T) eobject;
				}
			}
		}
		// Can't be adapted
		return null;

	}

	/**
	 *
	 * @param event
	 * @param errorMessage
	 * @throws ExecutionException
	 */
	private void showErrorDialog(ExecutionEvent event, String title, String errorMessage) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		MessageDialog.openError(
				window.getShell(),
				title,
				errorMessage);
	}


	/**
	 * Get the name used in the {@link RecordingCommand}. This name will be visible in
	 * undo/redo.
	 *
	 * @return The command name to show.
	 */
	public String getCommandName() {
		return "Create Association";
	}

	/**
	 * Do the execution of the command.
	 *
	 * @param objects
	 *            List of object to generate Java code from.
	 * @throws ServiceException
	 */
	protected void doExecute(final List<?> objects) throws ServiceException {

		// Execute the reverse with provided paramters
		TransactionalEditingDomain editingDomain = getEditingDomain();
		RecordingCommand command = new RecordingCommand(editingDomain, getCommandName()) {

			@Override
			protected void doExecute() {
				System.out.println(" Selections:");

				for (Object obj : objects) {

					System.out.println("found '" + obj + "'.");
					createAssociationFromProperty((Property) obj);
				}
			}

		};

		editingDomain.getCommandStack().execute(command);


	}


	/**
	 * Create an association from the specified Property. The association is created
	 * in the property's nearest package.
	 *
	 * @param p
	 *            The property to use to create an association.
	 */
	protected void createAssociationFromProperty(Property p) {

		Element owner = p.getOwner();

		if (owner instanceof Type) {

			Type classOwner = (Type) owner;

			// Compute other end name
			String name = classOwner.getName();
			String newname = name.substring(0, 1).toLowerCase() + name.substring(1);
			// Create
			TypeOperationsEnhanced.createAssociationFromProperty(p, true, AggregationKind.COMPOSITE_LITERAL,
					false, AggregationKind.NONE_LITERAL, newname, 0, 1);
		}
	}

	/**
	 * Lookup selected objects in UI.
	 *
	 * @return
	 */
	private List<Object> lookupSelectedElements() {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		ISelection selection = page.getSelection();
		System.out.println("check " + selection);
		if (selection instanceof IStructuredSelection)
		{
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			return structuredSelection.toList();
		}
		else if (selection instanceof TreeSelection) {
			TreeSelection treeSelection = (TreeSelection) selection;
			return treeSelection.toList();

		}
		return null;
	}

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

}

Back to the top