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