Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33a1691b118bac1848ee9817d29df30c353d384c (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) 2007, 2010 BMW Car IT, Technische Universitaet Muenchen, 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:
 * BMW Car IT - Initial API and implementation
 * Technische Universitaet Muenchen - Major refactoring and extension
 *******************************************************************************/
package org.eclipse.emf.edapt.history.recorder;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.Stack;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edapt.internal.common.LoggingUtils;
import org.eclipse.emf.edapt.internal.common.MetamodelExtent;
import org.eclipse.emf.edapt.internal.common.MetamodelUtils;
import org.eclipse.emf.edapt.spi.history.Change;
import org.eclipse.emf.edapt.spi.history.CompositeChange;
import org.eclipse.emf.edapt.spi.history.History;
import org.eclipse.emf.edapt.spi.history.provider.HistoryEditPlugin;

/**
 * Listens to command stack of an Ecore editor and collects changes to current
 * metamodel release.
 *
 * @author herrmama
 * @author $Author$
 * @version $Rev$
 * @levd.rating RED Rev:
 */
public class CommandStackListener implements
	org.eclipse.emf.common.command.CommandStackListener {

	/** Command stack of Ecore editor. */
	private final CommandStack commandStack;

	/**
	 * EMF change recorder to record metamodel changes during the execution of a
	 * command.
	 */
	private ChangeRecorder metamodelRecorder;

	/**
	 * EMF change recorder to record history changes during the execution of a
	 * command (to be immediately undone so that the history is not affected by
	 * a delete command).
	 */
	private HistoryChangeRecorder historyRecorder;

	/** Flag to indicate whether listener is active or not. */
	private boolean listening;

	/**
	 * Stack saving the number of changes which need to be removed in case of
	 * undoing a command.
	 */
	private final Stack<Integer> numberChanges;

	/** Metamodel extent. */
	private MetamodelExtent extent;

	/** The history resource. */
	private final Resource historyResource;

	/** Constructor. */
	public CommandStackListener(CommandStack commandStack,
		Resource historyResource) {
		this.commandStack = commandStack;
		numberChanges = new Stack<Integer>();
		listening = false;
		this.historyResource = historyResource;
	}

	/** Activate listener. */
	public void beginListening() {
		if (!isListening()) {
			commandStack.addCommandStackListener(this);
			extent = new MetamodelExtent(MetamodelUtils
				.getAllRootPackages(historyResource.getResourceSet()));

			metamodelRecorder = new ChangeRecorder(getHistoryRootPackages());
			historyRecorder = new HistoryChangeRecorder(getHistory());

			listening = true;
		} else {
			throw new IllegalStateException("Listener already activated"); //$NON-NLS-1$
		}
	}

	/** Deactivate listener. */
	public void endListening() {
		if (isListening()) {
			commandStack.removeCommandStackListener(this);
			commandStack.flush();
			numberChanges.clear();
			extent.dispose();

			metamodelRecorder.endRecording();
			historyRecorder.endRecording();

			listening = false;
		} else {
			throw new IllegalStateException("Listener already deactivated"); //$NON-NLS-1$
		}
	}

	/** Returns listening. */
	public boolean isListening() {
		return listening;
	}

	/** Reset the recorder if it no longer works on the real metamodel. */
	public void resetRecorder() {
		if (!checkRecorder()) {
			metamodelRecorder.endRecording();
			historyRecorder.endRecording();

			metamodelRecorder = new ChangeRecorder(getHistoryRootPackages());
			EcoreUtil.resolveAll(historyResource);
			historyRecorder = new HistoryChangeRecorder(getHistory());

			extent.setRootPackages(MetamodelUtils
				.getAllRootPackages(historyResource.getResourceSet()));

			LoggingUtils.logInfo(HistoryEditPlugin.getPlugin(),
				"Recorder got out of sync and was safely restarted."); //$NON-NLS-1$
		}
	}

	/**
	 * Check whether the recorder still operates on the real metamodel, and is
	 * not detached due to the save mechanism which reloads the metamodel.
	 */
	private boolean checkRecorder() {
		if (metamodelRecorder == null || metamodelRecorder.getElements().isEmpty()) {
			return false;
		}
		return !metamodelRecorder.getElements().get(0).eIsProxy();
	}

	/** {@inheritDoc} */
	@Override
	public void commandStackChanged(EventObject event) {
		final Command command = commandStack.getMostRecentCommand();
		process(command);
		extent.clearExtentMap();
	}

	/** Process a command. */
	@SuppressWarnings("unchecked")
	private void process(Command command) {

		metamodelRecorder.endRecording();
		historyRecorder.endRecording();

		if (!checkRecorder()) {
			LoggingUtils.logError(HistoryEditPlugin.getPlugin(),
				"Recorder no longer working"); //$NON-NLS-1$
		}

		if (command != null) {
			// command is undone
			if (isUndo(command)) {
				pop();
			} else {
				final CompositeChange changeContainer = metamodelRecorder
					.getChanges();
				// command able to provide a representation of the change
				if (command instanceof IChangeProvider) {
					final IChangeProvider changeProvider = (IChangeProvider) command;
					final List<Change> changes = changeProvider
						.getChanges(changeContainer.getChanges());
					push(changes);
				}
				// command not able to provide a representation of the
				// change
				else {
					push((List) changeContainer.getChanges());
				}
			}
		}

		metamodelRecorder = new ChangeRecorder(getHistoryRootPackages());
		historyRecorder = new HistoryChangeRecorder(getHistory());
	}

	/** Check whether a change of the command stack reflects an undone command. */
	private boolean isUndo(Command command) {
		return command == commandStack.getRedoCommand();
	}

	/** Remove last sequence of changes in order to cope with an undone command. */
	private void pop() {
		final int number = numberChanges.pop();
		if (number == 0) {
			return;
		}
		final List<Change> changes = getHistory().getLastRelease().getChanges();
		final List<Change> changesToRemove = new ArrayList<Change>();
		final int size = changes.size();
		for (int i = size - number; i < size; i++) {
			changesToRemove.add(changes.get(i));
		}
		changes.removeAll(changesToRemove);
	}

	/** Append changes grouped by metamodels to the current releases. */
	private void push(List<Change> changes) {
		numberChanges.push(changes.size());
		getHistory().getLastRelease().getChanges().addAll(changes);
	}

	/** Get the history that is listened to. */
	public History getHistory() {
		final History history = (History) historyResource.getContents().get(0);
		return history;
	}

	/** Get all root packages which are listened to. */
	public List<EPackage> getHistoryRootPackages() {
		return getHistory().getRootPackages();
	}

	/** Returns extent. */
	MetamodelExtent getExtent() {
		return extent;
	}
}

Back to the top