KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > CompareNavigator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.compare.internal;
12
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.compare.CompareEditorInput;
16 import org.eclipse.compare.*;
17
18 /**
19  * Supports cross-pane navigation through differences.
20  * XXX: Design is as it is because the feature had to be added without touching API.
21  */

22 public class CompareNavigator implements ICompareNavigator {
23     
24     private boolean fLastDirection= true;
25     private CompareViewerSwitchingPane[] fPanes;
26     // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
27
private boolean fNextFirstTime= true;
28     
29     public CompareNavigator(CompareViewerSwitchingPane[] panes) {
30         fPanes= panes;
31     }
32
33     public CompareViewerSwitchingPane[] getPanes() {
34         return fPanes;
35     }
36     
37     public boolean selectChange(boolean next) {
38         
39         fLastDirection= next;
40
41         // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
42
if (next && fNextFirstTime && mustOpen()) {
43             fNextFirstTime= false;
44             openElement();
45         }
46         
47         // find most down stream CompareViewerPane
48
int n= 0;
49         INavigatable[] navigators= new INavigatable[4];
50         for (int i= 0; i < fPanes.length; i++) {
51             navigators[n]= getNavigator(fPanes[i]);
52             if (navigators[n] != null)
53                 n++;
54         }
55                                     
56         while (n > 0) {
57             n--;
58             if (navigators[n].gotoDifference(next)) {
59                 // at end of this navigator
60
continue;
61             }
62             // not at end
63
return false;
64         }
65         
66         return true;
67     }
68     
69     private static INavigatable getNavigator(CompareViewerSwitchingPane pane) {
70         if (pane == null)
71             return null;
72         if (pane.isEmpty())
73             return null;
74         Viewer viewer= pane.getViewer();
75         if (viewer == null)
76             return null;
77         Control control= viewer.getControl();
78         if (control == null)
79             return null;
80         Object JavaDoc data= control.getData(INavigatable.NAVIGATOR_PROPERTY);
81         if (data instanceof INavigatable)
82             return (INavigatable) data;
83         return null;
84     }
85     
86     private static CompareNavigator findNavigator(Control c) {
87         while (c != null && !c.isDisposed()) { // PR 1GEUVV2
88
Object JavaDoc data= c.getData();
89             if (data instanceof CompareEditorInput) {
90                 CompareEditorInput cei= (CompareEditorInput) data;
91                 Object JavaDoc adapter= cei.getAdapter(CompareNavigator.class);
92                 if (adapter instanceof CompareNavigator)
93                     return (CompareNavigator)adapter;
94             }
95             c= c.getParent();
96         }
97         return null;
98     }
99     
100     private boolean resetDirection() {
101         boolean last= fLastDirection;
102         fLastDirection= true;
103         return last;
104     }
105     
106     public static boolean getDirection(Control c) {
107         CompareNavigator nav= findNavigator(c);
108         if (nav != null)
109             return nav.resetDirection();
110         return true;
111     }
112     
113     /*
114      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
115      */

116     private boolean mustOpen() {
117         if (fPanes == null || fPanes.length == 0)
118             return false;
119         for (int i= 1; i < fPanes.length; i++) {
120             CompareViewerSwitchingPane pane= fPanes[i];
121             if (pane != null && pane.getInput() != null)
122                 return false;
123         }
124         return true;
125     }
126     
127     /*
128      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
129      */

130     private void openElement() {
131         if (fPanes == null || fPanes.length == 0)
132             return;
133         IOpenable openable= getOpenable(fPanes[0]);
134         if (openable != null) {
135             openable.openSelected();
136         }
137     }
138     
139     /*
140      * Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=20106
141      */

142     private static IOpenable getOpenable(CompareViewerSwitchingPane pane) {
143         if (pane == null)
144             return null;
145         if (pane.isEmpty())
146             return null;
147         Viewer viewer= pane.getViewer();
148         if (viewer == null)
149             return null;
150         Control control= viewer.getControl();
151         if (control == null)
152             return null;
153         Object JavaDoc data= control.getData(IOpenable.OPENABLE_PROPERTY);
154         if (data instanceof IOpenable)
155             return (IOpenable) data;
156         return null;
157     }
158 }
159
Popular Tags