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