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