Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / impl / AbstractSymbol.java @ 47790

History | View | Annotate | Download (8.86 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
import java.awt.Color;
26
import java.awt.Graphics2D;
27
import java.awt.Rectangle;
28
import java.awt.geom.AffineTransform;
29
import org.gvsig.compat.print.PrintAttributes;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.mapcontext.MapContext;
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.SymbolDrawingException;
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.task.Cancellable;
45
import org.gvsig.tools.util.Callable;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
/**
50
 * Abstract class that implements the interface the interface for symbols.It is
51
 * considered as the father of all XXXSymbols and will implement all the methods
52
 * that these classes had not developed (and correspond with one of the methods
53
 * of AbstractSymbol class)
54
 *
55
 * @author gvSIG Team
56
 */
57
public abstract class AbstractSymbol extends AbstractCartographicSupport implements ISymbol_v2, CartographicSupport {
58

    
59
    protected static final Logger LOGGER = 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

    
74
    private boolean isShapeVisible = true;
75
    
76
    private transient Feature feature;
77
    
78
    public AbstractSymbol() {
79
        super();
80
    }
81

    
82
    @Override
83
    public Feature getFeature() {
84
        return this.feature;
85
    }
86

    
87
    @Override
88
    public void setFeature(Feature feature) {
89
        this.feature = feature;
90
    }
91
    
92
    @Override
93
    public final void setDescription(String desc) {
94
        this.desc = desc;
95
    }
96

    
97
    @Override
98
    public final String getDescription() {
99
        return desc;
100
    }
101

    
102
    @Override
103
    public final void setID(String id) {
104
        this.id = id;
105
    }
106

    
107
    @Override
108
    public final String getID() {
109
        return id;
110
    }
111

    
112
    @Override
113
    public boolean isShapeVisible() {
114
        return isShapeVisible;
115
    }
116

    
117
    /**
118
     * Sets this symbol to visible
119
     *
120
     * @param isShapeVisible
121
     */
122
    public final void setIsShapeVisible(boolean isShapeVisible) {
123
        this.isShapeVisible = isShapeVisible;
124
    }
125

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

    
140
    @Override
141
    @SuppressWarnings("UnusedAssignment")
142
    public boolean equals(Object obj) {
143
        ISymbol other = null;
144

    
145
        if (!(obj instanceof ISymbol)) {
146
            return false;
147
        }
148
        
149
        other = (ISymbol) obj;
150
        
151
        if (!areEquals(other.getClass(), this.getClass())) {
152
            return false;
153
        }
154

    
155
        if (!areEquals(other.getColor(), this.getColor())) {
156
            return false;
157
        }
158
        if (!areEquals(other.getDescription(), this.getDescription())) {
159
            return false;
160
        }
161
        return true;
162
    }
163

    
164
    @Override
165
    public Object clone() throws CloneNotSupportedException {
166
        return super.clone();
167
    }
168

    
169
    protected ISymbol cloneForSelection() {
170
        return cloneForSelection(MapContext.DEFAULT_SELECTION_COLOR);
171
    }
172
    
173
    protected ISymbol cloneForSelection(Color selectionColor) {
174
        try {
175
            ISymbol selectionSymbol = (ISymbol) clone();
176
            selectionSymbol.setColor(selectionColor);
177
            if (getDescription() != null) {
178
                selectionSymbol.setDescription(getDescription().concat(
179
                        " version for selection"));
180
            } else {
181
                selectionSymbol.setDescription("version for selection");
182
            }
183
            return selectionSymbol;
184
        } catch (CloneNotSupportedException e) {
185
            LOGGER.error(
186
                    "Error creating the selection symbol for the symbol "
187
                    + this, e);
188
        }
189
        return null;
190
    }
191

    
192
    @Override
193
    public void loadFromState(PersistentState state)
194
            throws PersistenceException {
195
        setDescription(state.getString(FIELD_DESCRIPTION));
196
        setIsShapeVisible(state.getBoolean(FIELD_IS_SHAPE_VISIBLE));
197
        setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM));
198
        setUnit(state.getInt(FIELD_UNIT));
199
    }
200

    
201
    @Override
202
    public void saveToState(PersistentState state) throws PersistenceException {
203
        state.set(FIELD_DESCRIPTION, getDescription());
204
        state.set(FIELD_IS_SHAPE_VISIBLE, isShapeVisible());
205
        state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem());
206
        state.set(FIELD_UNIT, getUnit());
207
    }
208

    
209
    public static class RegisterPersistence implements Callable {
210

    
211
        @Override
212
        public Object call() throws Exception {
213
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
214
            if (manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
215
                DynStruct definition = manager.addDefinition(
216
                        AbstractSymbol.class,
217
                        SYMBOL_PERSISTENCE_DEFINITION_NAME,
218
                        SYMBOL_PERSISTENCE_DEFINITION_NAME + " persistence definition",
219
                        null,
220
                        null
221
                );
222
                // Description
223
                definition.addDynFieldString(FIELD_DESCRIPTION);
224
                // Is Shape Visible
225
                definition.addDynFieldBoolean(FIELD_IS_SHAPE_VISIBLE).setMandatory(true);
226
                // Reference system
227
                definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
228
                // Unit
229
                definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
230
            }
231
            return Boolean.TRUE;
232
        }
233

    
234
    }
235

    
236
    public String[] getRequiredFeatureAttributeNames(FeatureStore featureStore) throws DataException {
237
        // By default only need the default Geometry
238
        return new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()};
239
    }
240

    
241
    @Override
242
    public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Feature f, Cancellable cancel) {
243
        draw(g, affineTransform, geom, f, cancel, null);
244
    }
245

    
246
    @Override
247
    public void drawInsideRectangle(Graphics2D g, AffineTransform affineTransform, Rectangle r) throws SymbolDrawingException {
248
        draw(g, affineTransform, null, null, null, r);
249
    }
250

    
251
    @Override
252
    public void print(Graphics2D g, AffineTransform affineTransform, Geometry geom, PrintAttributes properties) {
253
        draw(g, affineTransform, geom, null, null, null);
254
    }
255

    
256
    @Override
257
    public ISymbol getSymbolForSelection() {
258
        return this.getSymbolForSelection(MapContext.DEFAULT_SELECTION_COLOR);
259
    }
260

    
261
    @Override
262
    public ISymbol getSymbolForSelection(Color selectionColor) {
263
        //Only for compatibility purposes
264
        return this.getSymbolForSelection();
265
    }
266

    
267
}