KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > navigator > JavaNavigatorLabelProvider


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.navigator;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IResource;
15
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Image;
18
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.jface.viewers.ILabelDecorator;
22 import org.eclipse.jface.viewers.ILabelProviderListener;
23
24 import org.eclipse.ui.IMemento;
25 import org.eclipse.ui.navigator.ICommonContentExtensionSite;
26 import org.eclipse.ui.navigator.ICommonLabelProvider;
27 import org.eclipse.ui.navigator.IExtensionStateModel;
28
29 import org.eclipse.jdt.core.IJavaElement;
30
31 import org.eclipse.jdt.ui.JavaElementLabels;
32
33 import org.eclipse.jdt.internal.ui.navigator.IExtensionStateConstants.Values;
34 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider;
35 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerLabelProvider;
36
37 /**
38  * Provides the labels for the Project Explorer.
39  * <p>
40  * It provides labels for the packages in hierarchical layout and in all other
41  * cases delegates it to its super class.
42  * </p>
43  *
44  * @since 3.2
45  */

46 public class JavaNavigatorLabelProvider implements ICommonLabelProvider {
47
48     private final long LABEL_FLAGS = JavaElementLabels.DEFAULT_QUALIFIED
49             | JavaElementLabels.ROOT_POST_QUALIFIED
50             | JavaElementLabels.APPEND_ROOT_PATH
51             | JavaElementLabels.M_PARAMETER_TYPES
52             | JavaElementLabels.M_PARAMETER_NAMES
53             | JavaElementLabels.M_APP_RETURNTYPE
54             | JavaElementLabels.M_EXCEPTIONS
55             | JavaElementLabels.F_APP_TYPE_SIGNATURE
56             | JavaElementLabels.T_TYPE_PARAMETERS;
57
58     private PackageExplorerLabelProvider delegeteLabelProvider;
59
60     private PackageExplorerContentProvider fContentProvider;
61
62     private IExtensionStateModel fStateModel;
63
64     private IPropertyChangeListener fLayoutPropertyListener;
65
66     public JavaNavigatorLabelProvider() {
67
68     }
69     public void init(ICommonContentExtensionSite commonContentExtensionSite) {
70         fStateModel = commonContentExtensionSite.getExtensionStateModel();
71         fContentProvider = (PackageExplorerContentProvider) commonContentExtensionSite.getExtension().getContentProvider();
72         delegeteLabelProvider = createLabelProvider();
73
74         delegeteLabelProvider.setIsFlatLayout(fStateModel
75                 .getBooleanProperty(Values.IS_LAYOUT_FLAT));
76         fLayoutPropertyListener = new IPropertyChangeListener() {
77             public void propertyChange(PropertyChangeEvent event) {
78                 if (Values.IS_LAYOUT_FLAT.equals(event.getProperty())) {
79                     if (event.getNewValue() != null) {
80                         boolean newValue = ((Boolean JavaDoc) event.getNewValue())
81                                 .booleanValue() ? true : false;
82                         delegeteLabelProvider.setIsFlatLayout(newValue);
83                     }
84                 }
85
86             }
87         };
88         fStateModel.addPropertyChangeListener(fLayoutPropertyListener);
89     }
90
91     public String JavaDoc getDescription(Object JavaDoc element) {
92         return formatMessage(element);
93     }
94
95     private PackageExplorerLabelProvider createLabelProvider() {
96         return new PackageExplorerLabelProvider(fContentProvider);
97     }
98
99     public void dispose() {
100         delegeteLabelProvider.dispose();
101         fStateModel.removePropertyChangeListener(fLayoutPropertyListener);
102     }
103
104     public void propertyChange(PropertyChangeEvent event) {
105         delegeteLabelProvider.propertyChange(event);
106     }
107
108     public void addLabelDecorator(ILabelDecorator decorator) {
109         delegeteLabelProvider.addLabelDecorator(decorator);
110     }
111
112     public void addListener(ILabelProviderListener listener) {
113         delegeteLabelProvider.addListener(listener);
114     }
115
116     public Color getBackground(Object JavaDoc element) {
117         return delegeteLabelProvider.getBackground(element);
118     }
119
120     public Color getForeground(Object JavaDoc element) {
121         return delegeteLabelProvider.getForeground(element);
122     }
123
124     public Image getImage(Object JavaDoc element) {
125         return delegeteLabelProvider.getImage(element);
126     }
127
128     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
129         return delegeteLabelProvider.isLabelProperty(element, property);
130     }
131
132     public void removeListener(ILabelProviderListener listener) {
133         delegeteLabelProvider.removeListener(listener);
134     }
135
136     public boolean equals(Object JavaDoc obj) {
137         return delegeteLabelProvider.equals(obj);
138     }
139
140     public int hashCode() {
141         return delegeteLabelProvider.hashCode();
142     }
143
144     public String JavaDoc toString() {
145         return delegeteLabelProvider.toString();
146     }
147
148     public String JavaDoc getText(Object JavaDoc element) {
149         return delegeteLabelProvider.getText(element);
150     }
151
152     public void setIsFlatLayout(boolean state) {
153         delegeteLabelProvider.setIsFlatLayout(state);
154     }
155
156     // Taken from StatusBarUpdater
157

158     private String JavaDoc formatMessage(Object JavaDoc element) {
159         if (element instanceof IJavaElement) {
160             return formatJavaElementMessage((IJavaElement) element);
161         } else if (element instanceof IResource) {
162             return formatResourceMessage((IResource) element);
163         }
164         return ""; //$NON-NLS-1$
165
}
166
167     private String JavaDoc formatJavaElementMessage(IJavaElement element) {
168         return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
169     }
170
171     private String JavaDoc formatResourceMessage(IResource element) {
172         IContainer parent = element.getParent();
173         if (parent != null && parent.getType() != IResource.ROOT)
174             return element.getName() + JavaElementLabels.CONCAT_STRING
175                     + parent.getFullPath().makeRelative().toString();
176         else
177             return element.getName();
178     }
179     
180     public void restoreState(IMemento memento) {
181         
182     }
183     
184     public void saveState(IMemento memento) {
185         
186     }
187  
188 }
189
Popular Tags