Skip to main content
summaryrefslogtreecommitdiffstats
blob: b347ecf0546f07df678593de595ab167ce1a1f5f (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 *                                                                            
 *  Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) 
 *                                                                            
 *  All rights reserved. This program and the accompanying materials           
 *  are made available under the terms of the Eclipse Public License 2.0        
 *  which accompanies this distribution, and is available at                  
 *  https://www.eclipse.org/legal/epl-2.0/                                 
 *                                 
 *  SPDX-License-Identifier: EPL-2.0                                 
 *                                                                            
 *  Contributors:                                                      
 * 	   Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
 * 
 */
package org.eclipse.osbp.vaadin.emf.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.command.CreateChildCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider;
import org.eclipse.osbp.vaadin.emf.api.IModelingContext;
import org.eclipse.osbp.vaadin.emf.data.EmfModelTreeContainer;

import com.vaadin.event.Action;
import com.vaadin.server.Resource;
import com.vaadin.ui.AbstractSelect;

/**
 * A Vaadin ActionHandler that is aware about all containment references that
 * can be created for a single EObject.
 */
@SuppressWarnings("serial")
public class EObjectActionHandler implements Action.Handler {

	private EditCallback callback;
	private IModelingContext modelingContext;
	private IResourceProvider resourceProvider;

	public EObjectActionHandler(IModelingContext modelingContext,
			IResourceProvider resourceProvider, EditCallback callback) {
		this.modelingContext = modelingContext;
		this.resourceProvider = resourceProvider;
		this.callback = callback;
	}

	/**
	 * Gets the list of actions applicable to this handler.
	 * 
	 * @param target
	 *            the target handler to list actions for. For item containers
	 *            this is the item id.
	 * @param sender
	 *            the party that would be sending the actions. Most of this is
	 *            the action container.
	 * @return the list of Action
	 */
	@SuppressWarnings("unchecked")
	public Action[] getActions(Object target, Object sender) {

		List<CustomAction> result = new ArrayList<>();
		if (target != null) {
			CustomAction editAction = new CustomAction(Commands.EDIT_DIALOG,
					"Edit", null, null);
			editAction.modelElement = (EObject) target;
			result.add(editAction);

			result.add(new CustomAction(Commands.REFRESH_NODE, "Refresh", null,
					null));

			if (canDelete(target)) {
				result.add(new CustomAction(Commands.DELETE, "Delete", null,
						null));
			}

			if (modelingContext.getCommandStack().canUndo()) {
				result.add(new CustomAction(Commands.UNDO, "Undo", null, null));
			}

			if (modelingContext.getCommandStack().canRedo()) {
				result.add(new CustomAction(Commands.REDO, "Redo", null, null));
			}
		}

		return result.toArray(new Action[result.size()]);
	}

	private boolean canDelete(Object target) {
		Command command = RemoveCommand.create(
				modelingContext.getEditingDomain(), target);
		return command.canExecute();
	}

	/**
	 * Handles an action for the given target. The handler method may just
	 * discard the action if it's not suitable.
	 * 
	 * @param action
	 *            the action to be handled.
	 * @param sender
	 *            the sender of the action. This is most often the action
	 *            container.
	 * @param target
	 *            the target of the action. For item containers this is the item
	 *            id.
	 */
	public void handleAction(Action action, Object sender, Object target) {
		CustomAction customAction = (CustomAction) action;

		EditingDomain domain = modelingContext.getEditingDomain();
		if (customAction.getCommand() == Commands.CREATE_CHILD) {
			CommandParameter executeParam = new CommandParameter(target,
					customAction.getParam().getFeature(),
					customAction.getParam());
			Command command = domain.createCommand(CreateChildCommand.class,
					executeParam);
			domain.getCommandStack().execute(command);
		} else if (customAction.getCommand() == Commands.DELETE) {
			domain.getCommandStack().execute(
					RemoveCommand.create(domain, target));
		} else if (customAction.getCommand() == Commands.UNDO) {
			domain.getCommandStack().undo();
		} else if (customAction.getCommand() == Commands.REDO) {
			domain.getCommandStack().redo();
		} else if (customAction.getCommand() == Commands.REFRESH_NODE) {
			AbstractSelect select = (AbstractSelect) sender;
			EmfModelTreeContainer container = (EmfModelTreeContainer) select
					.getContainerDataSource();
			container.refreshNode(target);
		} else if (customAction.getCommand() == Commands.EDIT_DIALOG) {
			callback.edit(customAction.modelElement);
		}
	}

	/**
	 * Custom action wraps CommandParameter.
	 */
	private static class CustomAction extends Action {

		private final Commands command;
		private final CommandParameter param;
		private final EStructuralFeature feature;
		private EObject modelElement;

		public CustomAction(Commands command, String caption, Resource icon,
				CommandParameter param) {
			super(caption, icon);
			this.command = command;
			this.param = param;
			this.feature = null;
		}

		public CustomAction(Commands command, String caption, Resource icon,
				CommandParameter param, EStructuralFeature feature) {
			super(caption, icon);
			this.command = command;
			this.param = param;
			this.feature = feature;
		}

		public CustomAction(Commands command, String caption, Resource icon) {
			super(caption, icon);
			this.command = command;
			this.param = null;
			this.feature = null;
		}

		public CommandParameter getParam() {
			return param;
		}

		public Commands getCommand() {
			return command;
		}

		public EStructuralFeature getFeature() {
			return feature;
		}

	}

	enum Commands {
		EDIT_DIALOG, CREATE_CHILD, DELETE, UNDO, REDO, COPY, PASTE, REFRESH_NODE
	}

	public interface EditCallback {
		void edit(EObject model);
	}
}

Back to the top