Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4243ca0162f51793ca9e1d7d38c2c9d693825845 (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
169
170
/*****************************************************************************
 * Copyright (c) 2013, 2014 CEA LIST 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:
 *  Juan Cadavid (CEA LIST) juan.cadavid@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 431109
 *  
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.handlers;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.ui.util.WorkbenchPartHelper;
import org.eclipse.papyrus.uml.service.types.filter.ICommandFilter;
import org.eclipse.papyrus.uml.service.types.filter.UmlElementCommandFilter;
import org.eclipse.papyrus.uml.service.types.utils.ICommandContext;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Abstract handler for commands regarding creation of elements
 *
 * @deprecated since Eclipse Mars. Use AbstractCreateElementCommand instead of this class
 */
@Deprecated
public abstract class AbstractCreateCommandHandler extends AbstractCommandHandler {

	/** Current createCommand for selection (updated in {@link AbstractUmlCreateCommandHandler#isEnabled()}) */
	protected Command createCommand;

	protected CreateElementRequest createRequest;

	protected abstract IElementType getElementTypeToCreate();

	/**
	 * <pre>
	 * 
	 * Build the create command for an element creation in the selected container.
	 * The create command is given by the {@link IElementEditService} of selected 
	 * element.
	 * 
	 * @return the composite creation command for current selection
	 * 
	 * </pre>
	 */
	protected Command buildCommand() {
		ICommandContext commandContext = getCommandContext();
		if(commandContext == null) {
			return UnexecutableCommand.INSTANCE;
		}

		EObject container = commandContext.getContainer();

		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(container);
		if(provider == null) {
			return UnexecutableCommand.INSTANCE;
		}

		ICommand createGMFCommand = provider.getEditCommand(createRequest);
		if(createGMFCommand != null) {
			Command emfCommand = org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper.wrap(createGMFCommand);
			return emfCommand;
		}
		return UnexecutableCommand.INSTANCE;
	}

	/**
	 * 
	 * @return
	 *         the creation request to use in this handler
	 */
	protected CreateElementRequest buildRequest() {
		ICommandContext commandContext = getCommandContext();
		if(commandContext != null) {
			EObject container = commandContext.getContainer();
			EReference reference = commandContext.getReference();
			boolean nullReference = reference == null;
			return nullReference ? new CreateElementRequest(getEditingDomain(), container, getElementTypeToCreate()) : new CreateElementRequest(getEditingDomain(), container, getElementTypeToCreate(), reference);
		}
		return null;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.uml.service.types.handlers.modelexplorer.handler.AbstractCommandHandler#getCommand()
	 * 
	 * @return current command
	 */
	protected Command getCommand() {
		// In case we had one before, dispose it before replacing it
		disposeCommand();
		
		createRequest = buildRequest();
		createCommand = buildCommand();
		return createCommand;
	}

	protected IWorkbenchPart getActiveWorkbenchPart() {
		return WorkbenchPartHelper.getCurrentActiveWorkbenchPart();
	}


	/**
	 * This method is called by the commands service to validate if this particular handler is active.
	 * By default, the creation of UML handlers only verify that the element to be created is allowed by the applied filter (
	 * {@link UmlElementCommandFilter}, ...)
	 * 
	 * @see org.eclipse.papyrus.uml.service.types.handlers.AbstractCommandHandler#setEnabled(java.lang.Object)
	 * 
	 * 
	 * @param evaluationContext
	 */
	public void setEnabled(Object evaluationContext) {
		IElementType newElementType = getElementTypeToCreate();
		boolean isEnabled = getCommandFilter().getVisibleCommands().contains(newElementType);

		if(isEnabled) {
			Command command = getCommand();
			isEnabled = command.canExecute();
		}
		setBaseEnabled(isEnabled);
	}

	/** returns the command filter to use for this handler */
	public abstract ICommandFilter getCommandFilter();
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Object result;
		
		try {
			result = super.execute(event);
		} finally {
			// If execution succeeded, the command will be disposed later by the history.
			// If it failed, the history already disposed it.
			// Either way, we should not dispose it.
			createCommand = null;
			createRequest = null;
		}
		
		return result;
	}
	
	private void disposeCommand() {
		if(createCommand != null) {
			createCommand.dispose();
		}
		createRequest = null;
		createCommand = null;
	}
	
	@Override
	public void dispose() {
		disposeCommand();
		super.dispose();
	}
}

Back to the top