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 @ 47476

History | View | Annotate | Download (7.33 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
import org.gvsig.compat.CompatLocator;
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.getShape().getBounds());
86
                } catch (SymbolDrawingException e) {
87
                        // IMPOSSIBLE
88
                }
89
        }
90

    
91
        @Override
92
        public void setDrawExceptionType(int symbolDrawExceptionType) {
93
                this.exceptionType = symbolDrawExceptionType;
94
        }
95

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

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

    
105
        @Override
106
        public void drawInsideRectangle(Graphics2D g,
107
                        AffineTransform scaleInstance, Rectangle r) 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 (String messageLine : messageLines) {
150
                text.setText(messageLine);
151
                text.drawInsideRectangle(g, null, textRect);
152
                textRect.setLocation((int) r.getX(), (int) (r.getY() + r
153
                        .getHeight()));
154
            }
155
        }
156

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

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

    
165
                return copy;
166
        }
167

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

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

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

    
191
        @Override
192
        public void saveToState(PersistentState state) throws PersistenceException {
193
                state.set(FIELD_DESCRIPTION, getDescription());
194
                state.set(FIELD_EXCEPTION_TYPE, getExceptionType());
195
                state.set(FIELD_MESSAGE, getMessage());
196
        }
197

    
198
        public static class RegisterPersistence implements Callable {
199

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

    
223
        public static class RegisterSymbol implements Callable {
224

    
225
                @Override
226
                public Object call() throws Exception {
227
                SymbolManager manager = MapContextLocator.getSymbolManager();
228
                
229
                manager.registerSymbol(IWarningSymbol.SYMBOL_NAME,
230
                        WarningSymbol.class);
231

    
232
                        return Boolean.TRUE;
233
                }
234
                
235
        }
236

    
237
}