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

History | View | Annotate | Download (8.35 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40560 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40560 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40560 jjdelcerro
 *
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 40435 jjdelcerro
 */
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 41398 jjdelcerro
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2;
35 40435 jjdelcerro
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 41398 jjdelcerro
public abstract class AbstractSymbol implements ISymbol_v2, CartographicSupport {
55 40435 jjdelcerro
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 41398 jjdelcerro
        private String id;
71 40435 jjdelcerro
        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 41398 jjdelcerro
        /**
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 40435 jjdelcerro
        /**
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 40981 jjdelcerro
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 40435 jjdelcerro
161
        public boolean equals(Object obj) {
162 41016 jjdelcerro
                ISymbol other = null;
163 40981 jjdelcerro
164
                if( obj==null ) {
165 40435 jjdelcerro
                        return false;
166
                }
167 40981 jjdelcerro
                if( ! areEquals(obj.getClass(),this.getClass()) ) {
168 41016 jjdelcerro
                        // 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 40981 jjdelcerro
                        return false;
178 40435 jjdelcerro
                }
179 41015 jldominguez
180 41016 jjdelcerro
                other = (ISymbol) obj;
181 41015 jldominguez
182 40981 jjdelcerro
                if( ! areEquals(other.getColor(),this.getColor()) ) {
183 40435 jjdelcerro
                        return false;
184
                }
185 40981 jjdelcerro
                if( other.getOnePointRgb() != this.getOnePointRgb()) {
186
                        return false;
187 40435 jjdelcerro
                }
188 40981 jjdelcerro
                if( ! areEquals(other.getDescription(),this.getDescription()) ) {
189
                        return false;
190
                }
191 40435 jjdelcerro
                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
}