| 1 | /* |
| 2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.repository.part; |
| 5 | |
| 6 | import org.eclipse.core.commands.ExecutionException; |
| 7 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
| 8 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 9 | import org.eclipse.emf.common.util.URI; |
| 10 | import org.eclipse.emf.common.util.WrappedException; |
| 11 | import org.eclipse.emf.ecore.EObject; |
| 12 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 13 | import org.eclipse.gmf.runtime.diagram.ui.commands.CreateCommand; |
| 14 | import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest; |
| 15 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| 16 | import org.eclipse.gmf.runtime.notation.Node; |
| 17 | import org.eclipse.gmf.runtime.notation.View; |
| 18 | import org.eclipse.jface.action.IAction; |
| 19 | import org.eclipse.jface.viewers.ISelection; |
| 20 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 21 | import org.eclipse.jface.window.Window; |
| 22 | import org.eclipse.swt.widgets.Shell; |
| 23 | import org.eclipse.ui.IObjectActionDelegate; |
| 24 | import org.eclipse.ui.IWorkbenchPart; |
| 25 | |
| 26 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.commands.PalladioComponentModelCreateShortcutDecorationsCommand; |
| 27 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.parts.RepositoryEditPart; |
| 28 | |
| 29 | /** |
| 30 | * @generated |
| 31 | */ |
| 32 | public class PalladioComponentModelCreateShortcutAction implements |
| 33 | IObjectActionDelegate { |
| 34 | |
| 35 | /** |
| 36 | * @generated |
| 37 | */ |
| 38 | private RepositoryEditPart mySelectedElement; |
| 39 | |
| 40 | /** |
| 41 | * @generated |
| 42 | */ |
| 43 | private Shell myShell; |
| 44 | |
| 45 | /** |
| 46 | * @generated |
| 47 | */ |
| 48 | public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 49 | myShell = targetPart.getSite().getShell(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @generated |
| 54 | */ |
| 55 | public void selectionChanged(IAction action, ISelection selection) { |
| 56 | mySelectedElement = null; |
| 57 | if (selection instanceof IStructuredSelection) { |
| 58 | IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
| 59 | if (structuredSelection.size() == 1 |
| 60 | && structuredSelection.getFirstElement() instanceof RepositoryEditPart) { |
| 61 | mySelectedElement = (RepositoryEditPart) structuredSelection |
| 62 | .getFirstElement(); |
| 63 | } |
| 64 | } |
| 65 | action.setEnabled(isEnabled()); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | private boolean isEnabled() { |
| 72 | return mySelectedElement != null; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @generated |
| 77 | */ |
| 78 | public void run(IAction action) { |
| 79 | final View view = (View) mySelectedElement.getModel(); |
| 80 | PalladioComponentModelElementChooserDialog elementChooser = new PalladioComponentModelElementChooserDialog( |
| 81 | myShell, view); |
| 82 | int result = elementChooser.open(); |
| 83 | if (result != Window.OK) { |
| 84 | return; |
| 85 | } |
| 86 | URI selectedModelElementURI = elementChooser |
| 87 | .getSelectedModelElementURI(); |
| 88 | final EObject selectedElement; |
| 89 | try { |
| 90 | selectedElement = mySelectedElement.getEditingDomain() |
| 91 | .getResourceSet().getEObject(selectedModelElementURI, true); |
| 92 | } catch (WrappedException e) { |
| 93 | PalladioComponentModelRepositoryDiagramEditorPlugin |
| 94 | .getInstance() |
| 95 | .logError( |
| 96 | "Exception while loading object: " + selectedModelElementURI.toString(), e); //$NON-NLS-1$ |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (selectedElement == null) { |
| 101 | return; |
| 102 | } |
| 103 | CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor( |
| 104 | new EObjectAdapter(selectedElement), |
| 105 | Node.class, |
| 106 | null, |
| 107 | PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
| 108 | ICommand command = new CreateCommand(mySelectedElement |
| 109 | .getEditingDomain(), viewDescriptor, view); |
| 110 | command = command |
| 111 | .compose(new PalladioComponentModelCreateShortcutDecorationsCommand( |
| 112 | mySelectedElement.getEditingDomain(), view, |
| 113 | viewDescriptor)); |
| 114 | try { |
| 115 | OperationHistoryFactory.getOperationHistory().execute(command, |
| 116 | new NullProgressMonitor(), null); |
| 117 | } catch (ExecutionException e) { |
| 118 | PalladioComponentModelRepositoryDiagramEditorPlugin.getInstance() |
| 119 | .logError("Unable to create shortcut", e); //$NON-NLS-1$ |
| 120 | } |
| 121 | } |
| 122 | } |