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

History | View | Annotate | Download (10.3 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
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30

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

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

    
59
    private static final Logger LOG = LoggerFactory.getLogger(AbstractSymbol.class);
60

    
61
    public static final String SYMBOL_PERSISTENCE_DEFINITION_NAME = "Symbol";
62

    
63
    private static final String FIELD_UNIT = "unit";
64

    
65
    private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem";
66

    
67
    private static final String FIELD_IS_SHAPE_VISIBLE = "isShapeVisible";
68

    
69
    private static final String FIELD_DESCRIPTION = "description";
70

    
71
    private String desc;
72
    private String id;
73
    private int unit;
74
    private int referenceSystem;
75

    
76
    private boolean isShapeVisible = true;
77
    
78
    private transient Feature feature;
79

    
80
    public AbstractSymbol() {
81
        super();
82
        SymbolPreferences preferences
83
                = MapContextLocator.getSymbolManager().getSymbolPreferences();
84
        unit
85
                = preferences.getDefaultCartographicSupportMeasureUnit();
86
        referenceSystem
87
                = preferences.getDefaultCartographicSupportReferenceSystem();
88
    }
89

    
90
    @Override
91
    public Feature getFeature() {
92
        return this.feature;
93
    }
94

    
95
    @Override
96
    public void setFeature(Feature feature) {
97
        this.feature = feature;
98
    }
99
    
100
    @Override
101
    public final void setDescription(String desc) {
102
        this.desc = desc;
103
    }
104

    
105
    @Override
106
    public final String getDescription() {
107
        return desc;
108
    }
109

    
110
    @Override
111
    public final void setID(String id) {
112
        this.id = id;
113
    }
114

    
115
    @Override
116
    public final String getID() {
117
        return id;
118
    }
119

    
120
    @Override
121
    public final boolean isShapeVisible() {
122
        return isShapeVisible;
123
    }
124

    
125
    /**
126
     * Sets this symbol to visible
127
     *
128
     * @param isShapeVisible
129
     */
130
    public final void setIsShapeVisible(boolean isShapeVisible) {
131
        this.isShapeVisible = isShapeVisible;
132
    }
133

    
134
    @Override
135
    public void setUnit(int unitIndex) {
136
        this.unit = unitIndex;
137
    }
138

    
139
    @Override
140
    public int getUnit() {
141
        return this.unit;
142
    }
143

    
144
    @Override
145
    public int getReferenceSystem() {
146
        return this.referenceSystem;
147
    }
148

    
149
    @Override
150
    public void setReferenceSystem(int system) {
151
        this.referenceSystem = system;
152

    
153
    }
154

    
155
    private boolean areEquals(Object a, Object b) {
156
        if (a == null) {
157
            if (b == null) {
158
                return true;
159
            }
160
            return false;
161
        } else {
162
            if (b == null) {
163
                return false;
164
            }
165
        }
166
        return a.equals(b);
167
    }
168

    
169
    @Override
170
    @SuppressWarnings("UnusedAssignment")
171
    public boolean equals(Object obj) {
172
        ISymbol other = null;
173

    
174
        if (obj == null) {
175
            return false;
176
        }
177
        if (!areEquals(obj.getClass(), this.getClass())) {
178
            // El try/catch y la salida al log es una medida
179
            // temporal hasta que averiguemos quien causa que
180
            // pase por aqui, ya que no parece razonable que 
181
            // desde gvSIG se invoque con algo que no sea un ISymbol.
182
            try {
183
                other = (ISymbol) obj;
184
            } catch (Exception ex) {
185
                LOG.warn("Suspicious comparison between ISymbol and '" + obj.getClass().getName() + "'.", ex);
186
            }
187
            return false;
188
        }
189

    
190
        other = (ISymbol) obj;
191

    
192
        if (!areEquals(other.getColor(), this.getColor())) {
193
            return false;
194
        }
195
        if (other.getOnePointRgb() != this.getOnePointRgb()) {
196
            return false;
197
        }
198
        if (!areEquals(other.getDescription(), this.getDescription())) {
199
            return false;
200
        }
201
        return true;
202
    }
203

    
204
    @Override
205
    public boolean isOneDotOrPixel(Geometry geom,
206
            double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
207

    
208
        int type = geom.getType();
209
        switch (type) {
210
            case Geometry.TYPES.NULL:
211
            case Geometry.TYPES.POINT:
212
            case Geometry.TYPES.MULTIPOINT:
213
                return false;
214
            default:
215
                org.gvsig.fmap.geom.primitive.Envelope geomBounds = geom
216
                        .getEnvelope();
217

    
218
                double dist1Pixel = viewPort.getDist1pixel();
219

    
220
                float[] distances = new float[2];
221
                this.getPixExtentPlus(geom, distances, viewPort, dpi);
222

    
223
                boolean onePoint
224
                        = (geomBounds.getLength(0) + distances[0] <= dist1Pixel && geomBounds
225
                        .getLength(1)
226
                        + distances[1] <= dist1Pixel);
227

    
228
                if (onePoint) {
229
                    Rectangle bounds = geom.getShape().getBounds();
230
                    positionOfDotOrPixel[0] = bounds.x;
231
                    positionOfDotOrPixel[1] = bounds.y;
232
                }
233
                return onePoint;
234
        }
235
    }
236

    
237
    @Override
238
    public Object clone() throws CloneNotSupportedException {
239
        return super.clone();
240
    }
241

    
242
    protected ISymbol cloneForSelection() {
243
        try {
244
            ISymbol selectionSymbol = (ISymbol) clone();
245
            selectionSymbol.setColor(MapContext.getSelectionColor());
246
            if (getDescription() != null) {
247
                selectionSymbol.setDescription(getDescription().concat(
248
                        " version for selection"));
249
            } else {
250
                selectionSymbol.setDescription("version for selection");
251
            }
252
            return selectionSymbol;
253
        } catch (CloneNotSupportedException e) {
254
            LOG.error(
255
                    "Error creating the selection symbol for the symbol "
256
                    + this, e);
257
        }
258
        return null;
259
    }
260

    
261
    @Override
262
    public void loadFromState(PersistentState state)
263
            throws PersistenceException {
264
        setDescription(state.getString(FIELD_DESCRIPTION));
265
        setIsShapeVisible(state.getBoolean(FIELD_IS_SHAPE_VISIBLE));
266
        setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
267
        setUnit(state.getInt(FIELD_UNIT));
268
    }
269

    
270
    @Override
271
    public void saveToState(PersistentState state) throws PersistenceException {
272
        state.set(FIELD_DESCRIPTION, getDescription());
273
        state.set(FIELD_IS_SHAPE_VISIBLE, isShapeVisible());
274
        state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
275
        state.set(FIELD_UNIT, getUnit());
276
    }
277

    
278
    public static class RegisterPersistence implements Callable {
279

    
280
        @Override
281
        public Object call() throws Exception {
282
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
283
            if (manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
284
                DynStruct definition = manager.addDefinition(
285
                        AbstractSymbol.class,
286
                        SYMBOL_PERSISTENCE_DEFINITION_NAME,
287
                        SYMBOL_PERSISTENCE_DEFINITION_NAME + " persistence definition",
288
                        null,
289
                        null
290
                );
291
                // Description
292
                definition.addDynFieldString(FIELD_DESCRIPTION);
293
                // Is Shape Visible
294
                definition.addDynFieldBoolean(FIELD_IS_SHAPE_VISIBLE).setMandatory(true);
295
                // Reference system
296
                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
297
                // Unit
298
                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
299
            }
300
            return Boolean.TRUE;
301
        }
302

    
303
    }
304

    
305
    public String[] getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException {
306
        // By default only need the default Geometry
307
        return new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()};
308
    }
309

    
310
}