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

History | View | Annotate | Download (7.92 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.Color;
27
import java.awt.Rectangle;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.mapcontext.MapContext;
31
import org.gvsig.fmap.mapcontext.MapContextLocator;
32
import org.gvsig.fmap.mapcontext.ViewPort;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
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, 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 int unit;
71
        private int referenceSystem;
72

    
73
        private boolean isShapeVisible = true;
74

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

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

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

    
93
        /**
94
         * @return
95
         * @uml.property name="isShapeVisible"
96
         */
97
        public final boolean isShapeVisible() {
98
                return isShapeVisible;
99
        }
100

    
101
        /**
102
         * Sets this symbol to visible
103
         * 
104
         * @param isShapeVisible
105
         */
106
        public final void setIsShapeVisible(boolean isShapeVisible) {
107
                this.isShapeVisible = isShapeVisible;
108
        }
109

    
110
        public void setUnit(int unitIndex) {
111
                this.unit = unitIndex;
112
        }
113

    
114
        public int getUnit() {
115
                return this.unit;
116
        }
117

    
118
        public int getReferenceSystem() {
119
                return this.referenceSystem;
120
        }
121

    
122
        public void setReferenceSystem(int system) {
123
                this.referenceSystem = system;
124

    
125
        }
126
        
127
        private boolean areEquals(Object a,Object b) {
128
                if( a==null ) {
129
                        if( b==null) {
130
                                return true;
131
                        }
132
                        return false;
133
                } else {
134
                        if( b == null ) {
135
                                return false;
136
                        }
137
                }
138
                return a.equals(b);
139
        }
140

    
141
        public boolean equals(Object obj) {
142
                ISymbol other = null;
143
                
144
                if( obj==null ) {
145
                        return false;
146
                }
147
                if( ! areEquals(obj.getClass(),this.getClass()) ) {
148
                        // El try/catch y la salida al log es una medida
149
                        // temporal hasta que averiguemos quien causa que
150
                        // pase por aqui, ya que no parece razonable que 
151
                        // desde gvSIG se invoque con algo que no sea un ISymbol.
152
                        try {
153
                                other = (ISymbol) obj;
154
                        } catch(Exception ex) {
155
                                LOG.warn("Suspicious comparison between ISymbol and '"+obj.getClass().getName()+"'.",ex);
156
                        }
157
                        return false;
158
                }
159
                
160
                other = (ISymbol) obj;
161
                
162
                if( ! areEquals(other.getColor(),this.getColor()) ) {
163
                        return false;
164
                }
165
                if( other.getOnePointRgb() != this.getOnePointRgb()) {
166
                        return false;
167
                }
168
                if( ! areEquals(other.getDescription(),this.getDescription()) ) {
169
                        return false;
170
                }
171
                return true;
172
        }
173

    
174
        public boolean isOneDotOrPixel(Geometry geom,
175
                        double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
176

    
177
                int type = geom.getType();
178
                switch (type) {
179
                case Geometry.TYPES.NULL:
180
                case Geometry.TYPES.POINT:
181
                case Geometry.TYPES.MULTIPOINT:
182
                        return false;
183
                default:
184
                        org.gvsig.fmap.geom.primitive.Envelope geomBounds = geom
185
                        .getEnvelope();
186

    
187
                        double dist1Pixel = viewPort.getDist1pixel();
188

    
189
                        float[] distances = new float[2];
190
                        this.getPixExtentPlus(geom, distances, viewPort, dpi);
191

    
192
                        boolean onePoint =
193
                                        (geomBounds.getLength(0) + distances[0] <= dist1Pixel && geomBounds
194
                                        .getLength(1)
195
                                        + distances[1] <= dist1Pixel);
196

    
197
                        if (onePoint) {
198
                                Rectangle bounds = geom.getShape().getBounds();
199
                                positionOfDotOrPixel[0] = bounds.x;
200
                                positionOfDotOrPixel[1] = bounds.y;
201
                        }
202
                        return onePoint;
203
                }
204
        }
205

    
206
        public Object clone() throws CloneNotSupportedException {
207
                return super.clone();        
208
        }
209

    
210
        protected ISymbol cloneForSelection() {
211
                try {
212
                        ISymbol selectionSymbol = (ISymbol) clone();
213
                        selectionSymbol.setColor(MapContext.getSelectionColor());
214
                        if (getDescription() != null){
215
                                selectionSymbol.setDescription(getDescription().concat(
216
                                " version for selection"));
217
                        }else{
218
                                selectionSymbol.setDescription("version for selection");
219
                        }
220
                        return selectionSymbol;
221
                } catch (CloneNotSupportedException e) {
222
                        LOG.error(
223
                                        "Error creating the selection symbol for the symbol "
224
                                        + this, e);
225
                }
226
                return null;
227
        }
228

    
229
        public void loadFromState(PersistentState state)
230
                        throws PersistenceException {
231
                setDescription(state.getString(FIELD_DESCRIPTION));
232
                setIsShapeVisible(state.getBoolean(FIELD_IS_SHAPE_VISIBLE));
233
                setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
234
                setUnit(state.getInt(FIELD_UNIT));
235
        }
236

    
237
        public void saveToState(PersistentState state) throws PersistenceException {
238
                state.set(FIELD_DESCRIPTION, getDescription());
239
                state.set(FIELD_IS_SHAPE_VISIBLE, isShapeVisible());
240
                state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
241
                state.set(FIELD_UNIT, getUnit());
242
        }
243

    
244
        public static class RegisterPersistence implements Callable {
245

    
246
                public Object call() throws Exception {
247
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
248
                        if( manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
249
                                DynStruct definition = manager.addDefinition(
250
                                                AbstractSymbol.class, 
251
                                                SYMBOL_PERSISTENCE_DEFINITION_NAME, 
252
                                                SYMBOL_PERSISTENCE_DEFINITION_NAME+" persistence definition", 
253
                                                null, 
254
                                                null
255
                                );
256
                                // Description
257
                                definition.addDynFieldString(FIELD_DESCRIPTION);
258
                                // Is Shape Visible
259
                                definition.addDynFieldBoolean(FIELD_IS_SHAPE_VISIBLE).setMandatory(true);
260
                                // Reference system
261
                                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
262
                                // Unit
263
                                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
264
                        }
265
                        return Boolean.TRUE;
266
                }
267
                
268
        }
269

    
270
        
271
}