Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b15c532678a4ac7f7b38f5da3808989eb878761 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*****************************************************************************
 * Copyright (c) 2010, 2016 CEA LIST, Christian W. Damus, and others.
 *
 * 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:
 * 		Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 * 		Benoit Maggi (CEA LIST) benoit.maggi@cea.fr _ Bug 436952
 * 		Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec - Bug 436952
 *      Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.modelexplorer.handler;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.IHandler;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;

/**
 * Default handler for Delete command used in the ModelExplorer contextual menu.
 *
 */
public class DeleteCommandHandler extends AbstractCommandHandler implements IHandler {


	/**
	 * Check if the selection allow delete
	 *
	 * @param selectedElements
	 * @return
	 */
	public static boolean isDeleteEnabled(Collection<EObject> selectedElements) {
		if (selectedElements.size() == 0) {
			return false;
		}

		for (EObject current : selectedElements) {
			if (EMFHelper.isReadOnly(current)) {
				return false;
			}
			// the root of the model can't be deleted!
			if (current.eContainer() == null) {
				// Pages can be deleted even when they are root elements
				if (isPage(current)) {
					return true;
				}
				return false;
			}
		}

		// Don't compute the delete command to know if it is enabled,
		// it can be WAY too slow...
		return true;
	}

	/**
	 * Return if the parameter is page.
	 *
	 * @param current
	 * @return
	 */
	protected static boolean isPage(EObject current) {
		try {
			IPageManager pageManager = ServiceUtilsForEObject.getInstance().getService(IPageManager.class, current);
			if (pageManager.allPages().contains(current)) {
				return true;
			}
		} catch (ServiceException ex) {
			// Cannot retrieve the ServicesRegistry: ignore
		}
		return false;
	}

	/**
	 * <pre>
	 *
	 * Build the delete command for a set of EObject selected in the ModelExplorer.
	 * The delete command is given by the {@link IElementEditService} of selected
	 * elements.
	 * &#64;param selectedElements elements to delete
	 * &#64;return the composite deletion command for current selection
	 *
	 * &#64;TODO : Manage possible Diagrams listed in the selection
	 *
	 * </pre>
	 */
	public static Command buildDeleteCommand(Collection<EObject> selectedElements) {

		ICommand gmfCommand = null;

		// Parameters store the Request parameters of the previous request
		// if multiple elements are selected for deletion.
		Map parameters = new HashMap();

		for (EObject selectedEObject : selectedElements) {

			if (selectedEObject == null) {
				continue;
			}

			IElementEditService provider = ElementEditServiceUtils.getCommandProvider(selectedEObject);
			if (provider == null) {
				continue;
			}


			// Retrieve delete command from the Element Edit service
			DestroyElementRequest request = new DestroyElementRequest(selectedEObject, false);
			// Add parameters (contains the list of previously dependents to be deleted
			// by previous commands.
			request.getParameters().putAll(parameters);

			ICommand deleteCommand = provider.getEditCommand(request);

			// Add current EObject destroy command to the global command
			gmfCommand = CompositeCommand.compose(gmfCommand, deleteCommand);

			// Store the new parameters for next delete command.
			parameters.clear();
			parameters.putAll(request.getParameters());
		}

		if (gmfCommand == null) {
			return UnexecutableCommand.INSTANCE;
		}

		return GMFtoEMFCommandWrapper.wrap(gmfCommand.reduce());
	}

	/**
	 *
	 * @see org.eclipse.papyrus.views.modelexplorer.handler.AbstractCommandHandler#getCommand()
	 *
	 * @return current command (only built here when the stored command is null)
	 */
	@Override
	protected Command getCommand() {
		// Don't cache the command, as it is no more refreshed by isEnabled().
		return buildDeleteCommand(getSelectedElements());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean computeEnabled() {
		return isDeleteEnabled(getSelectedElements());
	}

}

Back to the top