001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.navigator;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IResource;
014:
015: import org.eclipse.swt.graphics.Color;
016: import org.eclipse.swt.graphics.Image;
017:
018: import org.eclipse.jface.util.IPropertyChangeListener;
019: import org.eclipse.jface.util.PropertyChangeEvent;
020: import org.eclipse.jface.viewers.ILabelDecorator;
021: import org.eclipse.jface.viewers.ILabelProviderListener;
022:
023: import org.eclipse.ui.IMemento;
024: import org.eclipse.ui.navigator.ICommonContentExtensionSite;
025: import org.eclipse.ui.navigator.ICommonLabelProvider;
026: import org.eclipse.ui.navigator.IExtensionStateModel;
027:
028: import org.eclipse.jdt.core.IJavaElement;
029:
030: import org.eclipse.jdt.ui.JavaElementLabels;
031:
032: import org.eclipse.jdt.internal.ui.navigator.IExtensionStateConstants.Values;
033: import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider;
034: import org.eclipse.jdt.internal.ui.packageview.PackageExplorerLabelProvider;
035:
036: /**
037: * Provides the labels for the Project Explorer.
038: * <p>
039: * It provides labels for the packages in hierarchical layout and in all other
040: * cases delegates it to its super class.
041: * </p>
042: *
043: * @since 3.2
044: */
045: public class JavaNavigatorLabelProvider implements ICommonLabelProvider {
046:
047: private final long LABEL_FLAGS = JavaElementLabels.DEFAULT_QUALIFIED
048: | JavaElementLabels.ROOT_POST_QUALIFIED
049: | JavaElementLabels.APPEND_ROOT_PATH
050: | JavaElementLabels.M_PARAMETER_TYPES
051: | JavaElementLabels.M_PARAMETER_NAMES
052: | JavaElementLabels.M_APP_RETURNTYPE
053: | JavaElementLabels.M_EXCEPTIONS
054: | JavaElementLabels.F_APP_TYPE_SIGNATURE
055: | JavaElementLabels.T_TYPE_PARAMETERS;
056:
057: private PackageExplorerLabelProvider delegeteLabelProvider;
058:
059: private PackageExplorerContentProvider fContentProvider;
060:
061: private IExtensionStateModel fStateModel;
062:
063: private IPropertyChangeListener fLayoutPropertyListener;
064:
065: public JavaNavigatorLabelProvider() {
066:
067: }
068:
069: public void init(
070: ICommonContentExtensionSite commonContentExtensionSite) {
071: fStateModel = commonContentExtensionSite
072: .getExtensionStateModel();
073: fContentProvider = (PackageExplorerContentProvider) commonContentExtensionSite
074: .getExtension().getContentProvider();
075: delegeteLabelProvider = createLabelProvider();
076:
077: delegeteLabelProvider.setIsFlatLayout(fStateModel
078: .getBooleanProperty(Values.IS_LAYOUT_FLAT));
079: fLayoutPropertyListener = new IPropertyChangeListener() {
080: public void propertyChange(PropertyChangeEvent event) {
081: if (Values.IS_LAYOUT_FLAT.equals(event.getProperty())) {
082: if (event.getNewValue() != null) {
083: boolean newValue = ((Boolean) event
084: .getNewValue()).booleanValue() ? true
085: : false;
086: delegeteLabelProvider.setIsFlatLayout(newValue);
087: }
088: }
089:
090: }
091: };
092: fStateModel.addPropertyChangeListener(fLayoutPropertyListener);
093: }
094:
095: public String getDescription(Object element) {
096: return formatMessage(element);
097: }
098:
099: private PackageExplorerLabelProvider createLabelProvider() {
100: return new PackageExplorerLabelProvider(fContentProvider);
101: }
102:
103: public void dispose() {
104: delegeteLabelProvider.dispose();
105: fStateModel
106: .removePropertyChangeListener(fLayoutPropertyListener);
107: }
108:
109: public void propertyChange(PropertyChangeEvent event) {
110: delegeteLabelProvider.propertyChange(event);
111: }
112:
113: public void addLabelDecorator(ILabelDecorator decorator) {
114: delegeteLabelProvider.addLabelDecorator(decorator);
115: }
116:
117: public void addListener(ILabelProviderListener listener) {
118: delegeteLabelProvider.addListener(listener);
119: }
120:
121: public Color getBackground(Object element) {
122: return delegeteLabelProvider.getBackground(element);
123: }
124:
125: public Color getForeground(Object element) {
126: return delegeteLabelProvider.getForeground(element);
127: }
128:
129: public Image getImage(Object element) {
130: return delegeteLabelProvider.getImage(element);
131: }
132:
133: public boolean isLabelProperty(Object element, String property) {
134: return delegeteLabelProvider.isLabelProperty(element, property);
135: }
136:
137: public void removeListener(ILabelProviderListener listener) {
138: delegeteLabelProvider.removeListener(listener);
139: }
140:
141: public boolean equals(Object obj) {
142: return delegeteLabelProvider.equals(obj);
143: }
144:
145: public int hashCode() {
146: return delegeteLabelProvider.hashCode();
147: }
148:
149: public String toString() {
150: return delegeteLabelProvider.toString();
151: }
152:
153: public String getText(Object element) {
154: return delegeteLabelProvider.getText(element);
155: }
156:
157: public void setIsFlatLayout(boolean state) {
158: delegeteLabelProvider.setIsFlatLayout(state);
159: }
160:
161: // Taken from StatusBarUpdater
162:
163: private String formatMessage(Object element) {
164: if (element instanceof IJavaElement) {
165: return formatJavaElementMessage((IJavaElement) element);
166: } else if (element instanceof IResource) {
167: return formatResourceMessage((IResource) element);
168: }
169: return ""; //$NON-NLS-1$
170: }
171:
172: private String formatJavaElementMessage(IJavaElement element) {
173: return JavaElementLabels.getElementLabel(element, LABEL_FLAGS);
174: }
175:
176: private String formatResourceMessage(IResource element) {
177: IContainer parent = element.getParent();
178: if (parent != null && parent.getType() != IResource.ROOT)
179: return element.getName() + JavaElementLabels.CONCAT_STRING
180: + parent.getFullPath().makeRelative().toString();
181: else
182: return element.getName();
183: }
184:
185: public void restoreState(IMemento memento) {
186:
187: }
188:
189: public void saveState(IMemento memento) {
190:
191: }
192:
193: }
|