1 11 package org.eclipse.jdt.internal.ui.browsing; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 16 import org.eclipse.core.resources.IFolder; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 23 import org.eclipse.jface.action.Action; 24 import org.eclipse.jface.action.GroupMarker; 25 import org.eclipse.jface.action.IAction; 26 import org.eclipse.jface.action.IContributionItem; 27 import org.eclipse.jface.action.IMenuManager; 28 import org.eclipse.jface.action.IStatusLineManager; 29 import org.eclipse.jface.action.MenuManager; 30 import org.eclipse.jface.preference.IPreferenceStore; 31 import org.eclipse.jface.viewers.IContentProvider; 32 import org.eclipse.jface.viewers.ILabelDecorator; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.StructuredSelection; 36 import org.eclipse.jface.viewers.StructuredViewer; 37 import org.eclipse.jface.viewers.TableViewer; 38 import org.eclipse.jface.viewers.Viewer; 39 40 import org.eclipse.ui.IActionBars; 41 import org.eclipse.ui.IMemento; 42 import org.eclipse.ui.IPageLayout; 43 import org.eclipse.ui.IViewSite; 44 import org.eclipse.ui.PartInitException; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.actions.ActionFactory; 47 import org.eclipse.ui.part.IShowInTargetList; 48 49 import org.eclipse.jdt.core.IClassFile; 50 import org.eclipse.jdt.core.ICompilationUnit; 51 import org.eclipse.jdt.core.IJavaElement; 52 import org.eclipse.jdt.core.IJavaProject; 53 import org.eclipse.jdt.core.IPackageFragment; 54 import org.eclipse.jdt.core.IPackageFragmentRoot; 55 import org.eclipse.jdt.core.IType; 56 import org.eclipse.jdt.core.JavaCore; 57 58 import org.eclipse.jdt.internal.corext.util.Messages; 59 60 import org.eclipse.jdt.ui.JavaElementComparator; 61 import org.eclipse.jdt.ui.JavaElementLabels; 62 import org.eclipse.jdt.ui.JavaUI; 63 import org.eclipse.jdt.ui.PreferenceConstants; 64 65 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 66 import org.eclipse.jdt.internal.ui.JavaPlugin; 67 import org.eclipse.jdt.internal.ui.JavaPluginImages; 68 import org.eclipse.jdt.internal.ui.actions.MultiActionGroup; 69 import org.eclipse.jdt.internal.ui.actions.SelectAllAction; 70 import org.eclipse.jdt.internal.ui.filters.NonJavaElementFilter; 71 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; 72 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; 73 import org.eclipse.jdt.internal.ui.viewsupport.LibraryFilter; 74 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer; 75 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer; 76 import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater; 77 78 79 public class PackagesView extends JavaBrowsingPart{ 80 81 private static final String TAG_VIEW_STATE= ".viewState"; private static final int LIST_VIEW_STATE= 0; 83 private static final int TREE_VIEW_STATE= 1; 84 85 86 private static class StatusBarUpdater4LogicalPackage extends StatusBarUpdater { 87 88 private StatusBarUpdater4LogicalPackage(IStatusLineManager statusLineManager) { 89 super(statusLineManager); 90 } 91 92 protected String formatMessage(ISelection sel) { 93 if (sel instanceof IStructuredSelection) { 94 IStructuredSelection selection= (IStructuredSelection)sel; 95 int nElements= selection.size(); 96 Object elem= selection.getFirstElement(); 97 if (nElements == 1 && (elem instanceof LogicalPackage)) 98 return formatLogicalPackageMessage((LogicalPackage) elem); 99 } 100 return super.formatMessage(sel); 101 } 102 103 private String formatLogicalPackageMessage(LogicalPackage logicalPackage) { 104 IPackageFragment[] fragments= logicalPackage.getFragments(); 105 StringBuffer buf= new StringBuffer (logicalPackage.getElementName()); 106 buf.append(JavaElementLabels.CONCAT_STRING); 107 String message= ""; boolean firstTime= true; 109 for (int i= 0; i < fragments.length; i++) { 110 IPackageFragment fragment= fragments[i]; 111 IJavaElement element= fragment.getParent(); 112 if (element instanceof IPackageFragmentRoot) { 113 IPackageFragmentRoot root= (IPackageFragmentRoot) element; 114 String label= JavaElementLabels.getElementLabel(root, JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_QUALIFIED); 115 if (firstTime) { 116 buf.append(label); 117 firstTime= false; 118 } 119 else 120 message= Messages.format(JavaBrowsingMessages.StatusBar_concat, new String [] {message, label}); 121 } 122 } 123 buf.append(message); 124 return buf.toString(); 125 } 126 } 127 128 129 private SelectAllAction fSelectAllAction; 130 131 private int fCurrViewState; 132 133 private PackageViewerWrapper fWrappedViewer; 134 135 private MultiActionGroup fSwitchActionGroup; 136 private boolean fLastInputWasProject; 137 138 141 protected void addFilters() { 142 super.addFilters(); 143 getViewer().addFilter(createNonJavaElementFilter()); 144 getViewer().addFilter(new LibraryFilter()); 145 } 146 147 148 153 protected NonJavaElementFilter createNonJavaElementFilter() { 154 return new NonJavaElementFilter(){ 155 public boolean select(Viewer viewer, Object parent, Object element){ 156 return ((element instanceof IJavaElement) || (element instanceof LogicalPackage) || (element instanceof IFolder)); 157 } 158 }; 159 } 160 161 public void init(IViewSite site, IMemento memento) throws PartInitException { 162 super.init(site, memento); 163 fWrappedViewer= new PackageViewerWrapper(); 165 restoreLayoutState(memento); 166 } 167 168 private void restoreLayoutState(IMemento memento) { 169 if (memento == null) { 170 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 172 fCurrViewState= store.getInt(this.getViewSite().getId() + TAG_VIEW_STATE); 173 } else { 174 Integer integer= memento.getInteger(this.getViewSite().getId() + TAG_VIEW_STATE); 176 if ((integer == null) || !isValidState(integer.intValue())) { 177 fCurrViewState= LIST_VIEW_STATE; 178 } else fCurrViewState= integer.intValue(); 179 } 180 } 181 182 private boolean isValidState(int state) { 183 return (state==LIST_VIEW_STATE) || (state==TREE_VIEW_STATE); 184 } 185 186 187 188 191 public void saveState(IMemento memento) { 192 super.saveState(memento); 193 memento.putInteger(this.getViewSite().getId()+TAG_VIEW_STATE,fCurrViewState); 194 } 195 196 199 protected StructuredViewer createViewer(Composite parent) { 200 StructuredViewer viewer; 202 if(isInListState()) 203 viewer= createTableViewer(parent); 204 else 205 viewer= createTreeViewer(parent); 206 207 fWrappedViewer.setViewer(viewer); 208 return fWrappedViewer; 209 } 210 211 214 public Object getAdapter(Class key) { 215 if (key == IShowInTargetList.class) { 216 return new IShowInTargetList() { 217 public String [] getShowInTargetIds() { 218 return new String [] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV }; 219 } 220 }; 221 } 222 return super.getAdapter(key); 223 } 224 225 protected boolean isInListState() { 226 return fCurrViewState== LIST_VIEW_STATE; 227 } 228 229 private ProblemTableViewer createTableViewer(Composite parent) { 230 return new PackagesViewTableViewer(parent, SWT.MULTI); 231 } 232 233 private ProblemTreeViewer createTreeViewer(Composite parent) { 234 return new PackagesViewTreeViewer(parent, SWT.MULTI); 235 } 236 237 241 protected IContentProvider createContentProvider() { 242 if(isInListState()) 243 return new PackagesViewFlatContentProvider(fWrappedViewer.getViewer()); 244 else return new PackagesViewHierarchicalContentProvider(fWrappedViewer.getViewer()); 245 } 246 247 protected JavaUILabelProvider createLabelProvider() { 248 if(isInListState()) 249 return createListLabelProvider(); 250 else return createTreeLabelProvider(); 251 } 252 253 private JavaUILabelProvider createTreeLabelProvider() { 254 return new PackagesViewLabelProvider(PackagesViewLabelProvider.HIERARCHICAL_VIEW_STATE); 255 } 256 257 private JavaUILabelProvider createListLabelProvider() { 258 return new PackagesViewLabelProvider(PackagesViewLabelProvider.FLAT_VIEW_STATE); 259 } 260 261 266 protected String getHelpContextId() { 267 return IJavaHelpContextIds.PACKAGES_BROWSING_VIEW; 268 } 269 270 protected String getLinkToEditorKey() { 271 return PreferenceConstants.LINK_BROWSING_PACKAGES_TO_EDITOR; 272 } 273 274 281 protected boolean isValidInput(Object element) { 282 if (element instanceof IJavaProject || (element instanceof IPackageFragmentRoot && ((IJavaElement)element).getElementName() != IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH)) 283 try { 284 IJavaProject jProject= ((IJavaElement)element).getJavaProject(); 285 if (jProject != null) 286 return jProject.getProject().hasNature(JavaCore.NATURE_ID); 287 } catch (CoreException ex) { 288 return false; 289 } 290 return false; 291 } 292 293 300 protected boolean isValidElement(Object element) { 301 if (element instanceof IPackageFragment) { 302 IJavaElement parent= ((IPackageFragment)element).getParent(); 303 if (parent != null) 304 return super.isValidElement(parent) || super.isValidElement(parent.getJavaProject()); 305 } 306 return false; 307 } 308 309 312 protected IJavaElement findElementToSelect(IJavaElement je) { 313 if (je == null) 314 return null; 315 316 switch (je.getElementType()) { 317 case IJavaElement.PACKAGE_FRAGMENT: 318 return je; 319 case IJavaElement.COMPILATION_UNIT: 320 return ((ICompilationUnit)je).getParent(); 321 case IJavaElement.CLASS_FILE: 322 return ((IClassFile)je).getParent(); 323 case IJavaElement.TYPE: 324 return ((IType)je).getPackageFragment(); 325 default: 326 return findElementToSelect(je.getParent()); 327 } 328 } 329 330 333 protected void setInput(Object input) { 334 setViewerWrapperInput(input); 335 super.updateTitle(); 336 } 337 338 private void setViewerWrapperInput(Object input) { 339 fWrappedViewer.setViewerInput(input); 340 } 341 342 345 protected void fillActionBars(IActionBars actionBars) { 346 super.fillActionBars(actionBars); 347 fSwitchActionGroup.fillActionBars(actionBars); 348 } 349 350 351 352 private void setUpViewer(StructuredViewer viewer){ 353 Assert.isTrue(viewer != null); 354 355 JavaUILabelProvider labelProvider= createLabelProvider(); 356 viewer.setLabelProvider(createDecoratingLabelProvider(labelProvider)); 357 358 viewer.setComparator(createJavaElementComparator()); 359 viewer.setUseHashlookup(true); 360 361 createContextMenu(); 362 363 addKeyListener(); 365 366 hookViewerListeners(); 369 370 viewer.setContentProvider(createContentProvider()); 372 initDragAndDrop(); 374 375 } 376 377 protected JavaElementComparator createJavaElementComparator() { 379 return new JavaElementComparator(){ 380 public int category(Object element) { 381 if (element instanceof LogicalPackage) { 382 LogicalPackage cp= (LogicalPackage) element; 383 return super.category(cp.getFragments()[0]); 384 } else return super.category(element); 385 } 386 public int compare(Viewer viewer, Object e1, Object e2){ 387 if (e1 instanceof LogicalPackage) { 388 LogicalPackage cp= (LogicalPackage) e1; 389 e1= cp.getFragments()[0]; 390 } 391 if (e2 instanceof LogicalPackage) { 392 LogicalPackage cp= (LogicalPackage) e2; 393 e2= cp.getFragments()[0]; 394 } 395 return super.compare(viewer, e1, e2); 396 } 397 }; 398 } 399 400 protected StatusBarUpdater createStatusBarUpdater(IStatusLineManager slManager) { 401 return new StatusBarUpdater4LogicalPackage(slManager); 402 } 403 404 protected void setSiteSelectionProvider(){ 405 getSite().setSelectionProvider(fWrappedViewer); 406 } 407 408 void adjustInputAndSetSelection(Object o) { 409 if (!(o instanceof LogicalPackage)) { 410 super.adjustInputAndSetSelection(o); 411 return; 412 } 413 414 LogicalPackage lp= (LogicalPackage)o; 415 if (!lp.getJavaProject().equals(getInput())) 416 setInput(lp.getJavaProject()); 417 418 setSelection(new StructuredSelection(lp), true); 419 } 420 421 protected void createActions() { 423 super.createActions(); 424 425 createSelectAllAction(); 426 427 fSwitchActionGroup= createSwitchActionGroup(); 429 } 430 431 private MultiActionGroup createSwitchActionGroup(){ 432 433 LayoutAction switchToFlatViewAction= new LayoutAction(JavaBrowsingMessages.PackagesView_flatLayoutAction_label,LIST_VIEW_STATE); 434 LayoutAction switchToHierarchicalViewAction= new LayoutAction(JavaBrowsingMessages.PackagesView_HierarchicalLayoutAction_label, TREE_VIEW_STATE); 435 JavaPluginImages.setLocalImageDescriptors(switchToFlatViewAction, "flatLayout.gif"); JavaPluginImages.setLocalImageDescriptors(switchToHierarchicalViewAction, "hierarchicalLayout.gif"); 438 return new LayoutActionGroup(new IAction[]{switchToFlatViewAction,switchToHierarchicalViewAction}, fCurrViewState); 439 } 440 441 private static class LayoutActionGroup extends MultiActionGroup { 442 443 LayoutActionGroup(IAction[] actions, int index) { 444 super(actions, index); 445 } 446 447 public void fillActionBars(IActionBars actionBars) { 448 IMenuManager manager= actionBars.getMenuManager(); 450 final IContributionItem groupMarker= new GroupMarker("layout"); manager.add(groupMarker); 452 IMenuManager newManager= new MenuManager(JavaBrowsingMessages.PackagesView_LayoutActionGroup_layout_label); 453 manager.appendToGroup("layout", newManager); super.addActions(newManager); 455 } 456 } 457 458 459 462 private class LayoutAction extends Action { 463 464 private int fState; 465 466 public LayoutAction(String text, int state) { 467 super(text, IAction.AS_RADIO_BUTTON); 468 fState= state; 469 if (state == PackagesView.LIST_VIEW_STATE) 470 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_FLAT_ACTION); 471 else 472 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_HIERARCHICAL_ACTION); 473 } 474 475 public int getState() { 476 return fState; 477 } 478 479 public void setRunnable(Runnable runnable) { 480 Assert.isNotNull(runnable); 481 } 482 483 486 public void run() { 487 switchViewer(fState); 488 } 489 } 490 491 private void switchViewer(int state) { 492 if (fCurrViewState == state) 494 return; 495 else { 496 fCurrViewState= state; 497 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 498 store.setValue(getViewSite().getId() + TAG_VIEW_STATE, state); 499 } 500 501 StructuredViewer viewer= fWrappedViewer.getViewer(); 503 Object object= viewer.getInput(); 504 ISelection selection= viewer.getSelection(); 505 506 Control control= createViewer(fWrappedViewer.getControl().getParent()).getControl(); 508 509 setUpViewer(fWrappedViewer); 510 511 createSelectAllAction(); 512 513 fWrappedViewer.setViewerInput(object); 515 fWrappedViewer.getControl().setFocus(); 516 fWrappedViewer.setSelection(selection, true); 517 518 viewer.getContentProvider().dispose(); 520 viewer.getControl().dispose(); 521 522 if (control != null && !control.isDisposed()) { 524 control.setVisible(true); 525 control.getParent().layout(true); 526 } 527 } 528 529 private void createSelectAllAction() { 530 IActionBars actionBars= getViewSite().getActionBars(); 531 if (isInListState()) { 532 fSelectAllAction= new SelectAllAction((TableViewer)fWrappedViewer.getViewer()); 533 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), fSelectAllAction); 534 } else { 535 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), null); 536 fSelectAllAction= null; 537 } 538 actionBars.updateActionBars(); 539 } 540 541 protected IJavaElement findInputForJavaElement(IJavaElement je) { 542 if (je == null) 546 return null; 547 if(je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT || je.getElementType() == IJavaElement.JAVA_PROJECT) 548 return findInputForJavaElement(je, true); 549 else 550 return findInputForJavaElement(je, false); 551 552 } 553 554 protected IJavaElement findInputForJavaElement(IJavaElement je, boolean canChangeInputType) { 555 if (je == null || !je.exists()) 556 return null; 557 558 if (isValidInput(je)) { 559 560 if (canChangeInputType) 562 fLastInputWasProject= je.getElementType() == IJavaElement.JAVA_PROJECT; 563 return je; 564 } else if (fLastInputWasProject) { 565 IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot)je.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 566 if (!packageFragmentRoot.isExternal()) 567 return je.getJavaProject(); 568 } 569 570 return findInputForJavaElement(je.getParent(), canChangeInputType); 571 } 572 573 579 protected DecoratingJavaLabelProvider createDecoratingLabelProvider(JavaUILabelProvider provider) { 580 return new DecoratingJavaLabelProvider(provider, false, isInListState()) { 581 582 public String getText(Object element){ 583 if (element instanceof LogicalPackage) { 584 LogicalPackage el= (LogicalPackage) element; 585 return super.getText(el.getFragments()[0]); 586 } else return super.getText(element); 587 } 588 589 public Image getImage(Object element) { 590 if(element instanceof LogicalPackage){ 591 LogicalPackage el= (LogicalPackage) element; 592 ILabelDecorator decorator= getLabelDecorator(); 593 IPackageFragment[] fragments= el.getFragments(); 594 595 Image image= super.getImage(el); 596 for (int i= 0; i < fragments.length; i++) { 597 IPackageFragment fragment= fragments[i]; 598 Image decoratedImage= decorator.decorateImage(image, fragment); 599 if(decoratedImage != null) 600 image= decoratedImage; 601 } 602 return image; 603 } else return super.getImage(element); 604 } 605 606 }; 607 } 608 609 614 void adjustInputAndSetSelection(IJavaElement je) { 615 616 IJavaElement jElementToSelect= findElementToSelect(je); 617 LogicalPackagesProvider p= (LogicalPackagesProvider) fWrappedViewer.getContentProvider(); 618 619 Object elementToSelect= jElementToSelect; 620 if (jElementToSelect != null && jElementToSelect.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 621 IPackageFragment pkgFragment= (IPackageFragment)jElementToSelect; 622 elementToSelect= p.findLogicalPackage(pkgFragment); 623 if (elementToSelect == null) 624 elementToSelect= pkgFragment; 625 } 626 627 IJavaElement newInput= findInputForJavaElement(je); 628 if (elementToSelect == null && !isValidInput(newInput)) 629 setInput(null); 630 else if (elementToSelect == null || getViewer().testFindItem(elementToSelect) == null) { 631 632 Object input= getViewer().getInput(); 634 if (elementToSelect != null && newInput != null) { 635 if (newInput.equals(input)) { 636 getViewer().reveal(elementToSelect); 637 } else { 639 setInput(newInput); 640 getViewer().reveal(elementToSelect); 641 } 642 } else 643 setInput(newInput); 644 645 if (elementToSelect instanceof IPackageFragment) { 646 IPackageFragment pkgFragment= (IPackageFragment)elementToSelect; 647 elementToSelect= p.findLogicalPackage(pkgFragment); 648 if (elementToSelect == null) 649 elementToSelect= pkgFragment; 650 } 651 } 652 653 ISelection selection; 654 if (elementToSelect != null) 655 selection= new StructuredSelection(elementToSelect); 656 else 657 selection= StructuredSelection.EMPTY; 658 setSelection(selection, true); 659 } 660 661 } 662 | Popular Tags |