Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / 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 @ 40560

History | View | Annotate | Download (7.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.warning.impl;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.Color;
28
import java.awt.Font;
29
import java.awt.Graphics2D;
30
import java.awt.Rectangle;
31
import java.awt.geom.AffineTransform;
32
import java.awt.geom.Ellipse2D;
33

    
34
import org.gvsig.compat.CompatLocator;
35
import org.gvsig.compat.print.PrintAttributes;
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.fmap.mapcontext.MapContextLocator;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
40
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
41
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.MultiShapeSymbol;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.text.impl.SimpleTextSymbol;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DynStruct;
45
import org.gvsig.tools.persistence.PersistenceManager;
46
import org.gvsig.tools.persistence.PersistentState;
47
import org.gvsig.tools.persistence.exception.PersistenceException;
48
import org.gvsig.tools.task.Cancellable;
49
import org.gvsig.tools.util.Callable;
50

    
51
/**
52
 * Warning symbol
53
 * 
54
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
55
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
56
 */
57
public class WarningSymbol extends MultiShapeSymbol implements IWarningSymbol {
58

    
59
        public static final String WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME = "WarningSymbol";
60

    
61
        private static final String FIELD_DESCRIPTION = "description";
62
        private static final String FIELD_MESSAGE = "message";
63
        private static final String FIELD_EXCEPTION_TYPE = "exceptionType";
64

    
65
        private String message;
66
        private int exceptionType;
67
        private SimpleTextSymbol text;
68

    
69
        /**
70
         * Empty constructor, only used in persistence.
71
         */
72
        public WarningSymbol() {
73
                super();
74
        }
75

    
76
        public WarningSymbol(String message, String symbolDesc,
77
                        int symbolDrawExceptionType) {
78
                super();
79
                this.message = message;
80
                this.exceptionType = symbolDrawExceptionType;
81
                setDescription(symbolDesc);
82
        }
83

    
84
        public void draw(Graphics2D g, AffineTransform affineTransform,
85
                        Geometry geom, Cancellable cancel) {
86
                try {
87
                        drawInsideRectangle(g, g.getTransform(), geom.getShape().getBounds(), null);
88
                } catch (SymbolDrawingException e) {
89
                        // IMPOSSIBLE
90
                }
91
        }
92

    
93
        public void setDrawExceptionType(int symbolDrawExceptionType) {
94
                this.exceptionType = symbolDrawExceptionType;
95
        }
96

    
97
        public void setMessage(String message) {
98
                this.message = message;
99
        }
100

    
101
        private String getMessage() {
102
                return message;
103
        }
104

    
105
        public void drawInsideRectangle(Graphics2D g,
106
                        AffineTransform scaleInstance, Rectangle r,
107
                        PrintAttributes properties) throws SymbolDrawingException {
108
                g.setClip(r);
109
                if (message == null) {
110
                        message = "Symbol undrawable.\nPlease, check errors.";
111
                }
112

    
113
                String[] messageLines = CompatLocator.getStringUtils().split(message,
114
                                "\n");
115
                int strokeWidth = (int) (Math.min(r.width, r.height) * .1);
116

    
117
                if (strokeWidth == 0) {
118
                        strokeWidth = 1;
119
                }
120
                g.setColor(Color.red);
121
                g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT,
122
                                BasicStroke.JOIN_ROUND));
123
                int width = r.width - (strokeWidth + strokeWidth);
124
                int height = r.height - (strokeWidth + strokeWidth);
125
                double radius = Math.min(width, height) * .5;
126
                double centerX = r.getCenterX();
127
                double centerY = r.getCenterY();
128
                Ellipse2D circle = new Ellipse2D.Double(centerX - radius, centerY
129
                                - radius, 2 * radius, 2 * radius);
130
                g.draw(circle);
131
                g.setClip(circle);
132
                double aux = Math.cos(Math.PI * 0.25) * radius;
133
                g.drawLine((int) (centerX - aux), (int) (centerY - aux),
134
                                (int) (centerX + aux), (int) (centerY + aux));
135
                int fontSize = 20;
136
                g.setFont(new Font("Arial", fontSize, Font.PLAIN));
137
                g.setColor(Color.black);
138
                g.setClip(null);
139

    
140
                if (text == null) {
141
                        text = new SimpleTextSymbol();
142
                        text.setAutoresizeEnabled(true);
143

    
144
                }
145

    
146
                double lineHeight = (r.getHeight() - 6) / messageLines.length;
147
                Rectangle textRect = new Rectangle((int) r.getMinX(),
148
                                (int) r.getMinY() + 6, (int) r.getWidth(), (int) lineHeight);
149
                for (int i = 0; i < messageLines.length; i++) {
150
                        text.setText(messageLines[i]);
151
                        text.drawInsideRectangle(g, null, textRect, null);
152
                        textRect.setLocation((int) r.getX(), (int) (r.getY() + r
153
                                        .getHeight()));
154
                }
155
        }
156

    
157
        public Object clone() throws CloneNotSupportedException {
158
                WarningSymbol copy = (WarningSymbol) super.clone();
159

    
160
                if (text != null) {
161
                        copy.text = (SimpleTextSymbol) text.clone();
162
                }
163

    
164
                return copy;
165
        }
166

    
167
        /**
168
         * @return the exceptionType
169
         */
170
        private int getExceptionType() {
171
                return exceptionType;
172
        }
173

    
174
        /**
175
         * @param exceptionType
176
         *            the exceptionType to set
177
         */
178
        private void setExceptionType(int exceptionType) {
179
                this.exceptionType = exceptionType;
180
        }
181

    
182
        public void loadFromState(PersistentState state)
183
                        throws PersistenceException {
184
                setDescription(state.getString(FIELD_DESCRIPTION));
185
                setExceptionType(state.getInt(FIELD_EXCEPTION_TYPE));
186
                setMessage(state.getString(FIELD_MESSAGE));
187
        }
188

    
189
        public void saveToState(PersistentState state) throws PersistenceException {
190
                state.set(FIELD_DESCRIPTION, getDescription());
191
                state.set(FIELD_EXCEPTION_TYPE, getExceptionType());
192
                state.set(FIELD_MESSAGE, getMessage());
193
        }
194

    
195
        public static class RegisterPersistence implements Callable {
196

    
197
                public Object call() throws Exception {
198
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
199
                        if( manager.getDefinition(WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
200
                                DynStruct definition = manager.addDefinition(
201
                                                WarningSymbol.class,
202
                                                WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME,
203
                                                WARNING_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
204
                                                null, 
205
                                                null
206
                                );
207
                                // Description
208
                                definition.addDynFieldString(FIELD_DESCRIPTION).setMandatory(true);
209
                                // Exception type
210
                                definition.addDynFieldInt(FIELD_EXCEPTION_TYPE).setMandatory(true);
211
                                // Message
212
                                definition.addDynFieldString(FIELD_MESSAGE).setMandatory(true);
213
                        }
214
                        return Boolean.TRUE;
215
                }
216
                
217
        }
218

    
219
        public static class RegisterSymbol implements Callable {
220

    
221
                public Object call() throws Exception {
222
                SymbolManager manager = MapContextLocator.getSymbolManager();
223
                
224
                manager.registerSymbol(IWarningSymbol.SYMBOL_NAME,
225
                        WarningSymbol.class);
226

    
227
                        return Boolean.TRUE;
228
                }
229
                
230
        }
231

    
232
}