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