| 1 | /* |
| 2 | *Copyright 2007, SDQ, IPD, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.usage.part; |
| 5 | |
| 6 | import org.eclipse.core.resources.IFile; |
| 7 | import org.eclipse.emf.common.util.URI; |
| 8 | import org.eclipse.emf.common.util.WrappedException; |
| 9 | import org.eclipse.emf.ecore.EObject; |
| 10 | import org.eclipse.emf.ecore.resource.Resource; |
| 11 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 12 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 13 | import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory; |
| 14 | import org.eclipse.jface.action.IAction; |
| 15 | import org.eclipse.jface.dialogs.MessageDialog; |
| 16 | import org.eclipse.jface.viewers.ISelection; |
| 17 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 18 | import org.eclipse.jface.wizard.Wizard; |
| 19 | import org.eclipse.osgi.util.NLS; |
| 20 | import org.eclipse.swt.widgets.Shell; |
| 21 | import org.eclipse.ui.IObjectActionDelegate; |
| 22 | import org.eclipse.ui.IWorkbenchPart; |
| 23 | |
| 24 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
| 25 | |
| 26 | /** |
| 27 | * @generated |
| 28 | */ |
| 29 | public class PalladioComponentModelInitDiagramFileAction implements |
| 30 | IObjectActionDelegate { |
| 31 | |
| 32 | /** |
| 33 | * @generated |
| 34 | */ |
| 35 | private IWorkbenchPart targetPart; |
| 36 | |
| 37 | /** |
| 38 | * @generated |
| 39 | */ |
| 40 | private URI domainModelURI; |
| 41 | |
| 42 | /** |
| 43 | * @generated |
| 44 | */ |
| 45 | public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 46 | this.targetPart = targetPart; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @generated |
| 51 | */ |
| 52 | public void selectionChanged(IAction action, ISelection selection) { |
| 53 | domainModelURI = null; |
| 54 | action.setEnabled(false); |
| 55 | if (selection instanceof IStructuredSelection == false |
| 56 | || selection.isEmpty()) { |
| 57 | return; |
| 58 | } |
| 59 | IFile file = (IFile) ((IStructuredSelection) selection) |
| 60 | .getFirstElement(); |
| 61 | domainModelURI = URI.createPlatformResourceURI(file.getFullPath() |
| 62 | .toString(), true); |
| 63 | action.setEnabled(true); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @generated |
| 68 | */ |
| 69 | private Shell getShell() { |
| 70 | return targetPart.getSite().getShell(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @generated |
| 75 | */ |
| 76 | public void run(IAction action) { |
| 77 | TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE |
| 78 | .createEditingDomain(); |
| 79 | ResourceSet resourceSet = editingDomain.getResourceSet(); |
| 80 | EObject diagramRoot = null; |
| 81 | try { |
| 82 | Resource resource = resourceSet.getResource(domainModelURI, true); |
| 83 | diagramRoot = (EObject) resource.getContents().get(0); |
| 84 | } catch (WrappedException ex) { |
| 85 | PalladioComponentModelUsageDiagramEditorPlugin.getInstance() |
| 86 | .logError("Unable to load resource: " + domainModelURI, ex); //$NON-NLS-1$ |
| 87 | } |
| 88 | if (diagramRoot == null) { |
| 89 | MessageDialog.openError(getShell(), |
| 90 | Messages.InitDiagramFile_ResourceErrorDialogTitle, |
| 91 | Messages.InitDiagramFile_ResourceErrorDialogMessage); |
| 92 | return; |
| 93 | } |
| 94 | Wizard wizard = new PalladioComponentModelNewDiagramFileWizard( |
| 95 | domainModelURI, diagramRoot, editingDomain); |
| 96 | wizard.setWindowTitle(NLS.bind(Messages.InitDiagramFile_WizardTitle, |
| 97 | UsageScenarioEditPart.MODEL_ID)); |
| 98 | PalladioComponentModelDiagramEditorUtil.runWizard(getShell(), wizard, |
| 99 | "InitDiagramFile"); //$NON-NLS-1$ |
| 100 | } |
| 101 | |
| 102 | } |