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