Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0da082a63b6dc3df64b2c1bcafd1587195534b9b (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
/*****************************************************************************
 * Copyright (c) 2015, 2016 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.commands.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.ICompositeCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper;
import org.eclipse.papyrus.commands.wrappers.GMFtoGEFCommandWrapper;
import org.eclipse.papyrus.commands.wrappers.OperationToGEFCommandWrapper;
import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;

/**
 * An iterator over the tree structure of EMF, GEF, and GMF commands that returns leaf commands of one or all of these kinds,
 * with accounting for the various kinds of wrappers employed to intermix them. This iterator does not support the
 * optional {@link Iterator#remove()} operation.
 * 
 * @deprecated Use the {@link org.eclipse.papyrus.infra.emf.gmf.util.CommandTreeIterator} API, instead.
 */
@Deprecated
public class CommandTreeIterator<C> implements Iterator<C> {
	private final Class<C> type;

	private Iterator<?> current;
	private List<Iterator<?>> iterators = new ArrayList<Iterator<?>>();

	private C preparedNext;

	private CommandTreeIterator(Object root, Class<C> type) {
		super();

		this.type = type;

		root = unwrap(root);

		if (isCompound(root)) {
			pushIterator(root);
		} else {
			prepareNext(root);
		}
	}

	public static CommandTreeIterator<Command> iterateEMF(Object command) {
		return iterate(command, Command.class);
	}

	public static CommandTreeIterator<org.eclipse.gef.commands.Command> iterateGEF(Object command) {
		return iterate(command, org.eclipse.gef.commands.Command.class);
	}

	public static CommandTreeIterator<ICommand> iterateGMF(Object command) {
		return iterate(command, ICommand.class);
	}

	public static CommandTreeIterator<?> iterate(Object command) {
		return iterate(command, Object.class);
	}

	public static <C> CommandTreeIterator<C> iterate(Object command, Class<C> leafCommandType) {
		return new CommandTreeIterator<C>(command, leafCommandType);
	}

	private boolean prepareNext(Object command) {
		if (type.isInstance(command)) {
			preparedNext = type.cast(command);
		}

		return preparedNext != null;
	}

	private Iterator<?> pushIterator(Object compoundCommand) {
		if (current != null) {
			iterators.add(current);
		}
		current = iterator(compoundCommand);
		return current;
	}

	private Iterator<?> popIterator() {
		if (iterators.isEmpty()) {
			current = null;
		} else {
			current = iterators.remove(iterators.size() - 1);
		}

		return current;
	}

	private Object internalNext() {
		Object result = null;

		while ((result == null) && (current != null)) {
			if (current.hasNext()) {
				Object next = unwrap(current.next());
				if (isCompound(next)) {
					// Dive into it
					pushIterator(next);
				} else {
					// We have the next leaf
					result = next;
				}
			} else {
				popIterator();
			}
		}

		return result;
	}

	boolean isDone() {
		return (current == null) && iterators.isEmpty();
	}

	@Override
	public boolean hasNext() {
		while (!isDone() && (preparedNext == null)) {
			Object next = internalNext();
			if (type.isInstance(next)) {
				preparedNext = type.cast(next);
			}
		}

		return preparedNext != null;
	}

	@Override
	public C next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}

		C result = preparedNext;
		preparedNext = null;
		return result;
	}

	/**
	 * Remove is not supported.
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException("remove"); //$NON-NLS-1$
	}

	private Object unwrap(Object command) {
		Object result = command;

		if (command instanceof ICommandProxy) {
			result = ((ICommandProxy) command).getICommand();
		} else if (command instanceof CommandProxy) {
			result = ((CommandProxy) command).getCommand();
		} else if (result instanceof EMFtoGEFCommandWrapper) {
			result = ((EMFtoGEFCommandWrapper) result).getEMFCommand();
		} else if (result instanceof EMFtoGMFCommandWrapper) {
			result = ((EMFtoGMFCommandWrapper) command).getEMFCommand();
		} else if (result instanceof GEFtoEMFCommandWrapper) {
			result = ((GEFtoEMFCommandWrapper) command).getGEFCommand();
		} else if (result instanceof GMFtoEMFCommandWrapper) {
			result = ((GMFtoEMFCommandWrapper) command).getGMFCommand();
		} else if (result instanceof GMFtoGEFCommandWrapper) {
			result = ((GMFtoGEFCommandWrapper) command).getGMFCommand();
		} else if (result instanceof OperationToGEFCommandWrapper) {
			result = ((OperationToGEFCommandWrapper) command).getOperation();
		}

		if (result != command) {
			// Could be turtles all the way down
			result = unwrap(result);
		}

		return result;
	}

	private boolean isCompound(Object command) {
		return ((command instanceof CompoundCommand) || (command instanceof org.eclipse.gef.commands.CompoundCommand) || (command instanceof ICompositeCommand));
	}

	private Iterator<?> iterator(Object compoundCommand) {
		if (compoundCommand instanceof CompoundCommand) {
			return ((CompoundCommand) compoundCommand).getCommandList().iterator();
		} else if (compoundCommand instanceof org.eclipse.gef.commands.CompoundCommand) {
			return ((org.eclipse.gef.commands.CompoundCommand) compoundCommand).getCommands().iterator();
		} else if (compoundCommand instanceof ICompositeCommand) {
			return ((ICompositeCommand) compoundCommand).iterator();
		} else {
			return Collections.emptyList().iterator();
		}
	}
}

Back to the top