Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11b7c2ce885158d0d284160ea1187d37af29a9fd (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Atos Origin - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagram.common.groups.core.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.papyrus.diagram.common.groups.core.PendingGroupNotificationsManager;
import org.eclipse.papyrus.ui.toolbox.notification.ICompositeCreator;
import org.eclipse.papyrus.ui.toolbox.notification.NotificationRunnable;
import org.eclipse.papyrus.ui.toolbox.notification.builders.IContext;

/**
 * The CompositeCreator with command is an abstract implementation of ICompositeCreator which take cares of storing a command which evolves with the
 * decisions the user takes in the built notification.
 * 
 * @author vhemery
 */
public abstract class CompositeCreatorWithCommand implements ICompositeCreator {

	/** the commands to execute at notification close */
	private Map<Object, Command> commandsMap = new HashMap<Object, Command>();

	/**
	 * Add a command which will be executed as result of the notification. If a command already exist for that key, it will be replaced.
	 * If cmd is null, any existing command for the given key is removed.
	 * 
	 * @param key
	 *        the object to use as a key for the command (may be null)
	 * @param cmd
	 *        the command to store
	 */
	protected void addResultingCommandForObject(Object key, Command cmd) {
		if(cmd != null) {
			commandsMap.put(key, cmd);
		} else {
			commandsMap.remove(key);
		}
	}

	/**
	 * Get the command to execute as result of the notification
	 * 
	 * @return command to execute
	 */
	public Command getResultingCommand() {
		if(!commandsMap.isEmpty()) {
			return new CompoundCommand(new ArrayList<Command>(commandsMap.values()));
		} else {
			return null;
		}
	}

	/**
	 * Get the runnable action to run the resulting command.
	 * In case of an asynchronous action, a command will be executed.
	 * In case of a synchronous action, the command is not executed yet and must be recovered with {@link #getResultingCommand()}.
	 * 
	 * @param isAsynchronous
	 *        true if the action is asynchronous
	 * 
	 * @return notification runnable
	 */
	public NotificationRunnable getCommandRunner(final boolean isAsynchronous) {
		return new NotificationRunnable() {

			/**
			 * Run the stored resulting commands
			 * 
			 * @see org.eclipse.papyrus.ui.toolbox.notification.NotificationRunnable#run(org.eclipse.papyrus.ui.toolbox.notification.builders.IContext)
			 * @param context
			 *        running context
			 */
			public void run(IContext context) {
				if(isAsynchronous) {
					Command cmd = getResultingCommand();
					if(cmd != null && cmd.canExecute()) {
						cmd.execute();
					}
				}
				removeNotification();
			}

			/**
			 * Get the action label
			 * 
			 * @see org.eclipse.papyrus.ui.toolbox.notification.NotificationRunnable#getLabel()
			 * @return stirng label
			 */
			public String getLabel() {
				return getRunLabel();
			}
		};
	}

	/**
	 * Remove the notification using {@link PendingGroupNotificationsManager} and the appropriate key
	 */
	abstract protected void removeNotification();

	/**
	 * Get the run action label
	 * 
	 * @return string label
	 */
	abstract protected String getRunLabel();

}

Back to the top