1 | /* |
2 | * Copyright 2007, SDQ, IPD, U KA |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.allocation.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.XMIResource; |
31 | import org.eclipse.emf.ecore.xmi.XMLResource; |
32 | import org.eclipse.emf.edit.ui.util.EditUIUtil; |
33 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
34 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
35 | import org.eclipse.gef.EditPart; |
36 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
37 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
38 | import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; |
39 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
40 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart; |
41 | import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer; |
42 | import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart; |
43 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
44 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
45 | import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil; |
46 | import org.eclipse.gmf.runtime.notation.Diagram; |
47 | import org.eclipse.gmf.runtime.notation.View; |
48 | import org.eclipse.jface.dialogs.IDialogSettings; |
49 | import org.eclipse.jface.wizard.Wizard; |
50 | import org.eclipse.jface.wizard.WizardDialog; |
51 | import org.eclipse.swt.widgets.Shell; |
52 | import org.eclipse.ui.IWorkbenchPage; |
53 | import org.eclipse.ui.PartInitException; |
54 | |
55 | import org.eclipse.ui.PlatformUI; |
56 | import org.eclipse.ui.part.FileEditorInput; |
57 | import de.uka.ipd.sdq.pcm.allocation.Allocation; |
58 | import de.uka.ipd.sdq.pcm.allocation.AllocationFactory; |
59 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts.AllocationEditPart; |
60 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceEnvironment; |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | public class PalladioComponentModelDiagramEditorUtil { |
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 | PalladioComponentModelAllocationDiagramEditor.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 | PalladioComponentModelAllocationDiagramEditorPlugin |
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 = PalladioComponentModelAllocationDiagramEditorPlugin |
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( |
167 | org.eclipse.emf.common.util.URI diagramURI, |
168 | org.eclipse.emf.common.util.URI modelURI, |
169 | final de.uka.ipd.sdq.pcm.system.System mySystem, |
170 | final ResourceEnvironment myResourceEnv, |
171 | IProgressMonitor progressMonitor) { |
172 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
173 | .createEditingDomain(); |
174 | progressMonitor.beginTask("Creating diagram and model files", 3); |
175 | final Resource diagramResource = editingDomain.getResourceSet() |
176 | .createResource(diagramURI); |
177 | final Resource modelResource = editingDomain.getResourceSet() |
178 | .createResource(modelURI); |
179 | final String diagramName = diagramURI.lastSegment(); |
180 | AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
181 | editingDomain, |
182 | "Creating diagram and model", Collections.EMPTY_LIST) { //$NON-NLS-1$ |
183 | protected CommandResult doExecuteWithResult( |
184 | IProgressMonitor monitor, IAdaptable info) |
185 | throws ExecutionException { |
186 | Allocation model = createInitialModel(); |
187 | model.setSystem_Allocation(mySystem); |
188 | model.setTargetResourceEnvironment_Allocation(myResourceEnv); |
189 | attachModelToResource(model, modelResource); |
190 | |
191 | Diagram diagram = ViewService |
192 | .createDiagram( |
193 | model, |
194 | AllocationEditPart.MODEL_ID, |
195 | PalladioComponentModelAllocationDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
196 | if (diagram != null) { |
197 | diagramResource.getContents().add(diagram); |
198 | diagram.setName(diagramName); |
199 | diagram.setElement(model); |
200 | } |
201 | |
202 | try { |
203 | Map options = new HashMap(); |
204 | options.put(XMIResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$ |
205 | modelResource.save(options); |
206 | diagramResource.save(options); |
207 | } catch (IOException e) { |
208 | |
209 | PalladioComponentModelAllocationDiagramEditorPlugin |
210 | .getInstance() |
211 | .logError( |
212 | "Unable to store model and diagram resources", e); //$NON-NLS-1$ |
213 | } |
214 | return CommandResult.newOKCommandResult(); |
215 | } |
216 | }; |
217 | try { |
218 | OperationHistoryFactory.getOperationHistory().execute(command, |
219 | new SubProgressMonitor(progressMonitor, 1), null); |
220 | } catch (ExecutionException e) { |
221 | PalladioComponentModelAllocationDiagramEditorPlugin.getInstance() |
222 | .logError("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 Allocation createInitialModel() { |
236 | Allocation alloc = AllocationFactory.eINSTANCE.createAllocation(); |
237 | |
238 | //set default entity name |
239 | alloc.setEntityName("defaultAllocation"); //$NON-NLS-1$ |
240 | |
241 | return alloc; |
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(Allocation model, |
251 | 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 | } |