1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IPath; 16 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.jface.action.IStatusLineManager; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.SelectionChangedEvent; 25 26 import org.eclipse.ui.model.IWorkbenchAdapter; 27 28 import org.eclipse.jdt.core.IJarEntryResource; 29 import org.eclipse.jdt.core.IJavaElement; 30 31 import org.eclipse.jdt.internal.corext.util.Messages; 32 33 import org.eclipse.jdt.ui.JavaElementLabels; 34 35 import org.eclipse.jdt.internal.ui.JavaUIMessages; 36 import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; 37 38 42 public class StatusBarUpdater implements ISelectionChangedListener { 43 44 private final long LABEL_FLAGS= JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_POST_QUALIFIED | JavaElementLabels.APPEND_ROOT_PATH | 45 JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_APP_RETURNTYPE | JavaElementLabels.M_EXCEPTIONS | 46 JavaElementLabels.F_APP_TYPE_SIGNATURE | JavaElementLabels.T_TYPE_PARAMETERS; 47 48 private IStatusLineManager fStatusLineManager; 49 50 public StatusBarUpdater(IStatusLineManager statusLineManager) { 51 fStatusLineManager= statusLineManager; 52 } 53 54 57 public void selectionChanged(SelectionChangedEvent event) { 58 String statusBarMessage= formatMessage(event.getSelection()); 59 fStatusLineManager.setMessage(statusBarMessage); 60 } 61 62 63 protected String formatMessage(ISelection sel) { 64 if (sel instanceof IStructuredSelection && !sel.isEmpty()) { 65 IStructuredSelection selection= (IStructuredSelection) sel; 66 67 int nElements= selection.size(); 68 if (nElements > 1) { 69 return Messages.format(JavaUIMessages.StatusBarUpdater_num_elements_selected, String.valueOf(nElements)); 70 } else { 71 Object elem= selection.getFirstElement(); 72 if (elem instanceof IJavaElement) { 73 return formatJavaElementMessage((IJavaElement) elem); 74 } else if (elem instanceof IResource) { 75 return formatResourceMessage((IResource) elem); 76 } else if (elem instanceof PackageFragmentRootContainer) { 77 PackageFragmentRootContainer container= (PackageFragmentRootContainer) elem; 78 return container.getLabel() + JavaElementLabels.CONCAT_STRING + container.getJavaProject().getElementName(); 79 } else if (elem instanceof IJarEntryResource) { 80 IJarEntryResource jarEntryResource= (IJarEntryResource) elem; 81 StringBuffer buf= new StringBuffer (jarEntryResource.getName()); 82 buf.append(JavaElementLabels.CONCAT_STRING); 83 IPath fullPath= jarEntryResource.getFullPath(); 84 if (fullPath.segmentCount() > 1) { 85 buf.append(fullPath.removeLastSegments(1).makeRelative()); 86 buf.append(JavaElementLabels.CONCAT_STRING); 87 } 88 JavaElementLabels.getPackageFragmentRootLabel(jarEntryResource.getPackageFragmentRoot(), JavaElementLabels.ROOT_POST_QUALIFIED, buf); 89 return buf.toString(); 90 } else if (elem instanceof IAdaptable) { 91 IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)elem).getAdapter(IWorkbenchAdapter.class); 92 if (wbadapter != null) { 93 return wbadapter.getLabel(elem); 94 } 95 } 96 } 97 } 98 return ""; } 100 101 private String formatJavaElementMessage(IJavaElement element) { 102 return JavaElementLabels.getElementLabel(element, LABEL_FLAGS); 103 } 104 105 private String formatResourceMessage(IResource element) { 106 IContainer parent= element.getParent(); 107 if (parent != null && parent.getType() != IResource.ROOT) 108 return element.getName() + JavaElementLabels.CONCAT_STRING + parent.getFullPath().makeRelative().toString(); 109 else 110 return element.getName(); 111 } 112 113 } 114 | Popular Tags |