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