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