Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / 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 @ 34294

History | View | Annotate | Download (7.15 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl;
23

    
24
import java.awt.Rectangle;
25

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

    
42
/**
43
 * Abstract class that implements the interface the interface for symbols.It is
44
 * considered as the father of all XXXSymbols and will implement all the methods
45
 * that these classes had not developed (and correspond with one of the methods
46
 * of AbstractSymbol class)
47
 * 
48
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
49
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
50
 */
51
public abstract class AbstractSymbol implements ISymbol, CartographicSupport {
52

    
53
        private static final Logger LOG = LoggerFactory.getLogger(AbstractSymbol.class);
54

    
55
        public static final String SYMBOL_PERSISTENCE_DEFINITION_NAME = "Symbol";
56
        
57
        private static final String FIELD_UNIT = "unit";
58

    
59
        private static final String FIELD_REFERENCE_SYSTEM = "referenceSystem";
60

    
61
        private static final String FIELD_IS_SHAPE_VISIBLE = "isShapeVisible";
62

    
63
        private static final String FIELD_DESCRIPTION = "description";
64

    
65

    
66
        private String desc;
67
        private int unit;
68
        private int referenceSystem;
69

    
70
        private boolean isShapeVisible = true;
71

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

    
82
        public final void setDescription(String desc) {
83
                this.desc = desc;
84
        }
85

    
86
        public final String getDescription() {
87
                return desc;
88
        }
89

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

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

    
107
        public void setUnit(int unitIndex) {
108
                this.unit = unitIndex;
109
        }
110

    
111
        public int getUnit() {
112
                return this.unit;
113
        }
114

    
115
        public int getReferenceSystem() {
116
                return this.referenceSystem;
117
        }
118

    
119
        public void setReferenceSystem(int system) {
120
                this.referenceSystem = system;
121

    
122
        }
123

    
124
        public boolean equals(Object obj) {
125
                if (!obj.getClass().equals(getClass())) {
126
                        return false;
127
                }
128
                if (((ISymbol) obj).getOnePointRgb() != getOnePointRgb()) {
129
                        return false;
130
                }
131
                if (getDescription() != null
132
                                && ((ISymbol) obj).getDescription() != null) {
133
                        if (!((ISymbol) obj).getDescription().equals(getDescription())) {
134
                                return false;
135
                        }
136
                }
137
                return true;
138
        }
139

    
140
        public boolean isOneDotOrPixel(Geometry geom,
141
                        double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) {
142

    
143
                int type = geom.getType();
144
                switch (type) {
145
                case Geometry.TYPES.NULL:
146
                case Geometry.TYPES.POINT:
147
                case Geometry.TYPES.MULTIPOINT:
148
                        return false;
149
                default:
150
                        org.gvsig.fmap.geom.primitive.Envelope geomBounds = geom
151
                        .getEnvelope();
152

    
153
                        double dist1Pixel = viewPort.getDist1pixel();
154

    
155
                        float[] distances = new float[2];
156
                        this.getPixExtentPlus(geom, distances, viewPort, dpi);
157

    
158
                        boolean onePoint =
159
                                        (geomBounds.getLength(0) + distances[0] <= dist1Pixel && geomBounds
160
                                        .getLength(1)
161
                                        + distances[1] <= dist1Pixel);
162

    
163
                        if (onePoint) {
164
                                Rectangle bounds = geom.getBounds();
165
                                positionOfDotOrPixel[0] = bounds.x;
166
                                positionOfDotOrPixel[1] = bounds.y;
167
                        }
168
                        return onePoint;
169
                }
170
        }
171

    
172
        public Object clone() throws CloneNotSupportedException {
173
                return super.clone();        
174
        }
175

    
176
        protected ISymbol cloneForSelection() {
177
                try {
178
                        ISymbol selectionSymbol = (ISymbol) clone();
179
                        selectionSymbol.setColor(MapContext.getSelectionColor());
180
                        if (getDescription() != null){
181
                                selectionSymbol.setDescription(getDescription().concat(
182
                                " version for selection"));
183
                        }else{
184
                                selectionSymbol.setDescription("version for selection");
185
                        }
186
                        return selectionSymbol;
187
                } catch (CloneNotSupportedException e) {
188
                        LOG.error(
189
                                        "Error creating the selection symbol for the symbol "
190
                                        + this, e);
191
                }
192
                return null;
193
        }
194

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

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

    
210
        public static class RegisterPersistence implements Callable {
211

    
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
        
237
}