| ... |
... |
@@ -1,0 +1,362 @@ |
|
1 |
+/* |
|
2 |
+ * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient |
|
3 |
+ * |
|
4 |
+ * http://www.informatik.uni-kiel.de/rtsys/kieler/ |
|
5 |
+ * |
|
6 |
+ * Copyright 2010 by |
|
7 |
+ * + Christian-Albrechts-University of Kiel |
|
8 |
+ * + Department of Computer Science |
|
9 |
+ * + Real-Time and Embedded Systems Group |
|
10 |
+ * |
|
11 |
+ * This code is provided under the terms of the Eclipse Public License (EPL). |
|
12 |
+ * See the file epl-v10.html for the license text. |
|
13 |
+ */ |
|
14 |
+package de.cau.cs.rtprak.turing.graphiti; |
|
15 |
+ |
|
16 |
+import java.io.IOException; |
|
17 |
+import java.lang.reflect.InvocationTargetException; |
|
18 |
+import java.util.HashMap; |
|
19 |
+import java.util.Map; |
|
20 |
+ |
|
21 |
+import org.eclipse.core.resources.IFile; |
|
22 |
+import org.eclipse.core.resources.IResource; |
|
23 |
+import org.eclipse.core.resources.ResourcesPlugin; |
|
24 |
+import org.eclipse.core.runtime.CoreException; |
|
25 |
+import org.eclipse.core.runtime.IProgressMonitor; |
|
26 |
+import org.eclipse.core.runtime.IStatus; |
|
27 |
+import org.eclipse.core.runtime.NullProgressMonitor; |
|
28 |
+import org.eclipse.core.runtime.Path; |
|
29 |
+import org.eclipse.core.runtime.Status; |
|
30 |
+import org.eclipse.emf.common.command.CommandStack; |
|
31 |
+import org.eclipse.emf.common.util.URI; |
|
32 |
+import org.eclipse.emf.ecore.EObject; |
|
33 |
+import org.eclipse.emf.ecore.resource.Resource; |
|
34 |
+import org.eclipse.emf.ecore.resource.ResourceSet; |
|
35 |
+import org.eclipse.emf.ecore.xmi.XMLResource; |
|
36 |
+import org.eclipse.emf.transaction.RecordingCommand; |
|
37 |
+import org.eclipse.emf.transaction.TransactionalEditingDomain; |
|
38 |
+import org.eclipse.emf.workspace.util.WorkspaceSynchronizer; |
|
39 |
+import org.eclipse.graphiti.mm.pictograms.Diagram; |
|
40 |
+import org.eclipse.graphiti.mm.pictograms.PictogramLink; |
|
41 |
+import org.eclipse.graphiti.mm.pictograms.PictogramsFactory; |
|
42 |
+import org.eclipse.graphiti.services.Graphiti; |
|
43 |
+import org.eclipse.graphiti.ui.services.GraphitiUi; |
|
44 |
+import org.eclipse.jface.operation.IRunnableWithProgress; |
|
45 |
+import org.eclipse.jface.viewers.IStructuredSelection; |
|
46 |
+import org.eclipse.jface.wizard.Wizard; |
|
47 |
+import org.eclipse.ui.INewWizard; |
|
48 |
+import org.eclipse.ui.IWorkbench; |
|
49 |
+import org.eclipse.ui.IWorkbenchPage; |
|
50 |
+import org.eclipse.ui.PartInitException; |
|
51 |
+import org.eclipse.ui.actions.WorkspaceModifyOperation; |
|
52 |
+import org.eclipse.ui.part.FileEditorInput; |
|
53 |
+import org.eclipse.ui.statushandlers.StatusManager; |
|
54 |
+ |
|
55 |
+/** |
|
56 |
+ * A generic wizard for creation of new Graphiti diagrams. |
|
57 |
+ * |
|
58 |
+ * @author msp |
|
59 |
+ */ |
|
60 |
+public abstract class GraphitiNewWizard extends Wizard implements INewWizard { |
|
61 |
+ |
|
62 |
+ /** the workbench. */ |
|
63 |
+ private IWorkbench workbench; |
|
64 |
+ /** the current selection. */ |
|
65 |
+ private IStructuredSelection selection; |
|
66 |
+ /** the diagram model wizard page. */ |
|
67 |
+ private CreationWizardPage diagramModelFilePage; |
|
68 |
+ /** the domain model wizard page. */ |
|
69 |
+ private CreationWizardPage domainModelFilePage; |
|
70 |
+ /** the name of the wizard. */ |
|
71 |
+ private String wizardName; |
|
72 |
+ /** the diagram file extension. */ |
|
73 |
+ private String diagramFileExtension; |
|
74 |
+ /** the domain model file extension. */ |
|
75 |
+ private String domainFileExtension; |
|
76 |
+ /** the diagram type name of the graphiti diagram. */ |
|
77 |
+ private String diagramTypeName; |
|
78 |
+ /** the identifier of the diagram editor. */ |
|
79 |
+ private String editorId; |
|
80 |
+ /** the grid size (0 means no grid). */ |
|
81 |
+ private int gridSize = 0; |
|
82 |
+ /** the setting for snapping to grid. */ |
|
83 |
+ private boolean snapToGrid = false; |
|
84 |
+ |
|
85 |
+ /** |
|
86 |
+ * Creates a new-wizard for Graphiti, with no grid. |
|
87 |
+ * |
|
88 |
+ * @param name |
|
89 |
+ * the name of the wizard |
|
90 |
+ * @param diagExt |
|
91 |
+ * the diagram file extension |
|
92 |
+ * @param domainExt |
|
93 |
+ * the domain model file extension |
|
94 |
+ * @param thediagramTypeName |
|
95 |
+ * the diagram type name of the graphiti diagram |
|
96 |
+ */ |
|
97 |
+ public GraphitiNewWizard(final String name, final String diagExt, final String domainExt, |
|
98 |
+ final String thediagramTypeName) { |
|
99 |
+ this.wizardName = name; |
|
100 |
+ this.diagramFileExtension = diagExt; |
|
101 |
+ this.domainFileExtension = domainExt; |
|
102 |
+ this.diagramTypeName = thediagramTypeName; |
|
103 |
+ } |
|
104 |
+ |
|
105 |
+ /** |
|
106 |
+ * Creates a new-wizard for Graphiti, with no grid. |
|
107 |
+ * |
|
108 |
+ * @param name |
|
109 |
+ * the name of the wizard |
|
110 |
+ * @param diagExt |
|
111 |
+ * the diagram file extension |
|
112 |
+ * @param domainExt |
|
113 |
+ * the domain model file extension |
|
114 |
+ * @param thediagramTypeName |
|
115 |
+ * the diagram type of the graphiti diagram |
|
116 |
+ * @param theeditorId |
|
117 |
+ * identifier of the diagram editor, or {@code null} if no editor shall be opened |
|
118 |
+ * after model creation |
|
119 |
+ */ |
|
120 |
+ public GraphitiNewWizard(final String name, final String diagExt, final String domainExt, |
|
121 |
+ final String thediagramTypeName, final String theeditorId) { |
|
122 |
+ this(name, diagExt, domainExt, thediagramTypeName); |
|
123 |
+ this.editorId = theeditorId; |
|
124 |
+ } |
|
125 |
+ |
|
126 |
+ /** |
|
127 |
+ * Creates a new-wizard for Graphiti, with customizable grid. |
|
128 |
+ * |
|
129 |
+ * @param name |
|
130 |
+ * the name of the wizard |
|
131 |
+ * @param diagExt |
|
132 |
+ * the diagram file extension |
|
133 |
+ * @param domainExt |
|
134 |
+ * the domain model file extension |
|
135 |
+ * @param thediagramTypeName |
|
136 |
+ * the diagram type name of the graphiti diagram |
|
137 |
+ * @param theeditorId |
|
138 |
+ * identifier of the diagram editor, or {@code null} if no editor shall be opened |
|
139 |
+ * after model creation |
|
140 |
+ * @param thegridSize |
|
141 |
+ * the grid size (0 means no grid) |
|
142 |
+ * @param thesnapToGrid |
|
143 |
+ * the setting for snapping to grid |
|
144 |
+ */ |
|
145 |
+ public GraphitiNewWizard(final String name, final String diagExt, final String domainExt, |
|
146 |
+ final String thediagramTypeName, final String theeditorId, final int thegridSize, |
|
147 |
+ final boolean thesnapToGrid) { |
|
148 |
+ this(name, diagExt, domainExt, thediagramTypeName, theeditorId); |
|
149 |
+ this.gridSize = thegridSize; |
|
150 |
+ this.snapToGrid = thesnapToGrid; |
|
151 |
+ } |
|
152 |
+ |
|
153 |
+ /** |
|
154 |
+ * {@inheritDoc} |
|
155 |
+ */ |
|
156 |
+ public final void init(final IWorkbench theworkbench, final IStructuredSelection theselection) { |
|
157 |
+ this.workbench = theworkbench; |
|
158 |
+ this.selection = theselection; |
|
159 |
+ setWindowTitle("New " + wizardName + " Diagram"); |
|
160 |
+ setNeedsProgressMonitor(true); |
|
161 |
+ } |
|
162 |
+ |
|
163 |
+ /** |
|
164 |
+ * {@inheritDoc} |
|
165 |
+ */ |
|
166 |
+ @Override |
|
167 |
+ public final void addPages() { |
|
168 |
+ diagramModelFilePage = new CreationWizardPage("DiagramModelFile", selection, |
|
169 |
+ diagramFileExtension); |
|
170 |
+ diagramModelFilePage.setTitle("Create " + wizardName + " Diagram"); |
|
171 |
+ diagramModelFilePage.setDescription("Select file that will contain diagram model."); |
|
172 |
+ addPage(diagramModelFilePage); |
|
173 |
+ |
|
174 |
+ domainModelFilePage = new CreationWizardPage("DomainModelFile", selection, |
|
175 |
+ domainFileExtension); |
|
176 |
+ domainModelFilePage.setTitle("Create " + wizardName + " Domain Model"); |
|
177 |
+ domainModelFilePage.setDescription("Select file that will contain domain model."); |
|
178 |
+ addPage(domainModelFilePage); |
|
179 |
+ } |
|
180 |
+ |
|
181 |
+ /** |
|
182 |
+ * {@inheritDoc} |
|
183 |
+ */ |
|
184 |
+ @Override |
|
185 |
+ public final boolean performFinish() { |
|
186 |
+ IRunnableWithProgress op = new WorkspaceModifyOperation(null) { |
|
187 |
+ @Override |
|
188 |
+ protected void execute(final IProgressMonitor monitor) throws CoreException, |
|
189 |
+ InterruptedException { |
|
190 |
+ Resource diagramResource = createDiagram(diagramModelFilePage.getURI(), |
|
191 |
+ domainModelFilePage.getURI(), monitor); |
|
192 |
+ if (diagramResource != null && editorId != null) { |
|
193 |
+ try { |
|
194 |
+ openDiagram(diagramResource); |
|
195 |
+ } catch (PartInitException exception) { |
|
196 |
+ throw new CoreException(new Status(IStatus.ERROR, |
|
197 |
+ "de.cau.cs.rtprak.turing.graphiti", |
|
198 |
+ "Error opening diagram", exception)); |
|
199 |
+ } |
|
200 |
+ } |
|
201 |
+ } |
|
202 |
+ }; |
|
203 |
+ try { |
|
204 |
+ getContainer().run(false, true, op); |
|
205 |
+ } catch (InterruptedException exception) { |
|
206 |
+ return false; |
|
207 |
+ } catch (InvocationTargetException exception) { |
|
208 |
+ if (exception.getTargetException() instanceof CoreException) { |
|
209 |
+ StatusManager.getManager().handle(((CoreException) exception.getTargetException()), |
|
210 |
+ "de.cau.cs.rtprak.turing.graphiti"); |
|
211 |
+ } else { |
|
212 |
+ IStatus status = new Status(IStatus.ERROR, "de.cau.cs.rtprak.turing.graphiti", |
|
213 |
+ "Error creating diagram", exception.getTargetException()); |
|
214 |
+ StatusManager.getManager().handle(status, StatusManager.LOG); |
|
215 |
+ } |
|
216 |
+ return false; |
|
217 |
+ } |
|
218 |
+ return true; |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ /** |
|
222 |
+ * Create a diagram with given URIs. |
|
223 |
+ * |
|
224 |
+ * @param diagramURI |
|
225 |
+ * URI for the diagram file |
|
226 |
+ * @param modelURI |
|
227 |
+ * URI for the model file |
|
228 |
+ * @param progressMonitor |
|
229 |
+ * progress monitor |
|
230 |
+ * @return a resource for the new diagram file |
|
231 |
+ */ |
|
232 |
+ private Resource createDiagram(final URI diagramURI, final URI modelURI, |
|
233 |
+ final IProgressMonitor progressMonitor) { |
|
234 |
+ progressMonitor.beginTask("Creating diagram and model files", 2); |
|
235 |
+ // create a resource set and editing domain |
|
236 |
+ TransactionalEditingDomain editingDomain = GraphitiUi.getEmfService() |
|
237 |
+ .createResourceSetAndEditingDomain(); |
|
238 |
+ ResourceSet resourceSet = editingDomain.getResourceSet(); |
|
239 |
+ CommandStack commandStack = editingDomain.getCommandStack(); |
|
240 |
+ // create resources for the diagram and domain model files |
|
241 |
+ final Resource diagramResource = resourceSet.createResource(diagramURI); |
|
242 |
+ final Resource modelResource = resourceSet.createResource(modelURI); |
|
243 |
+ if (diagramResource != null && modelResource != null) { |
|
244 |
+ commandStack.execute(new RecordingCommand(editingDomain) { |
|
245 |
+ @Override |
|
246 |
+ protected void doExecute() { |
|
247 |
+ createModel(diagramResource, diagramURI.lastSegment(), modelResource, |
|
248 |
+ modelURI.lastSegment()); |
|
249 |
+ } |
|
250 |
+ }); |
|
251 |
+ progressMonitor.worked(1); |
|
252 |
+ |
|
253 |
+ try { |
|
254 |
+ modelResource.save(createSaveOptions()); |
|
255 |
+ diagramResource.save(createSaveOptions()); |
|
256 |
+ } catch (IOException exception) { |
|
257 |
+ IStatus status = new Status(IStatus.ERROR, "de.cau.cs.rtprak.turing.graphiti", |
|
258 |
+ "Unable to store model and diagram resources", exception); |
|
259 |
+ StatusManager.getManager().handle(status); |
|
260 |
+ } |
|
261 |
+ setCharset(WorkspaceSynchronizer.getFile(modelResource)); |
|
262 |
+ setCharset(WorkspaceSynchronizer.getFile(diagramResource)); |
|
263 |
+ } |
|
264 |
+ progressMonitor.done(); |
|
265 |
+ return diagramResource; |
|
266 |
+ } |
|
267 |
+ |
|
268 |
+ /** |
|
269 |
+ * Creates save options for the resources. |
|
270 |
+ * |
|
271 |
+ * @return new save options |
|
272 |
+ */ |
|
273 |
+ public static Map<?, ?> createSaveOptions() { |
|
274 |
+ HashMap<String, Object> saveOptions = new HashMap<String, Object>(); |
|
275 |
+ saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$ |
|
276 |
+ saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, |
|
277 |
+ Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER); |
|
278 |
+ return saveOptions; |
|
279 |
+ } |
|
280 |
+ |
|
281 |
+ /** |
|
282 |
+ * Set the character set for the given file to UTF-8. |
|
283 |
+ * |
|
284 |
+ * @param file |
|
285 |
+ * a file |
|
286 |
+ */ |
|
287 |
+ public static void setCharset(final IFile file) { |
|
288 |
+ try { |
|
289 |
+ if (file != null) { |
|
290 |
+ file.setCharset("UTF-8", new NullProgressMonitor()); |
|
291 |
+ } |
|
292 |
+ } catch (CoreException e) { |
|
293 |
+ StatusManager.getManager().handle(e, "de.cau.cs.rtprak.turing.graphiti"); |
|
294 |
+ } |
|
295 |
+ } |
|
296 |
+ |
|
297 |
+ /** |
|
298 |
+ * Open the diagram from the given resource. |
|
299 |
+ * |
|
300 |
+ * @param diagramResource |
|
301 |
+ * a resource for a diagram file |
|
302 |
+ * @throws PartInitException |
|
303 |
+ * if the diagram could not be opened |
|
304 |
+ */ |
|
305 |
+ private void openDiagram(final Resource diagramResource) throws PartInitException { |
|
306 |
+ String path = diagramResource.getURI().toPlatformString(true); |
|
307 |
+ IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot() |
|
308 |
+ .findMember(new Path(path)); |
|
309 |
+ if (workspaceResource instanceof IFile) { |
|
310 |
+ IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); |
|
311 |
+ page.openEditor(new FileEditorInput((IFile) workspaceResource), editorId); |
|
312 |
+ } |
|
313 |
+ } |
|
314 |
+ |
|
315 |
+ /** |
|
316 |
+ * Create a model in the given resources. |
|
317 |
+ * |
|
318 |
+ * @param diagramResource |
|
319 |
+ * resource for the diagram model |
|
320 |
+ * @param diagramName |
|
321 |
+ * name of the diagram model |
|
322 |
+ * @param modelResource |
|
323 |
+ * resource for the domain model |
|
324 |
+ * @param modelName |
|
325 |
+ * name of the domain model |
|
326 |
+ */ |
|
327 |
+ private void createModel(final Resource diagramResource, final String diagramName, |
|
328 |
+ final Resource modelResource, final String modelName) { |
|
329 |
+ modelResource.setTrackingModification(true); |
|
330 |
+ EObject domainModel = createModel(modelName); |
|
331 |
+ modelResource.getContents().add(domainModel); |
|
332 |
+ diagramResource.setTrackingModification(true); |
|
333 |
+ Diagram diagram = Graphiti.getPeCreateService().createDiagram(diagramTypeName, diagramName, |
|
334 |
+ gridSize, snapToGrid); |
|
335 |
+ PictogramLink link = PictogramsFactory.eINSTANCE.createPictogramLink(); |
|
336 |
+ link.setPictogramElement(diagram); |
|
337 |
+ link.getBusinessObjects().add(domainModel); |
|
338 |
+ configureDiagram(diagram); |
|
339 |
+ diagramResource.getContents().add(diagram); |
|
340 |
+ } |
|
341 |
+ |
|
342 |
+ /** |
|
343 |
+ * Configure the diagram. May be overridden by subclasses. |
|
344 |
+ * |
|
345 |
+ * @param diagram |
|
346 |
+ * the top-level element of the pictogram model |
|
347 |
+ */ |
|
348 |
+ protected void configureDiagram(final Diagram diagram) { |
|
349 |
+ // the default implementation does nothing |
|
350 |
+ } |
|
351 |
+ |
|
352 |
+ /** |
|
353 |
+ * Create an instance of the top-level object for the domain model. |
|
354 |
+ * |
|
355 |
+ * @param name |
|
356 |
+ * name of the model |
|
357 |
+ * @return an instance of the domain model object |
|
358 |
+ */ |
|
359 |
+ protected abstract EObject createModel(final String name); |
|
360 |
+ |
|
361 |
+} |
|
362 |
+ |