KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > StatusBarUpdater


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
39  * Add the <code>StatusBarUpdater</code> to your ViewPart to have the statusbar
40  * describing the selected elements.
41  */

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     /*
55      * @see ISelectionChangedListener#selectionChanged
56      */

57     public void selectionChanged(SelectionChangedEvent event) {
58         String JavaDoc statusBarMessage= formatMessage(event.getSelection());
59         fStatusLineManager.setMessage(statusBarMessage);
60     }
61     
62     
63     protected String JavaDoc 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 JavaDoc 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 JavaDoc buf= new StringBuffer JavaDoc(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 ""; //$NON-NLS-1$
99
}
100         
101     private String JavaDoc formatJavaElementMessage(IJavaElement element) {
102         return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
103     }
104         
105     private String JavaDoc 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