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 net.refractions.udig.catalog.IGeoResource;
020: import net.refractions.udig.project.AdaptableFeature;
021: import net.refractions.udig.project.ui.internal.properties.CoordinatePropertySource;
022: import net.refractions.udig.project.ui.internal.properties.FeaturePropertySource;
023: import net.refractions.udig.project.ui.internal.properties.GeomPropertySource;
024: import net.refractions.udig.project.ui.internal.properties.IGeoResourcePropertySource;
025:
026: import org.eclipse.core.runtime.IAdaptable;
027: import org.eclipse.core.runtime.IAdapterFactory;
028: import org.eclipse.jface.viewers.IContentProvider;
029: import org.eclipse.jface.viewers.ILabelProvider;
030: import org.eclipse.jface.viewers.IStructuredContentProvider;
031: import org.eclipse.jface.viewers.ITableColorProvider;
032: import org.eclipse.jface.viewers.ITableLabelProvider;
033: import org.eclipse.jface.viewers.ITreeContentProvider;
034: import org.eclipse.ui.views.properties.IPropertySource;
035: import org.eclipse.ui.views.properties.IPropertySource2;
036: import org.geotools.feature.Feature;
037: import org.geotools.feature.SimpleFeature;
038:
039: import com.vividsolutions.jts.geom.Coordinate;
040: import com.vividsolutions.jts.geom.Geometry;
041:
042: /**
043: * Adapts Geotools objects to Eclipse objects, such as Adaptable objects, property sources, etc...
044: *
045: * @author jeichar
046: * @since 0.3
047: */
048: public class FeatureAdapterFactory implements IAdapterFactory {
049:
050: static final Class[] adapters = new Class[] {
051: IPropertySource2.class, IPropertySource.class,
052: ITreeContentProvider.class, ILabelProvider.class,
053: IContentProvider.class, IStructuredContentProvider.class,
054: ITableLabelProvider.class, ITableColorProvider.class,
055: FeatureTableProvider.class, IAdaptable.class };
056:
057: static final Class[] adaptableClasses = new Class[] {
058: Feature.class, Coordinate.class, Coordinate[].class,
059: Geometry.class, IGeoResource.class };
060:
061: /**
062: * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
063: */
064: public Object getAdapter(Object adaptableObject, Class adapterType) {
065: if (!canAdaptTo(adapterType) || !canAdapt(adaptableObject))
066: return null;
067: if (IPropertySource.class.isAssignableFrom(adapterType)) {
068: if (Feature.class.isAssignableFrom(adaptableObject
069: .getClass()))
070: return createFeaturePropertySource(adaptableObject);
071:
072: if (Coordinate.class.isAssignableFrom(adaptableObject
073: .getClass()))
074: return createCoordinatePropertySource(adaptableObject);
075:
076: if (Coordinate[].class.isAssignableFrom(adaptableObject
077: .getClass()))
078: return createCoordinatePropertySource(adaptableObject);
079:
080: if (Geometry.class.isAssignableFrom(adaptableObject
081: .getClass()))
082: return createGeometryPropertySource(adaptableObject);
083:
084: if (IGeoResource.class.isAssignableFrom(adaptableObject
085: .getClass()))
086: return createServicePropertySource(adaptableObject);
087: }
088: if (ITableColorProvider.class.isAssignableFrom(adapterType)
089: || ITableLabelProvider.class
090: .isAssignableFrom(adapterType)
091: || FeatureTableProvider.class
092: .isAssignableFrom(adapterType))
093: return new FeatureTableProvider();
094: if (IContentProvider.class.isAssignableFrom(adapterType)
095: || ILabelProvider.class.isAssignableFrom(adapterType))
096: return new FeatureViewerProvider();
097:
098: if (IAdaptable.class.isAssignableFrom(adapterType)) {
099: if (SimpleFeature.class.isAssignableFrom(adaptableObject
100: .getClass()))
101: return new AdaptableFeature(
102: (SimpleFeature) adaptableObject);
103: }
104: return null;
105: }
106:
107: /**
108: * Returns true if the adaptableObject can be adapted to one of the supported adapters.
109: *
110: * @param adaptableObject the object to adapt
111: * @return true if the adaptableObject can be adapted to one of the supported adapters.
112: */
113: public boolean canAdapt(Object adaptableObject) {
114: for (int i = 0; i < adaptableClasses.length; i++) {
115: if (adaptableClasses[i].isAssignableFrom(adaptableObject
116: .getClass()))
117: return true;
118: }
119: return false;
120: }
121:
122: /**
123: * Returns true if this factory can adapt to the adapterType class.
124: * <p>
125: * A convenience method that searches the array from getAdapterList()
126: * </p>
127: *
128: * @param adapterType the class to see if the factory can create an adapter for.
129: * @return true if an adapter can be made for the type.
130: */
131: public boolean canAdaptTo(Class adapterType) {
132: for (int i = 0; i < adapters.length; i++) {
133: if (adapterType.isAssignableFrom(adapters[i]))
134: return true;
135: }
136: return false;
137: }
138:
139: /**
140: * Creates a PropertySource for Features
141: *
142: * @param adaptableObject
143: * @return a PropertySource for Features
144: */
145: private IPropertySource2 createFeaturePropertySource(
146: Object adaptableObject) {
147: return new FeaturePropertySource((Feature) adaptableObject);
148: }
149:
150: /**
151: * Creates a PropertySource for Features
152: *
153: * @param adaptableObject
154: * @return a PropertySource for Features
155: */
156: private IPropertySource2 createCoordinatePropertySource(
157: Object adaptableObject) {
158: return new CoordinatePropertySource(
159: (Coordinate) adaptableObject);
160: }
161:
162: /**
163: * Creates a PropertySource for Features
164: *
165: * @param adaptableObject
166: * @return a PropertySource for Features
167: */
168: private IPropertySource2 createGeometryPropertySource(
169: Object adaptableObject) {
170: return new GeomPropertySource((Geometry) adaptableObject);
171: }
172:
173: /**
174: * Creates a PropertySource for Features
175: *
176: * @param adaptableObject
177: * @return a PropertySource for Features
178: */
179: private IPropertySource2 createServicePropertySource(
180: Object adaptableObject) {
181: return new IGeoResourcePropertySource(
182: (IGeoResource) adaptableObject);
183: }
184:
185: /**
186: * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
187: */
188: public Class[] getAdapterList() {
189: Class[] c = new Class[adapters.length];
190: System.arraycopy(adapters, 0, c, 0, c.length);
191: return c;
192: }
193:
194: }
|