1 11 package org.eclipse.search2.internal.ui.basic.views; 12 13 import org.eclipse.search.ui.text.AbstractTextSearchViewPage; 14 15 import org.eclipse.swt.widgets.Tree; 16 import org.eclipse.swt.widgets.TreeItem; 17 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.jface.viewers.TreeViewer; 21 22 public class TreeViewerNavigator implements INavigate { 23 private TreeViewer fViewer; 24 private AbstractTextSearchViewPage fPage; 25 26 public TreeViewerNavigator(AbstractTextSearchViewPage page, TreeViewer viewer) { 27 fViewer= viewer; 28 fPage= page; 29 } 30 31 public void navigateNext(boolean forward) { 32 TreeItem currentItem= getCurrentItem(forward); 33 if (currentItem == null) 34 return; 35 TreeItem nextItem= null; 36 if (forward) { 37 nextItem= getNextItemForward(currentItem); 38 if (nextItem == null) 39 nextItem= getFirstItem(); 40 } else { 41 nextItem= getNextItemBackward(currentItem); 42 if (nextItem == null) 43 nextItem= getLastItem(); 44 } 45 if (nextItem != null) { 46 internalSetSelection(nextItem); 47 } 48 } 49 50 private TreeItem getFirstItem() { 51 TreeItem[] roots= fViewer.getTree().getItems(); 52 if (roots.length == 0) 53 return null; 54 for (int i = 0; i < roots.length; i++) { 55 if (hasMatches(roots[i])) 56 return roots[i]; 57 TreeItem firstChild= getFirstChildWithMatches(roots[0]); 58 if (firstChild != null) 59 return firstChild; 60 } 61 return null; 62 } 63 64 private TreeItem getLastItem() { 65 TreeItem[] roots= fViewer.getTree().getItems(); 66 if (roots.length == 0) 67 return null; 68 return getLastChildWithMatches(roots[roots.length-1]); 69 } 70 71 72 private TreeItem getNextItemBackward(TreeItem currentItem) { 73 TreeItem previousSibling= getNextSibling(currentItem, false); 74 if (previousSibling != null) { 75 TreeItem lastChild= getLastChildWithMatches(previousSibling); 76 if (lastChild != null) 77 return lastChild; 78 if (hasMatches(previousSibling)) 79 return previousSibling; 80 return null; 81 } 82 TreeItem parent= currentItem.getParentItem(); 83 if (parent != null) { 84 if (hasMatches(parent)) 85 return parent; 86 return getNextItemBackward(parent); 87 } 88 return null; 89 } 90 91 private TreeItem getLastChildWithMatches(TreeItem currentItem) { 92 TreeItem[] children= getChildren(currentItem); 93 if (children.length == 0) 94 return null; 95 TreeItem recursiveChild= getLastChildWithMatches(children[children.length-1]); 96 if (recursiveChild == null) 97 return children[children.length-1]; 98 return recursiveChild; 99 } 100 101 private TreeItem getNextItemForward(TreeItem currentItem) { 102 TreeItem child= getFirstChildWithMatches(currentItem); 103 if (child != null) 104 return child; 105 TreeItem nextSibling= getNextSibling(currentItem, true); 106 if (nextSibling != null) { 107 if (hasMatches(nextSibling)) 108 return nextSibling; 109 return getFirstChildWithMatches(nextSibling); 110 } 111 TreeItem parent= currentItem.getParentItem(); 112 while (parent != null) { 113 nextSibling= getNextSibling(parent, true); 114 if (nextSibling != null) { 115 if (hasMatches(nextSibling)) 116 return nextSibling; 117 return getFirstChildWithMatches(nextSibling); 118 } 119 parent= parent.getParentItem(); 120 } 121 return null; 122 } 123 124 private TreeItem getFirstChildWithMatches(TreeItem item) { 125 TreeItem[] children= getChildren(item); 126 if (children.length == 0) 127 return null; 128 TreeItem child= children[0]; 129 130 if (hasMatches(child)) 131 return child; 132 return getFirstChildWithMatches(child); 133 } 134 135 private TreeItem[] getChildren(TreeItem item) { 136 fViewer.setExpandedState(item.getData(), true); 137 return item.getItems(); 138 } 139 140 private TreeItem getNextSibling(TreeItem currentItem, boolean forward) { 141 TreeItem[] siblings= getSiblings(currentItem); 142 if (siblings.length < 2) 143 return null; 144 int index= -1; 145 for (int i= 0; i <siblings.length; i++) { 146 if (siblings[i] == currentItem) { 147 index= i; 148 break; 149 } 150 } 151 if (forward && index == siblings.length-1) { 152 return null; 153 } else if (!forward && index == 0) { 154 return null; 155 } 156 return forward?siblings[index+1]:siblings[index-1]; 157 } 158 159 private TreeItem[] getSiblings(TreeItem currentItem) { 160 Tree tree= fViewer.getTree(); 161 TreeItem parentItem= currentItem.getParentItem(); 162 if (parentItem != null) 163 return parentItem.getItems(); 164 return tree.getItems(); 165 } 166 167 private boolean hasMatches(TreeItem item) { 168 Object element= item.getData(); 169 if (element == null) 170 return false; 171 return fPage.getDisplayedMatchCount(element) > 0; 172 } 173 174 private TreeItem getCurrentItem(boolean forward) { 175 Tree tree= fViewer.getTree(); 176 TreeItem[] selection= tree.getSelection(); 177 if (selection.length == 0) { 178 selection= tree.getItems(); 179 } 180 181 TreeItem nextItem= null; 182 if (selection.length > 0) { 183 nextItem= forward?selection[0]:selection[selection.length-1]; 184 } 185 return nextItem; 186 } 187 188 189 private void internalSetSelection(TreeItem ti) { 190 if (ti != null) { 191 Object data= ti.getData(); 192 if (data != null) { 193 ISelection selection= new StructuredSelection(data); 194 fViewer.setSelection(selection, true); 195 } 196 } 197 } 198 } 199 | Popular Tags |