svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / PointLabelPositioner.java @ 40911
History | View | Annotate | Download (4.84 KB)
1 |
package org.gvsig.labeling.placements; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Font; |
5 |
import java.awt.Graphics2D; |
6 |
import java.awt.Rectangle; |
7 |
import java.awt.RenderingHints; |
8 |
|
9 |
import org.gvsig.fmap.geom.Geometry; |
10 |
import org.gvsig.fmap.geom.GeometryLocator; |
11 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
12 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.AbstractStyle; |
13 |
import org.gvsig.tools.ToolsLocator; |
14 |
import org.gvsig.tools.dynobject.DynStruct; |
15 |
import org.gvsig.tools.persistence.PersistenceManager; |
16 |
import org.gvsig.tools.persistence.PersistentState; |
17 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
18 |
import org.slf4j.Logger; |
19 |
import org.slf4j.LoggerFactory; |
20 |
|
21 |
|
22 |
/**
|
23 |
*
|
24 |
* Specifies the point position for a label
|
25 |
*
|
26 |
*/
|
27 |
public class PointLabelPositioner extends AbstractStyle { |
28 |
|
29 |
private static Logger logger = LoggerFactory.getLogger(PointLabelPositioner.class); |
30 |
|
31 |
public static final String POINT_LABEL_POSITIONER_PERSISTENCE_NAME = |
32 |
"POINT_LABEL_POSITIONER_PERSISTENCE_NAME";
|
33 |
|
34 |
private static Geometry sampleGeometry = null; |
35 |
private int[] preferenceVector = new int[8]; |
36 |
private static final Color[] colorVector = new Color[] { |
37 |
new Color(140, 140, 140), // gray |
38 |
new Color(140, 245, 130), // green |
39 |
new Color(130, 170, 245), // light blue |
40 |
new Color(100, 100, 255), // dark blue |
41 |
}; |
42 |
|
43 |
public static final byte FORBIDDEN = 0; |
44 |
public static final byte PREFERENCE_HIGH = 1; |
45 |
public static final byte PREFERENCE_NORMAL = 2; |
46 |
public static final byte PREFERENCE_LOW = 3; |
47 |
/**
|
48 |
* Constructor method
|
49 |
*
|
50 |
*/
|
51 |
public PointLabelPositioner() {}
|
52 |
|
53 |
/**
|
54 |
* Constructor method
|
55 |
*
|
56 |
* @param preferenceVector
|
57 |
* @param description
|
58 |
*/
|
59 |
public PointLabelPositioner(int[] preferenceVector, String description) { |
60 |
this.preferenceVector = preferenceVector;
|
61 |
setDescription(description); |
62 |
} |
63 |
|
64 |
public void drawInsideRectangle(Graphics2D g, Rectangle r) { |
65 |
int size = Math.min(r.width, r.height) / 3; |
66 |
int j = -1; |
67 |
final int fontSize = (int) (size * 0.8); |
68 |
final Font font = new Font("Arial", Font.PLAIN, fontSize); |
69 |
RenderingHints old = g.getRenderingHints();
|
70 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
71 |
for (int i = 0; i < 9; i++) { |
72 |
if (i == 4) continue; |
73 |
j++; |
74 |
int value = Math.abs(preferenceVector[j] % colorVector.length); |
75 |
int col = i % 3; |
76 |
int row = i / 3; |
77 |
|
78 |
g.setColor(colorVector[value]); |
79 |
g.fillRect(size * col, size*row, size, size); |
80 |
g.setColor(Color.BLACK);
|
81 |
g.drawRect(size * col, size*row, size, size); |
82 |
g.setFont(font); |
83 |
g.drawString(String.valueOf(value),
|
84 |
(float) ((size/2) - (fontSize/4)) + size * col, |
85 |
(float) (size * 0.8) + size*row); |
86 |
} |
87 |
g.setRenderingHints(old); |
88 |
} |
89 |
|
90 |
public boolean isSuitableFor(ISymbol sym) { |
91 |
return sym.isSuitableFor(getSampleGeometry());
|
92 |
} |
93 |
|
94 |
public void loadFromState(PersistentState state) |
95 |
throws PersistenceException {
|
96 |
|
97 |
super.loadFromState(state);
|
98 |
preferenceVector = state.getIntArray("preferenceVector");
|
99 |
} |
100 |
|
101 |
public void saveToState(PersistentState state) throws PersistenceException { |
102 |
|
103 |
super.saveToState(state);
|
104 |
state.set("preferenceVector", preferenceVector);
|
105 |
} |
106 |
|
107 |
public static void registerPersistent() { |
108 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
109 |
if( manager.getDefinition(POINT_LABEL_POSITIONER_PERSISTENCE_NAME)==null ) { |
110 |
DynStruct definition = manager.addDefinition( |
111 |
PointLabelPositioner.class, |
112 |
POINT_LABEL_POSITIONER_PERSISTENCE_NAME, |
113 |
POINT_LABEL_POSITIONER_PERSISTENCE_NAME+" Persistence definition",
|
114 |
null,
|
115 |
null
|
116 |
); |
117 |
definition.extend(manager.getDefinition( |
118 |
AbstractStyle.STYLE_PERSISTENCE_DEFINITION_NAME)); |
119 |
definition.addDynFieldArray("preferenceVector").setClassOfItems(
|
120 |
int.class).setMandatory(true); |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
/*
|
126 |
public XMLEntity getXMLEntity() {
|
127 |
XMLEntity xml = new XMLEntity();
|
128 |
xml.putProperty("className", getClassName());
|
129 |
xml.putProperty("desc", getDescription());
|
130 |
StringBuffer sb = new StringBuffer();
|
131 |
for (int i = 0; i < preferenceVector.length; i++) {
|
132 |
sb.append(preferenceVector[i]+" ,");
|
133 |
}
|
134 |
String s = sb.substring(0, sb.length()-2);
|
135 |
xml.putProperty("preferenceVector", s);
|
136 |
return xml;
|
137 |
}
|
138 |
|
139 |
public void setXMLEntity(XMLEntity xml) {
|
140 |
setDescription(xml.getStringProperty("desc"));
|
141 |
preferenceVector = xml.getByteArrayProperty("preferenceVector");
|
142 |
}
|
143 |
*/
|
144 |
|
145 |
public Object clone() throws CloneNotSupportedException { |
146 |
return super.clone(); |
147 |
} |
148 |
|
149 |
public void drawOutline(Graphics2D g, Rectangle r) { |
150 |
drawInsideRectangle(g, r); |
151 |
} |
152 |
|
153 |
public int[] getPreferenceVector() { |
154 |
return preferenceVector;
|
155 |
} |
156 |
|
157 |
private static Geometry getSampleGeometry() { |
158 |
|
159 |
if (sampleGeometry == null) { |
160 |
try {
|
161 |
sampleGeometry = GeometryLocator.getGeometryManager().createPoint( |
162 |
0, 0, Geometry.SUBTYPES.GEOM2D); |
163 |
} catch (Exception e) { |
164 |
logger.error("While getting sample geometry.", e);
|
165 |
} |
166 |
} |
167 |
return sampleGeometry;
|
168 |
} |
169 |
} |