1 11 package org.eclipse.ui.model; 12 13 import org.eclipse.jface.resource.ImageDescriptor; 14 import org.eclipse.jface.viewers.DecoratingLabelProvider; 15 import org.eclipse.jface.viewers.IColorProvider; 16 import org.eclipse.jface.viewers.IFontProvider; 17 import org.eclipse.jface.viewers.ILabelProvider; 18 import org.eclipse.jface.viewers.LabelProvider; 19 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.FontData; 23 import org.eclipse.swt.graphics.Image; 24 import org.eclipse.swt.graphics.RGB; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.ui.IEditorRegistry; 27 import org.eclipse.ui.IPropertyListener; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.internal.util.SWTResourceUtil; 30 import org.eclipse.ui.internal.util.Util; 31 32 39 public class WorkbenchLabelProvider extends LabelProvider implements 40 IColorProvider, IFontProvider { 41 42 49 public static ILabelProvider getDecoratingWorkbenchLabelProvider() { 50 return new DecoratingLabelProvider(new WorkbenchLabelProvider(), 51 PlatformUI.getWorkbench().getDecoratorManager() 52 .getLabelDecorator()); 53 } 54 55 60 private IPropertyListener editorRegistryListener = new IPropertyListener() { 61 public void propertyChanged(Object source, int propId) { 62 if (propId == IEditorRegistry.PROP_CONTENTS) { 63 fireLabelProviderChanged(new LabelProviderChangedEvent(WorkbenchLabelProvider.this)); 64 } 65 } 66 }; 67 68 71 public WorkbenchLabelProvider() { 72 PlatformUI.getWorkbench().getEditorRegistry().addPropertyListener(editorRegistryListener); 73 } 74 75 88 protected ImageDescriptor decorateImage(ImageDescriptor input, 89 Object element) { 90 return input; 91 } 92 93 104 protected String decorateText(String input, Object element) { 105 return input; 106 } 107 108 111 public void dispose() { 112 PlatformUI.getWorkbench().getEditorRegistry().removePropertyListener(editorRegistryListener); 113 super.dispose(); 114 } 115 116 123 protected final IWorkbenchAdapter getAdapter(Object o) { 124 return (IWorkbenchAdapter)Util.getAdapter(o, IWorkbenchAdapter.class); 125 } 126 127 134 protected final IWorkbenchAdapter2 getAdapter2(Object o) { 135 return (IWorkbenchAdapter2)Util.getAdapter(o, IWorkbenchAdapter2.class); 136 } 137 138 141 public final Image getImage(Object element) { 142 IWorkbenchAdapter adapter = getAdapter(element); 144 if (adapter == null) { 145 return null; 146 } 147 ImageDescriptor descriptor = adapter.getImageDescriptor(element); 148 if (descriptor == null) { 149 return null; 150 } 151 152 descriptor = decorateImage(descriptor, element); 154 155 Image image = (Image) SWTResourceUtil.getImageTable().get(descriptor); 156 if (image == null) { 157 image = descriptor.createImage(); 158 SWTResourceUtil.getImageTable().put(descriptor, image); 159 } 160 return image; 161 } 162 163 166 public final String getText(Object element) { 167 IWorkbenchAdapter adapter = getAdapter(element); 169 if (adapter == null) { 170 return ""; } 172 String label = adapter.getLabel(element); 173 174 return decorateText(label, element); 176 } 177 178 181 public Color getForeground(Object element) { 182 return getColor(element, true); 183 } 184 185 188 public Color getBackground(Object element) { 189 return getColor(element, false); 190 } 191 192 195 public Font getFont(Object element) { 196 IWorkbenchAdapter2 adapter = getAdapter2(element); 197 if (adapter == null) { 198 return null; 199 } 200 201 FontData descriptor = adapter.getFont(element); 202 if (descriptor == null) { 203 return null; 204 } 205 206 Font font = (Font) SWTResourceUtil.getFontTable().get(descriptor); 207 if (font == null) { 208 font = new Font(Display.getCurrent(), descriptor); 209 SWTResourceUtil.getFontTable().put(descriptor, font); 210 } 211 return font; 212 } 213 214 private Color getColor(Object element, boolean forground) { 215 IWorkbenchAdapter2 adapter = getAdapter2(element); 216 if (adapter == null) { 217 return null; 218 } 219 RGB descriptor = forground ? adapter.getForeground(element) : adapter 220 .getBackground(element); 221 if (descriptor == null) { 222 return null; 223 } 224 225 Color color = (Color) SWTResourceUtil.getColorTable().get(descriptor); 226 if (color == null) { 227 color = new Color(Display.getCurrent(), descriptor); 228 SWTResourceUtil.getColorTable().put(descriptor, color); 229 } 230 return color; 231 } 232 } 233 | Popular Tags |