001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal.render.impl;
010:
011: import java.awt.Color;
012: import java.io.IOException;
013: import java.util.Collection;
014: import java.util.Collections;
015: import java.util.HashMap;
016: import java.util.LinkedList;
017: import java.util.List;
018: import java.util.Map;
019:
020: import net.refractions.udig.project.internal.Layer;
021: import net.refractions.udig.project.internal.Messages;
022: import net.refractions.udig.project.internal.ProjectPlugin;
023:
024: import org.geotools.data.wms.WebMapServer;
025: import org.geotools.styling.FeatureTypeStyle;
026: import org.geotools.styling.LineSymbolizer;
027: import org.geotools.styling.PointSymbolizer;
028: import org.geotools.styling.PolygonSymbolizer;
029: import org.geotools.styling.RasterSymbolizer;
030: import org.geotools.styling.Style;
031: import org.geotools.styling.StyleBuilder;
032:
033: /**
034: * A utility class for obtaining precanned or random styles
035: *
036: * @author Jesse Eichar
037: * @version $Revision: 1.9 $
038: */
039: public class Styling {
040:
041: /** A map of default styles. */
042: public static final Map STYLES;
043: static {
044: Map<String, Integer> styles = new HashMap<String, Integer>();
045: styles.put(Messages.Styling_blueLine, Integer.valueOf(0));
046: styles.put(Messages.Styling_greenLine, Integer.valueOf(1));
047: styles.put(Messages.Styling_blackLine, Integer.valueOf(2));
048: styles.put(Messages.Styling_blackLine_blueFill, Integer
049: .valueOf(3));
050: styles.put(Messages.Styling_blackLine_greenFill, Integer
051: .valueOf(4));
052: styles.put(Messages.Styling_blackLine_semitransparentBlueFill,
053: Integer.valueOf(5));
054: styles.put(
055: Messages.Styling_blackLine_semitransparentYellowFill,
056: Integer.valueOf(6));
057: styles.put(Messages.Styling_pointStyle, Integer.valueOf(7));
058: STYLES = Collections.unmodifiableMap(styles);
059: }
060:
061: /**
062: * Returns a Style object give a style name and a feature typename.
063: *
064: * @param styleName The name of the style to creates. The list of name can be obtained from
065: * {@link #getStyleNames(Layer)}
066: * @param typeName the TypeName of the feature type the style will style.
067: * @return a Style object give a style name and a feature typename.
068: */
069: public static Style getStyle(String styleName, String typeName) {
070: return getStyle(((Integer) STYLES.get(styleName)).intValue(),
071: typeName);
072: }
073:
074: /**
075: * Returns a Style object given a value from {@link #STYLES}and the feature typename.
076: *
077: * @param index a value from {@link #STYLES}
078: * @param typeName the TypeName of the feature type the style will style.
079: * @return a Style object given a value from {@link #STYLES}and the feature typename
080: */
081: public static Style getStyle(int index, String typeName) {
082: switch (index) {
083: case 0:
084: return createLineStyle(typeName, Color.BLUE);
085: case 1:
086: return createLineStyle(typeName, Color.GREEN);
087: case 2:
088: return createLineStyle(typeName, Color.BLACK);
089: case 3:
090: return createPolyStyle(typeName, Color.BLACK, Color.BLUE);
091: case 4:
092: return createPolyStyle(typeName, Color.BLACK, Color.GREEN);
093: case 5:
094: return createPolyStyle(typeName, Color.BLACK, new Color(0,
095: 0, 255, 127));
096: case 6:
097: return createPolyStyle(typeName, Color.BLACK, new Color(
098: 127, 127, 127, 127));
099: case 7:
100: return createPointStyle(typeName);
101:
102: default:
103: return createLineStyle(typeName, Color.BLUE);
104: }
105: }
106:
107: /**
108: * Returns a simple style to use in default cases.
109: *
110: * @param typeName the TypeName of the feature type the style will style.
111: * @return a simple style to use in default cases.
112: */
113: public static Style createLineStyle(String typeName) {
114: return createLineStyle(typeName, Color.blue);
115: }
116:
117: /**
118: * Returns a simple style to use in default cases.
119: *
120: * @param typeName the TypeName of the feature type the style will style.
121: * @param color the color of the style
122: * @return a simple style to use in default cases.
123: */
124: public static Style createLineStyle(String typeName, Color color) {
125: StyleBuilder sb = new StyleBuilder();
126: Style linestyle = sb.createStyle();
127:
128: LineSymbolizer line = sb.createLineSymbolizer(color);
129: linestyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(line));
130:
131: FeatureTypeStyle fts = linestyle.getFeatureTypeStyles()[0];
132: fts.setName(Messages.Styling_name); //tag as simple
133: fts.setFeatureTypeName(typeName);
134: fts.setSemanticTypeIdentifiers(new String[] {
135: "generic:geometry", "simple" }); //$NON-NLS-1$ //$NON-NLS-2$
136:
137: return linestyle;
138: }
139:
140: /**
141: * Returns a simple style to use in default cases.
142: *
143: * @param typeName the TypeName of the feature type the style will style.
144: * @return a simple style to use in default cases.
145: */
146: public static Style createPolyStyle(String typeName) {
147: return createPolyStyle(typeName, Color.BLACK, Color.GREEN);
148: }
149:
150: /**
151: * Returns a simple style to use in default cases.
152: *
153: * @param typeName the TypeName of the feature type the style will style.
154: * @param line the color of the outlines.
155: * @param fill The color of the fills
156: * @return a simple style to use in default cases.
157: */
158: public static Style createPolyStyle(String typeName, Color line,
159: Color fill) {
160: StyleBuilder sb = new StyleBuilder();
161: Style polystyle = sb.createStyle();
162:
163: PolygonSymbolizer poly = sb.createPolygonSymbolizer(fill, line,
164: 1);
165: polystyle.addFeatureTypeStyle(sb.createFeatureTypeStyle(poly));
166:
167: polystyle.getFeatureTypeStyles()[0]
168: .setFeatureTypeName(typeName);
169: return polystyle;
170: }
171:
172: /**
173: * Returns a simple style to use in default cases.
174: *
175: * @param typeName the TypeName of the feature type the style will style.
176: * @return a simple style to use in default cases.
177: */
178: public static Style createPointStyle(String typeName) {
179: StyleBuilder sb = new StyleBuilder();
180: Style pointstyle = sb.createStyle();
181: PointSymbolizer point = sb.createPointSymbolizer(sb
182: .createGraphic());
183:
184: pointstyle
185: .addFeatureTypeStyle(sb.createFeatureTypeStyle(point));
186:
187: pointstyle.getFeatureTypeStyles()[0]
188: .setFeatureTypeName(typeName);
189: return pointstyle;
190: }
191:
192: /**
193: * Returns a simple style to use in default cases.
194: *
195: * @param typeName the TypeName of the feature type the style will style.
196: * @return as simple style to use in default cases.
197: */
198: public static Style createRasterStyle(String typeName) {
199: StyleBuilder sb = new StyleBuilder();
200: Style rasterstyle = sb.createStyle();
201: RasterSymbolizer raster = sb.createRasterSymbolizer();
202:
203: rasterstyle.addFeatureTypeStyle(sb
204: .createFeatureTypeStyle(raster));
205: rasterstyle.getFeatureTypeStyles()[0]
206: .setFeatureTypeName(typeName);
207: return rasterstyle;
208: }
209:
210: /**
211: * Returns a list of style names that can be used on the given layer.
212: *
213: * @param currentLayer The layer to finds styles for.
214: * @return a list of style names that can be used on the given layer.
215: */
216: @SuppressWarnings("unchecked")
217: public static Collection getStyleNames(Layer currentLayer) {
218: // URI id=currentLayer.getID();
219: // if( id.containsKey(WMSRegistryEntry.GET_CAPABILITIES_URL) ){
220: if (currentLayer.isType(WebMapServer.class)) { // checking for wms
221:
222: List<String> l = new LinkedList<String>();
223: try {
224: l.addAll(currentLayer.getResource(
225: org.geotools.data.ows.Layer.class, null)
226: .getStyles());
227: } catch (IOException e) {
228: ProjectPlugin.log(null, e);
229: }
230: l.add(Messages.Styling_default);
231: return l;
232: }
233:
234: return STYLES.keySet();
235: }
236: }
|