1 | /* |
2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.edit.policies; |
5 | |
6 | import java.io.IOException; |
7 | import java.lang.reflect.InvocationTargetException; |
8 | import java.util.Iterator; |
9 | |
10 | import org.eclipse.core.commands.ExecutionException; |
11 | import org.eclipse.core.runtime.CoreException; |
12 | import org.eclipse.core.runtime.IAdaptable; |
13 | import org.eclipse.core.runtime.IProgressMonitor; |
14 | import org.eclipse.emf.common.ui.URIEditorInput; |
15 | import org.eclipse.emf.common.util.URI; |
16 | import org.eclipse.emf.ecore.EObject; |
17 | import org.eclipse.emf.ecore.resource.Resource; |
18 | import org.eclipse.emf.ecore.util.EcoreUtil; |
19 | import org.eclipse.emf.transaction.util.TransactionUtil; |
20 | import org.eclipse.gef.EditPart; |
21 | import org.eclipse.gef.Request; |
22 | import org.eclipse.gef.commands.Command; |
23 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
24 | import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; |
25 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
26 | import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
27 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy; |
28 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
29 | import org.eclipse.gmf.runtime.notation.Diagram; |
30 | import org.eclipse.gmf.runtime.notation.HintedDiagramLinkStyle; |
31 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
32 | import org.eclipse.gmf.runtime.notation.Style; |
33 | import org.eclipse.gmf.runtime.notation.View; |
34 | import org.eclipse.ui.IEditorInput; |
35 | import org.eclipse.ui.IWorkbenchPage; |
36 | import org.eclipse.ui.PlatformUI; |
37 | import org.eclipse.ui.actions.WorkspaceModifyOperation; |
38 | |
39 | import de.uka.ipd.sdq.pcm.gmf.repository.part.Messages; |
40 | import de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelDiagramEditorUtil; |
41 | import de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelRepositoryDiagramEditorPlugin; |
42 | import de.uka.ipd.sdq.pcm.repository.BasicComponent; |
43 | import de.uka.ipd.sdq.pcm.repository.CompositeComponent; |
44 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
45 | |
46 | /** |
47 | * @generated |
48 | */ |
49 | public class OpenCompositeDiagramEditPolicy extends OpenEditPolicy { |
50 | |
51 | /** |
52 | * @generated |
53 | */ |
54 | protected Command getOpenCommand(Request request) { |
55 | EditPart targetEditPart = getTargetEditPart(request); |
56 | if (false == targetEditPart.getModel() instanceof View) { |
57 | return null; |
58 | } |
59 | View view = (View) targetEditPart.getModel(); |
60 | Style link = view.getStyle(NotationPackage.eINSTANCE |
61 | .getHintedDiagramLinkStyle()); |
62 | if (false == link instanceof HintedDiagramLinkStyle) { |
63 | return null; |
64 | } |
65 | return new ICommandProxy(new OpenDiagramCommand( |
66 | (HintedDiagramLinkStyle) link)); |
67 | } |
68 | |
69 | /** |
70 | * @generated |
71 | */ |
72 | private static class OpenDiagramCommand extends |
73 | AbstractTransactionalCommand { |
74 | |
75 | /** |
76 | * @generated |
77 | */ |
78 | private final HintedDiagramLinkStyle diagramFacet; |
79 | |
80 | /** |
81 | * @generated |
82 | */ |
83 | OpenDiagramCommand(HintedDiagramLinkStyle linkStyle) { |
84 | // editing domain is taken for original diagram, |
85 | // if we open diagram from another file, we should use another editing domain |
86 | super(TransactionUtil.getEditingDomain(linkStyle), |
87 | Messages.CommandName_OpenDiagram, null); |
88 | diagramFacet = linkStyle; |
89 | } |
90 | |
91 | // FIXME canExecute if !(readOnly && getDiagramToOpen == null), i.e. open works on ro diagrams only when there's associated diagram already |
92 | |
93 | /** |
94 | * @generated not |
95 | */ |
96 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
97 | IAdaptable info) throws ExecutionException { |
98 | |
99 | // try { |
100 | // Diagram diagram = getDiagramToOpen(); |
101 | // if (diagram == null) { |
102 | // diagram = intializeNewDiagram(); |
103 | // } |
104 | // URI uri = EcoreUtil.getURI(diagram); |
105 | // String editorName = uri.lastSegment() |
106 | // + "#" |
107 | // + ((CompositeComponent) diagram.getElement().eContainer()).getEntityName(); |
108 | // IEditorInput editorInput = new URIEditorInput(uri, editorName); |
109 | // IWorkbenchPage page = PlatformUI.getWorkbench() |
110 | // .getActiveWorkbenchWindow().getActivePage(); |
111 | // page.openEditor(editorInput, getEditorID()); |
112 | // return CommandResult.newOKCommandResult(); |
113 | // } catch (Exception ex) { |
114 | // throw new ExecutionException("Can't open diagram", ex); |
115 | // } |
116 | |
117 | try { |
118 | Diagram diagram = getDiagramToOpen(); |
119 | if (diagram == null) { |
120 | diagram = intializeNewDiagram(); |
121 | } |
122 | URI uri = EcoreUtil.getURI(diagram); |
123 | String editorName = uri.lastSegment() |
124 | + "#" + ((CompositeComponent) diagram.getElement()).getEntityName(); //$NON-NLS-1$ |
125 | IEditorInput editorInput = new URIEditorInput(uri, editorName); |
126 | IWorkbenchPage page = PlatformUI.getWorkbench() |
127 | .getActiveWorkbenchWindow().getActivePage(); |
128 | page.openEditor(editorInput, getEditorID()); |
129 | return CommandResult.newOKCommandResult(); |
130 | } catch (Exception ex) { |
131 | throw new ExecutionException("Can't open diagram", ex); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * @generated |
137 | */ |
138 | protected Diagram getDiagramToOpen() { |
139 | return diagramFacet.getDiagramLink(); |
140 | } |
141 | |
142 | /** |
143 | * @generated |
144 | */ |
145 | protected Diagram intializeNewDiagram() throws ExecutionException { |
146 | Diagram d = ViewService.createDiagram(getDiagramDomainElement(), |
147 | getDiagramKind(), getPreferencesHint()); |
148 | if (d == null) { |
149 | throw new ExecutionException("Can't create diagram of '" |
150 | + getDiagramKind() + "' kind"); |
151 | } |
152 | diagramFacet.setDiagramLink(d); |
153 | assert diagramFacet.eResource() != null; |
154 | diagramFacet.eResource().getContents().add(d); |
155 | EObject container = diagramFacet.eContainer(); |
156 | while (container instanceof View) { |
157 | ((View) container).persist(); |
158 | container = container.eContainer(); |
159 | } |
160 | try { |
161 | new WorkspaceModifyOperation() { |
162 | protected void execute(IProgressMonitor monitor) |
163 | throws CoreException, InvocationTargetException, |
164 | InterruptedException { |
165 | try { |
166 | for (Iterator it = diagramFacet.eResource() |
167 | .getResourceSet().getResources().iterator(); it |
168 | .hasNext();) { |
169 | Resource nextResource = (Resource) it.next(); |
170 | if (nextResource.isLoaded() |
171 | && !getEditingDomain().isReadOnly( |
172 | nextResource)) { |
173 | nextResource |
174 | .save(PalladioComponentModelDiagramEditorUtil |
175 | .getSaveOptions()); |
176 | } |
177 | } |
178 | } catch (IOException ex) { |
179 | throw new InvocationTargetException(ex, |
180 | "Save operation failed"); |
181 | } |
182 | } |
183 | }.run(null); |
184 | } catch (InvocationTargetException e) { |
185 | throw new ExecutionException("Can't create diagram of '" |
186 | + getDiagramKind() + "' kind", e); |
187 | } catch (InterruptedException e) { |
188 | throw new ExecutionException("Can't create diagram of '" |
189 | + getDiagramKind() + "' kind", e); |
190 | } |
191 | return d; |
192 | } |
193 | |
194 | /** |
195 | * @generated |
196 | */ |
197 | protected EObject getDiagramDomainElement() { |
198 | // use same element as associated with EP |
199 | return ((View) diagramFacet.eContainer()).getElement(); |
200 | } |
201 | |
202 | /** |
203 | * @generated |
204 | */ |
205 | protected PreferencesHint getPreferencesHint() { |
206 | // XXX prefhint from target diagram's editor? |
207 | return PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; |
208 | } |
209 | |
210 | /** |
211 | * @generated |
212 | */ |
213 | protected String getDiagramKind() { |
214 | return "CompositeModel"; |
215 | } |
216 | |
217 | /** |
218 | * @generated |
219 | */ |
220 | protected String getEditorID() { |
221 | return "de.uka.ipd.sdq.pcm.gmf.composite.part.PalladioComponentModelDiagramEditorID"; |
222 | } |
223 | } |
224 | |
225 | } |