Skip to main content
summaryrefslogtreecommitdiffstats
blob: 043758a7e069acdc136093d8b3ca93e9e3d12d8f (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
 * Copyright (c) 2012 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.command.impl;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.compare.command.DelegatingCommandStack;
import org.eclipse.emf.compare.command.ICompareCommandStack;
import org.eclipse.emf.compare.command.ICompareCopyCommand;

/**
 * A simple {@link ICompareCommandStack} that delegate execution to another command stack but keep
 * informations about execution to properly reply to {@link ICompareCommandStack} protocol.
 * <p>
 * This implementation is not robust. If an error occurs during execution of a command, the whole state will
 * be corrupted and the undo/redo may have an unknown behavior.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class CompareCommandStack extends DelegatingCommandStack implements ICompareCommandStack {

	/** The data structure to keep info of command executed on the right side. */
	private final CompareSideCommandStack rightCommandStack;

	/** The data structure to keep info of command executed on the left side. */
	private final CompareSideCommandStack leftCommandStack;

	/** The command to which we delegate to. */
	private final CommandStack delegate;

	/**
	 * Creates a new instance that delegates to the given {@code commandStack}.
	 * 
	 * @param commandStack
	 *            the command stack to which this instance will delegate.
	 */
	public CompareCommandStack(CommandStack commandStack) {
		this.delegate = commandStack;
		this.rightCommandStack = new CompareSideCommandStack();
		this.leftCommandStack = new CompareSideCommandStack();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.DelegatingCommandStack#delegate()
	 */
	@Override
	protected CommandStack delegate() {
		return delegate;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.DelegatingCommandStack#execute(org.eclipse.emf.common.command.Command)
	 */
	@Override
	public void execute(Command command) {
		// should do that AFTER delegate.execute, but in this this case, notifiers will not see change in
		// side lists.
		if (command instanceof ICompareCopyCommand) {
			ICompareCopyCommand compareCommand = (ICompareCopyCommand)command;
			if (compareCommand.isLeftToRight()) {
				rightCommandStack.executed(compareCommand);
			} else {
				leftCommandStack.executed(compareCommand);
			}
		}
		super.execute(command);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.common.command.BasicCommandStack#undo()
	 */
	@Override
	public void undo() {
		// should do that AFTER delegate.execute, but in this this case, notifiers will not see change in
		// side lists.
		if (canUndo()) {
			if (getUndoCommand() instanceof ICompareCopyCommand) {
				ICompareCopyCommand compareCommand = (ICompareCopyCommand)getUndoCommand();
				if (compareCommand.isLeftToRight()) {
					rightCommandStack.undone();
				} else {
					leftCommandStack.undone();
				}
			}
		}
		super.undo();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.common.command.BasicCommandStack#redo()
	 */
	@Override
	public void redo() {
		// should do that AFTER delegate.execute, but in this this case, notifiers will not see change in
		// side lists.
		if (canRedo()) {
			if (getRedoCommand() instanceof ICompareCopyCommand) {
				ICompareCopyCommand compareCommand = (ICompareCopyCommand)getRedoCommand();
				if (compareCommand.isLeftToRight()) {
					rightCommandStack.redone();
				} else {
					leftCommandStack.redone();
				}
			}
		}
		super.redo();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.common.command.CommandStack#flush()
	 */
	@Override
	public void flush() {
		rightCommandStack.flushed();
		leftCommandStack.flushed();
		super.flush();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.ICompareCommandStack#isLeftSaveNeeded()
	 */
	public boolean isLeftSaveNeeded() {
		return leftCommandStack.isSaveNeeded();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.ICompareCommandStack#isRightSaveNeeded()
	 */
	public boolean isRightSaveNeeded() {
		return rightCommandStack.isSaveNeeded();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.ICompareCommandStack#leftSaveIsDone()
	 */
	public void leftSaveIsDone() {
		leftCommandStack.saveIsDone();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.command.ICompareCommandStack#rightSaveIsDone()
	 */
	public void rightSaveIsDone() {
		rightCommandStack.saveIsDone();
	}

	/**
	 * Simple data structure acting like a command stack but without any execution capability. It is used to
	 * record execution of {@link ICompareCopyCommand} on each side.
	 * 
	 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
	 */
	public static class CompareSideCommandStack {

		/**
		 * This will force the {@link #isSaveNeeded()} to return <code>true</code>.
		 */
		private static final int IS_SAVE_NEEDED_WILL_BE_TRUE = -2;

		/**
		 * The list of commands.
		 */
		private final List<ICompareCopyCommand> commandList;

		/**
		 * The current position within the list from which the next execute, undo, or redo, will be performed.
		 */
		private int top;

		/**
		 * The command most recently executed, undone, or redone.
		 */
		private Command mostRecentCommand;

		/**
		 * The value of {@link #top} when {@link #saveIsDone} is called.
		 */
		private int saveIndex = -1;

		/**
		 * Creates a new empty instance.
		 */
		public CompareSideCommandStack() {
			commandList = newArrayList();
			top = -1;
		}

		/**
		 * Record the execution of the given command.
		 * 
		 * @param command
		 *            the command to record.
		 */
		public void executed(ICompareCopyCommand command) {
			// If the command is executable, record it.
			//
			if (command != null) {
				if (command.canExecute()) {
					// Clear the list past the top.
					//
					Iterator<ICompareCopyCommand> commands = commandList.listIterator(top + 1);
					while (commands.hasNext()) {
						commands.next();
						commands.remove();
					}

					// Record the successfully executed command.
					//
					mostRecentCommand = command;
					commandList.add(command);
					++top;

					// This is kind of tricky.
					// If the saveIndex was in the redo part of the command list which has now been wiped
					// out,
					// then we can never reach a point where a save is not necessary, not even if we undo
					// all the way back to the beginning.
					//
					if (saveIndex >= top) {
						// This forces isSaveNeded to always be true.
						//
						saveIndex = IS_SAVE_NEEDED_WILL_BE_TRUE;
					}
				}
			}
		}

		/**
		 * Record that the top of the command list has been undone.
		 */
		public void undone() {
			Command command = commandList.get(top--);
			mostRecentCommand = command;
		}

		/**
		 * Record that the top of the command list has been redone.
		 */
		public void redone() {
			Command command = commandList.get(++top);
			mostRecentCommand = command;
		}

		/**
		 * Disposes all the commands in the stack.
		 */
		public void flushed() {
			// Clear the list.
			//
			Iterator<ICompareCopyCommand> commands = commandList.listIterator();
			while (commands.hasNext()) {
				commands.next();
				commands.remove();
			}
			commandList.clear();
			top = -1;
			saveIndex = -1;
			mostRecentCommand = null;
		}

		/**
		 * Called after a save has been successfully performed.
		 */
		public void saveIsDone() {
			// Remember where we are now.
			//
			saveIndex = top;
		}

		/**
		 * Returns whether the model has changes since {@link #saveIsDone} was call the last.
		 * 
		 * @return whether the model has changes since <code>saveIsDone</code> was call the last.
		 */
		public boolean isSaveNeeded() {
			boolean ret = false;

			if (saveIndex < -1) {
				ret = true;
			}

			if (!ret) {
				if (top > saveIndex) {
					for (int i = top; !ret && i > saveIndex; --i) {
						if (!(commandList.get(i) instanceof AbstractCommand.NonDirtying)) {
							ret = true;
						}
					}
				} else {
					for (int i = saveIndex; !ret && i > top; --i) {
						if (!(commandList.get(i) instanceof AbstractCommand.NonDirtying)) {
							ret = true;
						}
					}
				}
			}

			return ret;
		}

		/**
		 * Returns the command that will be undone if {@link #undo} is called.
		 * 
		 * @return the command that will be undone if {@link #undo} is called.
		 */
		public Command getUndoCommand() {
			final Command undoCommand;
			if (top == -1 || top == commandList.size()) {
				undoCommand = null;
			} else {
				undoCommand = commandList.get(top);
			}
			return undoCommand;
		}

		/**
		 * Returns the command that will be redone if {@link #redo} is called.
		 * 
		 * @return the command that will be redone if {@link #redo} is called.
		 */
		public Command getRedoCommand() {
			final Command redoCommand;
			if (top + 1 >= commandList.size()) {
				redoCommand = null;
			} else {
				redoCommand = commandList.get(top + 1);
			}
			return redoCommand;
		}

		/**
		 * Returns the command most recently executed, undone, or redone.
		 * 
		 * @return the command most recently executed, undone, or redone.
		 */
		public Command getMostRecentCommand() {
			return mostRecentCommand;
		}
	}
}

Back to the top