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