1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.core.runtime.SafeRunner; 17 18 import org.eclipse.core.resources.IStorage; 19 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Image; 22 23 import org.eclipse.jface.util.SafeRunnable; 24 import org.eclipse.jface.viewers.IColorProvider; 25 import org.eclipse.jface.viewers.ILabelDecorator; 26 import org.eclipse.jface.viewers.ILabelProvider; 27 import org.eclipse.jface.viewers.ILabelProviderListener; 28 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 29 30 import org.eclipse.jdt.ui.JavaElementLabels; 31 32 public class JavaUILabelProvider implements ILabelProvider, IColorProvider, IRichLabelProvider { 33 34 protected ListenerList fListeners = new ListenerList(); 35 36 protected JavaElementImageProvider fImageLabelProvider; 37 protected StorageLabelProvider fStorageLabelProvider; 38 39 private ArrayList fLabelDecorators; 40 41 private int fImageFlags; 42 private long fTextFlags; 43 44 47 public JavaUILabelProvider() { 48 this(JavaElementLabels.ALL_DEFAULT, JavaElementImageProvider.OVERLAY_ICONS); 49 } 50 51 55 public JavaUILabelProvider(long textFlags, int imageFlags) { 56 fImageLabelProvider= new JavaElementImageProvider(); 57 fLabelDecorators= null; 58 59 fStorageLabelProvider= new StorageLabelProvider(); 60 fImageFlags= imageFlags; 61 fTextFlags= textFlags; 62 } 63 64 68 public void addLabelDecorator(ILabelDecorator decorator) { 69 if (fLabelDecorators == null) { 70 fLabelDecorators= new ArrayList (2); 71 } 72 fLabelDecorators.add(decorator); 73 } 74 75 79 public final void setTextFlags(long textFlags) { 80 fTextFlags= textFlags; 81 } 82 83 87 public final void setImageFlags(int imageFlags) { 88 fImageFlags= imageFlags; 89 } 90 91 96 public final int getImageFlags() { 97 return fImageFlags; 98 } 99 100 104 public final long getTextFlags() { 105 return fTextFlags; 106 } 107 108 114 protected int evaluateImageFlags(Object element) { 115 return getImageFlags(); 116 } 117 118 123 protected long evaluateTextFlags(Object element) { 124 return getTextFlags(); 125 } 126 127 protected Image decorateImage(Image image, Object element) { 128 if (fLabelDecorators != null && image != null) { 129 for (int i= 0; i < fLabelDecorators.size(); i++) { 130 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i); 131 image= decorator.decorateImage(image, element); 132 } 133 } 134 return image; 135 } 136 137 140 public Image getImage(Object element) { 141 Image result= fImageLabelProvider.getImageLabel(element, evaluateImageFlags(element)); 142 if (result == null && (element instanceof IStorage)) { 143 result= fStorageLabelProvider.getImage(element); 144 } 145 146 return decorateImage(result, element); 147 } 148 149 protected String decorateText(String text, Object element) { 150 if (fLabelDecorators != null && text.length() > 0) { 151 for (int i= 0; i < fLabelDecorators.size(); i++) { 152 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i); 153 String decorated= decorator.decorateText(text, element); 154 if (decorated != null) { 155 text= decorated; 156 } 157 } 158 } 159 return text; 160 } 161 162 163 166 public String getText(Object element) { 167 String result= JavaElementLabels.getTextLabel(element, evaluateTextFlags(element)); 168 if (result.length() == 0 && (element instanceof IStorage)) { 169 result= fStorageLabelProvider.getText(element); 170 } 171 return decorateText(result, element); 172 } 173 174 public ColoredString getRichTextLabel(Object element) { 175 ColoredString string= ColoredJavaElementLabels.getTextLabel(element, evaluateTextFlags(element) | ColoredJavaElementLabels.COLORIZE); 176 if (string.length() == 0 && (element instanceof IStorage)) { 177 string= new ColoredString(fStorageLabelProvider.getText(element)); 178 } 179 String decorated= decorateText(string.getString(), element); 180 if (decorated != null) { 181 return ColoredJavaElementLabels.decorateColoredString(string, decorated, ColoredJavaElementLabels.DECORATIONS_STYLE); 182 } 183 return string; 184 } 185 186 189 public void dispose() { 190 if (fLabelDecorators != null) { 191 for (int i= 0; i < fLabelDecorators.size(); i++) { 192 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i); 193 decorator.dispose(); 194 } 195 fLabelDecorators= null; 196 } 197 fStorageLabelProvider.dispose(); 198 fImageLabelProvider.dispose(); 199 } 200 201 204 public void addListener(ILabelProviderListener listener) { 205 if (fLabelDecorators != null) { 206 for (int i= 0; i < fLabelDecorators.size(); i++) { 207 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i); 208 decorator.addListener(listener); 209 } 210 } 211 fListeners.add(listener); 212 } 213 214 217 public boolean isLabelProperty(Object element, String property) { 218 return true; 219 } 220 221 224 public void removeListener(ILabelProviderListener listener) { 225 if (fLabelDecorators != null) { 226 for (int i= 0; i < fLabelDecorators.size(); i++) { 227 ILabelDecorator decorator= (ILabelDecorator) fLabelDecorators.get(i); 228 decorator.removeListener(listener); 229 } 230 } 231 fListeners.remove(listener); 232 } 233 234 public static ILabelDecorator[] getDecorators(boolean errortick, ILabelDecorator extra) { 235 if (errortick) { 236 if (extra == null) { 237 return new ILabelDecorator[] {}; 238 } else { 239 return new ILabelDecorator[] { extra }; 240 } 241 } 242 if (extra != null) { 243 return new ILabelDecorator[] { extra }; 244 } 245 return null; 246 } 247 248 251 public Color getForeground(Object element) { 252 return null; 253 } 254 255 258 public Color getBackground(Object element) { 259 return null; 260 } 261 262 270 protected void fireLabelProviderChanged(final LabelProviderChangedEvent event) { 271 Object [] listeners = fListeners.getListeners(); 272 for (int i = 0; i < listeners.length; ++i) { 273 final ILabelProviderListener l = (ILabelProviderListener) listeners[i]; 274 SafeRunner.run(new SafeRunnable() { 275 public void run() { 276 l.labelProviderChanged(event); 277 } 278 }); 279 } 280 } 281 282 } 283 | Popular Tags |