1 11 package org.eclipse.ui.part; 12 13 import java.util.Arrays ; 14 import java.util.List ; 15 16 import org.eclipse.jface.action.Action; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.IToolBarManager; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.jface.viewers.TreeViewer; 24 import org.eclipse.ui.ISharedImages; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 27 import org.eclipse.ui.internal.WorkbenchImages; 28 import org.eclipse.ui.internal.WorkbenchMessages; 29 30 51 public class DrillDownAdapter implements ISelectionChangedListener { 52 private TreeViewer fChildTree; 53 54 private DrillStack fDrillStack; 55 56 private Action homeAction; 57 58 private Action backAction; 59 60 private Action forwardAction; 61 62 67 public DrillDownAdapter(TreeViewer tree) { 68 fDrillStack = new DrillStack(); 69 fChildTree = tree; 70 } 71 72 77 public void addNavigationActions(IMenuManager manager) { 78 createActions(); 79 manager.add(homeAction); 80 manager.add(backAction); 81 manager.add(forwardAction); 82 updateNavigationButtons(); 83 } 84 85 90 public void addNavigationActions(IToolBarManager toolBar) { 91 createActions(); 92 toolBar.add(homeAction); 93 toolBar.add(backAction); 94 toolBar.add(forwardAction); 95 updateNavigationButtons(); 96 } 97 98 106 public boolean canExpand(Object element) { 107 return fChildTree.isExpandable(element); 108 } 109 110 116 public boolean canGoBack() { 117 return fDrillStack.canGoBack(); 118 } 119 120 126 public boolean canGoHome() { 127 return fDrillStack.canGoHome(); 128 } 129 130 136 public boolean canGoInto() { 137 IStructuredSelection oSelection = (IStructuredSelection) fChildTree 138 .getSelection(); 139 if (oSelection == null || oSelection.size() != 1) { 140 return false; 141 } 142 Object anElement = oSelection.getFirstElement(); 143 return canExpand(anElement); 144 } 145 146 151 private void createActions() { 152 if (homeAction != null) { 154 return; 155 } 156 157 homeAction = new Action(WorkbenchMessages.GoHome_text) { 159 public void run() { 160 goHome(); 161 } 162 }; 163 homeAction 164 .setToolTipText(WorkbenchMessages.GoHome_toolTip); 165 homeAction 166 .setImageDescriptor(WorkbenchImages 167 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_HOME_NAV)); 168 169 ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); 171 backAction = new Action(WorkbenchMessages.GoBack_text) { 172 public void run() { 173 goBack(); 174 } 175 }; 176 backAction 177 .setToolTipText(WorkbenchMessages.GoBack_toolTip); 178 backAction.setImageDescriptor(images 179 .getImageDescriptor(ISharedImages.IMG_TOOL_BACK)); 180 backAction.setDisabledImageDescriptor(images 181 .getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED)); 182 183 forwardAction = new Action(WorkbenchMessages.GoInto_text) { 185 public void run() { 186 goInto(); 187 } 188 }; 189 forwardAction.setToolTipText(WorkbenchMessages.GoInto_toolTip); 190 forwardAction.setImageDescriptor(images 191 .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD)); 192 forwardAction.setDisabledImageDescriptor(images 193 .getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED)); 194 195 fChildTree.addSelectionChangedListener(this); 197 updateNavigationButtons(); 198 } 199 200 206 private void expand(List items) { 207 fChildTree.setExpandedElements(items.toArray()); 208 } 209 210 216 private List getExpanded() { 217 return Arrays.asList(fChildTree.getExpandedElements()); 218 } 219 220 228 public void goBack() { 229 Object currentInput = fChildTree.getInput(); 230 DrillFrame oFrame = fDrillStack.goBack(); 231 Object input = oFrame.getElement(); 232 fChildTree.setInput(input); 233 expand(oFrame.getExpansion()); 234 if (fChildTree.getSelection().isEmpty()) { 237 fChildTree 238 .setSelection(new StructuredSelection(currentInput), true); 239 } 240 updateNavigationButtons(); 241 } 242 243 251 public void goHome() { 252 Object currentInput = fChildTree.getInput(); 253 DrillFrame oFrame = fDrillStack.goHome(); 254 Object input = oFrame.getElement(); 255 fChildTree.setInput(input); 256 expand(oFrame.getExpansion()); 257 if (fChildTree.getSelection().isEmpty()) { 260 fChildTree 261 .setSelection(new StructuredSelection(currentInput), true); 262 } 263 updateNavigationButtons(); 264 } 265 266 277 public void goInto() { 278 IStructuredSelection sel = (IStructuredSelection) fChildTree 279 .getSelection(); 280 Object element = sel.getFirstElement(); 281 goInto(element); 282 } 283 284 297 public void goInto(Object newInput) { 298 if (canExpand(newInput)) { 300 Object oldInput = fChildTree.getInput(); 302 List expandedList = getExpanded(); 303 fDrillStack.add(new DrillFrame(oldInput, "null", expandedList)); 305 fChildTree.setInput(newInput); 307 expand(expandedList); 308 updateNavigationButtons(); 309 } 310 } 311 312 321 public void reset() { 322 fDrillStack.reset(); 323 updateNavigationButtons(); 324 } 325 326 330 public void selectionChanged(SelectionChangedEvent event) { 331 updateNavigationButtons(); 332 } 333 334 337 protected void updateNavigationButtons() { 338 if (homeAction != null) { 339 homeAction.setEnabled(canGoHome()); 340 backAction.setEnabled(canGoBack()); 341 forwardAction.setEnabled(canGoInto()); 342 } 343 } 344 } 345 | Popular Tags |