Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3711c1a54e526725c3a617e777aeb3e057fe0089 (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
/**
 * 
 */
package org.eclipse.papyrus.diagram.umltools.clazz;

import java.util.Collections;

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.commands.operations.OperationHistoryFactory;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.core.utils.BusinessModelResolver;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.sasheditor.contentprovider.di.IPageMngr;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.diagram.clazz.part.UMLDiagramEditorPlugin;
import org.eclipse.uml2.uml.UMLFactory;


/**
 * A Handler responsible to create the EObject representing a UmlTools class diagram.
 * The handler also add the created EObject to the SashContainerProvider. In reaction, the SashEditor 
 * will instanciate the corresponding Editor.
 * 
 * @author cedric dumoulin
 *
 */
public class CreateClassDiagramHandler extends AbstractHandler implements IHandler {

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

		// Choose one of the two execution mode.
		executeAsTransaction(event);
//		doExecute(event);
		// Return null, as required by the API
		return null;
	}

	/**
	 * Execute the handler in a undoable transaction.
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 *
	 * @param event
	 * @return
	 * @throws ExecutionException
	 */
	
	public void executeAsTransaction(final ExecutionEvent event) throws ExecutionException {

		TransactionalEditingDomain editingDomain = EditorUtils.getTransactionalEditingDomain();

		// Create a transactional command.
		AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain,"Create Class Diagram", Collections.EMPTY_LIST) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
					throws ExecutionException {

				CommandResult commandResult;
				try {
					// Call the real implementation
					CreateClassDiagramHandler.this.doExecute(event);
					commandResult = CommandResult.newOKCommandResult();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					commandResult = CommandResult.newErrorCommandResult("Error during diagram creation");
				}
				return commandResult;
			}
		};
		
		// Execute the command
		try {
			OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
		} catch (ExecutionException e) {
			e.printStackTrace();
			// TODO log the error
		}
	}

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

		// First, create the EObject used to represent the Editor.
		// This is generally the root object used to instantiate the corresponding editor.
		EObject editorRootObject = createEditorRootObject();
		// Then add the root object to the SashContainerProvider
		IPageMngr pageMngr = EditorUtils.getIPageMngr();
		pageMngr.openPage(editorRootObject);
	}

	/**
	 * Create the root object used to find the type of the editor to create, and provided to the new instance.
	 * 
	 * @return
	 */
	private EObject createEditorRootObject() {
		
		String diagramName = "newDiagram";
		EObject associatedDomainRoot = getDomainRoot();
		Resource diagramResource = getDiagramResource();
		
		Diagram diagram = ViewService.createDiagram(associatedDomainRoot, getDiagramNotationID(), getPreferenceHint());
		if(diagram != null) {
			diagram.setName(diagramName);
			// TODO : required ??
			diagram.setElement(associatedDomainRoot);
			diagramResource.getContents().add(diagram);
//			initializeModel(owner);
//			initializeDiagram(diagram);
		}
		
		
		return diagram;
	}

	/**
	 * Get the domain element that will be associated to the Diagram.
	 * chack if the selected element can be used as domain element.
	 * If nothing is found, create a domain element.
	 * @return The domain element that will be associated to the diagram.
	 * 
	 */
	private EObject getDomainRoot() {
		
		EObject domainElement = getSelectedDomainElement();
		if( domainElement != null)
			return domainElement;
		
		// Create one
		return createDomainElement();
	}

	/**
	 * Create the domain element that will be associated to the Diagram.
	 * @return
	 */
	protected EObject createDomainElement() {
		return UMLFactory.eINSTANCE.createModel();

	}

	/**
	 * Get the domain element associated to the current selection.
	 * @return The domain EObject associated to the current Eclipse selection, or null.
	 */
	private EObject getSelectedDomainElement() {
		EObject eObject = null;
		Object selection = getCurrentSelection();
		if(selection != null) {
			Object businessObject = getDomainModel(selection);
			if(businessObject instanceof EObject) {
				eObject = (EObject)businessObject;
			}
		}
		return eObject;
	}

	/**
	 * Get current selection first element.
	 * 
	 * @return the selected element or null.
	 */
	private Object getCurrentSelection() {
		ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			return structuredSelection.getFirstElement();
		}

		return null;
	}

	/**
	 * Get the domain object associated to the provided object, if any. 
	 * The provided object is a graphical artifact (EditPart, View, ...)
	 * This method navigate throw the object if the object is a graphical artifact or a
	 * diagram artifact.
	 * 
	 * @param object Graphical artifact associated to a domain object.
	 * @return Object The domain object associated to the graphical artifact.
	 */
	private Object getDomainModel(Object object) {
		if(object instanceof EditPart) {
			// Check model. It can be a GraphNode.
			Object model = ((EditPart)object).getModel();
			// Check if it is a notation View
			if(model instanceof View) { 
				return ((View)model).getElement();

			} else
				return model;
			
		} 
		else if(object instanceof View) {
			return ((View)object).getElement();
		}
		else
			return object;

	}


	/**
	 * Get the resource containing notation.Diagram.
	 * @return
	 */
	private Resource getDiagramResource() {
		return EditorUtils.getDiResourceSet().getNotationResource();
	}

	/**
	 * Get the ID identifying the type of the Diagram.
	 * {@inheritDoc}
	 */
	protected String getDiagramNotationID() {
		return org.eclipse.uml2.diagram.clazz.edit.parts.PackageEditPart.MODEL_ID;
	}

	/**
	 * {@inheritDoc}
	 */
	protected PreferencesHint getPreferenceHint() {
		return UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT;
	}
	
}

Back to the top