1 | /* |
2 | * Copyright 2007, SDQ, IPD, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.usage.part; |
5 | |
6 | import java.io.IOException; |
7 | import java.util.ArrayList; |
8 | import java.util.Collections; |
9 | import java.util.HashMap; |
10 | import java.util.Iterator; |
11 | import java.util.List; |
12 | import java.util.Map; |
13 | import java.util.Set; |
14 | |
15 | import org.eclipse.core.commands.ExecutionException; |
16 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
17 | import org.eclipse.core.resources.IFile; |
18 | import org.eclipse.core.resources.IResource; |
19 | import org.eclipse.core.resources.ResourcesPlugin; |
20 | import org.eclipse.core.runtime.CoreException; |
21 | import org.eclipse.core.runtime.IAdaptable; |
22 | import org.eclipse.core.runtime.IPath; |
23 | import org.eclipse.core.runtime.IProgressMonitor; |
24 | import org.eclipse.core.runtime.NullProgressMonitor; |
25 | import org.eclipse.core.runtime.Path; |
26 | import org.eclipse.core.runtime.SubProgressMonitor; |
27 | import org.eclipse.emf.common.util.URI; |
28 | import org.eclipse.emf.ecore.EObject; |
29 | import org.eclipse.emf.ecore.resource.Resource; |
30 | import org.eclipse.emf.ecore.xmi.XMLResource; |
31 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
32 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
33 | import org.eclipse.gef.EditPart; |
34 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
35 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
36 | import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; |
37 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
38 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart; |
39 | import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer; |
40 | import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart; |
41 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
42 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
43 | import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil; |
44 | import org.eclipse.gmf.runtime.notation.Diagram; |
45 | import org.eclipse.gmf.runtime.notation.View; |
46 | import org.eclipse.jface.dialogs.IDialogSettings; |
47 | import org.eclipse.jface.wizard.Wizard; |
48 | import org.eclipse.jface.wizard.WizardDialog; |
49 | import org.eclipse.swt.widgets.Shell; |
50 | import org.eclipse.ui.IWorkbenchPage; |
51 | import org.eclipse.ui.PartInitException; |
52 | import org.eclipse.ui.PlatformUI; |
53 | import org.eclipse.ui.part.FileEditorInput; |
54 | |
55 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
56 | import de.uka.ipd.sdq.pcm.usagemodel.ScenarioBehaviour; |
57 | import de.uka.ipd.sdq.pcm.usagemodel.UsageModel; |
58 | import de.uka.ipd.sdq.pcm.usagemodel.UsageScenario; |
59 | import de.uka.ipd.sdq.pcm.usagemodel.UsagemodelFactory; |
60 | |
61 | /** |
62 | * @generated |
63 | */ |
64 | public class PalladioComponentModelDiagramEditorUtil { |
65 | |
66 | /** |
67 | * @generated |
68 | */ |
69 | public static Map getSaveOptions() { |
70 | Map saveOptions = new HashMap(); |
71 | saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$ |
72 | saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, |
73 | Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER); |
74 | return saveOptions; |
75 | } |
76 | |
77 | /** |
78 | * @generated |
79 | */ |
80 | public static boolean openDiagram(Resource diagram) |
81 | throws PartInitException { |
82 | String path = diagram.getURI().toPlatformString(true); |
83 | IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot() |
84 | .findMember(new Path(path)); |
85 | if (workspaceResource instanceof IFile) { |
86 | IWorkbenchPage page = PlatformUI.getWorkbench() |
87 | .getActiveWorkbenchWindow().getActivePage(); |
88 | return null != page.openEditor(new FileEditorInput( |
89 | (IFile) workspaceResource), |
90 | PalladioComponentModelUsageDiagramEditor.ID); |
91 | } |
92 | return false; |
93 | } |
94 | |
95 | /** |
96 | * @generated |
97 | */ |
98 | public static void setCharset(IFile file) { |
99 | if (file == null) { |
100 | return; |
101 | } |
102 | try { |
103 | file.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$ |
104 | } catch (CoreException e) { |
105 | PalladioComponentModelUsageDiagramEditorPlugin |
106 | .getInstance() |
107 | .logError( |
108 | "Unable to set charset for file " + file.getFullPath(), e); //$NON-NLS-1$ |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * @generated |
114 | */ |
115 | public static String getUniqueFileName(IPath containerFullPath, |
116 | String fileName, String extension) { |
117 | if (containerFullPath == null) { |
118 | containerFullPath = new Path(""); //$NON-NLS-1$ |
119 | } |
120 | if (fileName == null || fileName.trim().length() == 0) { |
121 | fileName = "default"; //$NON-NLS-1$ |
122 | } |
123 | IPath filePath = containerFullPath.append(fileName); |
124 | if (extension != null && !extension.equals(filePath.getFileExtension())) { |
125 | filePath = filePath.addFileExtension(extension); |
126 | } |
127 | extension = filePath.getFileExtension(); |
128 | fileName = filePath.removeFileExtension().lastSegment(); |
129 | int i = 1; |
130 | while (ResourcesPlugin.getWorkspace().getRoot().exists(filePath)) { |
131 | i++; |
132 | filePath = containerFullPath.append(fileName + i); |
133 | if (extension != null) { |
134 | filePath = filePath.addFileExtension(extension); |
135 | } |
136 | } |
137 | return filePath.lastSegment(); |
138 | } |
139 | |
140 | /** |
141 | * Runs the wizard in a dialog. |
142 | * |
143 | * @generated |
144 | */ |
145 | public static void runWizard(Shell shell, Wizard wizard, String settingsKey) { |
146 | IDialogSettings pluginDialogSettings = PalladioComponentModelUsageDiagramEditorPlugin |
147 | .getInstance().getDialogSettings(); |
148 | IDialogSettings wizardDialogSettings = pluginDialogSettings |
149 | .getSection(settingsKey); |
150 | if (wizardDialogSettings == null) { |
151 | wizardDialogSettings = pluginDialogSettings |
152 | .addNewSection(settingsKey); |
153 | } |
154 | wizard.setDialogSettings(wizardDialogSettings); |
155 | WizardDialog dialog = new WizardDialog(shell, wizard); |
156 | dialog.create(); |
157 | dialog.getShell().setSize(Math.max(500, dialog.getShell().getSize().x), |
158 | 500); |
159 | dialog.open(); |
160 | } |
161 | |
162 | /** |
163 | * This method should be called within a workspace modify operation since it creates resources. |
164 | * @generated not |
165 | */ |
166 | public static Resource createDiagram(URI diagramURI, URI modelURI, |
167 | IProgressMonitor progressMonitor) { |
168 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
169 | .createEditingDomain(); |
170 | progressMonitor |
171 | .beginTask( |
172 | Messages.PalladioComponentModelDiagramEditorUtil_CreateDiagramProgressTask, |
173 | 3); |
174 | final Resource diagramResource = editingDomain.getResourceSet() |
175 | .createResource(diagramURI); |
176 | final Resource modelResource = editingDomain.getResourceSet() |
177 | .createResource(modelURI); |
178 | final String diagramName = diagramURI.lastSegment(); |
179 | AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
180 | editingDomain, |
181 | Messages.PalladioComponentModelDiagramEditorUtil_CreateDiagramCommandLabel, |
182 | Collections.EMPTY_LIST) { |
183 | protected CommandResult doExecuteWithResult( |
184 | IProgressMonitor monitor, IAdaptable info) |
185 | throws ExecutionException { |
186 | UsageModel model = createInitialModel(); |
187 | UsageScenario scenario = UsagemodelFactory.eINSTANCE |
188 | .createUsageScenario(); |
189 | |
190 | // set default scenario name |
191 | scenario.setEntityName("defaultUsageScenario"); //$NON-NLS-1$ |
192 | |
193 | ScenarioBehaviour behaviour = UsagemodelFactory.eINSTANCE |
194 | .createScenarioBehaviour(); |
195 | behaviour.getActions_ScenarioBehaviour().add( |
196 | UsagemodelFactory.eINSTANCE.createStart()); |
197 | behaviour.getActions_ScenarioBehaviour().add( |
198 | UsagemodelFactory.eINSTANCE.createStop()); |
199 | |
200 | // set default scenario behaviour name |
201 | behaviour.setEntityName("defaultUsageScenarioBehaviour"); //$NON-NLS-1$ |
202 | |
203 | scenario.setScenarioBehaviour_UsageScenario(behaviour); |
204 | model.getUsageScenario_UsageModel().add(scenario); |
205 | |
206 | attachModelToResource(model, modelResource); |
207 | |
208 | Diagram diagram = ViewService |
209 | .createDiagram( |
210 | model, |
211 | UsageScenarioEditPart.MODEL_ID, |
212 | PalladioComponentModelUsageDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
213 | if (diagram != null) { |
214 | diagramResource.getContents().add(diagram); |
215 | diagram.setName(diagramName); |
216 | diagram.setElement(model); |
217 | } |
218 | |
219 | try { |
220 | modelResource |
221 | .save(de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelDiagramEditorUtil |
222 | .getSaveOptions()); |
223 | diagramResource |
224 | .save(de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelDiagramEditorUtil |
225 | .getSaveOptions()); |
226 | } catch (IOException e) { |
227 | |
228 | PalladioComponentModelUsageDiagramEditorPlugin |
229 | .getInstance() |
230 | .logError( |
231 | "Unable to store model and diagram resources", e); //$NON-NLS-1$ |
232 | } |
233 | return CommandResult.newOKCommandResult(); |
234 | } |
235 | }; |
236 | try { |
237 | OperationHistoryFactory.getOperationHistory().execute(command, |
238 | new SubProgressMonitor(progressMonitor, 1), null); |
239 | } catch (ExecutionException e) { |
240 | PalladioComponentModelUsageDiagramEditorPlugin.getInstance() |
241 | .logError("Unable to create model and diagram", e); //$NON-NLS-1$ |
242 | } |
243 | setCharset(WorkspaceSynchronizer.getFile(modelResource)); |
244 | setCharset(WorkspaceSynchronizer.getFile(diagramResource)); |
245 | return diagramResource; |
246 | } |
247 | |
248 | /** |
249 | * Create a new instance of domain element associated with canvas. |
250 | * <!-- begin-user-doc --> |
251 | * <!-- end-user-doc --> |
252 | * @generated |
253 | */ |
254 | private static UsageModel createInitialModel() { |
255 | return UsagemodelFactory.eINSTANCE.createUsageModel(); |
256 | } |
257 | |
258 | /** |
259 | * Store model element in the resource. |
260 | * <!-- begin-user-doc --> |
261 | * <!-- end-user-doc --> |
262 | * @generated |
263 | */ |
264 | private static void attachModelToResource(UsageModel model, |
265 | Resource resource) { |
266 | resource.getContents().add(model); |
267 | } |
268 | |
269 | /** |
270 | * @generated |
271 | */ |
272 | public static void selectElementsInDiagram( |
273 | IDiagramWorkbenchPart diagramPart, List/*EditPart*/editParts) { |
274 | diagramPart.getDiagramGraphicalViewer().deselectAll(); |
275 | |
276 | EditPart firstPrimary = null; |
277 | for (Iterator it = editParts.iterator(); it.hasNext();) { |
278 | EditPart nextPart = (EditPart) it.next(); |
279 | diagramPart.getDiagramGraphicalViewer().appendSelection(nextPart); |
280 | if (firstPrimary == null && nextPart instanceof IPrimaryEditPart) { |
281 | firstPrimary = nextPart; |
282 | } |
283 | } |
284 | |
285 | if (!editParts.isEmpty()) { |
286 | diagramPart.getDiagramGraphicalViewer().reveal( |
287 | firstPrimary != null ? firstPrimary : (EditPart) editParts |
288 | .get(0)); |
289 | } |
290 | } |
291 | |
292 | /** |
293 | * @generated |
294 | */ |
295 | private static int findElementsInDiagramByID(DiagramEditPart diagramPart, |
296 | EObject element, List editPartCollector) { |
297 | IDiagramGraphicalViewer viewer = (IDiagramGraphicalViewer) diagramPart |
298 | .getViewer(); |
299 | final int intialNumOfEditParts = editPartCollector.size(); |
300 | |
301 | if (element instanceof View) { // support notation element lookup |
302 | EditPart editPart = (EditPart) viewer.getEditPartRegistry().get( |
303 | element); |
304 | if (editPart != null) { |
305 | editPartCollector.add(editPart); |
306 | return 1; |
307 | } |
308 | } |
309 | |
310 | String elementID = EMFCoreUtil.getProxyID(element); |
311 | List associatedParts = viewer.findEditPartsForElement(elementID, |
312 | IGraphicalEditPart.class); |
313 | // perform the possible hierarchy disjoint -> take the top-most parts only |
314 | for (Iterator editPartIt = associatedParts.iterator(); editPartIt |
315 | .hasNext();) { |
316 | EditPart nextPart = (EditPart) editPartIt.next(); |
317 | EditPart parentPart = nextPart.getParent(); |
318 | while (parentPart != null && !associatedParts.contains(parentPart)) { |
319 | parentPart = parentPart.getParent(); |
320 | } |
321 | if (parentPart == null) { |
322 | editPartCollector.add(nextPart); |
323 | } |
324 | } |
325 | |
326 | if (intialNumOfEditParts == editPartCollector.size()) { |
327 | if (!associatedParts.isEmpty()) { |
328 | editPartCollector.add(associatedParts.iterator().next()); |
329 | } else { |
330 | if (element.eContainer() != null) { |
331 | return findElementsInDiagramByID(diagramPart, element |
332 | .eContainer(), editPartCollector); |
333 | } |
334 | } |
335 | } |
336 | return editPartCollector.size() - intialNumOfEditParts; |
337 | } |
338 | |
339 | /** |
340 | * @generated |
341 | */ |
342 | public static View findView(DiagramEditPart diagramEditPart, |
343 | EObject targetElement, LazyElement2ViewMap lazyElement2ViewMap) { |
344 | boolean hasStructuralURI = false; |
345 | if (targetElement.eResource() instanceof XMLResource) { |
346 | hasStructuralURI = ((XMLResource) targetElement.eResource()) |
347 | .getID(targetElement) == null; |
348 | } |
349 | |
350 | View view = null; |
351 | if (hasStructuralURI |
352 | && !lazyElement2ViewMap.getElement2ViewMap().isEmpty()) { |
353 | view = (View) lazyElement2ViewMap.getElement2ViewMap().get( |
354 | targetElement); |
355 | } else if (findElementsInDiagramByID(diagramEditPart, targetElement, |
356 | lazyElement2ViewMap.editPartTmpHolder) > 0) { |
357 | EditPart editPart = (EditPart) lazyElement2ViewMap.editPartTmpHolder |
358 | .get(0); |
359 | lazyElement2ViewMap.editPartTmpHolder.clear(); |
360 | view = editPart.getModel() instanceof View ? (View) editPart |
361 | .getModel() : null; |
362 | } |
363 | |
364 | return (view == null) ? diagramEditPart.getDiagramView() : view; |
365 | } |
366 | |
367 | /** |
368 | * @generated |
369 | */ |
370 | public static class LazyElement2ViewMap { |
371 | /** |
372 | * @generated |
373 | */ |
374 | private Map element2ViewMap; |
375 | |
376 | /** |
377 | * @generated |
378 | */ |
379 | private View scope; |
380 | |
381 | /** |
382 | * @generated |
383 | */ |
384 | private Set elementSet; |
385 | |
386 | /** |
387 | * @generated |
388 | */ |
389 | public final List editPartTmpHolder = new ArrayList(); |
390 | |
391 | /** |
392 | * @generated |
393 | */ |
394 | public LazyElement2ViewMap(View scope, Set elements) { |
395 | this.scope = scope; |
396 | this.elementSet = elements; |
397 | } |
398 | |
399 | /** |
400 | * @generated |
401 | */ |
402 | public final Map getElement2ViewMap() { |
403 | if (element2ViewMap == null) { |
404 | element2ViewMap = new HashMap(); |
405 | // map possible notation elements to itself as these can't be found by view.getElement() |
406 | for (Iterator it = elementSet.iterator(); it.hasNext();) { |
407 | EObject element = (EObject) it.next(); |
408 | if (element instanceof View) { |
409 | View view = (View) element; |
410 | if (view.getDiagram() == scope.getDiagram()) { |
411 | element2ViewMap.put(element, element); // take only those that part of our diagram |
412 | } |
413 | } |
414 | } |
415 | |
416 | buildElement2ViewMap(scope, element2ViewMap, elementSet); |
417 | } |
418 | return element2ViewMap; |
419 | } |
420 | |
421 | /** |
422 | * @generated |
423 | */ |
424 | static Map buildElement2ViewMap(View parentView, Map element2ViewMap, |
425 | Set elements) { |
426 | if (elements.size() == element2ViewMap.size()) |
427 | return element2ViewMap; |
428 | |
429 | if (parentView.isSetElement() |
430 | && !element2ViewMap.containsKey(parentView.getElement()) |
431 | && elements.contains(parentView.getElement())) { |
432 | element2ViewMap.put(parentView.getElement(), parentView); |
433 | if (elements.size() == element2ViewMap.size()) |
434 | return element2ViewMap; |
435 | } |
436 | |
437 | for (Iterator it = parentView.getChildren().iterator(); it |
438 | .hasNext();) { |
439 | buildElement2ViewMap((View) it.next(), element2ViewMap, |
440 | elements); |
441 | if (elements.size() == element2ViewMap.size()) |
442 | return element2ViewMap; |
443 | } |
444 | for (Iterator it = parentView.getSourceEdges().iterator(); it |
445 | .hasNext();) { |
446 | buildElement2ViewMap((View) it.next(), element2ViewMap, |
447 | elements); |
448 | if (elements.size() == element2ViewMap.size()) |
449 | return element2ViewMap; |
450 | } |
451 | for (Iterator it = parentView.getSourceEdges().iterator(); it |
452 | .hasNext();) { |
453 | buildElement2ViewMap((View) it.next(), element2ViewMap, |
454 | elements); |
455 | if (elements.size() == element2ViewMap.size()) |
456 | return element2ViewMap; |
457 | } |
458 | return element2ViewMap; |
459 | } |
460 | } //LazyElement2ViewMap |
461 | |
462 | } |