root / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / warning / impl / WarningSymbol.java @ 32880
History | View | Annotate | Download (7.73 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.rendering.symbols.IWarningSymbol; |
36 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
37 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend; |
38 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.MultiShapeSymbol; |
39 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.dynobject.DynClass; |
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 |
|
48 |
/**
|
49 |
* Warning symbol
|
50 |
*
|
51 |
* @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
|
52 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
53 |
*/
|
54 |
public class WarningSymbol extends MultiShapeSymbol implements IWarningSymbol { |
55 |
|
56 |
public static final String WARNING_SYMBOL_DYNCLASS_NAME = "WarningSymbol"; |
57 |
|
58 |
private static final String FIELD_DESCRIPTION = "description"; |
59 |
private static final String FIELD_MESSAGE = "message"; |
60 |
private static final String FIELD_EXCEPTION_TYPE = "exceptionType"; |
61 |
|
62 |
private String message; |
63 |
private int exceptionType; |
64 |
private SimpleTextSymbol text;
|
65 |
|
66 |
/**
|
67 |
* Empty constructor, only used in persistence.
|
68 |
*/
|
69 |
public WarningSymbol() {
|
70 |
// Nothing to do
|
71 |
} |
72 |
|
73 |
public WarningSymbol(String message, String symbolDesc, |
74 |
int symbolDrawExceptionType) {
|
75 |
this.message = message;
|
76 |
this.exceptionType = symbolDrawExceptionType;
|
77 |
setDescription(symbolDesc); |
78 |
} |
79 |
|
80 |
public void draw(Graphics2D g, AffineTransform affineTransform, |
81 |
Geometry geom, Cancellable cancel) { |
82 |
try {
|
83 |
drawInsideRectangle(g, g.getTransform(), geom.getBounds(), null);
|
84 |
} catch (SymbolDrawingException e) {
|
85 |
// IMPOSSIBLE
|
86 |
} |
87 |
} |
88 |
|
89 |
public void setDrawExceptionType(int symbolDrawExceptionType) { |
90 |
this.exceptionType = symbolDrawExceptionType;
|
91 |
} |
92 |
|
93 |
public void setMessage(String message) { |
94 |
this.message = message;
|
95 |
} |
96 |
|
97 |
private String getMessage() { |
98 |
return message;
|
99 |
} |
100 |
|
101 |
public void drawInsideRectangle(Graphics2D g, |
102 |
AffineTransform scaleInstance, Rectangle r, |
103 |
PrintAttributes properties) throws SymbolDrawingException {
|
104 |
g.setClip(r); |
105 |
if (message == null) { |
106 |
message = "Symbol undrawable.\nPlease, check errors.";
|
107 |
} |
108 |
|
109 |
String[] messageLines = CompatLocator.getStringUtils().split(message, |
110 |
"\n");
|
111 |
int strokeWidth = (int) (Math.min(r.width, r.height) * .1); |
112 |
|
113 |
if (strokeWidth == 0) { |
114 |
strokeWidth = 1;
|
115 |
} |
116 |
g.setColor(Color.red);
|
117 |
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, |
118 |
BasicStroke.JOIN_ROUND));
|
119 |
int width = r.width - (strokeWidth + strokeWidth);
|
120 |
int height = r.height - (strokeWidth + strokeWidth);
|
121 |
double radius = Math.min(width, height) * .5; |
122 |
double centerX = r.getCenterX();
|
123 |
double centerY = r.getCenterY();
|
124 |
Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY |
125 |
- radius, 2 * radius, 2 * radius); |
126 |
g.draw(circle); |
127 |
g.setClip(circle); |
128 |
double aux = Math.cos(Math.PI * 0.25) * radius; |
129 |
g.drawLine((int) (centerX - aux), (int) (centerY - aux), |
130 |
(int) (centerX + aux), (int) (centerY + aux)); |
131 |
int fontSize = 20; |
132 |
g.setFont(new Font("Arial", fontSize, Font.PLAIN)); |
133 |
g.setColor(Color.black);
|
134 |
g.setClip(null);
|
135 |
|
136 |
if (text == null) { |
137 |
text = new SimpleTextSymbol();
|
138 |
text.setAutoresizeEnabled(true);
|
139 |
|
140 |
} |
141 |
|
142 |
double lineHeight = (r.getHeight() - 6) / messageLines.length; |
143 |
Rectangle textRect = new Rectangle((int) r.getMinX(), |
144 |
(int) r.getMinY() + 6, (int) r.getWidth(), (int) lineHeight); |
145 |
for (int i = 0; i < messageLines.length; i++) { |
146 |
text.setText(messageLines[i]); |
147 |
text.drawInsideRectangle(g, null, textRect, null); |
148 |
textRect.setLocation((int) r.getX(), (int) (r.getY() + r |
149 |
.getHeight())); |
150 |
} |
151 |
} |
152 |
|
153 |
public Object clone() throws CloneNotSupportedException { |
154 |
WarningSymbol copy = (WarningSymbol) super.clone();
|
155 |
|
156 |
if (text != null) { |
157 |
copy.text = (SimpleTextSymbol) text.clone(); |
158 |
} |
159 |
|
160 |
return copy;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @return the exceptionType
|
165 |
*/
|
166 |
private int getExceptionType() { |
167 |
return exceptionType;
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* @param exceptionType
|
172 |
* the exceptionType to set
|
173 |
*/
|
174 |
private void setExceptionType(int exceptionType) { |
175 |
this.exceptionType = exceptionType;
|
176 |
} |
177 |
|
178 |
public void loadFromState(PersistentState state) |
179 |
throws PersistenceException {
|
180 |
setDescription(state.getString(FIELD_DESCRIPTION)); |
181 |
setExceptionType(state.getInt(FIELD_EXCEPTION_TYPE)); |
182 |
setMessage(state.getString(FIELD_MESSAGE)); |
183 |
} |
184 |
|
185 |
public void saveToState(PersistentState state) throws PersistenceException { |
186 |
state.set(FIELD_DESCRIPTION, getDescription()); |
187 |
state.set(FIELD_EXCEPTION_TYPE, getExceptionType()); |
188 |
state.set(FIELD_MESSAGE, getMessage()); |
189 |
} |
190 |
|
191 |
public static void registerPersistence() { |
192 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
193 |
DynStruct definition = manager.addDefinition( |
194 |
WarningSymbol.class, |
195 |
WARNING_SYMBOL_DYNCLASS_NAME, |
196 |
WARNING_SYMBOL_DYNCLASS_NAME+" Persistence definition",
|
197 |
null,
|
198 |
null
|
199 |
); |
200 |
// Description
|
201 |
definition.addDynFieldString(FIELD_DESCRIPTION).setMandatory(true);
|
202 |
// Exception type
|
203 |
definition.addDynFieldInt(FIELD_EXCEPTION_TYPE).setMandatory(true);
|
204 |
// Message
|
205 |
definition.addDynFieldString(FIELD_MESSAGE).setMandatory(true);
|
206 |
} |
207 |
|
208 |
// public static void main(String[] args) {
|
209 |
// MapContextImplLibrary mapContextImplLibrary = new
|
210 |
// MapContextImplLibrary();
|
211 |
// SECompatLibrary compatLibrary = new SECompatLibrary();
|
212 |
// DefaultGeometryLibrary geometryLibrary = new DefaultGeometryLibrary();
|
213 |
//
|
214 |
// mapContextImplLibrary.initialize();
|
215 |
// compatLibrary.initialize();
|
216 |
// geometryLibrary.initialize();
|
217 |
// mapContextImplLibrary.postInitialize();
|
218 |
// compatLibrary.postInitialize();
|
219 |
// geometryLibrary.postInitialize();
|
220 |
//
|
221 |
// JFrame f = new JFrame();
|
222 |
// final ISymbol warning = new WarningSymbol(
|
223 |
// SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
|
224 |
// "a description",
|
225 |
// SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
|
226 |
// JPanel preview = new JPanel() {
|
227 |
// protected void paintComponent(Graphics g) {
|
228 |
// // TODO Auto-generated method stub
|
229 |
// super.paintComponent(g);
|
230 |
// Graphics2D g2 = (Graphics2D) g;
|
231 |
// try {
|
232 |
// warning.drawInsideRectangle(g2, g2.getTransform(),
|
233 |
// getBounds(), null);
|
234 |
// } catch (SymbolDrawingException e) {
|
235 |
// // TODO Auto-generated catch block
|
236 |
// e.printStackTrace();
|
237 |
// }
|
238 |
// }
|
239 |
// };
|
240 |
// f.setContentPane(preview);
|
241 |
// f.pack();
|
242 |
// f.setVisible(true);
|
243 |
// }
|
244 |
|
245 |
} |