01: /*
02: * uDig - User Friendly Desktop Internet GIS client
03: * http://udig.refractions.net
04: * (C) 2004, Refractions Research Inc.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package net.refractions.udig.catalog.ui;
18:
19: import net.refractions.udig.catalog.CatalogPlugin;
20: import net.refractions.udig.catalog.IResolve;
21: import net.refractions.udig.catalog.IResolveChangeEvent;
22: import net.refractions.udig.catalog.IResolveChangeListener;
23:
24: import org.eclipse.jface.viewers.LabelProvider;
25: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
26: import org.eclipse.swt.graphics.Image;
27: import org.eclipse.swt.widgets.Display;
28:
29: /**
30: * Label generation for IResolve (must be non-blocking and quick).
31: * <p>
32: * Compare and contrast with ResovleLabelDecorator which is allowed to block.
33: * </p>
34: *
35: * @author jgarnett
36: * @since 0.7.0
37: */
38: public class ResolveLabelProviderSimple extends LabelProvider implements
39: IResolveChangeListener {
40:
41: public ResolveLabelProviderSimple() {
42: CatalogPlugin.addListener(this );
43: }
44:
45: /*
46: * @see net.refractions.udig.catalog.IResolveChangeListener#changed(net.refractions.udig.catalog.IResolveChangeEvent)
47: */
48: public void changed(final IResolveChangeEvent event) {
49: if (event.getType() != IResolveChangeEvent.Type.POST_CHANGE)
50: return;
51:
52: final IResolve resolve = event.getResolve();
53: if (resolve == null)
54: return;
55:
56: Display.getDefault().asyncExec(new Runnable() {
57: public void run() {
58: fireLabelProviderChanged(new LabelProviderChangedEvent(
59: ResolveLabelProviderSimple.this , resolve));
60: }
61: });
62: }
63:
64: /**
65: * Generate text from the resolve.getURI()
66: * <p>
67: * Note this name is only used as a first try, the ResolveLabelDecorator is expected to provide
68: * a label based on Name or Title information.
69: * </p>
70: *
71: * @param element
72: * @return label based on IResolve.getIdentifier
73: */
74: public String getText(Object element) {
75: if (element instanceof IResolve) {
76: return CatalogUIPlugin.label((IResolve) element);
77: }
78: return super .getText(element);
79: }
80:
81: /**
82: * Obtain image for the provided element.
83: * <p>
84: * To accomplish this quickly we simply make use of constants from CatalogUIPlugin. We need a
85: * second pass that makes use of the real icon from the real resource.
86: * </p>
87: *
88: * @param element is expeced to be IResolve
89: * @return the image used to label the element, or <code>null</code> if there is no image for
90: * the given object
91: * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
92: */
93: public Image getImage(Object element) {
94: if (element instanceof IResolve) {
95: return CatalogUIPlugin.image((IResolve) element);
96: }
97: return super.getImage(element);
98: }
99: }
|