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 java.util.ArrayList;
15 import org.eclipse.core.resources.IStorage;
16 import org.eclipse.jface.viewers.ILabelDecorator;
17 import org.eclipse.jface.viewers.ILabelProviderListener;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.swt.graphics.Image;
21 public class JavaUILabelProvider extends LabelProvider {
23 protected JavaElementImageProvider fImageLabelProvider;
25 protected StorageLabelProvider fStorageLabelProvider;
27 protected ArrayList fLabelDecorators;
29 private int fImageFlags;
31 private int fTextFlags;
34 * Creates a new label provider with default flags.
36 // public JavaUILabelProvider() {
37 // this(JavaElementLabels.M_PARAMETER_TYPES,
38 // JavaElementImageProvider.OVERLAY_ICONS);
43 * Flags defined in <code>JavaElementLabels</code>.
45 * Flags defined in <code>JavaElementImageProvider</code>.
47 public JavaUILabelProvider(int textFlags, int imageFlags) {
48 fImageLabelProvider = new JavaElementImageProvider();
49 fLabelDecorators = null;
51 fStorageLabelProvider = new StorageLabelProvider();
52 fImageFlags = imageFlags;
53 fTextFlags = textFlags;
57 * Adds a decorator to the label provider
59 public void addLabelDecorator(ILabelDecorator decorator) {
60 if (fLabelDecorators == null) {
61 fLabelDecorators = new ArrayList(2);
63 fLabelDecorators.add(decorator);
70 * The textFlags to set
72 public final void setTextFlags(int textFlags) {
73 fTextFlags = textFlags;
80 * The imageFlags to set
82 public final void setImageFlags(int imageFlags) {
83 fImageFlags = imageFlags;
87 * Gets the image flags. Can be overwriten by super classes.
89 * @return Returns a int
91 public final int getImageFlags() {
96 * Gets the text flags.
98 * @return Returns a int
100 public final int getTextFlags() {
105 * Evaluates the image flags for a element. Can be overwriten by super
108 * @return Returns a int
110 protected int evaluateImageFlags(Object element) {
111 return getImageFlags();
115 * Evaluates the text flags for a element. Can be overwriten by super
118 * @return Returns a int
120 protected int evaluateTextFlags(Object element) {
121 return getTextFlags();
127 * @see ILabelProvider#getImage
129 public Image getImage(Object element) {
130 Image result = fImageLabelProvider.getImageLabel(element,
131 evaluateImageFlags(element));
132 if (result == null && (element instanceof IStorage)) {
133 result = fStorageLabelProvider.getImage(element);
135 if (fLabelDecorators != null && result != null) {
136 for (int i = 0; i < fLabelDecorators.size(); i++) {
137 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators
139 result = decorator.decorateImage(result, element);
148 * @see ILabelProvider#getText
150 public String getText(Object element) {
151 String result = JavaElementLabels.getTextLabel(element,
152 evaluateTextFlags(element));
153 if (result.length() == 0 && (element instanceof IStorage)) {
154 result = fStorageLabelProvider.getText(element);
156 if (fLabelDecorators != null && result.length() > 0) {
157 for (int i = 0; i < fLabelDecorators.size(); i++) {
158 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators
160 result = decorator.decorateText(result, element);
169 * @see IBaseLabelProvider#dispose
171 public void dispose() {
172 if (fLabelDecorators != null) {
173 for (int i = 0; i < fLabelDecorators.size(); i++) {
174 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators
178 fLabelDecorators = null;
180 fStorageLabelProvider.dispose();
181 fImageLabelProvider.dispose();
187 * @see IBaseLabelProvider#addListener(ILabelProviderListener)
189 public void addListener(ILabelProviderListener listener) {
190 if (fLabelDecorators != null) {
191 for (int i = 0; i < fLabelDecorators.size(); i++) {
192 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators
194 decorator.addListener(listener);
197 super.addListener(listener);
203 * @see IBaseLabelProvider#isLabelProperty(Object, String)
205 public boolean isLabelProperty(Object element, String property) {
212 * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
214 public void removeListener(ILabelProviderListener listener) {
215 if (fLabelDecorators != null) {
216 for (int i = 0; i < fLabelDecorators.size(); i++) {
217 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators
219 decorator.removeListener(listener);
222 super.removeListener(listener);
225 // public static ILabelDecorator[] getDecorators(boolean errortick,
226 // ILabelDecorator extra) {
228 // if (extra == null) {
229 // return new ILabelDecorator[] {};
231 // return new ILabelDecorator[] { extra };
234 // if (extra != null) {
235 // return new ILabelDecorator[] { extra };