1 | /* |
2 | * Copyright 2007, SDQ, IPD, U KA |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.allocation.part; |
5 | |
6 | import java.io.IOException; |
7 | import java.util.Collections; |
8 | import java.util.LinkedList; |
9 | import java.util.List; |
10 | |
11 | import org.eclipse.core.commands.ExecutionException; |
12 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
13 | import org.eclipse.core.resources.IFile; |
14 | import org.eclipse.core.runtime.CoreException; |
15 | import org.eclipse.core.runtime.IAdaptable; |
16 | import org.eclipse.core.runtime.IPath; |
17 | import org.eclipse.core.runtime.IProgressMonitor; |
18 | import org.eclipse.core.runtime.NullProgressMonitor; |
19 | import org.eclipse.core.runtime.Path; |
20 | import org.eclipse.emf.common.util.URI; |
21 | import org.eclipse.emf.ecore.EObject; |
22 | import org.eclipse.emf.ecore.resource.Resource; |
23 | import org.eclipse.emf.ecore.resource.ResourceSet; |
24 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
25 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
26 | import org.eclipse.gmf.runtime.diagram.core.services.ViewService; |
27 | import org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation; |
28 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
29 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
30 | import org.eclipse.gmf.runtime.notation.Diagram; |
31 | import org.eclipse.jface.viewers.StructuredSelection; |
32 | import org.eclipse.jface.wizard.Wizard; |
33 | import org.eclipse.osgi.util.NLS; |
34 | import org.eclipse.ui.PartInitException; |
35 | import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
36 | |
37 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts.AllocationEditPart; |
38 | |
39 | /** |
40 | * @generated |
41 | */ |
42 | public class PalladioComponentModelNewDiagramFileWizard extends Wizard { |
43 | |
44 | /** |
45 | * @generated |
46 | */ |
47 | private WizardNewFileCreationPage myFileCreationPage; |
48 | |
49 | /** |
50 | * @generated |
51 | */ |
52 | private ModelElementSelectionPage diagramRootElementSelectionPage; |
53 | |
54 | /** |
55 | * @generated |
56 | */ |
57 | private TransactionalEditingDomain myEditingDomain; |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public PalladioComponentModelNewDiagramFileWizard(URI domainModelURI, |
63 | EObject diagramRoot, TransactionalEditingDomain editingDomain) { |
64 | assert domainModelURI != null : "Domain model uri must be specified"; //$NON-NLS-1$ |
65 | assert diagramRoot != null : "Doagram root element must be specified"; //$NON-NLS-1$ |
66 | assert editingDomain != null : "Editing domain must be specified"; //$NON-NLS-1$ |
67 | |
68 | myFileCreationPage = new WizardNewFileCreationPage( |
69 | Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageName, |
70 | StructuredSelection.EMPTY); |
71 | myFileCreationPage |
72 | .setTitle(Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageTitle); |
73 | myFileCreationPage |
74 | .setDescription(NLS |
75 | .bind( |
76 | Messages.PalladioComponentModelNewDiagramFileWizard_CreationPageDescription, |
77 | AllocationEditPart.MODEL_ID)); |
78 | IPath filePath; |
79 | String fileName = domainModelURI.trimFileExtension().lastSegment(); |
80 | if (domainModelURI.isPlatformResource()) { |
81 | filePath = new Path(domainModelURI.trimSegments(1) |
82 | .toPlatformString(true)); |
83 | } else if (domainModelURI.isFile()) { |
84 | filePath = new Path(domainModelURI.trimSegments(1).toFileString()); |
85 | } else { |
86 | // TODO : use some default path |
87 | throw new IllegalArgumentException( |
88 | "Unsupported URI: " + domainModelURI); //$NON-NLS-1$ |
89 | } |
90 | myFileCreationPage.setContainerFullPath(filePath); |
91 | myFileCreationPage.setFileName(PalladioComponentModelDiagramEditorUtil |
92 | .getUniqueFileName(filePath, fileName, "allocation_diagram")); //$NON-NLS-1$ |
93 | |
94 | diagramRootElementSelectionPage = new DiagramRootElementSelectionPage( |
95 | Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageName); |
96 | diagramRootElementSelectionPage |
97 | .setTitle(Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageTitle); |
98 | diagramRootElementSelectionPage |
99 | .setDescription(Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageDescription); |
100 | diagramRootElementSelectionPage.setModelElement(diagramRoot); |
101 | |
102 | myEditingDomain = editingDomain; |
103 | } |
104 | |
105 | /** |
106 | * @generated |
107 | */ |
108 | public void addPages() { |
109 | addPage(myFileCreationPage); |
110 | addPage(diagramRootElementSelectionPage); |
111 | } |
112 | |
113 | /** |
114 | * @generated |
115 | */ |
116 | public boolean performFinish() { |
117 | List affectedFiles = new LinkedList(); |
118 | IFile diagramFile = myFileCreationPage.createNewFile(); |
119 | PalladioComponentModelDiagramEditorUtil.setCharset(diagramFile); |
120 | affectedFiles.add(diagramFile); |
121 | URI diagramModelURI = URI.createPlatformResourceURI(diagramFile |
122 | .getFullPath().toString(), true); |
123 | ResourceSet resourceSet = myEditingDomain.getResourceSet(); |
124 | final Resource diagramResource = resourceSet |
125 | .createResource(diagramModelURI); |
126 | AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
127 | myEditingDomain, |
128 | Messages.PalladioComponentModelNewDiagramFileWizard_InitDiagramCommand, |
129 | affectedFiles) { |
130 | |
131 | protected CommandResult doExecuteWithResult( |
132 | IProgressMonitor monitor, IAdaptable info) |
133 | throws ExecutionException { |
134 | int diagramVID = PalladioComponentModelVisualIDRegistry |
135 | .getDiagramVisualID(diagramRootElementSelectionPage |
136 | .getModelElement()); |
137 | if (diagramVID != AllocationEditPart.VISUAL_ID) { |
138 | return CommandResult |
139 | .newErrorCommandResult(Messages.PalladioComponentModelNewDiagramFileWizard_IncorrectRootError); |
140 | } |
141 | Diagram diagram = ViewService |
142 | .createDiagram( |
143 | diagramRootElementSelectionPage |
144 | .getModelElement(), |
145 | AllocationEditPart.MODEL_ID, |
146 | PalladioComponentModelAllocationDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
147 | diagramResource.getContents().add(diagram); |
148 | return CommandResult.newOKCommandResult(); |
149 | } |
150 | }; |
151 | try { |
152 | OperationHistoryFactory.getOperationHistory().execute(command, |
153 | new NullProgressMonitor(), null); |
154 | diagramResource.save(PalladioComponentModelDiagramEditorUtil |
155 | .getSaveOptions()); |
156 | PalladioComponentModelDiagramEditorUtil |
157 | .openDiagram(diagramResource); |
158 | } catch (ExecutionException e) { |
159 | PalladioComponentModelAllocationDiagramEditorPlugin.getInstance() |
160 | .logError("Unable to create model and diagram", e); //$NON-NLS-1$ |
161 | } catch (IOException ex) { |
162 | PalladioComponentModelAllocationDiagramEditorPlugin |
163 | .getInstance() |
164 | .logError( |
165 | "Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$ |
166 | } catch (PartInitException ex) { |
167 | PalladioComponentModelAllocationDiagramEditorPlugin.getInstance() |
168 | .logError("Unable to open editor", ex); //$NON-NLS-1$ |
169 | } |
170 | return true; |
171 | } |
172 | |
173 | /** |
174 | * @generated |
175 | */ |
176 | private static class DiagramRootElementSelectionPage extends |
177 | ModelElementSelectionPage { |
178 | |
179 | /** |
180 | * @generated |
181 | */ |
182 | protected DiagramRootElementSelectionPage(String pageName) { |
183 | super(pageName); |
184 | } |
185 | |
186 | /** |
187 | * @generated |
188 | */ |
189 | protected String getSelectionTitle() { |
190 | return Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageSelectionTitle; |
191 | } |
192 | |
193 | /** |
194 | * @generated |
195 | */ |
196 | protected boolean validatePage() { |
197 | if (selectedModelElement == null) { |
198 | setErrorMessage(Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageNoSelectionMessage); |
199 | return false; |
200 | } |
201 | boolean result = ViewService |
202 | .getInstance() |
203 | .provides( |
204 | new CreateDiagramViewOperation( |
205 | new EObjectAdapter(selectedModelElement), |
206 | AllocationEditPart.MODEL_ID, |
207 | PalladioComponentModelAllocationDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT)); |
208 | setErrorMessage(result ? null |
209 | : Messages.PalladioComponentModelNewDiagramFileWizard_RootSelectionPageInvalidSelectionMessage); |
210 | return result; |
211 | } |
212 | } |
213 | } |