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 @ 40981
History | View | Annotate | Download (7.52 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 = (ISymbol) obj; |
143 |
|
144 |
if( obj==null ) { |
145 |
return false; |
146 |
} |
147 |
if( ! areEquals(obj.getClass(),this.getClass()) ) { |
148 |
return false; |
149 |
} |
150 |
if( ! areEquals(other.getColor(),this.getColor()) ) { |
151 |
return false; |
152 |
} |
153 |
if( other.getOnePointRgb() != this.getOnePointRgb()) { |
154 |
return false; |
155 |
} |
156 |
if( ! areEquals(other.getDescription(),this.getDescription()) ) { |
157 |
return false; |
158 |
} |
159 |
return true; |
160 |
} |
161 |
|
162 |
public boolean isOneDotOrPixel(Geometry geom, |
163 |
double[] positionOfDotOrPixel, ViewPort viewPort, int dpi) { |
164 |
|
165 |
int type = geom.getType();
|
166 |
switch (type) {
|
167 |
case Geometry.TYPES.NULL:
|
168 |
case Geometry.TYPES.POINT:
|
169 |
case Geometry.TYPES.MULTIPOINT:
|
170 |
return false; |
171 |
default:
|
172 |
org.gvsig.fmap.geom.primitive.Envelope geomBounds = geom |
173 |
.getEnvelope(); |
174 |
|
175 |
double dist1Pixel = viewPort.getDist1pixel();
|
176 |
|
177 |
float[] distances = new float[2]; |
178 |
this.getPixExtentPlus(geom, distances, viewPort, dpi);
|
179 |
|
180 |
boolean onePoint =
|
181 |
(geomBounds.getLength(0) + distances[0] <= dist1Pixel && geomBounds |
182 |
.getLength(1)
|
183 |
+ distances[1] <= dist1Pixel);
|
184 |
|
185 |
if (onePoint) {
|
186 |
Rectangle bounds = geom.getShape().getBounds();
|
187 |
positionOfDotOrPixel[0] = bounds.x;
|
188 |
positionOfDotOrPixel[1] = bounds.y;
|
189 |
} |
190 |
return onePoint;
|
191 |
} |
192 |
} |
193 |
|
194 |
public Object clone() throws CloneNotSupportedException { |
195 |
return super.clone(); |
196 |
} |
197 |
|
198 |
protected ISymbol cloneForSelection() {
|
199 |
try {
|
200 |
ISymbol selectionSymbol = (ISymbol) clone(); |
201 |
selectionSymbol.setColor(MapContext.getSelectionColor()); |
202 |
if (getDescription() != null){ |
203 |
selectionSymbol.setDescription(getDescription().concat( |
204 |
" version for selection"));
|
205 |
}else{
|
206 |
selectionSymbol.setDescription("version for selection");
|
207 |
} |
208 |
return selectionSymbol;
|
209 |
} catch (CloneNotSupportedException e) { |
210 |
LOG.error( |
211 |
"Error creating the selection symbol for the symbol "
|
212 |
+ this, e);
|
213 |
} |
214 |
return null; |
215 |
} |
216 |
|
217 |
public void loadFromState(PersistentState state) |
218 |
throws PersistenceException {
|
219 |
setDescription(state.getString(FIELD_DESCRIPTION)); |
220 |
setIsShapeVisible(state.getBoolean(FIELD_IS_SHAPE_VISIBLE)); |
221 |
setReferenceSystem(state.getInt(FIELD_REFERENCE_SYSTEM)); |
222 |
setUnit(state.getInt(FIELD_UNIT)); |
223 |
} |
224 |
|
225 |
public void saveToState(PersistentState state) throws PersistenceException { |
226 |
state.set(FIELD_DESCRIPTION, getDescription()); |
227 |
state.set(FIELD_IS_SHAPE_VISIBLE, isShapeVisible()); |
228 |
state.set(FIELD_REFERENCE_SYSTEM, getReferenceSystem()); |
229 |
state.set(FIELD_UNIT, getUnit()); |
230 |
} |
231 |
|
232 |
public static class RegisterPersistence implements Callable { |
233 |
|
234 |
public Object call() throws Exception { |
235 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
236 |
if( manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
237 |
DynStruct definition = manager.addDefinition( |
238 |
AbstractSymbol.class, |
239 |
SYMBOL_PERSISTENCE_DEFINITION_NAME, |
240 |
SYMBOL_PERSISTENCE_DEFINITION_NAME+" persistence definition",
|
241 |
null,
|
242 |
null
|
243 |
); |
244 |
// Description
|
245 |
definition.addDynFieldString(FIELD_DESCRIPTION); |
246 |
// Is Shape Visible
|
247 |
definition.addDynFieldBoolean(FIELD_IS_SHAPE_VISIBLE).setMandatory(true);
|
248 |
// Reference system
|
249 |
definition.addDynFieldInt(FIELD_REFERENCE_SYSTEM).setMandatory(true);
|
250 |
// Unit
|
251 |
definition.addDynFieldInt(FIELD_UNIT).setMandatory(true);
|
252 |
} |
253 |
return Boolean.TRUE; |
254 |
} |
255 |
|
256 |
} |
257 |
|
258 |
|
259 |
} |