1 | /* |
2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.part; |
5 | |
6 | import java.io.IOException; |
7 | import java.util.Collections; |
8 | import java.util.LinkedList; |
9 | import java.util.List; |
10 | import java.util.Vector; |
11 | |
12 | import org.eclipse.core.commands.ExecutionException; |
13 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
14 | import org.eclipse.core.resources.IFile; |
15 | import org.eclipse.core.runtime.CoreException; |
16 | import org.eclipse.core.runtime.IAdaptable; |
17 | import org.eclipse.core.runtime.IPath; |
18 | import org.eclipse.core.runtime.IProgressMonitor; |
19 | import org.eclipse.core.runtime.NullProgressMonitor; |
20 | import org.eclipse.core.runtime.Path; |
21 | import org.eclipse.emf.common.util.TreeIterator; |
22 | import org.eclipse.emf.common.util.URI; |
23 | import org.eclipse.emf.ecore.EObject; |
24 | import org.eclipse.emf.ecore.resource.Resource; |
25 | import org.eclipse.emf.ecore.resource.ResourceSet; |
26 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
27 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
28 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
29 | import org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation; |
30 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
31 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
32 | import org.eclipse.gmf.runtime.notation.Diagram; |
33 | import org.eclipse.jface.viewers.StructuredSelection; |
34 | import org.eclipse.jface.wizard.Wizard; |
35 | import org.eclipse.jface.wizard.WizardPage; |
36 | import org.eclipse.osgi.util.NLS; |
37 | import org.eclipse.swt.SWT; |
38 | import org.eclipse.swt.events.SelectionEvent; |
39 | import org.eclipse.swt.events.SelectionListener; |
40 | import org.eclipse.swt.layout.GridData; |
41 | import org.eclipse.swt.layout.GridLayout; |
42 | import org.eclipse.swt.widgets.Combo; |
43 | import org.eclipse.swt.widgets.Composite; |
44 | import org.eclipse.swt.widgets.Label; |
45 | import org.eclipse.ui.PartInitException; |
46 | import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
47 | |
48 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.ResourceDemandingSEFFEditPart; |
49 | import de.uka.ipd.sdq.pcm.repository.BasicComponent; |
50 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
51 | |
52 | /** |
53 | * @generated |
54 | */ |
55 | public class PalladioComponentModelNewDiagramFileWizard extends Wizard { |
56 | |
57 | /** |
58 | * @generated |
59 | */ |
60 | private WizardNewFileCreationPage myFileCreationPage; |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | private ModelElementSelectionPage diagramRootElementSelectionPage; |
66 | |
67 | /** |
68 | * @generated |
69 | */ |
70 | private TransactionalEditingDomain myEditingDomain; |
71 | |
72 | /** |
73 | * @generated not |
74 | */ |
75 | private WizardNewFileCreationPage myDiagramFilenameSelectionPage; |
76 | |
77 | /** |
78 | * @generated not |
79 | */ |
80 | private DiagramRootElementSelectionPage myDiagramRootElementSelectionPage; |
81 | |
82 | /** |
83 | * @generated not |
84 | */ |
85 | public PalladioComponentModelNewDiagramFileWizard(URI domainModelURI, |
86 | EObject diagramRoot, TransactionalEditingDomain editingDomain) { |
87 | assert domainModelURI != null : "Domain model uri must be specified"; //$NON-NLS-1$ |
88 | assert diagramRoot != null : "Doagram root element must be specified"; //$NON-NLS-1$ |
89 | assert editingDomain != null : "Editing domain must be specified"; //$NON-NLS-1$ |
90 | |
91 | myDiagramFilenameSelectionPage = new WizardNewFileCreationPage( |
92 | Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageName, |
93 | StructuredSelection.EMPTY); |
94 | myDiagramFilenameSelectionPage |
95 | .setTitle(Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageTitle); |
96 | myDiagramFilenameSelectionPage |
97 | .setDescription(NLS |
98 | .bind( |
99 | Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageDescription, |
100 | ResourceDemandingSEFFEditPart.MODEL_ID)); |
101 | //force correct file extension |
102 | myDiagramFilenameSelectionPage.setFileExtension("seff_diagram"); |
103 | |
104 | IPath filePath; |
105 | if (domainModelURI.isPlatformResource()) { |
106 | filePath = new Path(domainModelURI.trimSegments(1) |
107 | .toPlatformString(true)); |
108 | } else if (domainModelURI.isFile()) { |
109 | filePath = new Path(domainModelURI.trimSegments(1).toFileString()); |
110 | } else { |
111 | // TODO : use some default path |
112 | throw new IllegalArgumentException( |
113 | "Unsupported URI: " + domainModelURI); //$NON-NLS-1$ |
114 | } |
115 | myDiagramFilenameSelectionPage.setContainerFullPath(filePath); |
116 | |
117 | myDiagramRootElementSelectionPage = new DiagramRootElementSelectionPage( |
118 | Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageName, |
119 | diagramRoot); |
120 | myDiagramRootElementSelectionPage |
121 | .setTitle(Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageTitle); |
122 | myDiagramRootElementSelectionPage |
123 | .setDescription(Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageDescription); |
124 | |
125 | myEditingDomain = editingDomain; |
126 | } |
127 | |
128 | /** |
129 | * @generated not |
130 | */ |
131 | public void addPages() { |
132 | addPage(myDiagramRootElementSelectionPage); |
133 | addPage(myDiagramFilenameSelectionPage); |
134 | } |
135 | |
136 | /** |
137 | * @generated not |
138 | */ |
139 | public boolean performFinish() { |
140 | List<IFile> affectedFiles = new LinkedList<IFile>(); |
141 | IFile diagramFile = myDiagramFilenameSelectionPage.createNewFile(); |
142 | try { |
143 | diagramFile.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$ |
144 | } catch (CoreException e) { |
145 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
146 | .logError("Unable to set charset for diagram file", e); //$NON-NLS-1$ |
147 | } |
148 | affectedFiles.add(diagramFile); |
149 | org.eclipse.emf.common.util.URI diagramModelURI = org.eclipse.emf.common.util.URI |
150 | .createPlatformResourceURI( |
151 | diagramFile.getFullPath().toString(), true); |
152 | ResourceSet resourceSet = myEditingDomain.getResourceSet(); |
153 | final Resource diagramResource = resourceSet |
154 | .createResource(diagramModelURI); |
155 | AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
156 | myEditingDomain, "Initializing diagram contents", affectedFiles) { //$NON-NLS-1$ |
157 | |
158 | protected CommandResult doExecuteWithResult( |
159 | IProgressMonitor monitor, IAdaptable info) |
160 | throws ExecutionException { |
161 | int diagramVID = PalladioComponentModelVisualIDRegistry |
162 | .getDiagramVisualID(myDiagramRootElementSelectionPage |
163 | .getSeff()); |
164 | if (diagramVID != ResourceDemandingSEFFEditPart.VISUAL_ID) { |
165 | return CommandResult |
166 | .newErrorCommandResult("Incorrect model object stored as a root resource object"); //$NON-NLS-1$ |
167 | } |
168 | Diagram diagram = ViewService |
169 | .createDiagram( |
170 | myDiagramRootElementSelectionPage.getSeff(), |
171 | ResourceDemandingSEFFEditPart.MODEL_ID, |
172 | PalladioComponentModelSeffDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
173 | diagramResource.getContents().add(diagram); |
174 | return CommandResult.newOKCommandResult(); |
175 | } |
176 | }; |
177 | try { |
178 | OperationHistoryFactory.getOperationHistory().execute(command, |
179 | new NullProgressMonitor(), null); |
180 | diagramResource.save(Collections.EMPTY_MAP); |
181 | PalladioComponentModelDiagramEditorUtil |
182 | .openDiagram(diagramResource); |
183 | } catch (ExecutionException e) { |
184 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
185 | .logError("Unable to create model and diagram", e); //$NON-NLS-1$ |
186 | } catch (IOException ex) { |
187 | PalladioComponentModelSeffDiagramEditorPlugin |
188 | .getInstance() |
189 | .logError( |
190 | "Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$ |
191 | } catch (PartInitException ex) { |
192 | PalladioComponentModelSeffDiagramEditorPlugin.getInstance() |
193 | .logError("Unable to open editor", ex); //$NON-NLS-1$ |
194 | } |
195 | return true; |
196 | } |
197 | |
198 | /** |
199 | * @generated not |
200 | */ |
201 | private class DiagramRootElementSelectionPage extends WizardPage { |
202 | |
203 | /** |
204 | * @generated |
205 | */ |
206 | protected DiagramRootElementSelectionPage(String pageName) { |
207 | super(pageName); |
208 | } |
209 | |
210 | private Combo myCombo; |
211 | private List<ResourceDemandingSEFF> myFoundSeffs; |
212 | private EObject myDiagramRoot; |
213 | private ResourceDemandingSEFF mySeff; |
214 | |
215 | /** |
216 | * @generated not |
217 | */ |
218 | protected DiagramRootElementSelectionPage(String pageName, |
219 | EObject diagramRoot) { |
220 | super(pageName); |
221 | myCombo = null; |
222 | myFoundSeffs = new Vector<ResourceDemandingSEFF>(); |
223 | myDiagramRoot = diagramRoot; |
224 | mySeff = null; |
225 | } |
226 | |
227 | public void createControl(Composite parent) { |
228 | initializeDialogUnits(parent); |
229 | Composite topLevel = new Composite(parent, SWT.NONE); |
230 | topLevel.setLayout(new GridLayout()); |
231 | topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL |
232 | | GridData.HORIZONTAL_ALIGN_FILL)); |
233 | topLevel.setFont(parent.getFont()); |
234 | setControl(topLevel); |
235 | createPageContent(topLevel); |
236 | setPageComplete(validatePage()); |
237 | } |
238 | |
239 | private void createPageContent(Composite parent) { |
240 | Composite panel = new Composite(parent, SWT.NONE); |
241 | panel.setLayoutData(new GridData(GridData.FILL_BOTH)); |
242 | GridLayout layout = new GridLayout(); |
243 | panel.setLayout(layout); |
244 | |
245 | Label label = new Label(panel, SWT.NONE); |
246 | label.setText("Select Resource Demanding SEFF:"); |
247 | label.setLayoutData(new GridData( |
248 | GridData.HORIZONTAL_ALIGN_BEGINNING)); |
249 | |
250 | myCombo = new Combo(panel, SWT.DROP_DOWN | SWT.READ_ONLY); |
251 | findSeffs(); |
252 | populateComboBox(); |
253 | myCombo.addSelectionListener(new ComboSelectionListener()); |
254 | } |
255 | |
256 | private void populateComboBox() { |
257 | if (myCombo == null) { |
258 | return; |
259 | } |
260 | |
261 | myCombo.removeAll(); |
262 | for (ResourceDemandingSEFF seff : myFoundSeffs) { |
263 | myCombo.add("Container: " + getComponentNameFromSEFF(seff) |
264 | + " - SEFF " + getServiceNameFromSEFF(seff) + " id: " |
265 | + seff.getId()); |
266 | } |
267 | } |
268 | |
269 | /** |
270 | * helper function to determine the name of the component of a seff. |
271 | * Assumes the seff can only be contained in a BasicComponent. |
272 | * |
273 | * @param seff any seff |
274 | * @return the name of the component the seff belongs to |
275 | * or "" if the seff is not contained in a BasicComponent |
276 | */ |
277 | protected String getComponentNameFromSEFF(ResourceDemandingSEFF seff) { |
278 | EObject container = seff.eContainer(); |
279 | if (container instanceof BasicComponent) { |
280 | BasicComponent component = (BasicComponent) container; |
281 | return component.getEntityName(); |
282 | } else { |
283 | return ""; |
284 | } |
285 | } |
286 | |
287 | /** |
288 | * helper function to determine the name of the service of a seff |
289 | * |
290 | * @param seff any seff |
291 | * @return the name of the service the seff belongs to |
292 | * or "" if the seff belongs to no service |
293 | * |
294 | * @generated not |
295 | */ |
296 | protected String getServiceNameFromSEFF(ResourceDemandingSEFF seff) { |
297 | return (seff.getDescribedService__SEFF() == null ? "" : seff |
298 | .getDescribedService__SEFF().getEntityName()); |
299 | } |
300 | |
301 | private void findSeffs() { |
302 | |
303 | myFoundSeffs.clear(); |
304 | TreeIterator<EObject> it = myDiagramRoot.eAllContents(); |
305 | |
306 | while (it.hasNext()) { |
307 | EObject possibleSeff = it.next(); |
308 | if (possibleSeff instanceof ResourceDemandingSEFF) { |
309 | myFoundSeffs.add((ResourceDemandingSEFF) possibleSeff); |
310 | } |
311 | } |
312 | } |
313 | |
314 | /** |
315 | * @generated |
316 | */ |
317 | protected String getSelectionTitle() { |
318 | return Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageSelectionTitle; |
319 | } |
320 | |
321 | protected ResourceDemandingSEFF getSeff() { |
322 | return mySeff; |
323 | } |
324 | |
325 | /** |
326 | * @generated not |
327 | */ |
328 | protected boolean validatePage() { |
329 | mySeff = null; |
330 | if (myCombo.getSelectionIndex() == -1) { |
331 | return false; |
332 | } |
333 | |
334 | try { |
335 | mySeff = myFoundSeffs.get(myCombo.getSelectionIndex()); |
336 | } catch (ArrayIndexOutOfBoundsException e) { |
337 | return false; |
338 | } |
339 | |
340 | //propose a name in the diagram filename selection page |
341 | String filename = getComponentNameFromSEFF(mySeff) + "." |
342 | + getServiceNameFromSEFF(mySeff) + ".seff_diagram"; |
343 | myDiagramFilenameSelectionPage.setFileName(filename); |
344 | |
345 | return true; |
346 | } |
347 | |
348 | private class ComboSelectionListener implements SelectionListener { |
349 | |
350 | public void widgetDefaultSelected(SelectionEvent e) { |
351 | setPageComplete(validatePage()); |
352 | } |
353 | |
354 | public void widgetSelected(SelectionEvent e) { |
355 | setPageComplete(validatePage()); |
356 | } |
357 | |
358 | } |
359 | } |
360 | } |