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 / impl / AbstractSymbol.java @ 41398

History | View | Annotate | Download (8.35 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.impl;
25

    
26
import java.awt.Rectangle;
27

    
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.mapcontext.MapContext;
30
import org.gvsig.fmap.mapcontext.MapContextLocator;
31
import org.gvsig.fmap.mapcontext.ViewPort;
32
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41
import org.gvsig.tools.util.Callable;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
/**
46
 * Abstract class that implements the interface the interface for symbols.It is
47
 * considered as the father of all XXXSymbols and will implement all the methods
48
 * that these classes had not developed (and correspond with one of the methods
49
 * of AbstractSymbol class)
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 abstract class AbstractSymbol implements ISymbol_v2, CartographicSupport {
55

    
56
        private static final Logger LOG = LoggerFactory.getLogger(AbstractSymbol.class);
57

    
58
        public static final String SYMBOL_PERSISTENCE_DEFINITION_NAME = "Symbol";
59
        
60
        private static final String FIELD_UNIT = "unit";
61

    
62
        private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem";
63

    
64
        private static final String FIELD_IS_SHAPE_VISIBLE = "isShapeVisible";
65

    
66
        private static final String FIELD_DESCRIPTION = "description";
67

    
68

    
69
        private String desc;
70
        private String id; 
71
        private int unit;
72
        private int referenceSystem;
73

    
74
        private boolean isShapeVisible = true;
75

    
76
        public AbstractSymbol() {
77
                super();
78
                SymbolPreferences preferences =
79
                                MapContextLocator.getSymbolManager().getSymbolPreferences();
80
                unit =
81
                                preferences.getDefaultCartographicSupportMeasureUnit();
82
                referenceSystem =
83
                                preferences.getDefaultCartographicSupportReferenceSystem();
84
        }
85

    
86
        public final void setDescription(String desc) {
87
                this.desc = desc;
88
        }
89

    
90
        public final String getDescription() {
91
                return desc;
92
        }
93

    
94
        /**
95
         * Set the id ob the symbol (the basename of file)
96
         * 
97
         * @param id 
98
         */
99
        public final void setID(String id) {
100
                this.id = id;
101
        }
102

    
103
        /**
104
         * Get the id ob the symbol (the basename of file)
105
         * 
106
         * @param id 
107
         */
108
        public final String getID() {
109
                return id;
110
        }
111

    
112
        
113
        /**
114
         * @return
115
         * @uml.property name="isShapeVisible"
116
         */
117
        public final boolean isShapeVisible() {
118
                return isShapeVisible;
119
        }
120

    
121
        /**
122
         * Sets this symbol to visible
123
         * 
124
         * @param isShapeVisible
125
         */
126
        public final void setIsShapeVisible(boolean isShapeVisible) {
127
                this.isShapeVisible = isShapeVisible;
128
        }
129

    
130
        public void setUnit(int unitIndex) {
131
                this.unit = unitIndex;
132
        }
133

    
134
        public int getUnit() {
135
                return this.unit;
136
        }
137

    
138
        public int getReferenceSystem() {
139
                return this.referenceSystem;
140
        }
141

    
142
        public void setReferenceSystem(int system) {
143
                this.referenceSystem = system;
144

    
145
        }
146
        
147
        private boolean areEquals(Object a,Object b) {
148
                if( a==null ) {
149
                        if( b==null) {
150
                                return true;
151
                        }
152
                        return false;
153
                } else {
154
                        if( b == null ) {
155
                                return false;
156
                        }
157
                }
158
                return a.equals(b);
159
        }
160

    
161
        public boolean equals(Object obj) {
162
                ISymbol other = null;
163
                
164
                if( obj==null ) {
165
                        return false;
166
                }
167
                if( ! areEquals(obj.getClass(),this.getClass()) ) {
168
                        // El try/catch y la salida al log es una medida
169
                        // temporal hasta que averiguemos quien causa que
170
                        // pase por aqui, ya que no parece razonable que 
171
                        // desde gvSIG se invoque con algo que no sea un ISymbol.
172
                        try {
173
                                other = (ISymbol) obj;
174
                        } catch(Exception ex) {
175
                                LOG.warn("Suspicious comparison between ISymbol and '"+obj.getClass().getName()+"'.",ex);
176
                        }
177
                        return false;
178
                }
179
                
180
                other = (ISymbol) obj;
181
                
182
                if( ! areEquals(other.getColor(),this.getColor()) ) {
183
                        return false;
184
                }
185
                if( other.getOnePointRgb() != this.getOnePointRgb()) {
186
                        return false;
187
                }
188
                if( ! areEquals(other.getDescription(),this.getDescription()) ) {
189
                        return false;
190
                }
191
                return true;
192
        }
193

    
194
        public boolean isOneDotOrPixel(Geometry geom,
195
                        double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
196

    
197
                int type = geom.getType();
198
                switch (type) {
199
                case Geometry.TYPES.NULL:
200
                case Geometry.TYPES.POINT:
201
                case Geometry.TYPES.MULTIPOINT:
202
                        return false;
203
                default:
204
                        org.gvsig.fmap.geom.primitive.Envelope geomBounds = geom
205
                        .getEnvelope();
206

    
207
                        double dist1Pixel = viewPort.getDist1pixel();
208

    
209
                        float[] distances = new float[2];
210
                        this.getPixExtentPlus(geom, distances, viewPort, dpi);
211

    
212
                        boolean onePoint =
213
                                        (geomBounds.getLength(0) + distances[0] <= dist1Pixel && geomBounds
214
                                        .getLength(1)
215
                                        + distances[1] <= dist1Pixel);
216

    
217
                        if (onePoint) {
218
                                Rectangle bounds = geom.getShape().getBounds();
219
                                positionOfDotOrPixel[0] = bounds.x;
220
                                positionOfDotOrPixel[1] = bounds.y;
221
                        }
222
                        return onePoint;
223
                }
224
        }
225

    
226
        public Object clone() throws CloneNotSupportedException {
227
                return super.clone();        
228
        }
229

    
230
        protected ISymbol cloneForSelection() {
231
                try {
232
                        ISymbol selectionSymbol = (ISymbol) clone();
233
                        selectionSymbol.setColor(MapContext.getSelectionColor());
234
                        if (getDescription() != null){
235
                                selectionSymbol.setDescription(getDescription().concat(
236
                                " version for selection"));
237
                        }else{
238
                                selectionSymbol.setDescription("version for selection");
239
                        }
240
                        return selectionSymbol;
241
                } catch (CloneNotSupportedException e) {
242
                        LOG.error(
243
                                        "Error creating the selection symbol for the symbol "
244
                                        + this, e);
245
                }
246
                return null;
247
        }
248

    
249
        public void loadFromState(PersistentState state)
250
                        throws PersistenceException {
251
                setDescription(state.getString(FIELD_DESCRIPTION));
252
                setIsShapeVisible(state.getBoolean(FIELD_IS_SHAPE_VISIBLE));
253
                setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
254
                setUnit(state.getInt(FIELD_UNIT));
255
        }
256

    
257
        public void saveToState(PersistentState state) throws PersistenceException {
258
                state.set(FIELD_DESCRIPTION, getDescription());
259
                state.set(FIELD_IS_SHAPE_VISIBLE, isShapeVisible());
260
                state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
261
                state.set(FIELD_UNIT, getUnit());
262
        }
263

    
264
        public static class RegisterPersistence implements Callable {
265

    
266
                public Object call() throws Exception {
267
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
268
                        if( manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
269
                                DynStruct definition = manager.addDefinition(
270
                                                AbstractSymbol.class, 
271
                                                SYMBOL_PERSISTENCE_DEFINITION_NAME, 
272
                                                SYMBOL_PERSISTENCE_DEFINITION_NAME+" persistence definition", 
273
                                                null, 
274
                                                null
275
                                );
276
                                // Description
277
                                definition.addDynFieldString(FIELD_DESCRIPTION);
278
                                // Is Shape Visible
279
                                definition.addDynFieldBoolean(FIELD_IS_SHAPE_VISIBLE).setMandatory(true);
280
                                // Reference system
281
                                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
282
                                // Unit
283
                                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
284
                        }
285
                        return Boolean.TRUE;
286
                }
287
                
288
        }
289

    
290
        
291
}