svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / LineFillSymbol.java @ 29244
History | View | Annotate | Download (9.54 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.symbology.fmap.symbols; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Graphics2D; |
45 |
import java.awt.Rectangle; |
46 |
import java.awt.geom.AffineTransform; |
47 |
import java.awt.geom.Line2D; |
48 |
import java.awt.geom.Rectangle2D; |
49 |
|
50 |
import javax.print.attribute.PrintRequestAttributeSet; |
51 |
|
52 |
import com.iver.cit.gvsig.fmap.ViewPort; |
53 |
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit; |
54 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
55 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
56 |
import com.iver.cit.gvsig.fmap.core.FShape; |
57 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
58 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
59 |
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol; |
60 |
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol; |
61 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
62 |
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
63 |
import com.iver.utiles.StringUtilities; |
64 |
import com.iver.utiles.XMLEntity; |
65 |
import com.iver.utiles.swing.threads.Cancellable; |
66 |
|
67 |
/**
|
68 |
* LineFillSymbol allows to define a filling pattern composed by lines to complete the padding
|
69 |
* of the polygon.
|
70 |
* @autor jaume dominguez faus - jaume.dominguez@iver.es
|
71 |
*/
|
72 |
public class LineFillSymbol extends AbstractFillSymbol { |
73 |
private double angle; |
74 |
private ILineSymbol lineSymbol = SymbologyFactory.createDefaultLineSymbol();
|
75 |
private double offset = 5, csOffset = offset; |
76 |
private double separation = 5, csSeparation = separation; |
77 |
private double symbolLineWidth; |
78 |
private LineFillSymbol symSel;
|
79 |
private PrintRequestAttributeSet properties; |
80 |
/**
|
81 |
* Returns the rotation angle of the lines that compose the filling pattern
|
82 |
* @return angle
|
83 |
*/
|
84 |
public double getAngle() { |
85 |
return angle;
|
86 |
} |
87 |
/**
|
88 |
* Sets the rotation angle of the lines that compose the filling pattern
|
89 |
* @param angle
|
90 |
*/
|
91 |
public void setAngle(double angle) { |
92 |
this.angle = angle;
|
93 |
} |
94 |
/**
|
95 |
* Gets the line symbol that is used to create the filling pattern.
|
96 |
* @return lineSymbol, ILineSymbol
|
97 |
*/
|
98 |
public ILineSymbol getLineSymbol() {
|
99 |
return lineSymbol;
|
100 |
} |
101 |
/**
|
102 |
* Establishes the line symbol that is used to create the filling pattern.
|
103 |
* @param lineSymbol,ILineSymbol
|
104 |
*/
|
105 |
public void setLineSymbol(ILineSymbol lineSymbol) { |
106 |
if (lineSymbol != null) |
107 |
symbolLineWidth = lineSymbol.getLineWidth(); |
108 |
else
|
109 |
symbolLineWidth = 0;
|
110 |
this.lineSymbol = lineSymbol;
|
111 |
|
112 |
} |
113 |
/**
|
114 |
* Gets the offset of the lines inside the filling pattern
|
115 |
* @return offset
|
116 |
*/
|
117 |
public double getOffset() { |
118 |
return offset;
|
119 |
} |
120 |
/**
|
121 |
* Sets the offset of the lines inside the filling pattern
|
122 |
* @param offset
|
123 |
*/
|
124 |
public void setOffset(double offset) { |
125 |
this.offset = offset;
|
126 |
this.csOffset = offset;
|
127 |
} |
128 |
/**
|
129 |
* Returns the separation between lines that is used to create the filling pattern
|
130 |
* @return
|
131 |
*/
|
132 |
public double getSeparation() { |
133 |
return separation;
|
134 |
} |
135 |
/**
|
136 |
* Establishes the separation between lines that is used to create the filling pattern
|
137 |
* @param separation
|
138 |
*/
|
139 |
public void setSeparation(double separation) { |
140 |
if (separation == 0) separation = 1D; |
141 |
this.separation = separation;
|
142 |
this.csSeparation = separation;
|
143 |
} |
144 |
|
145 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
146 |
Color fillColor = getFillColor();
|
147 |
if (fillColor!=null && hasFill()) { |
148 |
g.setColor(getFillColor()); |
149 |
g.fill(shp); |
150 |
} |
151 |
|
152 |
if (lineSymbol != null) { |
153 |
if (csSeparation < 1) csSeparation = 1; |
154 |
g.setClip(shp); |
155 |
g.setColor(Color.black);
|
156 |
|
157 |
Rectangle2D bounds = shp.getBounds();
|
158 |
// g.draw(bounds);
|
159 |
double radius = Math.abs(Math.max(bounds.getHeight(), bounds.getWidth())); |
160 |
double centerX = bounds.getCenterX();
|
161 |
double centerY = bounds.getCenterY();
|
162 |
double aux = -(radius+csOffset);
|
163 |
|
164 |
g.translate(centerX, centerY); |
165 |
g.rotate(angle); |
166 |
while ((cancel==null || !cancel.isCanceled()) && aux <= radius) { |
167 |
double y = aux;
|
168 |
double x1 = - radius;
|
169 |
double x2 = radius;
|
170 |
Line2D line;
|
171 |
line = new Line2D.Double(x1, y, x2, y); |
172 |
lineSymbol.draw(g, null, new FPolyline2D(new GeneralPathX(line)), null); |
173 |
aux += csSeparation; |
174 |
} |
175 |
g.rotate(-angle); |
176 |
g.translate(-centerX, -centerY); |
177 |
g.setClip(null);
|
178 |
} |
179 |
|
180 |
ILineSymbol outLineSymbol = getOutline(); |
181 |
if (outLineSymbol != null && hasOutline()) |
182 |
outLineSymbol.draw(g, affineTransform, shp, cancel); |
183 |
|
184 |
} |
185 |
|
186 |
public void drawInsideRectangle(Graphics2D g, |
187 |
AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException { |
188 |
if (properties==null) |
189 |
draw(g, null, new FPolygon2D(new GeneralPathX(r)), null); |
190 |
else
|
191 |
print(g,new AffineTransform(),new FPolygon2D(new GeneralPathX(r)), properties); |
192 |
} |
193 |
|
194 |
|
195 |
public ISymbol getSymbolForSelection() {
|
196 |
if (symSel == null) symSel = new LineFillSymbol(); |
197 |
symSel.setFillColor(ISymbol.SELECTION_COLOR); |
198 |
return symSel;
|
199 |
} |
200 |
|
201 |
public int getSymbolType() { |
202 |
return FShape.POLYGON;
|
203 |
} |
204 |
|
205 |
public XMLEntity getXMLEntity() {
|
206 |
XMLEntity xml = new XMLEntity();
|
207 |
xml.putProperty("className", getClassName());
|
208 |
xml.putProperty("desc", getDescription());
|
209 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
210 |
xml.putProperty("angle", angle);
|
211 |
xml.putProperty("offset", offset);
|
212 |
xml.putProperty("separation", separation);
|
213 |
xml.putProperty("symbolLineWidth", symbolLineWidth);
|
214 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
215 |
xml.putProperty("unit", getUnit());
|
216 |
|
217 |
if (getFillColor() !=null) |
218 |
xml.putProperty("fillColor", StringUtilities.color2String(getFillColor()));
|
219 |
xml.putProperty("hasFill", hasFill());
|
220 |
if (lineSymbol!=null) { |
221 |
XMLEntity xmlLine = lineSymbol.getXMLEntity(); |
222 |
xmlLine.putProperty("id", "fillLine"); |
223 |
xml.addChild(xmlLine); |
224 |
} |
225 |
|
226 |
ILineSymbol outline = getOutline(); |
227 |
if (outline!=null) { |
228 |
XMLEntity xmlOutline = outline.getXMLEntity(); |
229 |
xmlOutline.putProperty("id", "outline"); |
230 |
xml.addChild(xmlOutline); |
231 |
} |
232 |
xml.putProperty("hasOutline", hasOutline());
|
233 |
return xml;
|
234 |
} |
235 |
|
236 |
public void setXMLEntity(XMLEntity xml) { |
237 |
setDescription(xml.getStringProperty("desc"));
|
238 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
239 |
setAngle(xml.getDoubleProperty("angle"));
|
240 |
setOffset(xml.getDoubleProperty("offset"));
|
241 |
setSeparation(xml.getDoubleProperty("separation"));
|
242 |
|
243 |
if (xml.contains("fillColor")) |
244 |
setFillColor(StringUtilities. |
245 |
string2Color(xml.getStringProperty("fillColor")));
|
246 |
|
247 |
if (xml.contains("hasFill")) |
248 |
setHasFill(xml.getBooleanProperty("hasFill"));
|
249 |
|
250 |
XMLEntity lineSymbolXML = xml.firstChild("id", "fillLine"); |
251 |
if (lineSymbolXML != null) { |
252 |
setLineSymbol((ILineSymbol) SymbologyFactory. |
253 |
createSymbolFromXML(lineSymbolXML, "fill symbol"));
|
254 |
} |
255 |
|
256 |
XMLEntity outlineXML = xml.firstChild("id", "outline"); |
257 |
if (outlineXML != null) { |
258 |
setOutline((ILineSymbol) SymbologyFactory. |
259 |
createSymbolFromXML(outlineXML, "outline symbol"));
|
260 |
} |
261 |
if (xml.contains("hasOutline")) |
262 |
setHasOutline(xml.getBooleanProperty("hasOutline"));
|
263 |
|
264 |
if (xml.contains("unit")) { // remove this line when done |
265 |
// measure unit (for outline)
|
266 |
setUnit(xml.getIntProperty("unit"));
|
267 |
|
268 |
// reference system (for outline)
|
269 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
270 |
} |
271 |
} |
272 |
|
273 |
public String getClassName() { |
274 |
return getClass().getName();
|
275 |
} |
276 |
|
277 |
public void print(Graphics2D g, AffineTransform at, FShape shape, |
278 |
PrintRequestAttributeSet properties) {
|
279 |
this.properties=properties;
|
280 |
draw(g, at, shape, null);
|
281 |
this.properties=null; |
282 |
} |
283 |
|
284 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
285 |
super.setCartographicSize(cartographicSize, shp);
|
286 |
if (getLineSymbol()!=null) { |
287 |
getLineSymbol().setCartographicSize(cartographicSize, shp); |
288 |
} |
289 |
} |
290 |
|
291 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
292 |
double s = super.toCartographicSize(viewPort, dpi, shp); |
293 |
csOffset = CartographicSupportToolkit. |
294 |
getCartographicLength(this, offset, viewPort, dpi);
|
295 |
|
296 |
csSeparation = CartographicSupportToolkit. |
297 |
getCartographicLength(this, separation, viewPort, dpi);
|
298 |
double csLineWidth = CartographicSupportToolkit.
|
299 |
getCartographicLength(this, symbolLineWidth, viewPort, dpi);
|
300 |
if (getLineSymbol()!=null) { |
301 |
getLineSymbol().setCartographicSize(csLineWidth, shp); |
302 |
} |
303 |
return s;
|
304 |
} |
305 |
|
306 |
|
307 |
@Override
|
308 |
public void setUnit(int unitIndex) { |
309 |
super.setUnit(unitIndex);
|
310 |
if (getLineSymbol()!=null) { |
311 |
getLineSymbol().setUnit(unitIndex); |
312 |
} |
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void setReferenceSystem(int system) { |
317 |
super.setReferenceSystem(system);
|
318 |
if (getLineSymbol()!=null) { |
319 |
getLineSymbol().setReferenceSystem(system); |
320 |
} |
321 |
} |
322 |
} |