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.internal.adapters;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import net.refractions.udig.project.ui.internal.Messages;
023:
024: import org.eclipse.core.runtime.Platform;
025: import org.eclipse.jface.viewers.ITreeContentProvider;
026: import org.eclipse.jface.viewers.LabelProvider;
027: import org.eclipse.jface.viewers.Viewer;
028: import org.eclipse.ui.views.properties.IPropertyDescriptor;
029: import org.eclipse.ui.views.properties.IPropertySource;
030:
031: /**
032: * Provides ...TODO summary sentence
033: * <p>
034: * TODO Description
035: * </p>
036: * <p>
037: * Responsibilities:
038: * <ul>
039: * <li>
040: * <li>
041: * </ul>
042: * </p>
043: * <p>
044: * Example Use:
045: *
046: * <pre><code>
047: * FeatureViewerProvider x = new FeatureViewerProvider( ... );
048: * TODO code example
049: * </code></pre>
050: *
051: * </p>
052: *
053: * @author jones
054: * @since 0.3
055: */
056: public class FeatureViewerProvider extends LabelProvider implements
057: ITreeContentProvider {
058:
059: public static class Databag {
060: IPropertySource source;
061: IPropertyDescriptor descriptor;
062: }
063:
064: /**
065: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
066: */
067: public Object[] getElements(Object inputElement) {
068: Object[] children = getChildren(inputElement);
069: if (children == null)
070: return null;
071: List list = new ArrayList();
072: for (int i = 0; i < children.length; i++) {
073: if (hasChildren(children[i])) {
074: Object[] elements = getElements(children[i]);
075: if (elements == null)
076: continue;
077: for (int j = 0; j < elements.length; j++) {
078: list.add(elements[j]);
079: }
080: } else
081: list.add(children[i]);
082: }
083: return list.toArray();
084: }
085:
086: /**
087: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
088: * java.lang.Object, java.lang.Object)
089: */
090: public void inputChanged(Viewer viewer, Object oldInput,
091: Object newInput) {
092: // TODO implement method body
093: }
094:
095: /**
096: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
097: */
098: public Object[] getChildren(Object parentElement) {
099: if (parentElement instanceof Databag) {
100: Databag data = (Databag) parentElement;
101: Object value = data.source.getPropertyValue(data.descriptor
102: .getId());
103: if (value instanceof IPropertySource)
104: return createChildren((IPropertySource) value);
105: } else if (Platform.getAdapterManager().hasAdapter(
106: parentElement,
107: "org.eclipse.ui.views.properties.IPropertySource")) { //$NON-NLS-1$
108: IPropertySource source = (IPropertySource) Platform
109: .getAdapterManager()
110: .getAdapter(parentElement,
111: "org.eclipse.ui.views.properties.IPropertySource"); //$NON-NLS-1$
112: return createChildren(source);
113: }
114: return null;
115: }
116:
117: Object[] createChildren(IPropertySource source) {
118: IPropertyDescriptor[] descriptors = source
119: .getPropertyDescriptors();
120: Databag[] data = new Databag[descriptors.length];
121: for (int i = 0; i < data.length; i++) {
122: data[i] = new Databag();
123: data[i].source = source;
124: data[i].descriptor = descriptors[i];
125: }
126: return data;
127: }
128:
129: /**
130: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
131: */
132: public Object getParent(Object element) {
133: // TODO implement method body
134: return null;
135: }
136:
137: /**
138: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
139: */
140: public boolean hasChildren(Object element) {
141: if (Platform.getAdapterManager().hasAdapter(element,
142: "org.eclipse.ui.views.properties.IPropertySource")) //$NON-NLS-1$
143: return true;
144: if (element instanceof Databag) {
145: Databag data = (Databag) element;
146: Object value = data.source.getPropertyValue(data.descriptor
147: .getId());
148: if (value instanceof IPropertySource)
149: return true;
150: }
151: return false;
152: }
153:
154: /**
155: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
156: */
157: public String getText(Object element) {
158: // TODO implement method body
159: if (element instanceof Databag) {
160: Databag data = (Databag) element;
161: return data.descriptor.getDisplayName();
162: }
163: return Messages.FeatureViewerProvider_unknown;
164: }
165:
166: }
|