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