001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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;
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: /**
018: *
019: */package org.geotools.renderer.shape;
020:
021: import org.geotools.styling.AnchorPoint;
022: import org.geotools.styling.ColorMap;
023: import org.geotools.styling.ColorMapEntry;
024: import org.geotools.styling.Displacement;
025: import org.geotools.styling.ExternalGraphic;
026: import org.geotools.styling.FeatureTypeConstraint;
027: import org.geotools.styling.FeatureTypeStyle;
028: import org.geotools.styling.Fill;
029: import org.geotools.styling.Graphic;
030: import org.geotools.styling.Halo;
031: import org.geotools.styling.LinePlacement;
032: import org.geotools.styling.LineSymbolizer;
033: import org.geotools.styling.Mark;
034: import org.geotools.styling.NamedLayer;
035: import org.geotools.styling.PointPlacement;
036: import org.geotools.styling.PointSymbolizer;
037: import org.geotools.styling.PolygonSymbolizer;
038: import org.geotools.styling.RasterSymbolizer;
039: import org.geotools.styling.Rule;
040: import org.geotools.styling.Stroke;
041: import org.geotools.styling.Style;
042: import org.geotools.styling.StyleVisitor;
043: import org.geotools.styling.StyledLayerDescriptor;
044: import org.geotools.styling.Symbolizer;
045: import org.geotools.styling.TextSymbolizer;
046: import org.geotools.styling.UserLayer;
047: import org.opengis.filter.expression.Expression;
048: import org.opengis.filter.expression.Literal;
049:
050: /**
051: * Searches for symbolizers with opacity.
052: *
053: * @author jones
054: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/main/java/org/geotools/renderer/shape/OpacityFinder.java $
055: */
056: public class OpacityFinder implements StyleVisitor {
057: private Class[] acceptableTypes;
058: public boolean hasOpacity;
059:
060: public OpacityFinder(Class[] acceptableTypes) {
061: this .acceptableTypes = acceptableTypes;
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Style)
068: */
069: public void visit(Style style) {
070: FeatureTypeStyle[] styles = style.getFeatureTypeStyles();
071:
072: for (int i = 0; i < styles.length; i++) {
073: if (hasOpacity) {
074: break;
075: }
076:
077: styles[i].accept(this );
078: }
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Rule)
085: */
086: public void visit(Rule rule) {
087: Symbolizer[] symbs = rule.getSymbolizers();
088:
089: for (int i = 0; i < symbs.length; i++) {
090: if (hasOpacity) {
091: break;
092: }
093:
094: symbs[i].accept(this );
095: }
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.FeatureTypeStyle)
102: */
103: public void visit(FeatureTypeStyle fts) {
104: Rule[] rules = fts.getRules();
105:
106: for (int i = 0; i < rules.length; i++) {
107: if (hasOpacity) {
108: break;
109: }
110:
111: rules[i].accept(this );
112: }
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Fill)
119: */
120: public void visit(Fill fill) {
121: checkOpacity(fill.getOpacity());
122: }
123:
124: private void checkOpacity(Expression exp) {
125: if (exp != null) {
126: if (exp instanceof Literal) {
127: Literal literal = (Literal) exp;
128: Object obj = literal.getValue();
129: float opacity;
130:
131: if (obj instanceof Integer) {
132: Integer i = (Integer) obj;
133: opacity = i.floatValue();
134: } else if (obj instanceof Float) {
135: Float i = (Float) obj;
136: opacity = i.floatValue();
137: } else if (obj instanceof Double) {
138: Double i = (Double) obj;
139: opacity = i.floatValue();
140: } else if (obj instanceof Short) {
141: Short i = (Short) obj;
142: opacity = i.floatValue();
143: } else if (obj instanceof Byte) {
144: Byte i = (Byte) obj;
145: opacity = i.floatValue();
146: } else {
147: return;
148: }
149: if ((opacity > 0.01) && (opacity < 0.99)) {
150: this .hasOpacity = true;
151: }
152: }
153: }
154: }
155:
156: /*
157: * (non-Javadoc)
158: *
159: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Stroke)
160: */
161: public void visit(Stroke stroke) {
162: checkOpacity(stroke.getOpacity());
163: }
164:
165: /*
166: * (non-Javadoc)
167: *
168: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Symbolizer)
169: */
170: public void visit(Symbolizer sym) {
171: if (sym instanceof PointSymbolizer) {
172: PointSymbolizer ps = (PointSymbolizer) sym;
173: ps.accept(this );
174: }
175:
176: if (sym instanceof LineSymbolizer) {
177: LineSymbolizer ps = (LineSymbolizer) sym;
178: ps.accept(this );
179: }
180:
181: if (sym instanceof PolygonSymbolizer) {
182: PolygonSymbolizer ps = (PolygonSymbolizer) sym;
183: ps.accept(this );
184: }
185: }
186:
187: /*
188: * (non-Javadoc)
189: *
190: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.PointSymbolizer)
191: */
192: public void visit(PointSymbolizer ps) {
193: if (isAcceptable(ps)) {
194: ps.getGraphic().accept(this );
195: }
196: }
197:
198: private boolean isAcceptable(Symbolizer s) {
199: for (int i = 0; i < acceptableTypes.length; i++) {
200: Class type = acceptableTypes[i];
201:
202: if (type.isAssignableFrom(s.getClass())) {
203: return true;
204: }
205: }
206:
207: return false;
208: }
209:
210: /*
211: * (non-Javadoc)
212: *
213: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.LineSymbolizer)
214: */
215: public void visit(LineSymbolizer line) {
216: if (isAcceptable(line)) {
217: if (line.getStroke() != null)
218: line.getStroke().accept(this );
219: }
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.PolygonSymbolizer)
226: */
227: public void visit(PolygonSymbolizer poly) {
228: if (isAcceptable(poly)) {
229: if (poly.getStroke() != null)
230: poly.getStroke().accept(this );
231: if (poly.getFill() != null)
232: poly.getFill().accept(this );
233: }
234: }
235:
236: /*
237: * (non-Javadoc)
238: *
239: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.TextSymbolizer)
240: */
241: public void visit(TextSymbolizer text) {
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.RasterSymbolizer)
248: */
249: public void visit(RasterSymbolizer raster) {
250: // TODO Auto-generated method stub
251: }
252:
253: /*
254: * (non-Javadoc)
255: *
256: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Graphic)
257: */
258: public void visit(Graphic gr) {
259: // TODO Auto-generated method stub
260: }
261:
262: /*
263: * (non-Javadoc)
264: *
265: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Mark)
266: */
267: public void visit(Mark mark) {
268: // TODO Auto-generated method stub
269: }
270:
271: /*
272: * (non-Javadoc)
273: *
274: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.ExternalGraphic)
275: */
276: public void visit(ExternalGraphic exgr) {
277: // TODO Auto-generated method stub
278: }
279:
280: /*
281: * (non-Javadoc)
282: *
283: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.PointPlacement)
284: */
285: public void visit(PointPlacement pp) {
286: // TODO Auto-generated method stub
287: }
288:
289: /*
290: * (non-Javadoc)
291: *
292: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.AnchorPoint)
293: */
294: public void visit(AnchorPoint ap) {
295: // TODO Auto-generated method stub
296: }
297:
298: /*
299: * (non-Javadoc)
300: *
301: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Displacement)
302: */
303: public void visit(Displacement dis) {
304: // TODO Auto-generated method stub
305: }
306:
307: /*
308: * (non-Javadoc)
309: *
310: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.LinePlacement)
311: */
312: public void visit(LinePlacement lp) {
313: // TODO Auto-generated method stub
314: }
315:
316: /*
317: * (non-Javadoc)
318: *
319: * @see org.geotools.styling.StyleVisitor#visit(org.geotools.styling.Halo)
320: */
321: public void visit(Halo halo) {
322: // TODO Auto-generated method stub
323: }
324:
325: /**
326: * DOCUMENT ME!
327: *
328: * @param args
329: */
330: public static void main(String[] args) {
331: // TODO Auto-generated method stub
332: }
333:
334: public void visit(StyledLayerDescriptor sld) {
335: // TODO Auto-generated method stub
336:
337: }
338:
339: public void visit(NamedLayer layer) {
340: // TODO Auto-generated method stub
341:
342: }
343:
344: public void visit(UserLayer layer) {
345: // TODO Auto-generated method stub
346:
347: }
348:
349: public void visit(FeatureTypeConstraint ftc) {
350: // TODO Auto-generated method stub
351:
352: }
353:
354: public void visit(ColorMap arg0) {
355: // TODO Auto-generated method stub
356:
357: }
358:
359: public void visit(ColorMapEntry arg0) {
360: // TODO Auto-generated method stub
361:
362: }
363: }
|