1 11 package org.eclipse.ui.internal.navigator; 12 13 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.core.commands.common.EventManager; 18 import org.eclipse.core.runtime.SafeRunner; 19 import org.eclipse.jface.util.SafeRunnable; 20 import org.eclipse.jface.viewers.IColorProvider; 21 import org.eclipse.jface.viewers.IFontProvider; 22 import org.eclipse.jface.viewers.ILabelProvider; 23 import org.eclipse.jface.viewers.ILabelProviderListener; 24 import org.eclipse.jface.viewers.ITreePathLabelProvider; 25 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 26 import org.eclipse.jface.viewers.TreePath; 27 import org.eclipse.jface.viewers.ViewerLabel; 28 import org.eclipse.osgi.util.NLS; 29 import org.eclipse.swt.graphics.Color; 30 import org.eclipse.swt.graphics.Font; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentExtension; 33 import org.eclipse.ui.navigator.CommonViewer; 34 import org.eclipse.ui.navigator.INavigatorContentDescriptor; 35 import org.eclipse.ui.navigator.INavigatorContentService; 36 37 63 public class NavigatorContentServiceLabelProvider extends EventManager 64 implements ILabelProvider, IColorProvider, IFontProvider, ITreePathLabelProvider, ILabelProviderListener { 65 66 private final NavigatorContentService contentService; 67 private final boolean isContentServiceSelfManaged; 68 private final ReusableViewerLabel reusableLabel = new ReusableViewerLabel(); 69 70 71 79 public NavigatorContentServiceLabelProvider(NavigatorContentService aContentService) { 80 contentService = aContentService; 81 isContentServiceSelfManaged = false; 82 } 83 84 97 public Image getImage(Object anElement) { 98 99 Set contentExtensions = contentService.findContentExtensionsWithPossibleChild(anElement); 100 Image image = null; 101 for (Iterator itr = contentExtensions.iterator(); itr.hasNext() && image == null; ) { 102 image = findImage((NavigatorContentExtension) itr.next(), anElement); 103 } 104 return image; 105 } 106 107 119 public String getText(Object anElement) { 120 Set contentExtensions = contentService.findContentExtensionsWithPossibleChild(anElement); 121 String text = null; 122 for (Iterator itr = contentExtensions.iterator(); itr.hasNext() && text == null; ) { 123 text = findText((NavigatorContentExtension) itr.next(), anElement); 124 } 125 return text == null ? (NLS.bind(CommonNavigatorMessages.NavigatorContentServiceLabelProvider_Error_no_label_provider_for_0_, anElement)) : text; 127 } 128 129 133 private String findText(NavigatorContentExtension foundExtension, Object anElement) { 134 String text = null; 135 INavigatorContentDescriptor foundDescriptor; 136 text = foundExtension.getLabelProvider().getText(anElement); 137 if(text == null && (foundDescriptor = foundExtension.getDescriptor()).getOverriddenDescriptor() != null) { 138 return findText(contentService.getExtension(foundDescriptor.getOverriddenDescriptor()), anElement); 139 } 140 return text; 141 } 142 143 147 private Image findImage(NavigatorContentExtension foundExtension, Object anElement) { 148 Image image = null; 149 INavigatorContentDescriptor foundDescriptor; 150 image = foundExtension.getLabelProvider().getImage(anElement); 151 if(image == null && (foundDescriptor = foundExtension.getDescriptor()).getOverriddenDescriptor() != null) { 152 return findImage(contentService.getExtension(foundDescriptor.getOverriddenDescriptor()), anElement); 153 } 154 return image; 155 } 156 157 160 public Font getFont(Object anElement) { 161 ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement); 162 for (int i = 0; i < labelProviders.length; i++) { 163 ILabelProvider provider = labelProviders[i]; 164 if (provider instanceof IFontProvider) { 165 IFontProvider fontProvider = (IFontProvider) provider; 166 Font font = fontProvider.getFont(anElement); 167 if (font != null) { 168 return font; 169 } 170 } 171 } 172 return null; 173 } 174 175 178 public Color getForeground(Object anElement) { 179 ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement); 180 for (int i = 0; i < labelProviders.length; i++) { 181 ILabelProvider provider = labelProviders[i]; 182 if (provider instanceof IColorProvider) { 183 IColorProvider colorProvider = (IColorProvider) provider; 184 Color color = colorProvider.getForeground(anElement); 185 if (color != null) { 186 return color; 187 } 188 } 189 } 190 return null; 191 } 192 193 196 public Color getBackground(Object anElement) { 197 ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement); 198 for (int i = 0; i < labelProviders.length; i++) { 199 ILabelProvider provider = labelProviders[i]; 200 if (provider instanceof IColorProvider) { 201 IColorProvider colorProvider = (IColorProvider) provider; 202 Color color = colorProvider.getBackground(anElement); 203 if (color != null) { 204 return color; 205 } 206 } 207 } 208 return null; 209 } 210 211 226 public boolean isLabelProperty(Object anElement, String aProperty) { 227 boolean result = false; 228 ILabelProvider[] labelProviders = contentService.findRelevantLabelProviders(anElement); 229 for (int i = 0; i < labelProviders.length && !result; i++) { 230 result = labelProviders[i].isLabelProperty(anElement, aProperty); 231 } 232 return result; 233 } 234 235 236 244 public void addListener(ILabelProviderListener aListener) { 245 addListenerObject(aListener); 246 } 247 248 256 public void removeListener(ILabelProviderListener aListener) { 257 removeListenerObject(aListener); 258 } 259 260 272 public void dispose() { 273 if (isContentServiceSelfManaged) { 274 contentService.dispose(); 275 } 276 277 } 278 279 287 protected void fireLabelProviderChanged( 288 final LabelProviderChangedEvent event) { 289 Object [] theListeners = getListeners(); 290 for (int i = 0; i < theListeners.length; ++i) { 291 final ILabelProviderListener l = (ILabelProviderListener) theListeners[i]; 292 SafeRunner.run(new SafeRunnable() { 293 public void run() { 294 l.labelProviderChanged(event); 295 } 296 }); 297 298 } 299 } 300 301 304 public void updateLabel(ViewerLabel label, TreePath elementPath) { 305 306 Set contentExtensions = contentService.findContentExtensionsWithPossibleChild(elementPath.getLastSegment()); 307 reusableLabel.reset(label); 308 for (Iterator itr = contentExtensions.iterator(); itr.hasNext() && !(reusableLabel.isValid() && reusableLabel.hasChanged()); ) { 309 findUpdateLabel((NavigatorContentExtension)itr.next(), reusableLabel, elementPath); 310 } 311 reusableLabel.fill(label); 312 } 313 314 315 319 private void findUpdateLabel(NavigatorContentExtension foundExtension, ReusableViewerLabel label, TreePath elementPath) { 320 321 ILabelProvider labelProvider = foundExtension.getLabelProvider(); 322 if (labelProvider instanceof ITreePathLabelProvider) { 323 ITreePathLabelProvider tplp = (ITreePathLabelProvider) labelProvider; 324 tplp.updateLabel(label, elementPath); 325 } else { 326 label.setImage(labelProvider.getImage(elementPath.getLastSegment())); 327 label.setText(labelProvider.getText(elementPath.getLastSegment())); 328 } 329 330 if(shouldContinue(label, foundExtension)) { 331 findUpdateLabel(contentService.getExtension(foundExtension.getDescriptor().getOverriddenDescriptor()), label, elementPath); 332 } 333 } 334 335 private boolean shouldContinue(ReusableViewerLabel label, NavigatorContentExtension foundExtension) { 336 337 if(foundExtension.getDescriptor().getOverriddenDescriptor() != null) { 338 return !(label.isValid() && label.hasChanged()); 339 } 340 return false; 341 } 342 343 346 public void labelProviderChanged(LabelProviderChangedEvent event) { 347 fireLabelProviderChanged(event); 348 } 349 350 } 351 | Popular Tags |