KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > PartNavigator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.synchronize;
12
13 import org.eclipse.compare.CompareViewerSwitchingPane;
14 import org.eclipse.compare.internal.INavigatable;
15 import org.eclipse.compare.internal.IOpenable;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.team.internal.ui.synchronize.actions.NavigateAction;
20
21 /**
22  * A navigator that coordinates navigation between several navigable
23  * objects. This is copied from the compare plugin and enhanced to
24  * support navigating adaptables.
25  * <p>
26  * This navigator can be used as input to the {@link NavigateAction}
27  * actions and should be passed to the actions via the
28  * {@link SynchronizePageConfiguration#P_NAVIGATOR}.
29  * </p>
30  * @since 3.0
31  */

32 public class PartNavigator implements INavigatable {
33     
34     private Object JavaDoc[] fPanes;
35     // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
36
private boolean fNextFirstTime= true;
37     
38     public PartNavigator(Object JavaDoc[] panes) {
39         fPanes= panes;
40     }
41     
42     public boolean gotoDifference(boolean next) {
43
44         // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
45
if (next && fNextFirstTime && mustOpen()) {
46             fNextFirstTime= false;
47             openElement();
48         }
49         
50         // find most down stream CompareViewerPane
51
int n= 0;
52         INavigatable[] navigators= new INavigatable[4];
53         for (int i= 0; i < fPanes.length; i++) {
54             navigators[n]= getNavigator(fPanes[i]);
55             if (navigators[n] != null)
56                 n++;
57         }
58                                     
59         while (n > 0) {
60             n--;
61             if (navigators[n].gotoDifference(next)) {
62                 // at end of this navigator
63
continue;
64             } else // not at end
65
return false;
66         }
67         return true;
68     }
69     
70     private static INavigatable getNavigator(Object JavaDoc p) {
71         if (p == null)
72             return null;
73         Control control = null;
74         if (p instanceof CompareViewerSwitchingPane) {
75             CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
76             if (pane.isEmpty())
77                 return null;
78             Viewer viewer = pane.getViewer();
79             if (viewer == null)
80                 return null;
81             control = viewer.getControl();
82             if (control == null)
83                 return null;
84             Object JavaDoc data = control.getData(INavigatable.NAVIGATOR_PROPERTY);
85             if (data instanceof INavigatable)
86                 return (INavigatable) data;
87         } else if(p instanceof IAdaptable) {
88             return (INavigatable)((IAdaptable)p).getAdapter(INavigatable.class);
89         }
90         return null;
91     }
92     
93     /*
94      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
95      */

96     private boolean mustOpen() {
97         if (fPanes == null || fPanes.length == 0)
98             return false;
99         for (int i= 1; i < fPanes.length; i++) {
100             Object JavaDoc p= fPanes[i];
101             if (p instanceof CompareViewerSwitchingPane) {
102                 CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
103                 if (pane != null && pane.getInput() != null)
104                     return false;
105             }
106         }
107         return true;
108     }
109     
110     /*
111      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
112      */

113     private void openElement() {
114         if (fPanes == null || fPanes.length == 0)
115             return;
116         IOpenable openable= getOpenable(fPanes[0]);
117         if (openable != null) {
118             openable.openSelected();
119         }
120     }
121     
122     /*
123      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
124      */

125     private static IOpenable getOpenable(Object JavaDoc p) {
126         if (p instanceof CompareViewerSwitchingPane) {
127             CompareViewerSwitchingPane pane = (CompareViewerSwitchingPane) p;
128             if (pane == null)
129                 return null;
130             if (pane.isEmpty())
131                 return null;
132             Viewer viewer = pane.getViewer();
133             if (viewer == null)
134                 return null;
135             Control control = viewer.getControl();
136             if (control == null)
137                 return null;
138             Object JavaDoc data = control.getData(IOpenable.OPENABLE_PROPERTY);
139             if (data instanceof IOpenable)
140                 return (IOpenable) data;
141         }
142         return null;
143     }
144 }
145
Popular Tags