Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 75a41923ba35d011ab875e8963e29947adc0e3f7 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*****************************************************************************
 * Copyright (c) 2013, 2016 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - adapted for self-nesting behaviour
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.commands;

import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.papyrus.infra.emf.gmf.command.NotifyingWorkspaceCommandStack;

/**
 * @deprecated Use the {@link org.eclipse.papyrus.infra.emf.gmf.command.NestingNotifyingWorkspaceCommandStack} API, instead.
 */
@Deprecated
public class NestingNotifyingWorkspaceCommandStack extends NotifyingWorkspaceCommandStack {

	private NestingNotifyingWorkspaceCommandStack childCommandStack;

	private final boolean nested;

	private boolean executing;

	protected IUndoContext defaultUndoContext;

	public NestingNotifyingWorkspaceCommandStack(IOperationHistory history) {
		this(false, history, null);
	}

	protected NestingNotifyingWorkspaceCommandStack(boolean nested, IOperationHistory history, IUndoContext defaultUndoContext) {
		super(history);
		this.nested = nested;
		this.defaultUndoContext = defaultUndoContext;
	}

	protected NestingNotifyingWorkspaceCommandStack(boolean nested, IOperationHistory history) {
		this(nested, history, computeNestedUndoContext());
	}

	private static IUndoContext computeNestedUndoContext() {
		return new IUndoContext() {

			@Override
			public boolean matches(IUndoContext context) {
				return context == this;
			}

			@Override
			public String getLabel() {
				return "Nested Undo Context";
			}
		};
	}

	@Override
	public IUndoContext getDefaultUndoContext() {
		if (defaultUndoContext == null) {
			return super.getDefaultUndoContext();
		}
		return defaultUndoContext;
	}

	protected NestingNotifyingWorkspaceCommandStack getTopMostCommandStack() {
		if (childCommandStack == null) {
			return this;
		}
		return childCommandStack.getTopMostCommandStack();
	}

	protected void startNestedTransaction(Command command) {
		if (childCommandStack != null) {
			// Forwards to the current stack
			childCommandStack.startNestedTransaction(command);
		} else {
			// Start a new nested transaction in a new nested Stack
			childCommandStack = createNestedCommandStack(getOperationHistory());
			childCommandStack.setEditingDomain(getDomain());

			childCommandStack.execute(command);
		}
	}

	protected NestingNotifyingWorkspaceCommandStack createNestedCommandStack(IOperationHistory history) {
		return new NestingNotifyingWorkspaceCommandStack(true, history);
	}

	public void commit() {
		if (childCommandStack != null) {
			disposeLastCommandStack();
		}
	}

	private boolean disposeLastCommandStack() {
		if (childCommandStack == null) {
			// I'm the last command stack
			dispose();
			return true;
		}

		// Propagates
		if (childCommandStack.disposeLastCommandStack()) {
			childCommandStack = null;
		}

		return false;
	}

	public void rollback() {
		if (childCommandStack != null) {
			while (canUndo()) {
				undo();
			}
			disposeLastCommandStack();
		}
	}

	@Override
	public void execute(Command command) {
		if (childCommandStack == null) {
			if (!executing) {
				executing = true;

				try {
					super.execute(command);
				} finally {
					executing = false;
				}
			} else {
				// Re-entrant command execution goes on a nested stack
				try {
					startNestedTransaction(command);
					commit();
				} catch (OperationCanceledException e) {
					rollback();
					// Propagate
					throw e;
				}
			}
		} else {
			childCommandStack.execute(command);
		}
	}

	@Override
	protected void handleError(Exception exception) {
		if (nested && (exception instanceof RollbackException)) {
			// A nested transaction rolled back
			RollbackException rbe = (RollbackException) exception;
			if (rbe.getStatus().getSeverity() == IStatus.CANCEL) {
				// Propagate
				throw new OperationCanceledException();
			}
		}

		if (exception instanceof OperationCanceledException) {
			rollback();
		} else {
			super.handleError(exception);
		}
	}

	@Override
	public Command getMostRecentCommand() {
		if (childCommandStack == null) {
			return super.getMostRecentCommand();
		} else {
			return childCommandStack.getMostRecentCommand();
		}
	}

	@Override
	public Command getRedoCommand() {
		if (childCommandStack == null) {
			return super.getRedoCommand();
		} else {
			return childCommandStack.getRedoCommand();
		}
	}

	@Override
	public Command getUndoCommand() {
		if (childCommandStack == null) {
			return super.getUndoCommand();
		} else {
			return childCommandStack.getUndoCommand();
		}
	}

	@Override
	public void undo() {
		if (childCommandStack == null) {
			super.undo();
		} else {
			childCommandStack.undo();
		}
	}

	@Override
	public boolean canUndo() {
		if (childCommandStack == null) {
			return super.canUndo();
		} else {
			return childCommandStack.canUndo();
		}
	}

	@Override
	public boolean canRedo() {
		if (childCommandStack == null) {
			return super.canRedo();
		} else {
			return childCommandStack.canRedo();
		}
	}

	@Override
	public void redo() {
		if (childCommandStack == null) {
			super.redo();
		} else {
			childCommandStack.redo();
		}
	}

}

Back to the top