Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b968376d08a934c36d98586a950a86bf03a229ed (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
/*****************************************************************************
 * Copyright (c) 2014 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:
 *   Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation
 *   Christian W. Damus - bug 450536
 *   
 *****************************************************************************/

package org.eclipse.papyrus.views.modelexplorer.actionprovider;

import java.util.EventObject;

import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.navigator.CommonNavigator;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.eclipse.ui.navigator.ICommonViewerSite;
import org.eclipse.ui.navigator.ICommonViewerWorkbenchSite;
import org.eclipse.ui.operations.RedoActionHandler;
import org.eclipse.ui.operations.UndoActionHandler;

/**
 * Action Provider to provide Redo/Undo action trough workbench menu.
 * 
 * @author Gabriel Pascual
 *
 */
public class UndoRedoActionProvider extends AbstractCommonActionProvider implements CommandStackListener {


	/** The undo action. */
	private UndoActionHandler undoAction = null;

	/** The redo action. */
	private RedoActionHandler redoAction = null;

	/** Listened command stack. */
	private CommandStack commandStack;


	/**
	 * Instantiates a new undo redo action provider.
	 */
	public UndoRedoActionProvider() {
		super();
	}

	/**
	 * Gets the command stack.
	 *
	 * @return the command stack
	 */
	private CommandStack getCommandStack() {

		if (commandStack == null) {
			CommonNavigator navigator = getCommonNavigator();

			if (navigator instanceof IEditingDomainProvider) {
				EditingDomain editingDomain = ((IEditingDomainProvider) navigator).getEditingDomain();
				commandStack = editingDomain.getCommandStack();
			}

		}

		return commandStack;
	}

	@Override
	public void init(ICommonActionExtensionSite aSite) {
		super.init(aSite);

		ICommonViewerSite viewSite = aSite.getViewSite();
		if (viewSite instanceof ICommonViewerWorkbenchSite) {
			IWorkbenchPartSite partSite = ((ICommonViewerWorkbenchSite) viewSite).getSite();
			undoAction = new UndoActionHandler(partSite, getUndoContext());
			redoAction = new RedoActionHandler(partSite, getUndoContext());
		}

		getCommandStack().addCommandStackListener(this);

	}

	/**
	 * Gets the undo context.
	 *
	 * @return the undo context
	 */
	private IUndoContext getUndoContext() {
		IUndoContext context = null;

		CommonNavigator navigator = getCommonNavigator();
		if (navigator instanceof IAdaptable) {

			// Get undo context from navigator
			context = (IUndoContext) navigator.getAdapter(IUndoContext.class);
		}

		return context;
	}

	@Override
	public void fillActionBars(IActionBars actionBars) {

		actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
		actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoAction);
	}

	/**
	 * <p>
	 * Synchronise handlers after a Command stack's change.
	 * </p>
	 * 
	 * @see org.eclipse.emf.common.command.CommandStackListener#commandStackChanged(java.util.EventObject)
	 *
	 * @param event
	 */
	public void commandStackChanged(EventObject event) {

		// Refresh Undo handler after Command stack state has changed
		if (undoAction != null) {
			undoAction.update();
		}

		// Refresh Redo handler after Command stack state has changed
		if (redoAction != null) {
			redoAction.update();
		}

	}

	@Override
	public void dispose() {

		// Detach listener of command stack
		getCommandStack().removeCommandStackListener(this);
		commandStack = null;

		if (undoAction != null) {
			undoAction.dispose();
			undoAction = null;
		}
		if (redoAction != null) {
			redoAction.dispose();
			redoAction = null;
		}

		super.dispose();
	}
}

Back to the top