001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.renderer.style;
034:
035: import java.awt.BasicStroke;
036: import java.awt.Color;
037: import java.awt.Graphics2D;
038: import java.awt.Stroke;
039:
040: import javax.swing.Icon;
041:
042: import com.vividsolutions.jts.geom.Geometry;
043: import com.vividsolutions.jts.geom.GeometryCollection;
044: import com.vividsolutions.jts.geom.LineString;
045: import com.vividsolutions.jts.geom.MultiPoint;
046: import com.vividsolutions.jts.geom.MultiPolygon;
047: import com.vividsolutions.jts.geom.Polygon;
048: import com.vividsolutions.jts.util.Assert;
049: import com.vividsolutions.jump.feature.Feature;
050: import com.vividsolutions.jump.workbench.model.Layer;
051: import com.vividsolutions.jump.workbench.ui.GUIUtil;
052: import com.vividsolutions.jump.workbench.ui.Viewport;
053:
054: public abstract class LineStringStyle implements Style {
055: protected boolean enabled = true;
056:
057: protected Stroke stroke;
058:
059: public Object clone() {
060: try {
061: return super .clone();
062: } catch (CloneNotSupportedException e) {
063: Assert.shouldNeverReachHere();
064: return null;
065: }
066: }
067:
068: protected Color lineColorWithAlpha;
069:
070: protected Color fillColorWithAlpha;
071:
072: public LineStringStyle(String name, Icon icon) {
073: }
074:
075: protected void paintGeometry(Geometry geometry,
076: Graphics2D graphics, Viewport viewport) throws Exception {
077: if (geometry instanceof MultiPoint) {
078: return;
079: }
080:
081: // MD - removed, to allow MultiPolygons to be styled as well
082: // if (geometry instanceof MultiPolygon) {
083: // return;
084: // }
085:
086: if (geometry instanceof GeometryCollection) {
087: paintGeometryCollection((GeometryCollection) geometry,
088: graphics, viewport);
089: return;
090: }
091: if (geometry instanceof Polygon) {
092: paintPolygon((Polygon) geometry, graphics, viewport);
093: return;
094: }
095: if (!(geometry instanceof LineString)) {
096: return;
097: }
098: LineString lineString = (LineString) geometry;
099: if (lineString.getNumPoints() < 2) {
100: return;
101: }
102: paintLineString(lineString, viewport, graphics);
103: }
104:
105: /**
106: * @param lineString has 2 or more points
107: */
108: protected abstract void paintLineString(LineString lineString,
109: Viewport viewport, Graphics2D graphics) throws Exception;
110:
111: private void paintGeometryCollection(GeometryCollection gc,
112: Graphics2D graphics, Viewport viewport) throws Exception {
113: for (int i = 0; i < gc.getNumGeometries(); i++) {
114: paintGeometry(gc.getGeometryN(i), graphics, viewport);
115: }
116: }
117:
118: private void paintPolygon(Polygon polygon, Graphics2D graphics,
119: Viewport viewport) throws Exception {
120: paintGeometry(polygon.getExteriorRing(), graphics, viewport);
121:
122: for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
123: paintGeometry(polygon.getInteriorRingN(i), graphics,
124: viewport);
125: }
126: }
127:
128: public void setEnabled(boolean enabled) {
129: this .enabled = enabled;
130: }
131:
132: public boolean isEnabled() {
133: return enabled;
134: }
135:
136: public void initialize(Layer layer) {
137: stroke = new BasicStroke(layer.getBasicStyle().getLineWidth(),
138: BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
139: lineColorWithAlpha = GUIUtil.alphaColor(layer.getBasicStyle()
140: .getLineColor(), layer.getBasicStyle().getAlpha());
141: fillColorWithAlpha = GUIUtil.alphaColor(layer.getBasicStyle()
142: .getFillColor(), layer.getBasicStyle().getAlpha());
143: }
144:
145: public void paint(Feature f, Graphics2D g, Viewport viewport)
146: throws Exception {
147: paintGeometry(f.getGeometry(), g, viewport);
148: }
149: }
|