Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc7411f2d6826f0a67d12e913f5ffa2879b9de17 (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
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare;

import org.eclipse.core.runtime.Adapters;

/**
 * Supports cross-pane navigation through the differences of a compare container.
 * <p>
 * Clients may subclass this class.
 * </p>
 * @see INavigatable
 * @since 3.3
 */
public abstract class CompareNavigator implements ICompareNavigator {
	@Override
	public boolean selectChange(boolean next) {
		// find most down stream CompareViewerPane
		INavigatable[] navigators= getNavigatables();
		Object downStreamInput = null;
		for (int i = navigators.length - 1; i >=0; i--) {
			INavigatable navigatable = navigators[i];
			if (navigatable.getInput() == downStreamInput) {
				// Skip to up stream pane if it has the same input
				continue;
			}
			if (navigatable.selectChange(next ? INavigatable.NEXT_CHANGE : INavigatable.PREVIOUS_CHANGE)) {
				// at end of this navigator
				downStreamInput = navigatable.getInput();
				continue;
			}
			// not at end
			if (i + 1 < navigators.length && navigators[i + 1] != null
					&& navigators[i + 1].getInput() != downStreamInput) {
				// The navigation has invoked a change in a downstream pane.
				// Set the selected change depending on the direction we are navigating
				navigators[i+1].selectChange(next ? INavigatable.FIRST_CHANGE : INavigatable.LAST_CHANGE);
			}
			return false;
		}

		return true;
	}

	protected abstract INavigatable[] getNavigatables();

	/**
	 * Returns the {@link INavigatable} for the given object if the object
	 * adapts to {@link INavigatable}.
	 *
	 * @param object the object
	 * @return the {@link INavigatable} for the given object or {@code null}
	 */
	protected final INavigatable getNavigator(Object object) {
		if (object == null)
			return null;
		return Adapters.adapt(object, INavigatable.class);
	}

	/**
	 * Returns whether a call to {@link ICompareNavigator#selectChange(boolean)}
	 * with the same parameter would succeed.
	 *
	 * @param next if <code>true</code> the next change is selected, otherwise
	 *     the previous change
	 * @return whether a call to {@link ICompareNavigator#selectChange(boolean)}
	 *     with the same parameter would succeed.
	 * @since 3.3
	 */
	public boolean hasChange(boolean next) {
		INavigatable[] navigators= getNavigatables();
		Object downStreamInput = null;
		for (int i = navigators.length; --i >= 0;) {
			INavigatable navigatable = navigators[i];
			if (navigatable.getInput() == downStreamInput) {
				// Skip to up stream pane if it has the same input
				continue;
			}
			if (navigatable.hasChange(next ? INavigatable.NEXT_CHANGE : INavigatable.PREVIOUS_CHANGE)) {
				return true;
			}
			// at end of this navigator
			downStreamInput = navigatable.getInput();
		}
		return false;
	}
}

Back to the top