001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.renderer.style;
017:
018: import java.awt.AlphaComposite;
019: import java.awt.BasicStroke;
020: import java.awt.Color;
021: import java.awt.Composite;
022: import java.awt.Paint;
023:
024: import org.geotools.feature.Feature;
025: import org.geotools.styling.LineSymbolizer;
026: import org.geotools.styling.Stroke;
027: import org.opengis.filter.expression.Expression;
028:
029: /**
030: * A dynamic line style, that will compute its parameters each time they are requested instead of
031: * caching them
032: *
033: * @author jamesm
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/style/DynamicLineStyle2D.java $
035: */
036: public class DynamicLineStyle2D extends
037: org.geotools.renderer.style.LineStyle2D {
038: /** The feature that will be styled as a polygon */
039: protected Feature feature;
040:
041: /** The line symbolizer used to get stroke/composite/... */
042: protected LineSymbolizer ls;
043:
044: /**
045: * Creates a new instance of DynamicLineStyle2D
046: */
047: public DynamicLineStyle2D(Feature feature, LineSymbolizer sym) {
048: this .feature = feature;
049: ls = sym;
050: }
051:
052: /**
053: * Computes and returns the stroke
054: */
055: public java.awt.Stroke getStroke() {
056: Stroke stroke = ls.getStroke();
057:
058: if (stroke == null) {
059: return null;
060: }
061:
062: // resolve join type into a join code
063: String joinType;
064: int joinCode;
065:
066: joinType = evaluateExpression(stroke.getLineJoin(), feature,
067: "miter");
068:
069: joinCode = SLDStyleFactory.lookUpJoin(joinType);
070:
071: // resolve cap type into a cap code
072: String capType;
073: int capCode;
074:
075: capType = evaluateExpression(stroke.getLineCap(), feature,
076: "square");
077: capCode = SLDStyleFactory.lookUpCap(capType);
078:
079: // get the other properties needed for the stroke
080: float[] dashes = stroke.getDashArray();
081: float width = ((Float) stroke.getWidth().evaluate(feature,
082: Float.class)).floatValue();
083: float dashOffset = ((Float) stroke.getDashOffset().evaluate(
084: feature, Float.class)).floatValue();
085:
086: // Simple optimization: let java2d use the fast drawing path if the line width
087: // is small enough...
088: if (width <= 1) {
089: width = 0;
090: }
091:
092: // now set up the stroke
093: BasicStroke stroke2d;
094:
095: if ((dashes != null) && (dashes.length > 0)) {
096: stroke2d = new BasicStroke(width, capCode, joinCode, 1,
097: dashes, dashOffset);
098: } else {
099: stroke2d = new BasicStroke(width, capCode, joinCode, 1);
100: }
101:
102: return stroke2d;
103: }
104:
105: /**
106: * Computes and returns the contour style
107: */
108: public java.awt.Composite getContourComposite() {
109: Stroke stroke = ls.getStroke();
110:
111: if (stroke == null) {
112: return null;
113: }
114:
115: float opacity = ((Float) stroke.getOpacity().evaluate(feature,
116: Float.class)).floatValue();
117: Composite composite = AlphaComposite.getInstance(
118: AlphaComposite.SRC_OVER, opacity);
119:
120: return composite;
121: }
122:
123: /**
124: * Returns the contour paint
125: *
126: * @return the contour paint
127: */
128: public java.awt.Paint getContour() {
129: Stroke stroke = ls.getStroke();
130:
131: if (stroke == null) {
132: return null;
133: }
134:
135: // the foreground color
136: Paint contourPaint = (Color) stroke.getColor().evaluate(
137: feature, Color.class);
138: if (contourPaint == null) {
139: String text = (String) stroke.getColor().evaluate(feature,
140: String.class);
141: if (text != null) {
142: contourPaint = Color.decode(text);
143: }
144: }
145:
146: // if a graphic fill is to be used, prepare the paint accordingly....
147: org.geotools.styling.Graphic gr = stroke.getGraphicFill();
148: SLDStyleFactory fac = new SLDStyleFactory();
149:
150: if (gr != null) {
151: contourPaint = fac.getTexturePaint(gr, feature);
152: }
153:
154: return contourPaint;
155: }
156:
157: /**
158: * Evaluates an expression over the passed feature, if the expression or the result is null,
159: * the default value will be returned
160: */
161: private String evaluateExpression(Expression e, Feature feature,
162: String defaultValue) {
163: String result = defaultValue;
164:
165: if (e != null) {
166: result = (String) e.evaluate(feature, defaultValue
167: .getClass());
168:
169: if (result == null) {
170: result = defaultValue;
171: }
172: }
173:
174: return result;
175: }
176: }
|