1 | package de.uka.ipd.sdq.pcm.gmf.allocation.part; |
2 | |
3 | import java.io.IOException; |
4 | import java.util.HashMap; |
5 | import java.util.Map; |
6 | |
7 | import org.eclipse.core.commands.ExecutionException; |
8 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
9 | import org.eclipse.core.resources.IFile; |
10 | import org.eclipse.core.runtime.IAdaptable; |
11 | import org.eclipse.core.runtime.IProgressMonitor; |
12 | import org.eclipse.core.runtime.NullProgressMonitor; |
13 | import org.eclipse.emf.ecore.EObject; |
14 | import org.eclipse.emf.ecore.xmi.XMIResource; |
15 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
16 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
17 | import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; |
18 | import org.eclipse.jface.viewers.IStructuredSelection; |
19 | import org.eclipse.ui.IWorkbenchPage; |
20 | |
21 | import de.uka.ipd.sdq.pcm.allocation.Allocation; |
22 | import de.uka.ipd.sdq.pcm.resourceenvironment.ResourceEnvironment; |
23 | import de.uka.ipd.sdq.pcm.system.System; |
24 | |
25 | /** |
26 | * This wizard has added pages that require the user to select a System |
27 | * and a ResourceEnvironment, when initializing the diagram. |
28 | * |
29 | * @author Philipp Meier |
30 | * |
31 | */ |
32 | public class PcmNewAllocationDiagramFileWizard |
33 | extends PalladioComponentModelNewDiagramFileWizard { |
34 | |
35 | private Allocation myDiagramRoot; |
36 | private TransactionalEditingDomain myEditingDomain; |
37 | private ResourceEnvironmentSelectorPage myResourceEnvironmentSelectorPage; |
38 | private SystemSelectorPage mySystemSelectorPage; |
39 | |
40 | public PcmNewAllocationDiagramFileWizard(org.eclipse.emf.common.util.URI domainModelURI, |
41 | EObject diagramRoot, TransactionalEditingDomain editingDomain) { |
42 | super(domainModelURI, diagramRoot, editingDomain); |
43 | |
44 | myEditingDomain = editingDomain; |
45 | |
46 | myDiagramRoot = null; |
47 | if (diagramRoot instanceof Allocation) { |
48 | myDiagramRoot = (Allocation)diagramRoot; |
49 | } |
50 | |
51 | ResourceEnvironment resourceEnvironment = null; |
52 | System system = null; |
53 | if (myDiagramRoot != null) { |
54 | resourceEnvironment = myDiagramRoot.getTargetResourceEnvironment_Allocation(); |
55 | system = myDiagramRoot.getSystem_Allocation(); |
56 | } |
57 | myResourceEnvironmentSelectorPage = new ResourceEnvironmentSelectorPage(resourceEnvironment); |
58 | mySystemSelectorPage = new SystemSelectorPage(system); |
59 | } |
60 | |
61 | @Override |
62 | public void addPages() { |
63 | super.addPages(); |
64 | |
65 | addPage(myResourceEnvironmentSelectorPage); |
66 | addPage(mySystemSelectorPage); |
67 | } |
68 | |
69 | @Override |
70 | public boolean performFinish() { |
71 | if (myDiagramRoot == null) { |
72 | return false; |
73 | } |
74 | |
75 | // TODO retrieve the IFile handle to the allocation model and add to list of modified files for command |
76 | |
77 | AbstractTransactionalCommand command = new AbstractTransactionalCommand( |
78 | myEditingDomain, "Save allocation model.", null) { //$NON-NLS-1$ |
79 | protected CommandResult doExecuteWithResult( |
80 | IProgressMonitor monitor, IAdaptable info) |
81 | throws ExecutionException { |
82 | myDiagramRoot.setTargetResourceEnvironment_Allocation(myResourceEnvironmentSelectorPage.getSelectedResourceEnvironment()); |
83 | myDiagramRoot.setSystem_Allocation(mySystemSelectorPage.getSelectedSystem()); |
84 | try { |
85 | Map<String,String> options = new HashMap<String,String>(); |
86 | options.put(XMIResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$ |
87 | myDiagramRoot.eResource().save(options); |
88 | } catch (IOException e) { |
89 | PalladioComponentModelAllocationDiagramEditorPlugin |
90 | .getInstance() |
91 | .logError( |
92 | "Save operation failed for: " + myDiagramRoot.eResource().getURI().path(), e); //$NON-NLS-1$ |
93 | } |
94 | return CommandResult.newOKCommandResult(); |
95 | } |
96 | }; |
97 | |
98 | try { |
99 | OperationHistoryFactory.getOperationHistory().execute(command, |
100 | new NullProgressMonitor(), null); |
101 | } catch (ExecutionException e) { |
102 | PalladioComponentModelAllocationDiagramEditorPlugin.getInstance().logError( |
103 | "Unable to save allocation model.", e); //$NON-NLS-1$ |
104 | } |
105 | |
106 | return super.performFinish(); |
107 | } |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | } |