1 | /* |
2 | *Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.part; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collection; |
8 | import java.util.HashSet; |
9 | import java.util.Iterator; |
10 | import java.util.List; |
11 | |
12 | import org.eclipse.core.resources.IFile; |
13 | import org.eclipse.core.resources.IMarker; |
14 | import org.eclipse.core.resources.IWorkspaceRoot; |
15 | import org.eclipse.core.resources.ResourcesPlugin; |
16 | import org.eclipse.core.runtime.CoreException; |
17 | import org.eclipse.core.runtime.IAdaptable; |
18 | import org.eclipse.core.runtime.IPath; |
19 | import org.eclipse.core.runtime.IProgressMonitor; |
20 | import org.eclipse.core.runtime.IStatus; |
21 | import org.eclipse.core.runtime.NullProgressMonitor; |
22 | import org.eclipse.emf.common.ui.URIEditorInput; |
23 | import org.eclipse.emf.common.util.URI; |
24 | import org.eclipse.emf.ecore.EObject; |
25 | import org.eclipse.emf.ecore.resource.Resource; |
26 | import org.eclipse.emf.edit.ui.dnd.LocalTransfer; |
27 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
28 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
29 | import org.eclipse.gef.EditPartViewer; |
30 | import org.eclipse.gef.palette.PaletteRoot; |
31 | import org.eclipse.gmf.runtime.common.ui.services.marker.MarkerNavigationService; |
32 | import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; |
33 | import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds; |
34 | import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramDropTargetListener; |
35 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument; |
36 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument; |
37 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocumentProvider; |
38 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; |
39 | import org.eclipse.gmf.runtime.notation.Diagram; |
40 | import org.eclipse.gmf.runtime.notation.View; |
41 | import org.eclipse.jface.dialogs.ErrorDialog; |
42 | import org.eclipse.jface.dialogs.IMessageProvider; |
43 | import org.eclipse.jface.dialogs.MessageDialog; |
44 | import org.eclipse.jface.util.LocalSelectionTransfer; |
45 | import org.eclipse.jface.util.TransferDropTargetListener; |
46 | import org.eclipse.jface.viewers.ISelection; |
47 | import org.eclipse.jface.viewers.IStructuredSelection; |
48 | import org.eclipse.jface.viewers.StructuredSelection; |
49 | import org.eclipse.jface.window.Window; |
50 | import org.eclipse.osgi.util.NLS; |
51 | import org.eclipse.swt.dnd.DropTargetEvent; |
52 | import org.eclipse.swt.dnd.Transfer; |
53 | import org.eclipse.swt.dnd.TransferData; |
54 | import org.eclipse.swt.widgets.Shell; |
55 | import org.eclipse.ui.IEditorInput; |
56 | import org.eclipse.ui.IEditorMatchingStrategy; |
57 | import org.eclipse.ui.IEditorReference; |
58 | import org.eclipse.ui.IFileEditorInput; |
59 | import org.eclipse.ui.PlatformUI; |
60 | import org.eclipse.ui.dialogs.SaveAsDialog; |
61 | import org.eclipse.ui.ide.IGotoMarker; |
62 | import org.eclipse.ui.navigator.resources.ProjectExplorer; |
63 | import org.eclipse.ui.part.FileEditorInput; |
64 | import org.eclipse.ui.part.IShowInTargetList; |
65 | import org.eclipse.ui.part.ShowInContext; |
66 | |
67 | import de.uka.ipd.sdq.pcm.gmf.repository.navigator.PalladioComponentModelNavigatorItem; |
68 | |
69 | /** |
70 | * @generated |
71 | */ |
72 | public class PalladioComponentModelRepositoryDiagramEditor extends |
73 | DiagramDocumentEditor implements IGotoMarker { |
74 | |
75 | /** |
76 | * @generated |
77 | */ |
78 | public static final String ID = "de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelRepositoryDiagramEditorID"; //$NON-NLS-1$ |
79 | |
80 | /** |
81 | * @generated |
82 | */ |
83 | public static final String CONTEXT_ID = "de.uka.ipd.sdq.pcm.gmf.repository.ui.diagramContext"; //$NON-NLS-1$ |
84 | |
85 | /** |
86 | * @generated |
87 | */ |
88 | public PalladioComponentModelRepositoryDiagramEditor() { |
89 | super(true); |
90 | } |
91 | |
92 | /** |
93 | * @generated |
94 | */ |
95 | protected String getContextID() { |
96 | return CONTEXT_ID; |
97 | } |
98 | |
99 | /** |
100 | * @generated |
101 | */ |
102 | protected PaletteRoot createPaletteRoot(PaletteRoot existingPaletteRoot) { |
103 | PaletteRoot root = super.createPaletteRoot(existingPaletteRoot); |
104 | new PalladioComponentModelPaletteFactory().fillPalette(root); |
105 | return root; |
106 | } |
107 | |
108 | /** |
109 | * @generated |
110 | */ |
111 | protected PreferencesHint getPreferencesHint() { |
112 | return PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; |
113 | } |
114 | |
115 | /** |
116 | * @generated |
117 | */ |
118 | public String getContributorId() { |
119 | return PalladioComponentModelRepositoryDiagramEditorPlugin.ID; |
120 | } |
121 | |
122 | /** |
123 | * @generated |
124 | */ |
125 | public Object getAdapter(Class type) { |
126 | if (type == IShowInTargetList.class) { |
127 | return new IShowInTargetList() { |
128 | public String[] getShowInTargetIds() { |
129 | return new String[] { ProjectExplorer.VIEW_ID }; |
130 | } |
131 | }; |
132 | } |
133 | return super.getAdapter(type); |
134 | } |
135 | |
136 | /** |
137 | * @generated |
138 | */ |
139 | protected IDocumentProvider getDocumentProvider(IEditorInput input) { |
140 | if (input instanceof IFileEditorInput |
141 | || input instanceof URIEditorInput) { |
142 | return PalladioComponentModelRepositoryDiagramEditorPlugin |
143 | .getInstance().getDocumentProvider(); |
144 | } |
145 | return super.getDocumentProvider(input); |
146 | } |
147 | |
148 | /** |
149 | * @generated |
150 | */ |
151 | public TransactionalEditingDomain getEditingDomain() { |
152 | IDocument document = getEditorInput() != null ? getDocumentProvider() |
153 | .getDocument(getEditorInput()) : null; |
154 | if (document instanceof IDiagramDocument) { |
155 | return ((IDiagramDocument) document).getEditingDomain(); |
156 | } |
157 | return super.getEditingDomain(); |
158 | } |
159 | |
160 | /** |
161 | * @generated |
162 | */ |
163 | protected void setDocumentProvider(IEditorInput input) { |
164 | if (input instanceof IFileEditorInput |
165 | || input instanceof URIEditorInput) { |
166 | setDocumentProvider(PalladioComponentModelRepositoryDiagramEditorPlugin |
167 | .getInstance().getDocumentProvider()); |
168 | } else { |
169 | super.setDocumentProvider(input); |
170 | } |
171 | } |
172 | |
173 | /** |
174 | * @generated |
175 | */ |
176 | public void gotoMarker(IMarker marker) { |
177 | MarkerNavigationService.getInstance().gotoMarker(this, marker); |
178 | } |
179 | |
180 | /** |
181 | * @generated |
182 | */ |
183 | public boolean isSaveAsAllowed() { |
184 | return true; |
185 | } |
186 | |
187 | /** |
188 | * @generated |
189 | */ |
190 | public void doSaveAs() { |
191 | performSaveAs(new NullProgressMonitor()); |
192 | } |
193 | |
194 | /** |
195 | * @generated |
196 | */ |
197 | protected void performSaveAs(IProgressMonitor progressMonitor) { |
198 | Shell shell = getSite().getShell(); |
199 | IEditorInput input = getEditorInput(); |
200 | SaveAsDialog dialog = new SaveAsDialog(shell); |
201 | IFile original = input instanceof IFileEditorInput ? ((IFileEditorInput) input) |
202 | .getFile() |
203 | : null; |
204 | if (original != null) { |
205 | dialog.setOriginalFile(original); |
206 | } |
207 | dialog.create(); |
208 | IDocumentProvider provider = getDocumentProvider(); |
209 | if (provider == null) { |
210 | // editor has been programmatically closed while the dialog was open |
211 | return; |
212 | } |
213 | if (provider.isDeleted(input) && original != null) { |
214 | String message = NLS |
215 | .bind( |
216 | Messages.PalladioComponentModelRepositoryDiagramEditor_SavingDeletedFile, |
217 | original.getName()); |
218 | dialog.setErrorMessage(null); |
219 | dialog.setMessage(message, IMessageProvider.WARNING); |
220 | } |
221 | if (dialog.open() == Window.CANCEL) { |
222 | if (progressMonitor != null) { |
223 | progressMonitor.setCanceled(true); |
224 | } |
225 | return; |
226 | } |
227 | IPath filePath = dialog.getResult(); |
228 | if (filePath == null) { |
229 | if (progressMonitor != null) { |
230 | progressMonitor.setCanceled(true); |
231 | } |
232 | return; |
233 | } |
234 | IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
235 | IFile file = workspaceRoot.getFile(filePath); |
236 | final IEditorInput newInput = new FileEditorInput(file); |
237 | // Check if the editor is already open |
238 | IEditorMatchingStrategy matchingStrategy = getEditorDescriptor() |
239 | .getEditorMatchingStrategy(); |
240 | IEditorReference[] editorRefs = PlatformUI.getWorkbench() |
241 | .getActiveWorkbenchWindow().getActivePage() |
242 | .getEditorReferences(); |
243 | for (int i = 0; i < editorRefs.length; i++) { |
244 | if (matchingStrategy.matches(editorRefs[i], newInput)) { |
245 | MessageDialog |
246 | .openWarning( |
247 | shell, |
248 | Messages.PalladioComponentModelRepositoryDiagramEditor_SaveAsErrorTitle, |
249 | Messages.PalladioComponentModelRepositoryDiagramEditor_SaveAsErrorMessage); |
250 | return; |
251 | } |
252 | } |
253 | boolean success = false; |
254 | try { |
255 | provider.aboutToChange(newInput); |
256 | getDocumentProvider(newInput).saveDocument(progressMonitor, |
257 | newInput, |
258 | getDocumentProvider().getDocument(getEditorInput()), true); |
259 | success = true; |
260 | } catch (CoreException x) { |
261 | IStatus status = x.getStatus(); |
262 | if (status == null || status.getSeverity() != IStatus.CANCEL) { |
263 | ErrorDialog |
264 | .openError( |
265 | shell, |
266 | Messages.PalladioComponentModelRepositoryDiagramEditor_SaveErrorTitle, |
267 | Messages.PalladioComponentModelRepositoryDiagramEditor_SaveErrorMessage, |
268 | x.getStatus()); |
269 | } |
270 | } finally { |
271 | provider.changed(newInput); |
272 | if (success) { |
273 | setInput(newInput); |
274 | } |
275 | } |
276 | if (progressMonitor != null) { |
277 | progressMonitor.setCanceled(!success); |
278 | } |
279 | } |
280 | |
281 | /** |
282 | * @generated |
283 | */ |
284 | public ShowInContext getShowInContext() { |
285 | return new ShowInContext(getEditorInput(), getNavigatorSelection()); |
286 | } |
287 | |
288 | /** |
289 | * @generated |
290 | */ |
291 | private ISelection getNavigatorSelection() { |
292 | IDiagramDocument document = getDiagramDocument(); |
293 | if (document == null) { |
294 | return StructuredSelection.EMPTY; |
295 | } |
296 | Diagram diagram = document.getDiagram(); |
297 | IFile file = WorkspaceSynchronizer.getFile(diagram.eResource()); |
298 | if (file != null) { |
299 | PalladioComponentModelNavigatorItem item = new PalladioComponentModelNavigatorItem( |
300 | diagram, file, false); |
301 | return new StructuredSelection(item); |
302 | } |
303 | return StructuredSelection.EMPTY; |
304 | } |
305 | |
306 | /** |
307 | * @generated |
308 | */ |
309 | protected void configureGraphicalViewer() { |
310 | super.configureGraphicalViewer(); |
311 | DiagramEditorContextMenuProvider provider = new DiagramEditorContextMenuProvider( |
312 | this, getDiagramGraphicalViewer()); |
313 | getDiagramGraphicalViewer().setContextMenu(provider); |
314 | getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTEXT_MENU, |
315 | provider, getDiagramGraphicalViewer()); |
316 | } |
317 | |
318 | /* Manual code to enable drag and drop */ |
319 | @Override |
320 | protected void initializeGraphicalViewer() { |
321 | // TODO Auto-generated method stub |
322 | super.initializeGraphicalViewer(); |
323 | |
324 | getDiagramGraphicalViewer().addDropTargetListener( |
325 | (TransferDropTargetListener) new DiagramDropTargetListener( |
326 | getDiagramGraphicalViewer(), LocalSelectionTransfer |
327 | .getTransfer()) { |
328 | protected List getObjectsBeingDropped() { |
329 | TransferData[] data = getCurrentEvent().dataTypes; |
330 | List eObjects = new ArrayList(); |
331 | |
332 | for (int i = 0; i < data.length; i++) { |
333 | if (LocalSelectionTransfer.getTransfer() |
334 | .isSupportedType(data[i])) { |
335 | IStructuredSelection selection = (IStructuredSelection) LocalSelectionTransfer |
336 | .getTransfer().nativeToJava(data[i]); |
337 | eObjects.addAll(selection.toList()); |
338 | } |
339 | } |
340 | return adaptAllObjects(eObjects); |
341 | } |
342 | |
343 | private List adaptAllObjects(List objects) { |
344 | ArrayList result = new ArrayList(); |
345 | for (Object o : objects) { |
346 | Object resultingAdaptedObject = null; |
347 | if (o instanceof EObject) { |
348 | resultingAdaptedObject = o; |
349 | } else if (o instanceof IAdaptable) { |
350 | IAdaptable adaptable = (IAdaptable) o; |
351 | resultingAdaptedObject = adaptable |
352 | .getAdapter(EObject.class); |
353 | } |
354 | if (!result.contains(resultingAdaptedObject)) { |
355 | result.add(resultingAdaptedObject); |
356 | } |
357 | } |
358 | return result; |
359 | } |
360 | |
361 | public boolean isEnabled(DropTargetEvent event) { |
362 | if (super.isEnabled(event)) { |
363 | Object modelObj = getViewer().getContents() |
364 | .getModel(); |
365 | if (modelObj instanceof EObject) { |
366 | List eObjects = getDropObjectsRequest() |
367 | .getObjects(); |
368 | |
369 | if (eObjects == null) |
370 | return false; |
371 | |
372 | return true; |
373 | } |
374 | } |
375 | |
376 | return false; |
377 | } |
378 | }); |
379 | } |
380 | |
381 | /** |
382 | * @generated |
383 | */ |
384 | private abstract class DropTargetListener extends DiagramDropTargetListener { |
385 | |
386 | /** |
387 | * @generated |
388 | */ |
389 | public DropTargetListener(EditPartViewer viewer, Transfer xfer) { |
390 | super(viewer, xfer); |
391 | } |
392 | |
393 | /** |
394 | * @generated |
395 | */ |
396 | protected List getObjectsBeingDropped() { |
397 | TransferData data = getCurrentEvent().currentDataType; |
398 | Collection uris = new HashSet(); |
399 | |
400 | Object transferedObject = getJavaObject(data); |
401 | if (transferedObject instanceof IStructuredSelection) { |
402 | IStructuredSelection selection = (IStructuredSelection) transferedObject; |
403 | for (Iterator it = selection.iterator(); it.hasNext();) { |
404 | Object nextSelectedObject = it.next(); |
405 | if (nextSelectedObject instanceof PalladioComponentModelNavigatorItem) { |
406 | View view = ((PalladioComponentModelNavigatorItem) nextSelectedObject) |
407 | .getView(); |
408 | nextSelectedObject = view.getElement(); |
409 | } else if (nextSelectedObject instanceof IAdaptable) { |
410 | IAdaptable adaptable = (IAdaptable) nextSelectedObject; |
411 | nextSelectedObject = adaptable |
412 | .getAdapter(EObject.class); |
413 | } |
414 | |
415 | if (nextSelectedObject instanceof EObject) { |
416 | EObject modelElement = (EObject) nextSelectedObject; |
417 | Resource modelElementResource = modelElement |
418 | .eResource(); |
419 | uris.add(modelElementResource.getURI().appendFragment( |
420 | modelElementResource |
421 | .getURIFragment(modelElement))); |
422 | } |
423 | } |
424 | } |
425 | |
426 | List result = new ArrayList(); |
427 | for (Iterator it = uris.iterator(); it.hasNext();) { |
428 | URI nextURI = (URI) it.next(); |
429 | EObject modelObject = getEditingDomain().getResourceSet() |
430 | .getEObject(nextURI, true); |
431 | result.add(modelObject); |
432 | } |
433 | return result; |
434 | } |
435 | |
436 | /** |
437 | * @generated |
438 | */ |
439 | protected abstract Object getJavaObject(TransferData data); |
440 | |
441 | } |
442 | |
443 | } |