1 | /* |
2 | *Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.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.seff.navigator.PalladioComponentModelNavigatorItem; |
48 | |
49 | /** |
50 | * @generated |
51 | */ |
52 | public class SeffDiagramEditor extends DiagramDocumentEditor implements |
53 | IGotoMarker { |
54 | |
55 | /** |
56 | * @generated |
57 | */ |
58 | public static final String ID = "de.uka.ipd.sdq.pcm.gmf.seff.part.SeffDiagramEditorID"; //$NON-NLS-1$ |
59 | |
60 | /** |
61 | * @generated |
62 | */ |
63 | public static final String CONTEXT_ID = "de.uka.ipd.sdq.pcm.gmf.seff.ui.diagramContext"; //$NON-NLS-1$ |
64 | |
65 | /** |
66 | * @generated |
67 | */ |
68 | public SeffDiagramEditor() { |
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 PalladioComponentModelSeffDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; |
93 | } |
94 | |
95 | /** |
96 | * @generated |
97 | */ |
98 | public String getContributorId() { |
99 | return PalladioComponentModelSeffDiagramEditorPlugin.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 PalladioComponentModelSeffDiagramEditorPlugin.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(PalladioComponentModelSeffDiagramEditorPlugin |
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.bind( |
195 | Messages.SeffDiagramEditor_SavingDeletedFile, original |
196 | .getName()); |
197 | dialog.setErrorMessage(null); |
198 | dialog.setMessage(message, IMessageProvider.WARNING); |
199 | } |
200 | if (dialog.open() == Window.CANCEL) { |
201 | if (progressMonitor != null) { |
202 | progressMonitor.setCanceled(true); |
203 | } |
204 | return; |
205 | } |
206 | IPath filePath = dialog.getResult(); |
207 | if (filePath == null) { |
208 | if (progressMonitor != null) { |
209 | progressMonitor.setCanceled(true); |
210 | } |
211 | return; |
212 | } |
213 | IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
214 | IFile file = workspaceRoot.getFile(filePath); |
215 | final IEditorInput newInput = new FileEditorInput(file); |
216 | // Check if the editor is already open |
217 | IEditorMatchingStrategy matchingStrategy = getEditorDescriptor() |
218 | .getEditorMatchingStrategy(); |
219 | IEditorReference[] editorRefs = PlatformUI.getWorkbench() |
220 | .getActiveWorkbenchWindow().getActivePage() |
221 | .getEditorReferences(); |
222 | for (int i = 0; i < editorRefs.length; i++) { |
223 | if (matchingStrategy.matches(editorRefs[i], newInput)) { |
224 | MessageDialog.openWarning(shell, |
225 | Messages.SeffDiagramEditor_SaveAsErrorTitle, |
226 | Messages.SeffDiagramEditor_SaveAsErrorMessage); |
227 | return; |
228 | } |
229 | } |
230 | boolean success = false; |
231 | try { |
232 | provider.aboutToChange(newInput); |
233 | getDocumentProvider(newInput).saveDocument(progressMonitor, |
234 | newInput, |
235 | getDocumentProvider().getDocument(getEditorInput()), true); |
236 | success = true; |
237 | } catch (CoreException x) { |
238 | IStatus status = x.getStatus(); |
239 | if (status == null || status.getSeverity() != IStatus.CANCEL) { |
240 | ErrorDialog.openError(shell, |
241 | Messages.SeffDiagramEditor_SaveErrorTitle, |
242 | Messages.SeffDiagramEditor_SaveErrorMessage, x |
243 | .getStatus()); |
244 | } |
245 | } finally { |
246 | provider.changed(newInput); |
247 | if (success) { |
248 | setInput(newInput); |
249 | } |
250 | } |
251 | if (progressMonitor != null) { |
252 | progressMonitor.setCanceled(!success); |
253 | } |
254 | } |
255 | |
256 | /** |
257 | * @generated |
258 | */ |
259 | public ShowInContext getShowInContext() { |
260 | return new ShowInContext(getEditorInput(), getNavigatorSelection()); |
261 | } |
262 | |
263 | /** |
264 | * @generated |
265 | */ |
266 | private ISelection getNavigatorSelection() { |
267 | IDiagramDocument document = getDiagramDocument(); |
268 | if (document == null) { |
269 | return StructuredSelection.EMPTY; |
270 | } |
271 | Diagram diagram = document.getDiagram(); |
272 | IFile file = WorkspaceSynchronizer.getFile(diagram.eResource()); |
273 | if (file != null) { |
274 | PalladioComponentModelNavigatorItem item = new PalladioComponentModelNavigatorItem( |
275 | diagram, file, false); |
276 | return new StructuredSelection(item); |
277 | } |
278 | return StructuredSelection.EMPTY; |
279 | } |
280 | |
281 | /** |
282 | * @generated |
283 | */ |
284 | protected void configureGraphicalViewer() { |
285 | super.configureGraphicalViewer(); |
286 | DiagramEditorContextMenuProvider provider = new DiagramEditorContextMenuProvider( |
287 | this, getDiagramGraphicalViewer()); |
288 | getDiagramGraphicalViewer().setContextMenu(provider); |
289 | getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTEXT_MENU, |
290 | provider, getDiagramGraphicalViewer()); |
291 | } |
292 | |
293 | } |