Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9877fe100d29f73992791d2de26c150b09954e5 (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
/*****************************************************************************
 * Copyright (c) 2011 Atos.
 *
 *    
 * 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:
 *  Mathieu Velten (Atos) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.commands;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IOperationHistoryListener;
import org.eclipse.core.commands.operations.OperationHistoryEvent;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.workspace.impl.WorkspaceCommandStackImpl;

public class NotifyingWorkspaceCommandStack extends WorkspaceCommandStackImpl {

	/**
	 * map with registered listeners and the corresponding proxy registered to
	 * actual map
	 */
	private Map<CommandStackListener, IOperationHistoryListener> proxyOperationListeners = new HashMap<CommandStackListener, IOperationHistoryListener>();

	public NotifyingWorkspaceCommandStack(IOperationHistory history) {
		super(history);
	}

	@Override
	public void addCommandStackListener(final CommandStackListener listener) {
		removeCommandStackListener(listener);
		IOperationHistoryListener proxy = new IOperationHistoryListener() {

			public void historyNotification(OperationHistoryEvent event) {
				int type = event.getEventType();

				// emf stack only needs to be notified when an operation is finished
				if(OperationHistoryEvent.DONE == type || OperationHistoryEvent.REDONE == type || OperationHistoryEvent.UNDONE == type) {
					listener.commandStackChanged(new EventObject(this));
				}
			}
		};
		getOperationHistory().addOperationHistoryListener(proxy);
		proxyOperationListeners.put(listener, proxy);
	}

	@Override
	public void removeCommandStackListener(CommandStackListener listener) {
		IOperationHistoryListener proxy = proxyOperationListeners.remove(listener);
		if(proxy != null) {
			getOperationHistory().removeOperationHistoryListener(proxy);
		}
	}
}

Back to the top