refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / viewsupport / StatusBarUpdater.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
15
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;
23
24 /**
25  * Add the <code>StatusBarUpdater</code> to your ViewPart to have the
26  * statusbar describing the selected elements.
27  */
28 public class StatusBarUpdater implements ISelectionChangedListener {
29
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;
38
39         private IStatusLineManager fStatusLineManager;
40
41         public StatusBarUpdater(IStatusLineManager statusLineManager) {
42                 fStatusLineManager = statusLineManager;
43         }
44
45         /*
46          * @see ISelectionChangedListener#selectionChanged
47          */
48         public void selectionChanged(SelectionChangedEvent event) {
49                 String statusBarMessage = formatMessage(event.getSelection());
50                 fStatusLineManager.setMessage(statusBarMessage);
51         }
52
53         protected String formatMessage(ISelection sel) {
54                 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
55                         IStructuredSelection selection = (IStructuredSelection) sel;
56
57                         int nElements = selection.size();
58                         if (nElements > 1) {
59                                 return PHPUIMessages
60                                                 .getFormattedString(
61                                                                 "StatusBarUpdater.num_elements_selected", String.valueOf(nElements)); //$NON-NLS-1$
62                         } else {
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);
68                                 }
69                         }
70                 }
71                 return ""; //$NON-NLS-1$
72         }
73
74         private String formatJavaElementMessage(IJavaElement element) {
75                 return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
76         }
77
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();
83                 else
84                         return element.getName();
85         }
86
87 }