new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / 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
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.action.IStatusLineManager;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24
25 /**
26  * Add the <code>StatusBarUpdater</code> to your ViewPart to have the statusbar
27  * describing the selected elements.
28  */
29 public class StatusBarUpdater implements ISelectionChangedListener {
30         
31         private final int LABEL_FLAGS= JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_POST_QUALIFIED | JavaElementLabels.APPEND_ROOT_PATH |
32                         JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_APP_RETURNTYPE | JavaElementLabels.M_EXCEPTIONS | 
33                         JavaElementLabels.F_APP_TYPE_SIGNATURE;
34                         
35         private IStatusLineManager fStatusLineManager;
36         
37         public StatusBarUpdater(IStatusLineManager statusLineManager) {
38                 fStatusLineManager= statusLineManager;
39         }
40                 
41         /*
42          * @see ISelectionChangedListener#selectionChanged
43          */
44         public void selectionChanged(SelectionChangedEvent event) {
45                 String statusBarMessage= formatMessage(event.getSelection());
46                 fStatusLineManager.setMessage(statusBarMessage);
47         }
48         
49         
50         protected String formatMessage(ISelection sel) {
51                 if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
52                         IStructuredSelection selection= (IStructuredSelection) sel;
53                         
54                         int nElements= selection.size();
55                         if (nElements > 1) {
56                                 return PHPUIMessages.getFormattedString("StatusBarUpdater.num_elements_selected", String.valueOf(nElements)); //$NON-NLS-1$
57                         } else { 
58                                 Object elem= selection.getFirstElement();
59                                 if (elem instanceof IJavaElement) {
60                                         return formatJavaElementMessage((IJavaElement) elem);
61                                 } else if (elem instanceof IResource) {
62                                         return formatResourceMessage((IResource) elem);
63                                 }
64                         }
65                 }
66                 return "";  //$NON-NLS-1$
67         }
68                 
69         private String formatJavaElementMessage(IJavaElement element) {
70                 return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
71         }
72                 
73         private String formatResourceMessage(IResource element) {
74                 IContainer parent= element.getParent();
75                 if (parent != null && parent.getType() != IResource.ROOT)
76                         return element.getName() + JavaElementLabels.CONCAT_STRING + parent.getFullPath().makeRelative().toString();
77                 else
78                         return element.getName();
79         }       
80
81 }