Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a099e314c81b298645c3e8561f307bbd8455ab0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2011 THALES GLOBAL SERVICES.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.tools.internal.delete;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.ui.action.global.GlobalActionId;
import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionContext;
import org.eclipse.gmf.runtime.common.ui.services.action.global.IGlobalActionHandler;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.diagram.ui.providers.DiagramGlobalActionHandler;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.sirius.DSemanticDecorator;

/**
 * A {@link IGlobalActionHandler} to manage the delete action.
 * 
 * @author <a href="mailto:esteban.dugueperoux@obeo.fr">Esteban Dugueperoux</a>
 */
public class SiriusDeleteGlobalActionHandler extends DiagramGlobalActionHandler implements IGlobalActionHandler {

    /**
     * Overridden to tells that we handle only delete action.
     * 
     * {@inheritDoc}
     */
    @Override
    public boolean canHandle(IGlobalActionContext cntxt) {
        boolean canHandle = false;
        /* Check if the active part is a IDiagramWorkbenchPart */
        /* Check if the selection is a structured selection */
        IWorkbenchPart part = cntxt.getActivePart();
        if (part instanceof IDiagramWorkbenchPart && cntxt.getSelection() instanceof IStructuredSelection) {

            /* Check the action id */
            String actionId = cntxt.getActionId();
            if (GlobalActionId.DELETE.equals(actionId)) {
                canHandle = getCommand(cntxt) != null;
            }
        }

        return canHandle;
    }

    @Override
    public ICommand getCommand(IGlobalActionContext cntxt) {
        ICommand deleteCommand = super.getCommand(cntxt);
        if (deleteCommand != null && deleteCommand.canExecute()) {
            TransactionalEditingDomain domain = getEditingDomain(cntxt);
            deleteCommand = new DeleteWrapperHookExecutorCommand(domain, computeSelections(cntxt), deleteCommand);
        }
        return deleteCommand;
    }

    private TransactionalEditingDomain getEditingDomain(IGlobalActionContext cntxt) {
        IWorkbenchPart part = cntxt.getActivePart();
        TransactionalEditingDomain result = null;

        IEditingDomainProvider provider = (IEditingDomainProvider) part.getAdapter(IEditingDomainProvider.class);

        if (provider != null) {
            EditingDomain domain = provider.getEditingDomain();

            if (domain instanceof TransactionalEditingDomain) {
                result = (TransactionalEditingDomain) domain;
            }
        }

        return result;
    }

    private Collection<DSemanticDecorator> computeSelections(IGlobalActionContext globalActionContext) {

        final Collection<DSemanticDecorator> diagramElements = new ArrayList<DSemanticDecorator>();
        ISelection selection = globalActionContext.getSelection();
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
            Iterator<?> iterator = structuredSelection.iterator();
            while (iterator.hasNext()) {
                Object selectedObject = iterator.next();
                if (selectedObject instanceof IGraphicalEditPart) {
                    final IGraphicalEditPart gEditPart = (IGraphicalEditPart) selectedObject;
                    final EObject element = gEditPart.resolveSemanticElement();
                    if (element instanceof DSemanticDecorator) {
                        diagramElements.add((DSemanticDecorator) element);
                    }
                }
            }
        }
        return diagramElements;
    }

}

Back to the top