| 1 | /* |
| 2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.resource.part; |
| 5 | |
| 6 | import org.eclipse.core.resources.IFile; |
| 7 | import org.eclipse.core.resources.IMarker; |
| 8 | import org.eclipse.core.resources.IWorkspaceRoot; |
| 9 | import org.eclipse.core.resources.ResourcesPlugin; |
| 10 | import org.eclipse.core.runtime.CoreException; |
| 11 | import org.eclipse.core.runtime.IPath; |
| 12 | import org.eclipse.core.runtime.IProgressMonitor; |
| 13 | import org.eclipse.core.runtime.IStatus; |
| 14 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 15 | import org.eclipse.emf.common.ui.URIEditorInput; |
| 16 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 17 | import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
| 18 | import org.eclipse.gef.palette.PaletteRoot; |
| 19 | import org.eclipse.gmf.runtime.common.ui.services.marker.MarkerNavigationService; |
| 20 | import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; |
| 21 | import org.eclipse.gmf.runtime.diagram.ui.actions.ActionIds; |
| 22 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDiagramDocument; |
| 23 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocument; |
| 24 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.document.IDocumentProvider; |
| 25 | import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; |
| 26 | import org.eclipse.gmf.runtime.notation.Diagram; |
| 27 | import org.eclipse.jface.dialogs.ErrorDialog; |
| 28 | import org.eclipse.jface.dialogs.IMessageProvider; |
| 29 | import org.eclipse.jface.dialogs.MessageDialog; |
| 30 | import org.eclipse.jface.viewers.ISelection; |
| 31 | import org.eclipse.jface.viewers.StructuredSelection; |
| 32 | import org.eclipse.jface.window.Window; |
| 33 | import org.eclipse.osgi.util.NLS; |
| 34 | import org.eclipse.swt.widgets.Shell; |
| 35 | import org.eclipse.ui.IEditorInput; |
| 36 | import org.eclipse.ui.IEditorMatchingStrategy; |
| 37 | import org.eclipse.ui.IEditorReference; |
| 38 | import org.eclipse.ui.IFileEditorInput; |
| 39 | import org.eclipse.ui.PlatformUI; |
| 40 | import org.eclipse.ui.dialogs.SaveAsDialog; |
| 41 | import org.eclipse.ui.ide.IGotoMarker; |
| 42 | import org.eclipse.ui.navigator.resources.ProjectExplorer; |
| 43 | import org.eclipse.ui.part.FileEditorInput; |
| 44 | import org.eclipse.ui.part.IShowInTargetList; |
| 45 | import org.eclipse.ui.part.ShowInContext; |
| 46 | |
| 47 | import de.uka.ipd.sdq.pcm.gmf.resource.navigator.PalladioComponentModelNavigatorItem; |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | public class PalladioComponentModelDiagramEditor extends DiagramDocumentEditor |
| 53 | implements IGotoMarker { |
| 54 | |
| 55 | /** |
| 56 | * @generated |
| 57 | */ |
| 58 | public static final String ID = "de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelDiagramEditorID"; //$NON-NLS-1$ |
| 59 | |
| 60 | /** |
| 61 | * @generated |
| 62 | */ |
| 63 | public static final String CONTEXT_ID = "de.uka.ipd.sdq.pcm.gmf.resource.ui.diagramContext"; //$NON-NLS-1$ |
| 64 | |
| 65 | /** |
| 66 | * @generated |
| 67 | */ |
| 68 | public PalladioComponentModelDiagramEditor() { |
| 69 | super(true); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @generated |
| 74 | */ |
| 75 | protected String getContextID() { |
| 76 | return CONTEXT_ID; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @generated |
| 81 | */ |
| 82 | protected PaletteRoot createPaletteRoot(PaletteRoot existingPaletteRoot) { |
| 83 | PaletteRoot root = super.createPaletteRoot(existingPaletteRoot); |
| 84 | new PalladioComponentModelPaletteFactory().fillPalette(root); |
| 85 | return root; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @generated |
| 90 | */ |
| 91 | protected PreferencesHint getPreferencesHint() { |
| 92 | return PalladioComponentModelDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @generated |
| 97 | */ |
| 98 | public String getContributorId() { |
| 99 | return PalladioComponentModelDiagramEditorPlugin.ID; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @generated |
| 104 | */ |
| 105 | public Object getAdapter(Class type) { |
| 106 | if (type == IShowInTargetList.class) { |
| 107 | return new IShowInTargetList() { |
| 108 | public String[] getShowInTargetIds() { |
| 109 | return new String[] { ProjectExplorer.VIEW_ID }; |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | return super.getAdapter(type); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @generated |
| 118 | */ |
| 119 | protected IDocumentProvider getDocumentProvider(IEditorInput input) { |
| 120 | if (input instanceof IFileEditorInput |
| 121 | || input instanceof URIEditorInput) { |
| 122 | return PalladioComponentModelDiagramEditorPlugin.getInstance() |
| 123 | .getDocumentProvider(); |
| 124 | } |
| 125 | return super.getDocumentProvider(input); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @generated |
| 130 | */ |
| 131 | public TransactionalEditingDomain getEditingDomain() { |
| 132 | IDocument document = getEditorInput() != null ? getDocumentProvider() |
| 133 | .getDocument(getEditorInput()) : null; |
| 134 | if (document instanceof IDiagramDocument) { |
| 135 | return ((IDiagramDocument) document).getEditingDomain(); |
| 136 | } |
| 137 | return super.getEditingDomain(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @generated |
| 142 | */ |
| 143 | protected void setDocumentProvider(IEditorInput input) { |
| 144 | if (input instanceof IFileEditorInput |
| 145 | || input instanceof URIEditorInput) { |
| 146 | setDocumentProvider(PalladioComponentModelDiagramEditorPlugin |
| 147 | .getInstance().getDocumentProvider()); |
| 148 | } else { |
| 149 | super.setDocumentProvider(input); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @generated |
| 155 | */ |
| 156 | public void gotoMarker(IMarker marker) { |
| 157 | MarkerNavigationService.getInstance().gotoMarker(this, marker); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @generated |
| 162 | */ |
| 163 | public boolean isSaveAsAllowed() { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @generated |
| 169 | */ |
| 170 | public void doSaveAs() { |
| 171 | performSaveAs(new NullProgressMonitor()); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @generated |
| 176 | */ |
| 177 | protected void performSaveAs(IProgressMonitor progressMonitor) { |
| 178 | Shell shell = getSite().getShell(); |
| 179 | IEditorInput input = getEditorInput(); |
| 180 | SaveAsDialog dialog = new SaveAsDialog(shell); |
| 181 | IFile original = input instanceof IFileEditorInput ? ((IFileEditorInput) input) |
| 182 | .getFile() |
| 183 | : null; |
| 184 | if (original != null) { |
| 185 | dialog.setOriginalFile(original); |
| 186 | } |
| 187 | dialog.create(); |
| 188 | IDocumentProvider provider = getDocumentProvider(); |
| 189 | if (provider == null) { |
| 190 | // editor has been programmatically closed while the dialog was open |
| 191 | return; |
| 192 | } |
| 193 | if (provider.isDeleted(input) && original != null) { |
| 194 | String message = NLS |
| 195 | .bind( |
| 196 | Messages.PalladioComponentModelDiagramEditor_SavingDeletedFile, |
| 197 | original.getName()); |
| 198 | dialog.setErrorMessage(null); |
| 199 | dialog.setMessage(message, IMessageProvider.WARNING); |
| 200 | } |
| 201 | if (dialog.open() == Window.CANCEL) { |
| 202 | if (progressMonitor != null) { |
| 203 | progressMonitor.setCanceled(true); |
| 204 | } |
| 205 | return; |
| 206 | } |
| 207 | IPath filePath = dialog.getResult(); |
| 208 | if (filePath == null) { |
| 209 | if (progressMonitor != null) { |
| 210 | progressMonitor.setCanceled(true); |
| 211 | } |
| 212 | return; |
| 213 | } |
| 214 | IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
| 215 | IFile file = workspaceRoot.getFile(filePath); |
| 216 | final IEditorInput newInput = new FileEditorInput(file); |
| 217 | // Check if the editor is already open |
| 218 | IEditorMatchingStrategy matchingStrategy = getEditorDescriptor() |
| 219 | .getEditorMatchingStrategy(); |
| 220 | IEditorReference[] editorRefs = PlatformUI.getWorkbench() |
| 221 | .getActiveWorkbenchWindow().getActivePage() |
| 222 | .getEditorReferences(); |
| 223 | for (int i = 0; i < editorRefs.length; i++) { |
| 224 | if (matchingStrategy.matches(editorRefs[i], newInput)) { |
| 225 | MessageDialog |
| 226 | .openWarning( |
| 227 | shell, |
| 228 | Messages.PalladioComponentModelDiagramEditor_SaveAsErrorTitle, |
| 229 | Messages.PalladioComponentModelDiagramEditor_SaveAsErrorMessage); |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | boolean success = false; |
| 234 | try { |
| 235 | provider.aboutToChange(newInput); |
| 236 | getDocumentProvider(newInput).saveDocument(progressMonitor, |
| 237 | newInput, |
| 238 | getDocumentProvider().getDocument(getEditorInput()), true); |
| 239 | success = true; |
| 240 | } catch (CoreException x) { |
| 241 | IStatus status = x.getStatus(); |
| 242 | if (status == null || status.getSeverity() != IStatus.CANCEL) { |
| 243 | ErrorDialog |
| 244 | .openError( |
| 245 | shell, |
| 246 | Messages.PalladioComponentModelDiagramEditor_SaveErrorTitle, |
| 247 | Messages.PalladioComponentModelDiagramEditor_SaveErrorMessage, |
| 248 | x.getStatus()); |
| 249 | } |
| 250 | } finally { |
| 251 | provider.changed(newInput); |
| 252 | if (success) { |
| 253 | setInput(newInput); |
| 254 | } |
| 255 | } |
| 256 | if (progressMonitor != null) { |
| 257 | progressMonitor.setCanceled(!success); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @generated |
| 263 | */ |
| 264 | public ShowInContext getShowInContext() { |
| 265 | return new ShowInContext(getEditorInput(), getNavigatorSelection()); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @generated |
| 270 | */ |
| 271 | private ISelection getNavigatorSelection() { |
| 272 | IDiagramDocument document = getDiagramDocument(); |
| 273 | if (document == null) { |
| 274 | return StructuredSelection.EMPTY; |
| 275 | } |
| 276 | Diagram diagram = document.getDiagram(); |
| 277 | IFile file = WorkspaceSynchronizer.getFile(diagram.eResource()); |
| 278 | if (file != null) { |
| 279 | PalladioComponentModelNavigatorItem item = new PalladioComponentModelNavigatorItem( |
| 280 | diagram, file, false); |
| 281 | return new StructuredSelection(item); |
| 282 | } |
| 283 | return StructuredSelection.EMPTY; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @generated |
| 288 | */ |
| 289 | protected void configureGraphicalViewer() { |
| 290 | super.configureGraphicalViewer(); |
| 291 | DiagramEditorContextMenuProvider provider = new DiagramEditorContextMenuProvider( |
| 292 | this, getDiagramGraphicalViewer()); |
| 293 | getDiagramGraphicalViewer().setContextMenu(provider); |
| 294 | getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTEXT_MENU, |
| 295 | provider, getDiagramGraphicalViewer()); |
| 296 | } |
| 297 | |
| 298 | } |