Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15d0e6473431b6e21bbbb2a8a4d0fa1162ae9d46 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 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.team.internal.ui.synchronize;

import org.eclipse.compare.CompareViewerSwitchingPane;
import org.eclipse.compare.internal.INavigatable;
import org.eclipse.compare.internal.IOpenable;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.team.internal.ui.synchronize.actions.NavigateAction;

/**
 * A navigator that coordinates navigation between several navigable
 * objects. This is copied from the compare plugin and enhanced to
 * support navigating adaptables.
 * <p>
 * This navigator can be used as input to the {@link NavigateAction}
 * actions and should be passed to the actions via the 
 * {@link SynchronizePageConfiguration#P_NAVIGATOR}.
 * </p>
 * @since 3.0
 */
public class PartNavigator implements INavigatable {
	
	private Object[] fPanes;
	// Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
	private boolean fNextFirstTime= true;
	
	public PartNavigator(Object[] panes) {
		fPanes= panes;
	}
	
	public boolean gotoDifference(boolean next) {

		// Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
		if (next && fNextFirstTime && mustOpen()) {
			fNextFirstTime= false;
			openElement();
		}
		
		// find most down stream CompareViewerPane
		int n= 0;
		INavigatable[] navigators= new INavigatable[4];
		for (int i= 0; i < fPanes.length; i++) {
			navigators[n]= getNavigator(fPanes[i]);
			if (navigators[n] != null)
				n++;
		}
									
		while (n > 0) {
			n--;
			if (navigators[n].gotoDifference(next)) {
				// at end of this navigator
				continue;
			} else // not at end
				return false;
		}		
		return true;
	}
	
	private static INavigatable getNavigator(Object p) {
		if (p == null)
			return null;
		Control control = null;
		if (p instanceof CompareViewerSwitchingPane) {
			CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
			if (pane.isEmpty())
				return null;
			Viewer viewer = pane.getViewer();
			if (viewer == null)
				return null;
			control = viewer.getControl();
			if (control == null)
				return null;
			Object data = control.getData(INavigatable.NAVIGATOR_PROPERTY);
			if (data instanceof INavigatable)
				return (INavigatable) data;
		} else if(p instanceof IAdaptable) {
			return (INavigatable)((IAdaptable)p).getAdapter(INavigatable.class);
		}	
		return null;
	}
	
	/*
	 * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
	 */
	private boolean mustOpen() {
		if (fPanes == null || fPanes.length == 0)
			return false;
		for (int i= 1; i < fPanes.length; i++) {
			Object p= fPanes[i];
			if (p instanceof CompareViewerSwitchingPane) {
				CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
				if (pane != null && pane.getInput() != null)
					return false;
			}
		}
		return true;
	}
	
	/*
	 * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
	 */
	private void openElement() {
		if (fPanes == null || fPanes.length == 0)
			return;
		IOpenable openable= getOpenable(fPanes[0]);
		if (openable != null) {
			openable.openSelected();
		}
	}
	
	/*
	 * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
	 */
	private static IOpenable getOpenable(Object p) {
		if (p instanceof CompareViewerSwitchingPane) {
			CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
			if (pane == null)
				return null;
			if (pane.isEmpty())
				return null;
			Viewer viewer = pane.getViewer();
			if (viewer == null)
				return null;
			Control control = viewer.getControl();
			if (control == null)
				return null;
			Object data = control.getData(IOpenable.OPENABLE_PROPERTY);
			if (data instanceof IOpenable)
				return (IOpenable) data;
		}
		return null;
	}	
}

Back to the top