root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / warning / impl / WarningSymbol.java @ 34294
History | View | Annotate | Download (7.14 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.warning.impl; |
23 |
|
24 |
import java.awt.BasicStroke; |
25 |
import java.awt.Color; |
26 |
import java.awt.Font; |
27 |
import java.awt.Graphics2D; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.geom.AffineTransform; |
30 |
import java.awt.geom.Ellipse2D; |
31 |
|
32 |
import org.gvsig.compat.CompatLocator; |
33 |
import org.gvsig.compat.print.PrintAttributes; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
36 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
37 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
38 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
39 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.MultiShapeSymbol; |
40 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol; |
41 |
import org.gvsig.tools.ToolsLocator; |
42 |
import org.gvsig.tools.dynobject.DynStruct; |
43 |
import org.gvsig.tools.persistence.PersistenceManager; |
44 |
import org.gvsig.tools.persistence.PersistentState; |
45 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
46 |
import org.gvsig.tools.task.Cancellable; |
47 |
import org.gvsig.tools.util.Callable; |
48 |
|
49 |
/**
|
50 |
* Warning symbol
|
51 |
*
|
52 |
* @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
53 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
54 |
*/
|
55 |
public class WarningSymbol extends MultiShapeSymbol implements IWarningSymbol { |
56 |
|
57 |
public static final String WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME = "WarningSymbol"; |
58 |
|
59 |
private static final String FIELD_DESCRIPTION = "description"; |
60 |
private static final String FIELD_MESSAGE = "message"; |
61 |
private static final String FIELD_EXCEPTION_TYPE = "exceptionType"; |
62 |
|
63 |
private String message; |
64 |
private int exceptionType; |
65 |
private SimpleTextSymbol text;
|
66 |
|
67 |
/**
|
68 |
* Empty constructor, only used in persistence.
|
69 |
*/
|
70 |
public WarningSymbol() {
|
71 |
super();
|
72 |
} |
73 |
|
74 |
public WarningSymbol(String message, String symbolDesc, |
75 |
int symbolDrawExceptionType) {
|
76 |
super();
|
77 |
this.message = message;
|
78 |
this.exceptionType = symbolDrawExceptionType;
|
79 |
setDescription(symbolDesc); |
80 |
} |
81 |
|
82 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
83 |
Geometry geom, Cancellable cancel) { |
84 |
try {
|
85 |
drawInsideRectangle(g, g.getTransform(), geom.getBounds(), null);
|
86 |
} catch (SymbolDrawingException e) {
|
87 |
// IMPOSSIBLE
|
88 |
} |
89 |
} |
90 |
|
91 |
public void setDrawExceptionType(int symbolDrawExceptionType) { |
92 |
this.exceptionType = symbolDrawExceptionType;
|
93 |
} |
94 |
|
95 |
public void setMessage(String message) { |
96 |
this.message = message;
|
97 |
} |
98 |
|
99 |
private String getMessage() { |
100 |
return message;
|
101 |
} |
102 |
|
103 |
public void drawInsideRectangle(Graphics2D g, |
104 |
AffineTransform scaleInstance, Rectangle r, |
105 |
PrintAttributes properties) throws SymbolDrawingException {
|
106 |
g.setClip(r); |
107 |
if (message == null) { |
108 |
message = "Symbol undrawable.\nPlease, check errors.";
|
109 |
} |
110 |
|
111 |
String[] messageLines = CompatLocator.getStringUtils().split(message, |
112 |
"\n");
|
113 |
int strokeWidth = (int) (Math.min(r.width, r.height) * .1); |
114 |
|
115 |
if (strokeWidth == 0) { |
116 |
strokeWidth = 1;
|
117 |
} |
118 |
g.setColor(Color.red);
|
119 |
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, |
120 |
BasicStroke.JOIN_ROUND));
|
121 |
int width = r.width - (strokeWidth + strokeWidth);
|
122 |
int height = r.height - (strokeWidth + strokeWidth);
|
123 |
double radius = Math.min(width, height) * .5; |
124 |
double centerX = r.getCenterX();
|
125 |
double centerY = r.getCenterY();
|
126 |
Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY |
127 |
- radius, 2 * radius, 2 * radius); |
128 |
g.draw(circle); |
129 |
g.setClip(circle); |
130 |
double aux = Math.cos(Math.PI * 0.25) * radius; |
131 |
g.drawLine((int) (centerX - aux), (int) (centerY - aux), |
132 |
(int) (centerX + aux), (int) (centerY + aux)); |
133 |
int fontSize = 20; |
134 |
g.setFont(new Font("Arial", fontSize, Font.PLAIN)); |
135 |
g.setColor(Color.black);
|
136 |
g.setClip(null);
|
137 |
|
138 |
if (text == null) { |
139 |
text = new SimpleTextSymbol();
|
140 |
text.setAutoresizeEnabled(true);
|
141 |
|
142 |
} |
143 |
|
144 |
double lineHeight = (r.getHeight() - 6) / messageLines.length; |
145 |
Rectangle textRect = new Rectangle((int) r.getMinX(), |
146 |
(int) r.getMinY() + 6, (int) r.getWidth(), (int) lineHeight); |
147 |
for (int i = 0; i < messageLines.length; i++) { |
148 |
text.setText(messageLines[i]); |
149 |
text.drawInsideRectangle(g, null, textRect, null); |
150 |
textRect.setLocation((int) r.getX(), (int) (r.getY() + r |
151 |
.getHeight())); |
152 |
} |
153 |
} |
154 |
|
155 |
public Object clone() throws CloneNotSupportedException { |
156 |
WarningSymbol copy = (WarningSymbol) super.clone();
|
157 |
|
158 |
if (text != null) { |
159 |
copy.text = (SimpleTextSymbol) text.clone(); |
160 |
} |
161 |
|
162 |
return copy;
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* @return the exceptionType
|
167 |
*/
|
168 |
private int getExceptionType() { |
169 |
return exceptionType;
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* @param exceptionType
|
174 |
* the exceptionType to set
|
175 |
*/
|
176 |
private void setExceptionType(int exceptionType) { |
177 |
this.exceptionType = exceptionType;
|
178 |
} |
179 |
|
180 |
public void loadFromState(PersistentState state) |
181 |
throws PersistenceException {
|
182 |
setDescription(state.getString(FIELD_DESCRIPTION)); |
183 |
setExceptionType(state.getInt(FIELD_EXCEPTION_TYPE)); |
184 |
setMessage(state.getString(FIELD_MESSAGE)); |
185 |
} |
186 |
|
187 |
public void saveToState(PersistentState state) throws PersistenceException { |
188 |
state.set(FIELD_DESCRIPTION, getDescription()); |
189 |
state.set(FIELD_EXCEPTION_TYPE, getExceptionType()); |
190 |
state.set(FIELD_MESSAGE, getMessage()); |
191 |
} |
192 |
|
193 |
public static class RegisterPersistence implements Callable { |
194 |
|
195 |
public Object call() throws Exception { |
196 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
197 |
if( manager.getDefinition(WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
198 |
DynStruct definition = manager.addDefinition( |
199 |
WarningSymbol.class, |
200 |
WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
201 |
WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
202 |
null,
|
203 |
null
|
204 |
); |
205 |
// Description
|
206 |
definition.addDynFieldString(FIELD_DESCRIPTION).setMandatory(true);
|
207 |
// Exception type
|
208 |
definition.addDynFieldInt(FIELD_EXCEPTION_TYPE).setMandatory(true);
|
209 |
// Message
|
210 |
definition.addDynFieldString(FIELD_MESSAGE).setMandatory(true);
|
211 |
} |
212 |
return Boolean.TRUE; |
213 |
} |
214 |
|
215 |
} |
216 |
|
217 |
public static class RegisterSymbol implements Callable { |
218 |
|
219 |
public Object call() throws Exception { |
220 |
SymbolManager manager = MapContextLocator.getSymbolManager(); |
221 |
|
222 |
manager.registerSymbol(IWarningSymbol.SYMBOL_NAME, |
223 |
WarningSymbol.class); |
224 |
|
225 |
return Boolean.TRUE; |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
} |