1 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 22 public class CompareNavigator implements ICompareNavigator { 23 24 private boolean fLastDirection= true; 25 private CompareViewerSwitchingPane[] fPanes; 26 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 if (next && fNextFirstTime && mustOpen()) { 43 fNextFirstTime= false; 44 openElement(); 45 } 46 47 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 continue; 61 } 62 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 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()) { Object data= c.getData(); 89 if (data instanceof CompareEditorInput) { 90 CompareEditorInput cei= (CompareEditorInput) data; 91 Object 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 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 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 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 data= control.getData(IOpenable.OPENABLE_PROPERTY); 154 if (data instanceof IOpenable) 155 return (IOpenable) data; 156 return null; 157 } 158 } 159 | Popular Tags |