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.tool.info.internal;
018:
019: import java.util.List;
020:
021: import net.refractions.udig.tool.info.LayerPointInfo;
022: import net.refractions.udig.ui.ImageCache;
023:
024: import org.eclipse.jface.viewers.ILabelProvider;
025: import org.eclipse.jface.viewers.ILabelProviderListener;
026: import org.eclipse.jface.viewers.ITreeContentProvider;
027: import org.eclipse.jface.viewers.TreeViewer;
028: import org.eclipse.jface.viewers.Viewer;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.swt.widgets.Composite;
031:
032: /**
033: * Display Information from InfoTool as a Tree.
034: * <p>
035: * InfoTool currently grabs a list from the Renderstack, this may be replaced
036: * with real Information object at somepoint in the future.
037: * </p>
038: * <p>
039: * For the List>LayerPointInfo<:
040: * <ul>
041: * <li>Each LayerPointInfo is a node in the Tree
042: * <li>LayerPointInfo of type gml the value will be a FeatureCollection, each Feature will
043: * be a node in the tree (at least until we get a FeatureCollection Viewer)
044: * </ul>
045: * </p>
046: *
047: * @author Jody Garnett, Refractions Research
048: * @since 0.6
049: */
050: public class InfoTreeViewer extends TreeViewer implements
051: ITreeContentProvider, ILabelProvider {
052:
053: List<LayerPointInfo> information = null;
054: ImageCache imageCache = new ImageCache();
055:
056: /**
057: * Construct <code>CatalogTreeViewer</code>.
058: *
059: * @param parent
060: */
061: public InfoTreeViewer(Composite parent) {
062: super (parent);
063: setContentProvider(this );
064: setLabelProvider(this );
065: setInput(information);
066: expandToLevel(2);
067: }
068:
069: /**
070: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
071: */
072: public void dispose() {
073: if (information != null) {
074: information.clear();
075: information = null;
076: }
077: if (imageCache != null) {
078: imageCache.dispose();
079: imageCache = null;
080: }
081: }
082:
083: /**
084: * Provides a mapping from Information to TreeViewer.
085: * <p>
086: * For the List>LayerPointInfo<:
087: * <ul>
088: * <li>Each LayerPointInfo is a node in the Tree
089: * <li>LayerPointInfo of type gml the value will be a FeatureCollection, each Feature will
090: * be a node in the tree (at least until we get a FeatureCollection Viewer)
091: * </ul>
092: * </p>
093: */
094:
095: /**
096: * Returns the child elements of the given parent element.
097: * <p>
098: * The difference between this method and <code>IStructuredContentProvider.getElements</code>
099: * is that <code>getElements</code> is called to obtain the
100: * tree viewer's root elements, whereas <code>getChildren</code> is used
101: * to obtain the children of a given parent element in the tree (including a root).
102: * </p>
103: * The result is not modified by the viewer.
104: * </p>
105: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
106: *
107: * @param parent the parent element
108: * @return an array of child elements
109: *
110: */
111: public Object[] getChildren(Object parent) {
112: if (parent == information) {
113: return information.toArray();
114: }
115: return null;
116: }
117:
118: /**
119: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
120: */
121: public Object getParent(Object element) {
122: if (element == information) {
123: return null;
124: } else if (element instanceof LayerPointInfo) {
125: return information;
126: }
127: return null;
128: }
129:
130: /**
131: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
132: */
133: public boolean hasChildren(Object element) {
134: return (element == information);
135: }
136:
137: /**
138: * Returns the elements to display in the viewer
139: * when its input is set to the given element.
140: * These elements can be presented as rows in a table, items in a list, etc.
141: * The result is not modified by the viewer.
142: *
143: * @param inputElement the input element
144: * @return the array of elements to display in the viewer
145: */
146: public Object[] getElements(Object inputElement) {
147: return getChildren(inputElement);
148: }
149:
150: /**
151: * Notificaiton that the input has been changed.
152: *
153: * @see org.eclipse.jface.viewers.AbstractTreeViewer#inputChanged(java.lang.Object, java.lang.Object)
154: * @param input
155: * @param oldInput
156: */
157: @SuppressWarnings("unchecked")
158: protected void inputChanged(Object input, Object oldInput) {
159: if (oldInput != null && oldInput instanceof List) {
160: List oldInformation = (List) oldInput;
161: oldInformation.clear();
162: }
163: if (input == null) {
164: information = null;
165: } else if (input instanceof List) {
166: information = (List<LayerPointInfo>) input;
167: }
168: }
169:
170: /**
171: * Reset the viewer to new information
172: *
173: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
174: * @param viewer
175: * @param oldInput
176: * @param newInput
177: */
178: public void inputChanged(Viewer viewer, Object oldInput,
179: Object newInput) {
180: // viewer should equal InfoTreeViewer.this
181: inputChanged(newInput, oldInput);
182: }
183:
184: /*
185: * Image based on layer of selected info
186: */
187: public Image getImage(Object element) {
188: if (element == information) {
189: return null; // Use information symbol?
190: }
191: if (element instanceof LayerPointInfo) {
192: LayerPointInfo info = (LayerPointInfo) element;
193: return imageCache.getImage(info.getLayer().getGlyph());
194: }
195: return null;
196: }
197:
198: public String getText(Object element) {
199: if (element == information) {
200: return "information"; //$NON-NLS-1$
201: }
202: if (element instanceof LayerPointInfo) {
203: LayerPointInfo info = (LayerPointInfo) element;
204: return info.getLayer().getName();
205: }
206: return null;
207: }
208:
209: public void addListener(ILabelProviderListener listener) {
210: // ignore as we reset the information on each request
211: }
212:
213: public boolean isLabelProperty(Object element, String property) {
214: return false; // we don't issue events
215: }
216:
217: public void removeListener(ILabelProviderListener listener) {
218: // ignore as we reset the information on each request
219: }
220: }
|