Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28bc3af1348b198410198565cb1720ca6d55f138 (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
/*******************************************************************************
 * Copyright (c) 2013 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.ide.ui.internal.structuremergeviewer;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.UnmodifiableIterator;

import java.util.List;
import java.util.ListIterator;

import org.eclipse.compare.INavigatable;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.ide.ui.internal.contentmergeviewer.util.DynamicObject;
import org.eclipse.emf.compare.ide.ui.internal.util.JFaceUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.tree.TreeNode;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;

/**
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public class Navigatable implements INavigatable {

	/**
	 * A predicate that checks if the given input is a TreeNode that contains a diff.
	 * 
	 * @return true, if the given input is a TreeNode that contains a diff, false otherwise.
	 */
	private static final Predicate<EObject> IS_DIFF_TREE_NODE = new Predicate<EObject>() {
		public boolean apply(EObject t) {
			return isDiffTreeNode(t);
		}
	};

	private final AdapterFactory adapterFactory;

	private final StructuredViewer viewer;

	private final DynamicObject dynamicObject;

	/**
	 * @param adapterFactory
	 */
	public Navigatable(AdapterFactory adapterFactory, StructuredViewer viewer) {
		this.adapterFactory = adapterFactory;
		this.viewer = viewer;
		this.dynamicObject = new DynamicObject(viewer);
	}

	public boolean selectChange(int flag) {
		TreeNode nextOrPrev = null;
		Object firstElement = ((IStructuredSelection)viewer.getSelection()).getFirstElement();
		if (firstElement instanceof Adapter) {
			Notifier target = ((Adapter)firstElement).getTarget();
			if (target instanceof TreeNode) {
				TreeNode treeNode = (TreeNode)target;
				if (flag == INavigatable.NEXT_CHANGE) {
					nextOrPrev = getNextDiffNode(treeNode);
				} else if (flag == INavigatable.PREVIOUS_CHANGE) {
					nextOrPrev = getPrevDiffNode(treeNode);
				} else if (flag == INavigatable.FIRST_CHANGE || flag == INavigatable.LAST_CHANGE) {
					return true;
				}
				if (nextOrPrev != null) {
					StructuredSelection newSelection = new StructuredSelection(adapterFactory.adapt(
							nextOrPrev, ICompareInput.class));
					viewer.setSelection(newSelection);
					dynamicObject
							.invoke("fireOpen", new Class<?>[] {OpenEvent.class, }, new OpenEvent(viewer, newSelection)); //$NON-NLS-1$
				}
			}
		}
		return nextOrPrev == null;
	}

	public Object getInput() {
		return viewer.getInput();
	}

	public boolean openSelectedChange() {
		return true;
	}

	public boolean hasChange(int changeFlag) {
		if (changeFlag == INavigatable.NEXT_CHANGE || changeFlag == INavigatable.PREVIOUS_CHANGE) {
			return true;
		}
		return false;
	}

	/**
	 * Returns, from the given TreeNode, the previous TreeNode that contains a diff.
	 * 
	 * @param treeNode
	 *            the given TreeNode for which we want to find the previous.
	 * @return the previous TreeNode that contains a diff.
	 */
	private TreeNode getPrevDiffNode(TreeNode treeNode) {
		TreeNode previousNode = null;
		TreeNode parentNode = treeNode.getParent();
		if (parentNode != null) {
			List<TreeNode> children = getSortedTreeNodeChildren(
					adapterFactory.adapt(parentNode, ICompareInput.class)).reverse();
			int indexOfTreeNode = children.indexOf(treeNode);
			if (indexOfTreeNode == children.size() - 1) {
				if (isDiffTreeNode(parentNode)) {
					previousNode = parentNode;
				} else {
					previousNode = getPrevDiffNode(parentNode);
				}
			} else {
				boolean stop = false;
				while (!stop) {
					if (children.size() > indexOfTreeNode + 1) {
						TreeNode prevSibling = children.get(indexOfTreeNode + 1);
						previousNode = getLastChildDiffNode(prevSibling);
						if (previousNode != null) {
							stop = true;
						} else if (isDiffTreeNode(prevSibling)) {
							previousNode = prevSibling;
							stop = true;
						}
						indexOfTreeNode++;
					} else {
						previousNode = getPrevDiffNode(parentNode);
						stop = true;
					}
				}
			}
		}
		return previousNode;
	}

	/**
	 * Returns, from the given TreeNode, the next TreeNode that contains a diff.
	 * 
	 * @param treeNode
	 *            the given TreeNode for which we want to find the next.
	 * @return the next TreeNode that contains a diff.
	 */
	private TreeNode getNextDiffNode(TreeNode treeNode) {
		TreeNode next = getFirstChildDiffNode(treeNode);
		if (next == null) {
			TreeNode currentNode = treeNode;
			boolean stop = false;
			while (!stop && currentNode != null) {
				next = getNextSiblingDiffNode(currentNode);
				if (next == null) {
					currentNode = currentNode.getParent();
				} else {
					stop = true;
				}
			}
		}
		return next;
	}

	/**
	 * Returns, from the given TreeNode, the next sibling TreeNode that contains a diff.
	 * 
	 * @param treeNode
	 *            the given TreeNode for which we want to find the next sibling.
	 * @return the next sibling TreeNode that contains a diff.
	 */
	private TreeNode getNextSiblingDiffNode(TreeNode treeNode) {
		TreeNode next = null;
		TreeNode parent = treeNode.getParent();
		if (parent != null) {
			List<TreeNode> children = getSortedTreeNodeChildren(adapterFactory.adapt(parent,
					ICompareInput.class));
			int indexOfTreeNode = children.indexOf(treeNode);
			boolean stop = false;
			while (!stop) {
				if (children.size() > indexOfTreeNode + 1) {
					TreeNode nextSibling = children.get(indexOfTreeNode + 1);
					if (isDiffTreeNode(nextSibling)) {
						next = nextSibling;
					} else {
						next = getFirstChildDiffNode(nextSibling);
					}
					if (next != null) {
						stop = true;
					}
					indexOfTreeNode++;
				} else {
					stop = true;
				}
			}
		}
		return next;
	}

	/**
	 * Returns, from the given TreeNode, the first TreeNode child that contains a diff.
	 * 
	 * @param treeNode
	 *            the given TreeNode for which we want to find the first child.
	 * @return the first TreeNode child that contains a diff.
	 */
	private TreeNode getFirstChildDiffNode(TreeNode treeNode) {
		UnmodifiableIterator<EObject> diffChildren = Iterators.filter(treeNode.eAllContents(),
				IS_DIFF_TREE_NODE);
		while (diffChildren.hasNext()) {
			TreeNode next = (TreeNode)diffChildren.next();
			if (!JFaceUtil.isFiltered(viewer, adapterFactory.adapt(next, ICompareInput.class), adapterFactory
					.adapt(next.getParent(), ICompareInput.class))) {
				return next;
			}
		}
		return null;
	}

	/**
	 * Returns, from the given TreeNode, the last TreeNode child that contains a diff.
	 * 
	 * @param treeNode
	 *            the given TreeNode for which we want to find the last child.
	 * @return the last TreeNode child that contains a diff.
	 */
	private TreeNode getLastChildDiffNode(TreeNode treeNode) {
		UnmodifiableIterator<EObject> diffChildren = Iterators.filter(treeNode.eAllContents(),
				IS_DIFF_TREE_NODE);
		List<EObject> l = Lists.newArrayList(diffChildren);
		ListIterator<EObject> li = l.listIterator(l.size());
		while (li.hasPrevious()) {
			TreeNode prev = (TreeNode)li.previous();
			if (!JFaceUtil.isFiltered(viewer, adapterFactory.adapt(prev, ICompareInput.class), adapterFactory
					.adapt(prev.getParent(), ICompareInput.class))) {
				return prev;
			}
		}
		return null;
	}

	/**
	 * Returns the sorted and filtered set of TreeNode children of the given element.
	 * 
	 * @param treeNodeAdapter
	 *            the given TreeNode adapter.
	 * @return the sorted and filtered set of TreeNode children of the given element.
	 */
	private ImmutableList<TreeNode> getSortedTreeNodeChildren(Adapter treeNodeAdapter) {
		List<TreeNode> treeNodeChildren = Lists.newArrayList();
		Object[] sortedChildren = (Object[])dynamicObject.invoke("getSortedChildren", //$NON-NLS-1$
				new Class<?>[] {Object.class, }, treeNodeAdapter);
		for (Object object : sortedChildren) {
			if (object instanceof Adapter) {

				Notifier target = ((Adapter)object).getTarget();
				if (target instanceof TreeNode) {
					treeNodeChildren.add((TreeNode)target);
				}
			}
		}
		return ImmutableList.copyOf(treeNodeChildren);
	}

	private static boolean isDiffTreeNode(EObject t) {
		return t instanceof TreeNode && ((TreeNode)t).getData() instanceof Diff;
	}
}

Back to the top