1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.jface.action.IStatusLineManager;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 * Add the <code>StatusBarUpdater</code> to your ViewPart to have the
26 * statusbar describing the selected elements.
28 public class StatusBarUpdater implements ISelectionChangedListener {
30 private final int LABEL_FLAGS = JavaElementLabels.DEFAULT_QUALIFIED
31 | JavaElementLabels.ROOT_POST_QUALIFIED
32 | JavaElementLabels.APPEND_ROOT_PATH
33 | JavaElementLabels.M_PARAMETER_TYPES
34 | JavaElementLabels.M_PARAMETER_NAMES
35 | JavaElementLabels.M_APP_RETURNTYPE
36 | JavaElementLabels.M_EXCEPTIONS
37 | JavaElementLabels.F_APP_TYPE_SIGNATURE;
39 private IStatusLineManager fStatusLineManager;
41 public StatusBarUpdater(IStatusLineManager statusLineManager) {
42 fStatusLineManager = statusLineManager;
46 * @see ISelectionChangedListener#selectionChanged
48 public void selectionChanged(SelectionChangedEvent event) {
49 String statusBarMessage = formatMessage(event.getSelection());
50 fStatusLineManager.setMessage(statusBarMessage);
53 protected String formatMessage(ISelection sel) {
54 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
55 IStructuredSelection selection = (IStructuredSelection) sel;
57 int nElements = selection.size();
61 "StatusBarUpdater.num_elements_selected", String.valueOf(nElements)); //$NON-NLS-1$
63 Object elem = selection.getFirstElement();
64 if (elem instanceof IJavaElement) {
65 return formatJavaElementMessage((IJavaElement) elem);
66 } else if (elem instanceof IResource) {
67 return formatResourceMessage((IResource) elem);
71 return ""; //$NON-NLS-1$
74 private String formatJavaElementMessage(IJavaElement element) {
75 return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
78 private String formatResourceMessage(IResource element) {
79 IContainer parent = element.getParent();
80 if (parent != null && parent.getType() != IResource.ROOT)
81 return element.getName() + JavaElementLabels.CONCAT_STRING
82 + parent.getFullPath().makeRelative().toString();
84 return element.getName();