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: package com.vividsolutions.jump.workbench.ui.renderer.style;
033:
034: import com.vividsolutions.jts.util.Assert;
035:
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.feature.Feature;
038: import com.vividsolutions.jump.util.StringUtil;
039: import com.vividsolutions.jump.workbench.model.Layer;
040: import com.vividsolutions.jump.workbench.ui.GUIUtil;
041: import com.vividsolutions.jump.workbench.ui.Viewport;
042:
043: import java.awt.*;
044: import java.awt.BasicStroke;
045: import java.awt.Color;
046: import java.awt.Graphics2D;
047: import java.awt.Stroke;
048: import java.awt.geom.NoninvertibleTransformException;
049:
050: import java.util.List;
051:
052: public class BasicStyle implements Style {
053: private boolean renderingFill = true;
054: private boolean renderingLine = true;
055: private boolean renderingLinePattern = false;
056: private boolean renderingFillPattern = false;
057:
058: public static final Color DEFAULT_FILL_COLOR = new Color(0, 0, 0,
059: 255);
060: public static final Color DEFAULT_LINE_COLOR = DEFAULT_FILL_COLOR;
061: public static final BasicStroke DEFAULT_FILL_STROKE = new BasicStroke(
062: 1);
063:
064: //The important thing here is the initial alpha. [Jon Aquino]
065: private Color fillColor = DEFAULT_FILL_COLOR;
066: private Color lineColor = DEFAULT_LINE_COLOR;
067:
068: private BasicStroke lineStroke;
069: private Stroke fillStroke = DEFAULT_FILL_STROKE;
070: private boolean enabled = true;
071: private String linePattern = "3";
072:
073: //Set fill pattern to something, so that the BasicStylePanel combobox won't
074: //start empty. [Jon Aquino]
075: // Fixing the GUI is a better idea! [s-l-teichmann]
076: private Paint fillPattern;
077:
078: public BasicStyle(Color fillColor) {
079: setFillColor(fillColor);
080: setLineColor(Layer.defaultLineColor(fillColor));
081: setLineWidth(1);
082: }
083:
084: public BasicStyle() {
085: this (Color.black);
086: }
087:
088: public boolean isRenderingFillPattern() {
089: return renderingFillPattern;
090: }
091:
092: public BasicStyle setRenderingFillPattern(
093: boolean renderingFillPattern) {
094: this .renderingFillPattern = renderingFillPattern;
095: return this ;
096: }
097:
098: public Paint getFillPattern() {
099: return fillPattern;
100: }
101:
102: /**
103: * Remember to call #setRenderingFillPattern(true).
104: */
105: public BasicStyle setFillPattern(Paint fillPattern) {
106: this .fillPattern = fillPattern;
107: if (fillPattern instanceof BasicFillPattern) {
108: ((BasicFillPattern) fillPattern).setColor(this .fillColor);
109: }
110: return this ;
111: }
112:
113: public String getLinePattern() {
114: return linePattern;
115: }
116:
117: /**
118: * The actual dash pattern used internally will be the given dash pattern
119: * multiplied by the line length. Remember to call #setRenderingLinePattern(true).
120: * @param linePattern e.g. "5,2,3,2"
121: */
122: public BasicStyle setLinePattern(String linePattern) {
123: this .linePattern = linePattern;
124: lineStroke = createLineStroke(lineStroke.getLineWidth());
125: return this ;
126: }
127:
128: public void initialize(Layer layer) {
129: }
130:
131: public void setEnabled(boolean enabled) {
132: this .enabled = enabled;
133: }
134:
135: public boolean isEnabled() {
136: return enabled;
137: }
138:
139: public void paint(Feature f, Graphics2D g, Viewport viewport)
140: throws NoninvertibleTransformException {
141: StyleUtil
142: .paint(
143: f.getGeometry(),
144: g,
145: viewport,
146: renderingFill,
147: fillStroke,
148: (renderingFillPattern && (fillPattern != null)) ? fillPattern
149: : fillColor, renderingLine, lineStroke,
150: lineColor);
151: }
152:
153: public Object clone() {
154: try {
155: return super .clone();
156: } catch (CloneNotSupportedException e) {
157: Assert.shouldNeverReachHere();
158:
159: return null;
160: }
161: }
162:
163: public boolean isRenderingFill() {
164: return renderingFill;
165: }
166:
167: public boolean isRenderingLine() {
168: return renderingLine;
169: }
170:
171: public boolean isRenderingLinePattern() {
172: return renderingLinePattern;
173: }
174:
175: public void setRenderingFill(boolean renderingFill) {
176: this .renderingFill = renderingFill;
177: }
178:
179: public void setRenderingLine(boolean renderingLine) {
180: this .renderingLine = renderingLine;
181: }
182:
183: public BasicStyle setRenderingLinePattern(
184: boolean renderingLinePattern) {
185: this .renderingLinePattern = renderingLinePattern;
186: lineStroke = createLineStroke(lineStroke.getLineWidth());
187: return this ;
188: }
189:
190: public void setFillColor(Color fillColor) {
191: setFillColor(fillColor, getAlpha());
192: }
193:
194: private BasicStyle setFillColor(Color fillColor, int alpha) {
195: this .fillColor = GUIUtil.alphaColor(fillColor, alpha);
196: if (fillPattern instanceof BasicFillPattern) {
197: ((BasicFillPattern) fillPattern).setColor(this .fillColor);
198: }
199: return this ;
200: }
201:
202: public void setLineColor(Color lineColor) {
203: this .lineColor = GUIUtil.alphaColor(lineColor, getAlpha());
204: }
205:
206: public void setLineWidth(int lineWidth) {
207: //Don't use BasicStroke.JOIN_ROUND or JOIN_BEVEL -- when the line
208: //width is 1, one of the corners will not be drawn. [Jon Aquino]
209: lineStroke = createLineStroke(lineWidth);
210: }
211:
212: private BasicStroke createLineStroke(float lineWidth) {
213: return (renderingLinePattern
214: && (linePattern.trim().length() != 0) && (lineWidth > 0)) ? new BasicStroke(
215: lineWidth, BasicStroke.CAP_BUTT,
216: BasicStroke.JOIN_BEVEL, 1.0f, toArray(linePattern,
217: lineWidth), 0)
218: : new BasicStroke(lineWidth, BasicStroke.CAP_BUTT,
219: BasicStroke.JOIN_BEVEL);
220: }
221:
222: public static float[] toArray(String linePattern, float lineWidth) {
223: List strings = StringUtil.fromCommaDelimitedString(linePattern);
224: float[] array = new float[strings.size()];
225:
226: for (int i = 0; i < strings.size(); i++) {
227: String string = (String) strings.get(i);
228: array[i] = Float.parseFloat(string) * lineWidth;
229:
230: if (array[i] <= 0) {
231: throw new IllegalArgumentException(
232: I18N
233: .get("ui.renderer.style.BasicStyle.negative-dash-length"));
234: }
235: }
236: return array;
237: }
238:
239: /**
240: * @return 0-255 (255 is opaque)
241: */
242: public int getAlpha() {
243: return fillColor.getAlpha();
244: }
245:
246: public Color getFillColor() {
247: return GUIUtil.alphaColor(fillColor, 255);
248: }
249:
250: public Color getLineColor() {
251: return GUIUtil.alphaColor(lineColor, 255);
252: }
253:
254: public int getLineWidth() {
255: return (int) lineStroke.getLineWidth();
256: }
257:
258: /**
259: * @param alpha 0-255 (255 is opaque)
260: */
261: public void setAlpha(int alpha) {
262: setFillColor(fillColor, alpha);
263: lineColor = GUIUtil.alphaColor(lineColor, alpha);
264: }
265:
266: public BasicStroke getLineStroke() {
267: return lineStroke;
268: }
269: }
|