Revision 23396 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/rendering/styling/labeling/AttrInTableLabelingStrategy.java
AttrInTableLabelingStrategy.java | ||
---|---|---|
1 |
package com.iver.cit.gvsig.fmap.rendering.styling.labeling; |
|
2 |
|
|
3 |
import java.awt.Color; |
|
4 |
import java.awt.Font; |
|
5 |
import java.awt.Graphics2D; |
|
6 |
import java.awt.image.BufferedImage; |
|
7 |
import java.util.Vector; |
|
8 |
import java.util.logging.Level; |
|
9 |
import java.util.logging.Logger; |
|
10 |
|
|
11 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
12 |
import javax.print.attribute.standard.PrintQuality; |
|
13 |
|
|
14 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
15 |
import com.hardcode.gdbms.engine.values.NullValue; |
|
16 |
import com.hardcode.gdbms.engine.values.NumericValue; |
|
17 |
import com.hardcode.gdbms.engine.values.Value; |
|
18 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
|
19 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
|
20 |
import com.iver.cit.gvsig.fmap.MapContext; |
|
21 |
import com.iver.cit.gvsig.fmap.ViewPort; |
|
22 |
import com.iver.cit.gvsig.fmap.core.CartographicSupport; |
|
23 |
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit; |
|
24 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
|
25 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
26 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
27 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol; |
|
28 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
|
29 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
|
30 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
|
31 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
|
32 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
33 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
|
34 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
|
35 |
import com.iver.utiles.XMLEntity; |
|
36 |
import com.iver.utiles.swing.threads.Cancellable; |
|
37 |
|
|
38 |
/** |
|
39 |
* LabelingStrategy used when the user wants to use label sizes, rotations, etc. from |
|
40 |
* the values included in fields of the datasource's table |
|
41 |
* |
|
42 |
* @author jaume dominguez faus - jaume.dominguez@iver.es |
|
43 |
* |
|
44 |
*/ |
|
45 |
public class AttrInTableLabelingStrategy implements ILabelingStrategy, CartographicSupport { |
|
46 |
public static final double MIN_TEXT_SIZE = 3; |
|
47 |
private ILabelingMethod method = new DefaultLabelingMethod(); |
|
48 |
private IZoomConstraints zoom; |
|
49 |
private int idTextField; |
|
50 |
private int idHeightField; |
|
51 |
private int idRotationField; |
|
52 |
private FLyrVect layer; |
|
53 |
// private double unitFactor = 1D; |
|
54 |
private double fixedSize; |
|
55 |
private int unit = -1; //(pixel) |
|
56 |
private boolean useFixedSize; |
|
57 |
private int referenceSystem; |
|
58 |
private boolean isPrinting; |
|
59 |
private double printDPI; |
|
60 |
private Font font; |
|
61 |
private Color colorFont; |
|
62 |
|
|
63 |
public ILabelingMethod getLabelingMethod() { |
|
64 |
return this.method; |
|
65 |
} |
|
66 |
|
|
67 |
public void setLabelingMethod(ILabelingMethod method) { |
|
68 |
this.method = method; |
|
69 |
} |
|
70 |
|
|
71 |
public IPlacementConstraints getPlacementConstraints() { |
|
72 |
return null; // (automatically handled by the driver) |
|
73 |
} |
|
74 |
|
|
75 |
public void setPlacementConstraints(IPlacementConstraints constraints) { |
|
76 |
// nothing |
|
77 |
} |
|
78 |
|
|
79 |
public IZoomConstraints getZoomConstraints() { |
|
80 |
return zoom; |
|
81 |
} |
|
82 |
|
|
83 |
public void setZoomConstraints(IZoomConstraints constraints) { |
|
84 |
this.zoom = constraints; |
|
85 |
} |
|
86 |
|
|
87 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double dpi) |
|
88 |
throws ReadDriverException { |
|
89 |
double scale = viewPort.getScale(); |
|
90 |
double fontScaleFactor = FConstant.FONT_HEIGHT_SCALE_FACTOR; |
|
91 |
|
|
92 |
|
|
93 |
SimpleTextSymbol sym = new SimpleTextSymbol(); |
|
94 |
|
|
95 |
sym.setFont(getFont()); |
|
96 |
sym.setTextColor(getColorFont()); |
|
97 |
|
|
98 |
sym.setUnit(unit); |
|
99 |
sym.setReferenceSystem(referenceSystem); |
|
100 |
|
|
101 |
if (zoom==null || |
|
102 |
( zoom.isUserDefined() && (scale >= zoom.getMaxScale()) |
|
103 |
&& (scale <= zoom.getMinScale()) ) ) { |
|
104 |
try { |
|
105 |
// limit the labeling to the visible extent |
|
106 |
FBitSet bs = layer.queryByRect(viewPort.getAdjustedExtent()); |
|
107 |
|
|
108 |
ReadableVectorial source = layer.getSource(); |
|
109 |
SelectableDataSource recordSet = source.getRecordset(); |
|
110 |
|
|
111 |
for(int i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) { |
|
112 |
Value[] vv = recordSet.getRow(i); |
|
113 |
double size; |
|
114 |
|
|
115 |
if (useFixedSize){ |
|
116 |
// uses fixed size |
|
117 |
size = fixedSize * fontScaleFactor; |
|
118 |
} else if (idHeightField != -1) { |
|
119 |
// text size is defined in the table |
|
120 |
try { |
|
121 |
size = ((NumericValue) vv[idHeightField]).doubleValue() * fontScaleFactor; |
|
122 |
} catch (ClassCastException ccEx) { |
|
123 |
if (!NullValue.class.equals(vv[idHeightField].getClass())) { |
|
124 |
throw new ReadDriverException("Unknown", ccEx); |
|
125 |
} |
|
126 |
// a null value |
|
127 |
Logger.getAnonymousLogger(). |
|
128 |
warning("Null text height value for text '"+vv[idTextField].toString()+"'"); |
|
129 |
continue; |
|
130 |
} |
|
131 |
} else { |
|
132 |
// otherwise will use the size in the symbol |
|
133 |
size = sym.getFont().getSize(); |
|
134 |
} |
|
135 |
|
|
136 |
size = CartographicSupportToolkit. |
|
137 |
getCartographicLength(this, |
|
138 |
size, |
|
139 |
viewPort, |
|
140 |
MapContext.getScreenDPI()); |
|
141 |
// dpi); |
|
142 |
// toScreenUnitYAxis(this, |
|
143 |
// size, |
|
144 |
// viewPort |
|
145 |
// ); |
|
146 |
|
|
147 |
if (size <= MIN_TEXT_SIZE) { |
|
148 |
// label is too small to be readable, will be skipped |
|
149 |
// this speeds up the rendering in wider zooms |
|
150 |
continue; |
|
151 |
} |
|
152 |
|
|
153 |
sym.setFontSize(size); |
|
154 |
double rotation = 0D; |
|
155 |
if (idRotationField!= -1) { |
|
156 |
// text rotation is defined in the table |
|
157 |
rotation = -Math.toRadians(((NumericValue) vv[idRotationField]).doubleValue()); |
|
158 |
} |
|
159 |
|
|
160 |
IGeometry geom = source.getShape(i); |
|
161 |
sym.setText(vv[idTextField].toString()); |
|
162 |
sym.setRotation(rotation); |
|
163 |
|
|
164 |
FLabel[] aux = geom.createLabels(0, true); |
|
165 |
for (int j = 0; j < aux.length; j++) { |
|
166 |
FPoint2D p = new FPoint2D(aux[j].getOrig()); |
|
167 |
p.transform(viewPort.getAffineTransform()); |
|
168 |
sym.draw(g, null, p, cancel); |
|
169 |
} |
|
170 |
} |
|
171 |
} catch (VisitorException e) { |
|
172 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not get the layer extent.\n" + |
|
173 |
e.getMessage()); |
|
174 |
} catch (ExpansionFileReadException e) { |
|
175 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not draw annotation in the layer" + |
|
176 |
"\""+layer.getName()+"\".\nIs the layer being edited?.\n"+e.getMessage()); |
|
177 |
} catch (ReadDriverException e) { |
|
178 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not draw annotation in the layer.\n" + |
|
179 |
e.getMessage()); |
|
180 |
} |
|
181 |
|
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
public String getClassName() { |
|
186 |
return getClass().getName(); |
|
187 |
} |
|
188 |
|
|
189 |
public XMLEntity getXMLEntity() { |
|
190 |
XMLEntity xml = new XMLEntity(); |
|
191 |
xml.putProperty("className", getClassName()); |
|
192 |
|
|
193 |
try { |
|
194 |
if(getHeightField() != null) |
|
195 |
xml.putProperty("HeightField", getHeightField()); |
|
196 |
} catch (ReadDriverException e) { |
|
197 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing TextHeight field.\n"+e.getMessage()); |
|
198 |
} |
|
199 |
|
|
200 |
try { |
|
201 |
if(getTextField() != null) |
|
202 |
xml.putProperty("TextField", getTextField()); |
|
203 |
} catch (ReadDriverException e) { |
|
204 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing TextField field.\n"+e.getMessage()); |
|
205 |
} |
|
206 |
|
|
207 |
try { |
|
208 |
if (getRotationField() != null) |
|
209 |
xml.putProperty("RotationField", getRotationField()); |
|
210 |
} catch (ReadDriverException e) { |
|
211 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing RotationField field.\n"+e.getMessage()); |
|
212 |
} |
|
213 |
|
|
214 |
if(getFont() != null) |
|
215 |
xml.putProperty("Font", getFont()); |
|
216 |
if(getColorFont() != null) |
|
217 |
xml.putProperty("Color", getColorFont()); |
|
218 |
|
|
219 |
xml.putProperty("Unit", unit); |
|
220 |
return xml; |
|
221 |
|
|
222 |
} |
|
223 |
|
|
224 |
public String getRotationField() throws ReadDriverException { |
|
225 |
if (idRotationField == -1) return null; |
|
226 |
return ((SelectableDataSource) layer.getRecordset()) |
|
227 |
.getFieldName(idRotationField); |
|
228 |
} |
|
229 |
|
|
230 |
public int getRotationFieldId() { |
|
231 |
return idRotationField; |
|
232 |
} |
|
233 |
|
|
234 |
public void setRotationFieldId(int fieldId) { |
|
235 |
this.idRotationField = fieldId; |
|
236 |
} |
|
237 |
|
|
238 |
public String getTextField() throws ReadDriverException { |
|
239 |
if (idTextField == -1) return null; |
|
240 |
return ((SelectableDataSource) layer.getRecordset()) |
|
241 |
.getFieldName(idTextField); |
|
242 |
} |
|
243 |
|
|
244 |
public int getTextFieldId() { |
|
245 |
return idTextField; |
|
246 |
} |
|
247 |
|
|
248 |
public void setTextFieldId(int fieldId) { |
|
249 |
this.idTextField = fieldId; |
|
250 |
} |
|
251 |
|
|
252 |
public String getHeightField() throws ReadDriverException { |
|
253 |
if (idHeightField == -1) return null; |
|
254 |
return ((SelectableDataSource) layer.getRecordset()) |
|
255 |
.getFieldName(idHeightField); |
|
256 |
} |
|
257 |
|
|
258 |
public int getHeightFieldId() { |
|
259 |
return idHeightField; |
|
260 |
} |
|
261 |
|
|
262 |
public void setHeightFieldId(int fieldId) { |
|
263 |
this.idHeightField = fieldId; |
|
264 |
} |
|
265 |
|
|
266 |
public void setXMLEntity(XMLEntity xml) { |
|
267 |
if (xml.contains("TextField" )) |
|
268 |
setTextField(xml.getStringProperty("TextField")); |
|
269 |
|
|
270 |
if (xml.contains("HeightField")) |
|
271 |
setHeightField("HeightField"); |
|
272 |
|
|
273 |
if (xml.contains("RotationField")) |
|
274 |
setRotationField("RotationField"); |
|
275 |
|
|
276 |
if (xml.contains("UnitFactor")) |
|
277 |
setUnit(xml.getIntProperty("Unit")); |
|
278 |
|
|
279 |
if (xml.contains("Font")) |
|
280 |
setUnit(xml.getIntProperty("Font")); |
|
281 |
|
|
282 |
if (xml.contains("Color")) |
|
283 |
setUnit(xml.getIntProperty("Color")); |
|
284 |
} |
|
285 |
|
|
286 |
public void setTextField(String textFieldName) { |
|
287 |
try { |
|
288 |
idTextField = ((SelectableDataSource) layer.getRecordset()) |
|
289 |
.getFieldIndexByName(textFieldName); |
|
290 |
} catch (ReadDriverException e) { |
|
291 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
292 |
} |
|
293 |
} |
|
294 |
|
|
295 |
public void setRotationField(String rotationFieldName) { |
|
296 |
if (rotationFieldName != null) { |
|
297 |
try { |
|
298 |
idRotationField = ((SelectableDataSource) layer.getRecordset()) |
|
299 |
.getFieldIndexByName(rotationFieldName); |
|
300 |
} catch (ReadDriverException e) { |
|
301 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
302 |
} |
|
303 |
} else idRotationField = -1; |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Sets the field that contains the size of the text. The size is computed |
|
308 |
* in meters. To use any other unit, call setUnit(int) with the scale factor from meters |
|
309 |
* (for centimeters, call <b>setUnitFactor(0.01))</b>. |
|
310 |
* @param heightFieldName |
|
311 |
*/ |
|
312 |
public void setHeightField(String heightFieldName) { |
|
313 |
if (heightFieldName != null) { |
|
314 |
try { |
|
315 |
idHeightField = ((SelectableDataSource) layer.getRecordset()) |
|
316 |
.getFieldIndexByName(heightFieldName); |
|
317 |
} catch (ReadDriverException e) { |
|
318 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
319 |
} |
|
320 |
} else idHeightField = -1; |
|
321 |
} |
|
322 |
|
|
323 |
|
|
324 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException { |
|
325 |
isPrinting = true; |
|
326 |
PrintQuality resolution=(PrintQuality)properties.get(PrintQuality.class); |
|
327 |
if (resolution.equals(PrintQuality.NORMAL)){ |
|
328 |
printDPI=300; |
|
329 |
} else if (resolution.equals(PrintQuality.HIGH)) { |
|
330 |
printDPI=600; |
|
331 |
} else if (resolution.equals(PrintQuality.DRAFT)) { |
|
332 |
printDPI=72; |
|
333 |
} |
|
334 |
draw(null, g, viewPort, cancel, printDPI); |
|
335 |
isPrinting = false; |
|
336 |
} |
|
337 |
|
|
338 |
public void setUsesFixedSize(boolean b) { |
|
339 |
useFixedSize = b; |
|
340 |
} |
|
341 |
|
|
342 |
public boolean usesFixedSize() { |
|
343 |
return useFixedSize; |
|
344 |
} |
|
345 |
|
|
346 |
public double getFixedSize() { |
|
347 |
return fixedSize; |
|
348 |
} |
|
349 |
|
|
350 |
public void setFixedSize(double fixedSize) { |
|
351 |
this.fixedSize = fixedSize; |
|
352 |
} |
|
353 |
|
|
354 |
public void setUnit(int unitIndex) { |
|
355 |
unit = unitIndex; |
|
356 |
|
|
357 |
} |
|
358 |
|
|
359 |
public int getUnit() { |
|
360 |
return unit; |
|
361 |
} |
|
362 |
|
|
363 |
public String[] getUsedFields() { |
|
364 |
Vector v = new Vector(); |
|
365 |
try { |
|
366 |
if (getHeightField()!=null) v.add(getHeightField()); |
|
367 |
if (getRotationField()!=null) v.add(getRotationField()); |
|
368 |
if (getTextField()!=null) v.add(getTextField()); |
|
369 |
if (getHeightField()!=null) v.add(getHeightField()); |
|
370 |
} catch (ReadDriverException e) { |
|
371 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
372 |
} |
|
373 |
return (String[]) v.toArray(new String[v.size()]); |
|
374 |
} |
|
375 |
|
|
376 |
public int getReferenceSystem() { |
|
377 |
return referenceSystem; |
|
378 |
} |
|
379 |
|
|
380 |
public void setReferenceSystem(int referenceSystem) { |
|
381 |
this.referenceSystem = referenceSystem; |
|
382 |
} |
|
383 |
|
|
384 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
|
385 |
// not required here |
|
386 |
throw new Error("Undefined in this context"); |
|
387 |
} |
|
388 |
|
|
389 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
|
390 |
// not required here |
|
391 |
throw new Error("Undefined in this context"); |
|
392 |
} |
|
393 |
|
|
394 |
public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
|
395 |
// not required here |
|
396 |
throw new Error("Undefined in this context"); |
|
397 |
|
|
398 |
} |
|
399 |
|
|
400 |
public void setLayer(FLayer layer) { |
|
401 |
this.layer = (FLyrVect) layer; |
|
402 |
} |
|
403 |
|
|
404 |
public boolean shouldDrawLabels(double scale) { |
|
405 |
return layer.isWithinScale(scale); |
|
406 |
} |
|
407 |
|
|
408 |
public Color getColorFont() { |
|
409 |
return colorFont; |
|
410 |
} |
|
411 |
|
|
412 |
public void setColorFont(Color colorFont) { |
|
413 |
this.colorFont = colorFont; |
|
414 |
} |
|
415 |
|
|
416 |
public Font getFont() { |
|
417 |
return font; |
|
418 |
} |
|
419 |
|
|
420 |
public void setFont(Font selFont) { |
|
421 |
this.font = selFont; |
|
422 |
} |
|
423 |
} |
|
1 |
package com.iver.cit.gvsig.fmap.rendering.styling.labeling; |
|
2 |
|
|
3 |
import java.awt.Color; |
|
4 |
import java.awt.Font; |
|
5 |
import java.awt.Graphics2D; |
|
6 |
import java.awt.image.BufferedImage; |
|
7 |
import java.util.Vector; |
|
8 |
import java.util.logging.Level; |
|
9 |
import java.util.logging.Logger; |
|
10 |
|
|
11 |
import javax.print.attribute.PrintRequestAttributeSet; |
|
12 |
import javax.print.attribute.standard.PrintQuality; |
|
13 |
|
|
14 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
15 |
import com.hardcode.gdbms.engine.values.NullValue; |
|
16 |
import com.hardcode.gdbms.engine.values.NumericValue; |
|
17 |
import com.hardcode.gdbms.engine.values.Value; |
|
18 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
|
19 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
|
20 |
import com.iver.cit.gvsig.fmap.MapContext; |
|
21 |
import com.iver.cit.gvsig.fmap.ViewPort; |
|
22 |
import com.iver.cit.gvsig.fmap.core.CartographicSupport; |
|
23 |
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit; |
|
24 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
|
25 |
import com.iver.cit.gvsig.fmap.core.FShape; |
|
26 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
27 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol; |
|
28 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
|
29 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
|
30 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
|
31 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
|
32 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
33 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
|
34 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
|
35 |
import com.iver.utiles.XMLEntity; |
|
36 |
import com.iver.utiles.swing.threads.Cancellable; |
|
37 |
|
|
38 |
/** |
|
39 |
* LabelingStrategy used when the user wants to use label sizes, rotations, etc. from |
|
40 |
* the values included in fields of the datasource's table |
|
41 |
* |
|
42 |
* @author jaume dominguez faus - jaume.dominguez@iver.es |
|
43 |
* |
|
44 |
*/ |
|
45 |
public class AttrInTableLabelingStrategy implements ILabelingStrategy, CartographicSupport { |
|
46 |
public static final double MIN_TEXT_SIZE = 3; |
|
47 |
private ILabelingMethod method = new DefaultLabelingMethod(); |
|
48 |
private IZoomConstraints zoom; |
|
49 |
private int idTextField; |
|
50 |
private int idHeightField; |
|
51 |
private int idRotationField; |
|
52 |
private int idColorField; |
|
53 |
private FLyrVect layer; |
|
54 |
// private double unitFactor = 1D; |
|
55 |
private double fixedSize; |
|
56 |
private Color fixedColor; |
|
57 |
private int unit = -1; //(pixel) |
|
58 |
private boolean useFixedSize; |
|
59 |
private boolean useFixedColor; |
|
60 |
private int referenceSystem; |
|
61 |
private boolean isPrinting; |
|
62 |
private double printDPI; |
|
63 |
private Font font; |
|
64 |
private Color colorFont; |
|
65 |
|
|
66 |
public ILabelingMethod getLabelingMethod() { |
|
67 |
return this.method; |
|
68 |
} |
|
69 |
|
|
70 |
public void setLabelingMethod(ILabelingMethod method) { |
|
71 |
this.method = method; |
|
72 |
} |
|
73 |
|
|
74 |
public IPlacementConstraints getPlacementConstraints() { |
|
75 |
return null; // (automatically handled by the driver) |
|
76 |
} |
|
77 |
|
|
78 |
public void setPlacementConstraints(IPlacementConstraints constraints) { |
|
79 |
// nothing |
|
80 |
} |
|
81 |
|
|
82 |
public IZoomConstraints getZoomConstraints() { |
|
83 |
return zoom; |
|
84 |
} |
|
85 |
|
|
86 |
public void setZoomConstraints(IZoomConstraints constraints) { |
|
87 |
this.zoom = constraints; |
|
88 |
} |
|
89 |
|
|
90 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double dpi) |
|
91 |
throws ReadDriverException { |
|
92 |
double scale = viewPort.getScale(); |
|
93 |
double fontScaleFactor = FConstant.FONT_HEIGHT_SCALE_FACTOR; |
|
94 |
|
|
95 |
|
|
96 |
SimpleTextSymbol sym = new SimpleTextSymbol(); |
|
97 |
|
|
98 |
sym.setFont(getFont()); |
|
99 |
|
|
100 |
sym.setUnit(unit); |
|
101 |
sym.setReferenceSystem(referenceSystem); |
|
102 |
|
|
103 |
if (zoom==null || |
|
104 |
( zoom.isUserDefined() && (scale >= zoom.getMaxScale()) |
|
105 |
&& (scale <= zoom.getMinScale()) ) ) { |
|
106 |
try { |
|
107 |
// limit the labeling to the visible extent |
|
108 |
FBitSet bs = layer.queryByRect(viewPort.getAdjustedExtent()); |
|
109 |
|
|
110 |
ReadableVectorial source = layer.getSource(); |
|
111 |
SelectableDataSource recordSet = source.getRecordset(); |
|
112 |
|
|
113 |
for(int i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) { |
|
114 |
Value[] vv = recordSet.getRow(i); |
|
115 |
double size; |
|
116 |
Color color = null; |
|
117 |
if (useFixedSize){ |
|
118 |
// uses fixed size |
|
119 |
size = fixedSize * fontScaleFactor; |
|
120 |
} else if (idHeightField != -1) { |
|
121 |
// text size is defined in the table |
|
122 |
try { |
|
123 |
size = ((NumericValue) vv[idHeightField]).doubleValue() * fontScaleFactor; |
|
124 |
} catch (ClassCastException ccEx) { |
|
125 |
if (!NullValue.class.equals(vv[idHeightField].getClass())) { |
|
126 |
throw new ReadDriverException("Unknown", ccEx); |
|
127 |
} |
|
128 |
// a null value |
|
129 |
Logger.getAnonymousLogger(). |
|
130 |
warning("Null text height value for text '"+vv[idTextField].toString()+"'"); |
|
131 |
continue; |
|
132 |
} |
|
133 |
} else { |
|
134 |
// otherwise will use the size in the symbol |
|
135 |
size = sym.getFont().getSize(); |
|
136 |
} |
|
137 |
|
|
138 |
size = CartographicSupportToolkit. |
|
139 |
getCartographicLength(this, |
|
140 |
size, |
|
141 |
viewPort, |
|
142 |
MapContext.getScreenDPI()); |
|
143 |
// dpi); |
|
144 |
// toScreenUnitYAxis(this, |
|
145 |
// size, |
|
146 |
// viewPort |
|
147 |
// ); |
|
148 |
|
|
149 |
if (size <= MIN_TEXT_SIZE) { |
|
150 |
// label is too small to be readable, will be skipped |
|
151 |
// this speeds up the rendering in wider zooms |
|
152 |
continue; |
|
153 |
} |
|
154 |
|
|
155 |
sym.setFontSize(size); |
|
156 |
|
|
157 |
if (useFixedColor){ |
|
158 |
color = fixedColor; |
|
159 |
} else if (idColorField != -1) { |
|
160 |
// text size is defined in the table |
|
161 |
try { |
|
162 |
color = new Color(((NumericValue) vv[idColorField]).intValue()); |
|
163 |
} catch (ClassCastException ccEx) { |
|
164 |
if (!NullValue.class.equals(vv[idColorField].getClass())) { |
|
165 |
throw new ReadDriverException("Unknown", ccEx); |
|
166 |
} |
|
167 |
// a null value |
|
168 |
Logger.getAnonymousLogger(). |
|
169 |
warning("Null color value for text '"+vv[idTextField].toString()+"'"); |
|
170 |
continue; |
|
171 |
} |
|
172 |
} else { |
|
173 |
color = sym.getTextColor(); |
|
174 |
} |
|
175 |
|
|
176 |
sym.setTextColor(color); |
|
177 |
|
|
178 |
double rotation = 0D; |
|
179 |
if (idRotationField!= -1) { |
|
180 |
// text rotation is defined in the table |
|
181 |
rotation = -Math.toRadians(((NumericValue) vv[idRotationField]).doubleValue()); |
|
182 |
} |
|
183 |
|
|
184 |
IGeometry geom = source.getShape(i); |
|
185 |
sym.setText(vv[idTextField].toString()); |
|
186 |
sym.setRotation(rotation); |
|
187 |
|
|
188 |
FLabel[] aux = geom.createLabels(0, true); |
|
189 |
for (int j = 0; j < aux.length; j++) { |
|
190 |
FPoint2D p = new FPoint2D(aux[j].getOrig()); |
|
191 |
p.transform(viewPort.getAffineTransform()); |
|
192 |
sym.draw(g, null, p, cancel); |
|
193 |
} |
|
194 |
|
|
195 |
|
|
196 |
} |
|
197 |
} catch (VisitorException e) { |
|
198 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not get the layer extent.\n" + |
|
199 |
e.getMessage()); |
|
200 |
} catch (ExpansionFileReadException e) { |
|
201 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not draw annotation in the layer" + |
|
202 |
"\""+layer.getName()+"\".\nIs the layer being edited?.\n"+e.getMessage()); |
|
203 |
} catch (ReadDriverException e) { |
|
204 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Could not draw annotation in the layer.\n" + |
|
205 |
e.getMessage()); |
|
206 |
} |
|
207 |
|
|
208 |
} |
|
209 |
} |
|
210 |
|
|
211 |
public String getClassName() { |
|
212 |
return getClass().getName(); |
|
213 |
} |
|
214 |
|
|
215 |
public XMLEntity getXMLEntity() { |
|
216 |
XMLEntity xml = new XMLEntity(); |
|
217 |
xml.putProperty("className", getClassName()); |
|
218 |
|
|
219 |
try { |
|
220 |
if(getHeightField() != null) |
|
221 |
xml.putProperty("HeightField", getHeightField()); |
|
222 |
} catch (ReadDriverException e) { |
|
223 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing TextHeight field.\n"+e.getMessage()); |
|
224 |
} |
|
225 |
|
|
226 |
try { |
|
227 |
if(getColorField() != null) |
|
228 |
xml.putProperty("ColorField", getColorField()); |
|
229 |
} catch (ReadDriverException e) { |
|
230 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing ColorField field.\n"+e.getMessage()); |
|
231 |
} |
|
232 |
|
|
233 |
try { |
|
234 |
if(getTextField() != null) |
|
235 |
xml.putProperty("TextField", getTextField()); |
|
236 |
} catch (ReadDriverException e) { |
|
237 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing TextField field.\n"+e.getMessage()); |
|
238 |
} |
|
239 |
|
|
240 |
try { |
|
241 |
if (getRotationField() != null) |
|
242 |
xml.putProperty("RotationField", getRotationField()); |
|
243 |
} catch (ReadDriverException e) { |
|
244 |
Logger.getAnonymousLogger().log(Level.SEVERE, "Acessing RotationField field.\n"+e.getMessage()); |
|
245 |
} |
|
246 |
|
|
247 |
if(getFont() != null) |
|
248 |
xml.putProperty("Font", getFont()); |
|
249 |
if(getColorFont() != null) |
|
250 |
xml.putProperty("Color", getColorFont()); |
|
251 |
|
|
252 |
xml.putProperty("Unit", unit); |
|
253 |
return xml; |
|
254 |
|
|
255 |
} |
|
256 |
|
|
257 |
public String getRotationField() throws ReadDriverException { |
|
258 |
if (idRotationField == -1) return null; |
|
259 |
return ((SelectableDataSource) layer.getRecordset()) |
|
260 |
.getFieldName(idRotationField); |
|
261 |
} |
|
262 |
|
|
263 |
public int getRotationFieldId() { |
|
264 |
return idRotationField; |
|
265 |
} |
|
266 |
|
|
267 |
public void setRotationFieldId(int fieldId) { |
|
268 |
this.idRotationField = fieldId; |
|
269 |
} |
|
270 |
|
|
271 |
public String getTextField() throws ReadDriverException { |
|
272 |
if (idTextField == -1) return null; |
|
273 |
return ((SelectableDataSource) layer.getRecordset()) |
|
274 |
.getFieldName(idTextField); |
|
275 |
} |
|
276 |
|
|
277 |
public int getTextFieldId() { |
|
278 |
return idTextField; |
|
279 |
} |
|
280 |
|
|
281 |
public void setTextFieldId(int fieldId) { |
|
282 |
this.idTextField = fieldId; |
|
283 |
} |
|
284 |
|
|
285 |
public String getHeightField() throws ReadDriverException { |
|
286 |
if (idHeightField == -1) return null; |
|
287 |
return ((SelectableDataSource) layer.getRecordset()) |
|
288 |
.getFieldName(idHeightField); |
|
289 |
} |
|
290 |
|
|
291 |
public int getHeightFieldId() { |
|
292 |
return idHeightField; |
|
293 |
} |
|
294 |
|
|
295 |
public void setHeightFieldId(int fieldId) { |
|
296 |
this.idHeightField = fieldId; |
|
297 |
} |
|
298 |
|
|
299 |
public String getColorField() throws ReadDriverException { |
|
300 |
if (idColorField == -1) return null; |
|
301 |
return ((SelectableDataSource) layer.getRecordset()) |
|
302 |
.getFieldName(idColorField); |
|
303 |
} |
|
304 |
|
|
305 |
public int getColorFieldId() { |
|
306 |
return idColorField; |
|
307 |
} |
|
308 |
|
|
309 |
public void setColorFieldId(int fieldId) { |
|
310 |
this.idColorField = fieldId; |
|
311 |
} |
|
312 |
|
|
313 |
|
|
314 |
public void setXMLEntity(XMLEntity xml) { |
|
315 |
if (xml.contains("TextField" )) |
|
316 |
setTextField(xml.getStringProperty("TextField")); |
|
317 |
|
|
318 |
if (xml.contains("HeightField")) |
|
319 |
setHeightField("HeightField"); |
|
320 |
|
|
321 |
if (xml.contains("ColorField")) |
|
322 |
setHeightField("ColorField"); |
|
323 |
|
|
324 |
if (xml.contains("RotationField")) |
|
325 |
setRotationField("RotationField"); |
|
326 |
|
|
327 |
if (xml.contains("UnitFactor")) |
|
328 |
setUnit(xml.getIntProperty("Unit")); |
|
329 |
|
|
330 |
if (xml.contains("Font")) |
|
331 |
setUnit(xml.getIntProperty("Font")); |
|
332 |
|
|
333 |
if (xml.contains("Color")) |
|
334 |
setUnit(xml.getIntProperty("Color")); |
|
335 |
} |
|
336 |
|
|
337 |
public void setTextField(String textFieldName) { |
|
338 |
try { |
|
339 |
idTextField = ((SelectableDataSource) layer.getRecordset()) |
|
340 |
.getFieldIndexByName(textFieldName); |
|
341 |
} catch (ReadDriverException e) { |
|
342 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
343 |
} |
|
344 |
} |
|
345 |
|
|
346 |
public void setRotationField(String rotationFieldName) { |
|
347 |
if (rotationFieldName != null) { |
|
348 |
try { |
|
349 |
idRotationField = ((SelectableDataSource) layer.getRecordset()) |
|
350 |
.getFieldIndexByName(rotationFieldName); |
|
351 |
} catch (ReadDriverException e) { |
|
352 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
353 |
} |
|
354 |
} else idRotationField = -1; |
|
355 |
} |
|
356 |
|
|
357 |
/** |
|
358 |
* Sets the field that contains the size of the text. The size is computed |
|
359 |
* in meters. To use any other unit, call setUnit(int) with the scale factor from meters |
|
360 |
* (for centimeters, call <b>setUnitFactor(0.01))</b>. |
|
361 |
* @param heightFieldName |
|
362 |
*/ |
|
363 |
public void setHeightField(String heightFieldName) { |
|
364 |
if (heightFieldName != null) { |
|
365 |
try { |
|
366 |
idHeightField = ((SelectableDataSource) layer.getRecordset()) |
|
367 |
.getFieldIndexByName(heightFieldName); |
|
368 |
} catch (ReadDriverException e) { |
|
369 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
370 |
} |
|
371 |
} else idHeightField = -1; |
|
372 |
} |
|
373 |
|
|
374 |
|
|
375 |
public void setColorField(String colorFieldName) { |
|
376 |
if (colorFieldName != null) { |
|
377 |
try { |
|
378 |
idColorField = ((SelectableDataSource) layer.getRecordset()) |
|
379 |
.getFieldIndexByName(colorFieldName); |
|
380 |
} catch (ReadDriverException e) { |
|
381 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
382 |
} |
|
383 |
} else idColorField = -1; |
|
384 |
} |
|
385 |
|
|
386 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException { |
|
387 |
isPrinting = true; |
|
388 |
PrintQuality resolution=(PrintQuality)properties.get(PrintQuality.class); |
|
389 |
if (resolution.equals(PrintQuality.NORMAL)){ |
|
390 |
printDPI=300; |
|
391 |
} else if (resolution.equals(PrintQuality.HIGH)) { |
|
392 |
printDPI=600; |
|
393 |
} else if (resolution.equals(PrintQuality.DRAFT)) { |
|
394 |
printDPI=72; |
|
395 |
} |
|
396 |
draw(null, g, viewPort, cancel, printDPI); |
|
397 |
isPrinting = false; |
|
398 |
} |
|
399 |
|
|
400 |
public void setUsesFixedSize(boolean b) { |
|
401 |
useFixedSize = b; |
|
402 |
} |
|
403 |
|
|
404 |
public boolean usesFixedSize() { |
|
405 |
return useFixedSize; |
|
406 |
} |
|
407 |
|
|
408 |
public double getFixedSize() { |
|
409 |
return fixedSize; |
|
410 |
} |
|
411 |
|
|
412 |
public void setFixedSize(double fixedSize) { |
|
413 |
this.fixedSize = fixedSize; |
|
414 |
} |
|
415 |
|
|
416 |
public void setUsesFixedColor(boolean b) { |
|
417 |
useFixedColor = b; |
|
418 |
} |
|
419 |
|
|
420 |
public boolean usesFixedColor() { |
|
421 |
return useFixedColor; |
|
422 |
} |
|
423 |
|
|
424 |
public Color getFixedColor() { |
|
425 |
return fixedColor; |
|
426 |
} |
|
427 |
|
|
428 |
public void setFixedColor(Color fixedColor) { |
|
429 |
this.fixedColor = fixedColor; |
|
430 |
} |
|
431 |
|
|
432 |
|
|
433 |
public void setUnit(int unitIndex) { |
|
434 |
unit = unitIndex; |
|
435 |
|
|
436 |
} |
|
437 |
|
|
438 |
public int getUnit() { |
|
439 |
return unit; |
|
440 |
} |
|
441 |
|
|
442 |
public String[] getUsedFields() { |
|
443 |
Vector v = new Vector(); |
|
444 |
try { |
|
445 |
if (getHeightField()!=null) v.add(getHeightField()); |
|
446 |
if (getRotationField()!=null) v.add(getRotationField()); |
|
447 |
if (getTextField()!=null) v.add(getTextField()); |
|
448 |
if (getHeightField()!=null) v.add(getHeightField()); |
|
449 |
} catch (ReadDriverException e) { |
|
450 |
Logger.getAnonymousLogger().log(Level.SEVERE, e.getMessage()); |
|
451 |
} |
|
452 |
return (String[]) v.toArray(new String[v.size()]); |
|
453 |
} |
|
454 |
|
|
455 |
public int getReferenceSystem() { |
|
456 |
return referenceSystem; |
|
457 |
} |
|
458 |
|
|
459 |
public void setReferenceSystem(int referenceSystem) { |
|
460 |
this.referenceSystem = referenceSystem; |
|
461 |
} |
|
462 |
|
|
463 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
|
464 |
// not required here |
|
465 |
throw new Error("Undefined in this context"); |
|
466 |
} |
|
467 |
|
|
468 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
|
469 |
// not required here |
|
470 |
throw new Error("Undefined in this context"); |
|
471 |
} |
|
472 |
|
|
473 |
public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
|
474 |
// not required here |
|
475 |
throw new Error("Undefined in this context"); |
|
476 |
|
|
477 |
} |
|
478 |
|
|
479 |
public void setLayer(FLayer layer) { |
|
480 |
this.layer = (FLyrVect) layer; |
|
481 |
} |
|
482 |
|
|
483 |
public boolean shouldDrawLabels(double scale) { |
|
484 |
return layer.isWithinScale(scale); |
|
485 |
} |
|
486 |
|
|
487 |
public Color getColorFont() { |
|
488 |
return colorFont; |
|
489 |
} |
|
490 |
|
|
491 |
public void setColorFont(Color colorFont) { |
|
492 |
this.colorFont = colorFont; |
|
493 |
} |
|
494 |
|
|
495 |
public Font getFont() { |
|
496 |
return font; |
|
497 |
} |
|
498 |
|
|
499 |
public void setFont(Font selFont) { |
|
500 |
this.font = selFont; |
|
501 |
} |
|
502 |
} |
Also available in: Unified diff