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;
17 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.jface.viewers.ILabelDecorator;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.LabelProvider;
23 public class JavaUILabelProvider extends LabelProvider {
25 protected JavaElementImageProvider fImageLabelProvider;
26 protected StorageLabelProvider fStorageLabelProvider;
28 protected ArrayList fLabelDecorators;
30 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, JavaElementImageProvider.OVERLAY_ICONS);
41 * @param textFlags Flags defined in <code>JavaElementLabels</code>.
42 * @param imageFlags Flags defined in <code>JavaElementImageProvider</code>.
44 public JavaUILabelProvider(int textFlags, int imageFlags) {
45 fImageLabelProvider= new JavaElementImageProvider();
46 fLabelDecorators= null;
48 fStorageLabelProvider= new StorageLabelProvider();
49 fImageFlags= imageFlags;
50 fTextFlags= textFlags;
54 * Adds a decorator to the label provider
56 public void addLabelDecorator(ILabelDecorator decorator) {
57 if (fLabelDecorators == null) {
58 fLabelDecorators= new ArrayList(2);
60 fLabelDecorators.add(decorator);
65 * @param textFlags The textFlags to set
67 public final void setTextFlags(int textFlags) {
68 fTextFlags= textFlags;
73 * @param imageFlags The imageFlags to set
75 public final void setImageFlags(int imageFlags) {
76 fImageFlags= imageFlags;
80 * Gets the image flags.
81 * Can be overwriten by super classes.
82 * @return Returns a int
84 public final int getImageFlags() {
89 * Gets the text flags.
90 * @return Returns a int
92 public final int getTextFlags() {
97 * Evaluates the image flags for a element.
98 * Can be overwriten by super classes.
99 * @return Returns a int
101 protected int evaluateImageFlags(Object element) {
102 return getImageFlags();
106 * Evaluates the text flags for a element. Can be overwriten by super classes.
107 * @return Returns a int
109 protected int evaluateTextFlags(Object element) {
110 return getTextFlags();
115 * @see ILabelProvider#getImage
117 public Image getImage(Object element) {
118 Image result= fImageLabelProvider.getImageLabel(element, evaluateImageFlags(element));
119 if (result == null && (element instanceof IStorage)) {
120 result= fStorageLabelProvider.getImage(element);
122 if (fLabelDecorators != null && result != null) {
123 for (int i= 0; i < fLabelDecorators.size(); i++) {
124 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
125 result= decorator.decorateImage(result, element);
132 * @see ILabelProvider#getText
134 public String getText(Object element) {
135 String result= JavaElementLabels.getTextLabel(element, evaluateTextFlags(element));
136 if (result.length() == 0 && (element instanceof IStorage)) {
137 result= fStorageLabelProvider.getText(element);
139 if (fLabelDecorators != null && result.length() > 0) {
140 for (int i= 0; i < fLabelDecorators.size(); i++) {
141 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
142 result= decorator.decorateText(result, element);
149 * @see IBaseLabelProvider#dispose
151 public void dispose() {
152 if (fLabelDecorators != null) {
153 for (int i= 0; i < fLabelDecorators.size(); i++) {
154 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
157 fLabelDecorators= null;
159 fStorageLabelProvider.dispose();
160 fImageLabelProvider.dispose();
164 * @see IBaseLabelProvider#addListener(ILabelProviderListener)
166 public void addListener(ILabelProviderListener listener) {
167 if (fLabelDecorators != null) {
168 for (int i= 0; i < fLabelDecorators.size(); i++) {
169 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
170 decorator.addListener(listener);
173 super.addListener(listener);
177 * @see IBaseLabelProvider#isLabelProperty(Object, String)
179 public boolean isLabelProperty(Object element, String property) {
184 * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
186 public void removeListener(ILabelProviderListener listener) {
187 if (fLabelDecorators != null) {
188 for (int i= 0; i < fLabelDecorators.size(); i++) {
189 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i);
190 decorator.removeListener(listener);
193 super.removeListener(listener);
196 public static ILabelDecorator[] getDecorators(boolean errortick, ILabelDecorator extra) {
199 return new ILabelDecorator[] {};
201 return new ILabelDecorator[] { extra };
205 return new ILabelDecorator[] { extra };