001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui;
018:
019: import net.refractions.udig.project.internal.provider.LoadingPlaceHolder;
020:
021: import org.eclipse.emf.common.notify.AdapterFactory;
022: import org.eclipse.emf.edit.provider.IItemLabelProvider;
023: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
024: import org.eclipse.jface.viewers.DecoratingLabelProvider;
025: import org.eclipse.jface.viewers.IColorProvider;
026: import org.eclipse.jface.viewers.ILabelDecorator;
027: import org.eclipse.jface.viewers.StructuredViewer;
028: import org.eclipse.swt.graphics.Color;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.ui.IDecoratorManager;
031: import org.eclipse.ui.PlatformUI;
032:
033: /**
034: * Connects Layers with the usual workbench decorators.
035: * <p>
036: * Similar to the usual eclipse boilerplate code:<pre><code>
037: * new DecoratingLabelProvider(
038: * ProjectExplorer.getProjectExplorer().getAdapterFactory(),
039: * PlatformUI.getWorkbench().getDecoratorManager()
040: * )
041: * </code></pre>
042: *
043: * @author jones
044: * @since 0.6.0
045: */
046: public class AdapterFactoryLabelProviderDecorator extends
047: DecoratingLabelProvider {
048:
049: StructuredViewer viewer;
050:
051: public AdapterFactoryLabelProviderDecorator(AdapterFactory factory,
052: StructuredViewer viewer) {
053: super (new LabelProvider(factory), getWorkbenchDecorators());
054: this .viewer = viewer;
055: }
056:
057: /** Check to see that our lightweight decorators make it into the mix */
058: static final ILabelDecorator getWorkbenchDecorators() {
059: IDecoratorManager manager = PlatformUI.getWorkbench()
060: .getDecoratorManager();
061: ILabelDecorator decorator = manager.getLabelDecorator();
062: return decorator;
063: }
064:
065: /**
066: * @see org.eclipse.jface.viewers.DecoratingLabelProvider#getText(java.lang.Object)
067: */
068: public String getText(Object element) {
069: if (element instanceof LoadingPlaceHolder) {
070: return ((LoadingPlaceHolder) element).getText();
071: }
072: if (!viewer.getControl().isDisposed())
073: return super .getText(element);
074: return null;
075: }
076:
077: @Override
078: public Image getImage(Object element) {
079: if (element instanceof LoadingPlaceHolder) {
080: return ((LoadingPlaceHolder) element).getImage();
081: }
082: if (!viewer.getControl().isDisposed())
083: return super .getImage(element);
084: return null;
085: }
086:
087: @Override
088: public void dispose() {
089: getLabelProvider().dispose();
090: super .dispose();
091: }
092:
093: private static class LabelProvider extends
094: AdapterFactoryLabelProvider implements IColorProvider {
095:
096: public LabelProvider(AdapterFactory adapterFactory) {
097: super (adapterFactory);
098: }
099:
100: public Color getForeground(Object object) {
101: // Get the adapter from the factory.
102: //
103: IColorProvider colorProvider = (IColorProvider) adapterFactory
104: .adapt(object, IColorProvider.class);
105: if (colorProvider != null) {
106: return colorProvider.getForeground(object);
107: }
108:
109: IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory
110: .adapt(object, IItemLabelProvider.class);
111: if (itemLabelProvider instanceof IColorProvider) {
112: colorProvider = (IColorProvider) itemLabelProvider;
113: return colorProvider.getForeground(object);
114: }
115: return null;
116: }
117:
118: public Color getBackground(Object object) {
119: // Get the adapter from the factory.
120: //
121: IColorProvider colorProvider = (IColorProvider) adapterFactory
122: .adapt(object, IColorProvider.class);
123: if (colorProvider != null) {
124: return colorProvider.getBackground(object);
125: }
126:
127: IItemLabelProvider itemLabelProvider = (IItemLabelProvider) adapterFactory
128: .adapt(object, IItemLabelProvider.class);
129: if (itemLabelProvider instanceof IColorProvider) {
130: colorProvider = (IColorProvider) itemLabelProvider;
131: return colorProvider.getBackground(object);
132: }
133: return null;
134: }
135:
136: }
137:
138: }
|