KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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;
12
13 import org.eclipse.compare.internal.Utilities;
14 import org.eclipse.core.runtime.IAdaptable;
15
16 /**
17  * Supports cross-pane navigation through the differences of a compare container.
18  * <p>
19  * Clients may subclass this class.
20  * </p>
21  * @see INavigatable
22  * @since 3.3
23  */

24 public abstract class CompareNavigator implements ICompareNavigator {
25     
26     /* (non-Javadoc)
27      * @see org.eclipse.compare.ICompareNavigator#selectChange(boolean)
28      */

29     public boolean selectChange(boolean next) {
30         // find most down stream CompareViewerPane
31
INavigatable[] navigators= getNavigatables();
32         Object JavaDoc downStreamInput = null;
33         for (int i = navigators.length - 1; i >=0; i--) {
34             INavigatable navigatable = navigators[i];
35             if (navigatable.getInput() == downStreamInput) {
36                 // Skip to up stream pane if it has the same input
37
continue;
38             }
39             if (navigatable.selectChange(next ? INavigatable.NEXT_CHANGE : INavigatable.PREVIOUS_CHANGE)) {
40                 // at end of this navigator
41
downStreamInput = navigatable.getInput();
42                 continue;
43             }
44             // not at end
45
if (i + 1 < navigators.length && navigators[i+1] != null && navigators[i+1].getInput() != downStreamInput) {
46                 // The navigation has invoked a change in a downstream pane.
47
// Set the selected change depending on the direction we are navigating
48
navigators[i+1].selectChange(next ? INavigatable.FIRST_CHANGE : INavigatable.LAST_CHANGE);
49             }
50             return false;
51         }
52         
53         return true;
54     }
55     
56     protected abstract INavigatable[] getNavigatables();
57     
58     /**
59      * Return the {@link INavigatable} for the given object.
60      * If the object implements {@link INavigatable}, then
61      * the object is returned. Otherwise, if the object
62      * implements {@link IAdaptable}, the object is
63      * adapted to {@link INavigatable}.
64      * @param object the object
65      * @return the {@link INavigatable} for the given object or <code>null</code>
66      */

67     protected final INavigatable getNavigator(Object JavaDoc object) {
68         if (object == null)
69             return null;
70         Object JavaDoc data= Utilities.getAdapter(object, INavigatable.class);
71         if (data instanceof INavigatable)
72             return (INavigatable) data;
73         return null;
74     }
75
76     /**
77      * Return whether a call to {@link ICompareNavigator#selectChange(boolean)} with the same parameter
78      * would succeed.
79      * @param next if <code>true</code> the next change is selected, otherwise the previous change
80      * @return whether a call to {@link ICompareNavigator#selectChange(boolean)} with the same parameter
81      * would succeed.
82      * @since 3.3
83      */

84     public boolean hasChange(boolean next) {
85         INavigatable[] navigators= getNavigatables();
86         Object JavaDoc downStreamInput = null;
87         for (int i = navigators.length - 1; i >=0; i--) {
88             INavigatable navigatable = navigators[i];
89             if (navigatable.getInput() == downStreamInput) {
90                 // Skip to up stream pane if it has the same input
91
continue;
92             }
93             if (navigatable.hasChange(next ? INavigatable.NEXT_CHANGE : INavigatable.PREVIOUS_CHANGE)) {
94                 return true;
95             }
96             // at end of this navigator
97
downStreamInput = navigatable.getInput();
98         }
99         return false;
100     }
101 }
102
Popular Tags