001: package net.refractions.udig.style.sld;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import net.refractions.udig.project.ILayer;
007:
008: import org.eclipse.jface.resource.ImageDescriptor;
009: import org.eclipse.swt.SWT;
010: import org.geotools.feature.FeatureType;
011: import org.geotools.feature.GeometryAttributeType;
012: import org.geotools.styling.LineSymbolizer;
013: import org.geotools.styling.PointSymbolizer;
014: import org.geotools.styling.PolygonSymbolizer;
015: import org.geotools.styling.RasterSymbolizer;
016: import org.geotools.styling.TextSymbolizer;
017:
018: import com.vividsolutions.jts.geom.LineString;
019: import com.vividsolutions.jts.geom.MultiLineString;
020: import com.vividsolutions.jts.geom.MultiPoint;
021: import com.vividsolutions.jts.geom.MultiPolygon;
022: import com.vividsolutions.jts.geom.Point;
023: import com.vividsolutions.jts.geom.Polygon;
024:
025: public enum SLD {
026: POINT(PointSymbolizer.class) {
027:
028: public ImageDescriptor createImageDescriptor() {
029: return StyleGlyph.point(SWT.COLOR_GREEN, 7);
030: }
031:
032: public ImageDescriptor createDisabledImageDescriptor() {
033: return StyleGlyph.point(SWT.COLOR_GRAY, 7);
034: }
035:
036: // TODO: inline this?
037: public PointSymbolizer createDefault() {
038: return SLDContent.createPointSymbolizer(null);
039: }
040:
041: public boolean supports(ILayer layer) {
042: FeatureType featureType = layer.getSchema();
043: if (featureType == null)
044: return false;
045:
046: return isPoint(featureType.getDefaultGeometry());
047: }
048: },
049: LINE(LineSymbolizer.class) {
050: public ImageDescriptor createImageDescriptor() {
051: return StyleGlyph.line(SWT.COLOR_BLACK, 2);
052: }
053:
054: public ImageDescriptor createDisabledImageDescriptor() {
055: return StyleGlyph.line(SWT.COLOR_GRAY, 2);
056: }
057:
058: public LineSymbolizer createDefault() {
059: return SLDContent.createLineSymbolizer(null);
060: }
061:
062: public boolean supports(ILayer layer) {
063: return isLine(layer.getSchema());
064: }
065: },
066: POLYGON(PolygonSymbolizer.class) {
067: public ImageDescriptor createImageDescriptor() {
068: return StyleGlyph
069: .polygon(SWT.COLOR_BLACK, SWT.COLOR_RED, 1);
070: }
071:
072: public ImageDescriptor createDisabledImageDescriptor() {
073: return StyleGlyph.polygon(SWT.COLOR_BLACK, SWT.COLOR_GRAY,
074: 1);
075: }
076:
077: public PolygonSymbolizer createDefault() {
078: return SLDContent.createPolygonSymbolizer(null);
079: }
080:
081: public boolean supports(ILayer layer) {
082: return isPolygon(layer.getSchema());
083: }
084: },
085: TEXT(TextSymbolizer.class) {
086: public ImageDescriptor createImageDescriptor() {
087: return StyleGlyph.text(SWT.COLOR_BLACK, 2);
088: }
089:
090: public ImageDescriptor createDisabledImageDescriptor() {
091: return StyleGlyph.text(SWT.COLOR_GRAY, 2);
092: }
093:
094: public TextSymbolizer createDefault() {
095: return SLDContent.createTextSymbolizer();
096: }
097:
098: public boolean supports(ILayer layer) {
099: FeatureType featureType = layer.getSchema();
100: if (featureType == null)
101: return false;
102:
103: //TODO: Can a text symbolizer be applied to a raster?
104: return true;
105: }
106: },
107: RASTER(RasterSymbolizer.class) {
108: public ImageDescriptor createImageDescriptor() {
109: return StyleGlyph.raster(SWT.COLOR_BLACK, SWT.COLOR_RED, 1);
110: }
111:
112: public ImageDescriptor createDisabledImageDescriptor() {
113: return StyleGlyph
114: .raster(SWT.COLOR_BLACK, SWT.COLOR_GRAY, 1);
115: }
116:
117: public Object createDefault() {
118: return SLDContent.createRasterSymbolizer();
119: }
120:
121: public boolean supports(ILayer layer) {
122: return layer.hasResource(org.geotools.data.ows.Layer.class);
123: }
124: };
125:
126: //private static SLD[] all = new SLD[]{POINT, LINE, POLYGON, RASTER, TEXT};
127:
128: Class type;
129:
130: SLD(Class object) {
131: this .type = object;
132: }
133:
134: public ImageDescriptor createImageDescriptor() {
135: return null;
136: }
137:
138: public ImageDescriptor createDisabledImageDescriptor() {
139: return null;
140: }
141:
142: public Object createDefault() {
143: return null;
144: }
145:
146: /**
147: * Determines if the style component supports the specified feature type.
148: *
149: * @param layer Feature type, must not be null and must have a valid geometry type attribute.
150: * @return true if the feature type is supported, otherwise false.
151: */
152: public boolean supports(ILayer layer) {
153: return false;
154: }
155:
156: public static <T> T createDefault(Class<T> theClass) {
157: SLD sld = get(theClass);
158: if (sld != null) {
159: return theClass.cast(sld.createDefault());
160: }
161: return null;
162: }
163:
164: public static ImageDescriptor createImageDescriptor(Class theClass) {
165: SLD symbolizer = get(theClass);
166: if (symbolizer != null)
167: return symbolizer.createImageDescriptor();
168: return null;
169: }
170:
171: public static ImageDescriptor createDisabledImageDescriptor(
172: Class theClass) {
173: SLD object = get(theClass);
174: if (object != null)
175: return object.createDisabledImageDescriptor();
176: return null;
177: }
178:
179: /**
180: * Returns the types of SLD style components that support the specified feature type. This
181: * method returns an empty list if no style types support the specified feature type.
182: *
183: * @param layer the layer.
184: * @return A list (possibly empty) of types that support the feature type.
185: */
186: public static List<Class> getSupportedTypes(ILayer layer) {
187: ArrayList<Class> l = new ArrayList<Class>();
188: for (SLD sld : SLD.values()) {
189: if (sld.supports(layer))
190: l.add(sld.type);
191: }
192:
193: return l;
194: }
195:
196: @SuppressWarnings("unchecked")
197: private static SLD get(Class symbolizer) {
198: for (SLD sld : SLD.values()) {
199: if (symbolizer.isAssignableFrom(sld.type)) {
200: return sld;
201: }
202: }
203: /*
204: if (PointSymbolizer.class.isAssignableFrom( symbolizer ))
205: return POINT;
206: if (LineSymbolizer.class.isAssignableFrom( symbolizer ))
207: return LINE;
208: if (PolygonSymbolizer.class.isAssignableFrom( symbolizer ))
209: return POLYGON;
210: if (TextSymbolizer.class.isAssignableFrom( symbolizer ))
211: return TEXT;
212: if (RasterSymbolizer.class.isAssignableFrom(symbolizer))
213: return RASTER;
214: */
215: return null;
216: }
217:
218: public static final boolean isPolygon(FeatureType featureType) {
219: if (featureType == null)
220: return false;
221: return isPolygon(featureType.getDefaultGeometry());
222: }
223:
224: /* This needed to be a function as it was being writen poorly everywhere */
225: public static final boolean isPolygon(
226: GeometryAttributeType geometryType) {
227: if (geometryType == null)
228: return false;
229: Class type = geometryType.getType();
230: return Polygon.class.isAssignableFrom(type)
231: || MultiPolygon.class.isAssignableFrom(type);
232: }
233:
234: public static final boolean isLine(FeatureType featureType) {
235: if (featureType == null)
236: return false;
237: return isLine(featureType.getDefaultGeometry());
238: }
239:
240: /* This needed to be a function as it was being writen poorly everywhere */
241: public static final boolean isLine(
242: GeometryAttributeType geometryType) {
243: if (geometryType == null)
244: return false;
245: Class type = geometryType.getType();
246: return LineString.class.isAssignableFrom(type)
247: || MultiLineString.class.isAssignableFrom(type);
248: }
249:
250: public static final boolean isPoint(FeatureType featureType) {
251: if (featureType == null)
252: return false;
253: return isPoint(featureType.getDefaultGeometry());
254: }
255:
256: /* This needed to be a function as it was being writen poorly everywhere */
257: public static final boolean isPoint(
258: GeometryAttributeType geometryType) {
259: if (geometryType == null)
260: return false;
261: Class type = geometryType.getType();
262: return Point.class.isAssignableFrom(type)
263: || MultiPoint.class.isAssignableFrom(type);
264: }
265: }
|