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.seff.ResourceDemandingSEFF; |
44 | |
45 | /** |
46 | * @generated |
47 | */ |
48 | public class OpenSeffDiagramEditPolicy extends OpenEditPolicy { |
49 | |
50 | /** |
51 | * @generated |
52 | */ |
53 | protected Command getOpenCommand(Request request) { |
54 | EditPart targetEditPart = getTargetEditPart(request); |
55 | if (false == targetEditPart.getModel() instanceof View) { |
56 | return null; |
57 | } |
58 | View view = (View) targetEditPart.getModel(); |
59 | Style link = view.getStyle(NotationPackage.eINSTANCE |
60 | .getHintedDiagramLinkStyle()); |
61 | if (false == link instanceof HintedDiagramLinkStyle) { |
62 | return null; |
63 | } |
64 | return new ICommandProxy(new OpenDiagramCommand( |
65 | (HintedDiagramLinkStyle) link)); |
66 | } |
67 | |
68 | /** |
69 | * @generated |
70 | */ |
71 | private static class OpenDiagramCommand extends |
72 | AbstractTransactionalCommand { |
73 | |
74 | /** |
75 | * @generated |
76 | */ |
77 | private final HintedDiagramLinkStyle diagramFacet; |
78 | |
79 | /** |
80 | * @generated |
81 | */ |
82 | OpenDiagramCommand(HintedDiagramLinkStyle linkStyle) { |
83 | // editing domain is taken for original diagram, |
84 | // if we open diagram from another file, we should use another editing domain |
85 | super(TransactionUtil.getEditingDomain(linkStyle), |
86 | Messages.CommandName_OpenDiagram, null); |
87 | diagramFacet = linkStyle; |
88 | } |
89 | |
90 | // FIXME canExecute if !(readOnly && getDiagramToOpen == null), i.e. open works on ro diagrams only when there's associated diagram already |
91 | |
92 | /** |
93 | * @generated not |
94 | */ |
95 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, |
96 | IAdaptable info) throws ExecutionException { |
97 | try { |
98 | Diagram diagram = getDiagramToOpen(); |
99 | if (diagram == null) { |
100 | diagram = intializeNewDiagram(); |
101 | } |
102 | URI uri = EcoreUtil.getURI(diagram); |
103 | String editorName = uri.lastSegment() |
104 | + "#" |
105 | + ((BasicComponent) ((ResourceDemandingSEFF) diagram |
106 | .getElement()).eContainer()).getEntityName() |
107 | + "." |
108 | + ((ResourceDemandingSEFF) diagram.getElement()) |
109 | .getDescribedService__SEFF().getEntityName(); //$NON-NLS-1$ |
110 | IEditorInput editorInput = new URIEditorInput(uri, editorName); |
111 | IWorkbenchPage page = PlatformUI.getWorkbench() |
112 | .getActiveWorkbenchWindow().getActivePage(); |
113 | page.openEditor(editorInput, getEditorID()); |
114 | return CommandResult.newOKCommandResult(); |
115 | } catch (Exception ex) { |
116 | throw new ExecutionException("Can't open diagram", ex); |
117 | } |
118 | } |
119 | |
120 | /** |
121 | * @generated |
122 | */ |
123 | protected Diagram getDiagramToOpen() { |
124 | return diagramFacet.getDiagramLink(); |
125 | } |
126 | |
127 | /** |
128 | * @generated |
129 | */ |
130 | protected Diagram intializeNewDiagram() throws ExecutionException { |
131 | Diagram d = ViewService.createDiagram(getDiagramDomainElement(), |
132 | getDiagramKind(), getPreferencesHint()); |
133 | if (d == null) { |
134 | throw new ExecutionException("Can't create diagram of '" |
135 | + getDiagramKind() + "' kind"); |
136 | } |
137 | diagramFacet.setDiagramLink(d); |
138 | assert diagramFacet.eResource() != null; |
139 | diagramFacet.eResource().getContents().add(d); |
140 | EObject container = diagramFacet.eContainer(); |
141 | while (container instanceof View) { |
142 | ((View) container).persist(); |
143 | container = container.eContainer(); |
144 | } |
145 | try { |
146 | new WorkspaceModifyOperation() { |
147 | protected void execute(IProgressMonitor monitor) |
148 | throws CoreException, InvocationTargetException, |
149 | InterruptedException { |
150 | try { |
151 | for (Iterator it = diagramFacet.eResource() |
152 | .getResourceSet().getResources().iterator(); it |
153 | .hasNext();) { |
154 | Resource nextResource = (Resource) it.next(); |
155 | if (nextResource.isLoaded() |
156 | && !getEditingDomain().isReadOnly( |
157 | nextResource)) { |
158 | nextResource |
159 | .save(PalladioComponentModelDiagramEditorUtil |
160 | .getSaveOptions()); |
161 | } |
162 | } |
163 | } catch (IOException ex) { |
164 | throw new InvocationTargetException(ex, |
165 | "Save operation failed"); |
166 | } |
167 | } |
168 | }.run(null); |
169 | } catch (InvocationTargetException e) { |
170 | throw new ExecutionException("Can't create diagram of '" |
171 | + getDiagramKind() + "' kind", e); |
172 | } catch (InterruptedException e) { |
173 | throw new ExecutionException("Can't create diagram of '" |
174 | + getDiagramKind() + "' kind", e); |
175 | } |
176 | return d; |
177 | } |
178 | |
179 | /** |
180 | * @generated |
181 | */ |
182 | protected EObject getDiagramDomainElement() { |
183 | // use same element as associated with EP |
184 | return ((View) diagramFacet.eContainer()).getElement(); |
185 | } |
186 | |
187 | /** |
188 | * @generated |
189 | */ |
190 | protected PreferencesHint getPreferencesHint() { |
191 | // XXX prefhint from target diagram's editor? |
192 | return PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; |
193 | } |
194 | |
195 | /** |
196 | * @generated |
197 | */ |
198 | protected String getDiagramKind() { |
199 | return "PCM SEFF Model"; |
200 | } |
201 | |
202 | /** |
203 | * @generated |
204 | */ |
205 | protected String getEditorID() { |
206 | return "de.uka.ipd.sdq.pcm.gmf.seff.part.SeffDiagramEditorID"; |
207 | } |
208 | } |
209 | |
210 | } |