| 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.AbstractHandler; |
| 7 | import org.eclipse.core.commands.ExecutionEvent; |
| 8 | import org.eclipse.core.commands.ExecutionException; |
| 9 | import org.eclipse.core.commands.operations.OperationHistoryFactory; |
| 10 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 11 | import org.eclipse.emf.common.util.URI; |
| 12 | import org.eclipse.emf.common.util.WrappedException; |
| 13 | import org.eclipse.emf.ecore.EObject; |
| 14 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 15 | import org.eclipse.gef.EditPart; |
| 16 | import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 17 | import org.eclipse.gmf.runtime.diagram.ui.commands.CreateCommand; |
| 18 | import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor; |
| 19 | import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest; |
| 20 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| 21 | import org.eclipse.gmf.runtime.notation.Node; |
| 22 | import org.eclipse.gmf.runtime.notation.View; |
| 23 | import org.eclipse.jface.viewers.ISelection; |
| 24 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 25 | import org.eclipse.jface.window.Window; |
| 26 | import org.eclipse.swt.widgets.Shell; |
| 27 | import org.eclipse.ui.IEditorPart; |
| 28 | import org.eclipse.ui.handlers.HandlerUtil; |
| 29 | |
| 30 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.commands.PalladioComponentModelCreateShortcutDecorationsCommand; |
| 31 | |
| 32 | /** |
| 33 | * @generated |
| 34 | */ |
| 35 | public class CreateShortcutAction extends AbstractHandler { |
| 36 | /** |
| 37 | * @generated |
| 38 | */ |
| 39 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 40 | IEditorPart diagramEditor = HandlerUtil.getActiveEditorChecked(event); |
| 41 | Shell shell = diagramEditor.getEditorSite().getShell(); |
| 42 | assert diagramEditor instanceof DiagramEditor; |
| 43 | TransactionalEditingDomain editingDomain = ((DiagramEditor) diagramEditor) |
| 44 | .getEditingDomain(); |
| 45 | ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); |
| 46 | assert selection instanceof IStructuredSelection; |
| 47 | assert ((IStructuredSelection) selection).size() == 1; |
| 48 | assert ((IStructuredSelection) selection).getFirstElement() instanceof EditPart; |
| 49 | EditPart selectedDiagramPart = (EditPart) ((IStructuredSelection) selection) |
| 50 | .getFirstElement(); |
| 51 | final View view = (View) selectedDiagramPart.getModel(); |
| 52 | PalladioComponentModelElementChooserDialog elementChooser = new PalladioComponentModelElementChooserDialog( |
| 53 | shell, view); |
| 54 | int result = elementChooser.open(); |
| 55 | if (result != Window.OK) { |
| 56 | return null; |
| 57 | } |
| 58 | URI selectedModelElementURI = elementChooser |
| 59 | .getSelectedModelElementURI(); |
| 60 | final EObject selectedElement; |
| 61 | try { |
| 62 | selectedElement = editingDomain.getResourceSet().getEObject( |
| 63 | selectedModelElementURI, true); |
| 64 | } catch (WrappedException e) { |
| 65 | PalladioComponentModelRepositoryDiagramEditorPlugin |
| 66 | .getInstance() |
| 67 | .logError( |
| 68 | "Exception while loading object: " + selectedModelElementURI.toString(), e); //$NON-NLS-1$ |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | if (selectedElement == null) { |
| 73 | return null; |
| 74 | } |
| 75 | CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor( |
| 76 | new EObjectAdapter(selectedElement), |
| 77 | Node.class, |
| 78 | null, |
| 79 | PalladioComponentModelRepositoryDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); |
| 80 | ICommand command = new CreateCommand(editingDomain, viewDescriptor, |
| 81 | view); |
| 82 | command = command |
| 83 | .compose(new PalladioComponentModelCreateShortcutDecorationsCommand( |
| 84 | editingDomain, view, viewDescriptor)); |
| 85 | try { |
| 86 | OperationHistoryFactory.getOperationHistory().execute(command, |
| 87 | new NullProgressMonitor(), null); |
| 88 | } catch (ExecutionException e) { |
| 89 | PalladioComponentModelRepositoryDiagramEditorPlugin.getInstance() |
| 90 | .logError("Unable to create shortcut", e); //$NON-NLS-1$ |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | } |