Revision 23907

View differences:

trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/rendering/styling/labeling/LabelClass.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
 *
44
 * $Id: LabelClass.java 13953 2007-09-21 12:26:04Z jaume $
45
 * $Log$
46
 * Revision 1.14  2007-09-21 12:26:04  jaume
47
 * cancellation support extended down to the IGeometry and ISymbol level
48
 *
49
 * Revision 1.13  2007/09/17 14:16:11  jaume
50
 * multilayer symbols sizing bug fixed
51
 *
52
 * Revision 1.12  2007/08/22 09:48:13  jvidal
53
 * javadoc
54
 *
55
 * Revision 1.11  2007/05/09 11:04:58  jaume
56
 * refactored legend hierarchy
57
 *
58
 * Revision 1.10  2007/05/08 08:47:40  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.9  2007/04/26 11:41:00  jaume
62
 * attempting to let defining size in world units
63
 *
64
 * Revision 1.8  2007/04/18 15:35:11  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.7  2007/04/12 14:28:43  jaume
68
 * basic labeling support for lines
69
 *
70
 * Revision 1.6  2007/04/11 16:01:08  jaume
71
 * maybe a label placer refactor
72
 *
73
 * Revision 1.5  2007/04/10 16:34:01  jaume
74
 * towards a styled labeling
75
 *
76
 * Revision 1.4  2007/04/05 16:07:14  jaume
77
 * Styled labeling stuff
78
 *
79
 * Revision 1.3  2007/04/02 16:34:56  jaume
80
 * Styled labeling (start commiting)
81
 *
82
 * Revision 1.2  2007/03/09 08:33:43  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1.2.6  2007/02/15 16:23:44  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.1.2.5  2007/02/09 07:47:05  jaume
89
 * Isymbol moved
90
 *
91
 * Revision 1.1.2.4  2007/02/02 16:21:24  jaume
92
 * start commiting labeling stuff
93
 *
94
 * Revision 1.1.2.3  2007/02/01 17:46:49  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.1.2.2  2007/02/01 11:42:47  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.1.2.1  2007/01/30 18:10:45  jaume
101
 * start commiting labeling stuff
102
 *
103
 *
104
 */
105
package com.iver.cit.gvsig.fmap.rendering.styling.labeling;
106

  
107
import java.awt.Color;
108
import java.awt.Dimension;
109
import java.awt.Graphics2D;
110
import java.awt.Rectangle;
111
import java.awt.Shape;
112
import java.awt.geom.AffineTransform;
113
import java.awt.geom.Point2D;
114
import java.awt.geom.Rectangle2D;
115

  
116
import com.iver.cit.gvsig.fmap.ViewPort;
117
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
118
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
119
import com.iver.cit.gvsig.fmap.core.FPoint2D;
120
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
121
import com.iver.cit.gvsig.fmap.core.FShape;
122
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
123
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
124
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
125
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
126
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
127
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
128
import com.iver.utiles.IPersistence;
129
import com.iver.utiles.XMLEntity;
130

  
131
/**
132
 *
133
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
134
 * class is contained its definition, the expresion that defines the text which
135
 * is going to be showed, if it will be visible or not, the text symbol that is
136
 * going to paint the label and the style for its background.
137
 *
138
 * @author jaume dominguez faus - jaume.dominguez@iver.es
139
 */
140
public class LabelClass implements IPersistence, CartographicSupport {
141
	private String name; 
142
	private ITextSymbol textSymbol;
143
	private String labelExpression;
144
	private boolean isVisible = true;
145
	private ILabelStyle labelStyle;
146
	private String[] texts;
147
	private int priority;
148
	private double scale = 1;
149
	private String sqlQuery;
150
	
151
	/**
152
	 * Returns true if the label will be showed in the map
153
	 *
154
	 * @return isVisible boolean
155
	 */
156
	public boolean isVisible() {
157
		return isVisible;
158
	}
159
	
160
	/**
161
	 * Sets the visibility of the label in the map.
162
	 *
163
	 * @param isVisible boolean
164
	 */
165
	public void setVisible(boolean isVisible) {
166
		this.isVisible = isVisible;
167
	}
168

  
169
	/**
170
	 * Returns the expression that defines the text which will be showed in
171
	 * the label
172
	 *
173
	 * @return labelExpression String
174
	 */
175
	public String getLabelExpression() {
176
		return labelExpression;
177
	}
178

  
179
	/**
180
	 * Stablishes the expresion that, when it is evaluated, returns the text
181
	 * which will be showed by the label.
182
	 *
183
	 * @param labelExpression String
184
	 */
185
	public void setLabelExpression(String labelExpression) {
186
		this.labelExpression = labelExpression;
187
	}
188

  
189
	/**
190
	 * Returns the text symbol that is being used for the text(the font,
191
	 * size,style,aligment)
192
	 *
193
	 * @return label ITextSymbol
194
	 */
195
	public ITextSymbol getTextSymbol() {
196
		if (textSymbol == null) {
197
			textSymbol = new SimpleTextSymbol();
198
		}
199
		return textSymbol;
200
	}
201
	
202
	private Dimension getSize() {
203
		if (labelStyle == null) {
204
			if (texts!=null && texts.length >0) {
205
				String t = "";
206
				for (int i = 0; i < texts.length; i++) {
207
					t += texts[i];
208
				}
209
				getTextSymbol().setText(t);
210
			}
211
				
212
			Rectangle bounds = getTextSymbol().getBounds();
213
			bounds.setLocation(
214
					(int) Math.round(bounds.getX()), 
215
					(int) Math.round(bounds.getY()+bounds.getHeight()));
216
			return new Dimension(bounds.width, bounds.height);
217
		} else { 
218
			labelStyle.setTextFields(texts); 
219
			return labelStyle.getSize();
220
		}
221
	}
222
	/**
223
	 * Stablishes the text symbol that is going to be used for the text(the
224
	 * font,size,style,aligment)
225
	 *
226
	 * @param textSymbol ITextSymbol
227
	 */
228
	public void setTextSymbol(ITextSymbol textSymbol) {
229
		this.textSymbol = textSymbol;
230
		if (textSymbol == null) {
231
			this.textSymbol = new SimpleTextSymbol();
232
		}
233
		setReferenceSystem(referenceSystem);
234
		setUnit(unit);
235
	}
236
	
237
	/**
238
	 * Stablishes the style for the label.
239
	 *
240
	 * @param labelStyle ILabelStyle
241
	 */
242
	public void setLabelStyle(ILabelStyle labelStyle) {
243
		this.labelStyle = labelStyle;
244
	}
245
	
246
	/**
247
	 * Returns the style of the label
248
	 *
249
	 */
250
	public ILabelStyle getLabelStyle() {
251
		return this.labelStyle;
252
	}
253
	
254
	/**
255
	 * Returns the name of the label
256
	 *
257
	 */
258
	public String getName() {
259
		return name;
260
	}
261
	
262
	/**
263
	 * Stablishes the name of the label
264
	 * @param name
265
	 */
266
	public void setName(String name) {
267
		this.name = name;
268
	}
269

  
270
	public String toString() {
271
		// for debugging
272
//		return name+"{label expression="+labelExpression+", visible="+isVisible+", priority="+priority+"}";
273
		return getName();
274
	}
275
	
276
	/**
277
	 * Sets the text for the label
278
	 *
279
	 * @param texts String[]
280
	 */
281
	public void setTexts(String[] texts) {
282
		this.texts = texts;
283
		
284
	}
285
	
286
	
287
	/**
288
	 * <p>
289
	 * LabelLocationMetrics, contains the anchor point, rotation, and some
290
	 * other geometric calculations computed by the PlacementManager.
291
	 * </p>
292
	 * 
293
	 * <p>
294
	 * The shp argument is passed as an accessory for subclasses of this 
295
	 * class in case they need futher geometric calculations
296
	 * </p>
297
	 * @param graphics, graphics to use to paint the label.
298
	 * @param llm, concrete settings of the placement of this layer
299
	 * @param shp, the Shape over whose the label is painted
300
	 */
301
	public void draw(Graphics2D graphics, LabelLocationMetrics llm, FShape shp) {
302
		if (scale == 0)
303
			return;
304

  
305
		
306
		Dimension size = getSize();
307
		int width = (int) Math.round(size.getWidth()*scale);
308
		if (width  < 1)
309
			return;
310
		
311
		int height = (int) Math.round(size.getHeight()*scale);
312
		if (height < 1)
313
			return;
314
		
315
		Rectangle r = new Rectangle(0,0, width, height);
316
		FPoint2D anchor = new FPoint2D(llm.getAnchor());
317
		double xAnchor = anchor.getX();
318
		double yAnchor = anchor.getY();
319
		double theta = llm.getRotation();
320
 
321

  
322
		graphics.translate(xAnchor, yAnchor);
323
		graphics.rotate(theta);
324
		try {
325
			drawInsideRectangle(graphics, r);
326
		} catch (SymbolDrawingException e) {
327
			// TODO Auto-generated catch block
328
			e.printStackTrace();
329
		}
330
		graphics.rotate(-theta);
331
		graphics.translate(-xAnchor, -yAnchor);
332
	}
333
	
334
	
335
	private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
336
		int x;
337
		int y;
338
		if (ratioViewPort > ratioLabel) {
339
			// size is defined by the viewport height
340
			y = (int) (r.height*xy[1]);
341
			x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
342
		} else {
343
			// size is defined by the viewport width
344
			x = (int) (r.width * xy[0]);
345
			y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
346
		}
347
		xy[0] = x;
348
		xy[1] = y;
349
	}
350
	
351
	/**
352
	 * Useful to render a Label with size inside little rectangles.
353
	 *
354
	 * @param graphics Graphics2D
355
	 * @param bounds Rectangle
356
	 * @throws SymbolDrawingException 
357
	 */
358
	public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
359
		if (labelStyle != null) {
360
			labelStyle.drawInsideRectangle(graphics, bounds);
361
			Rectangle2D[] textBounds = labelStyle.getTextBounds();
362
			Dimension labelSz = getSize();
363
			final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
364
			final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
365
			final double[] xy = new double[2];
366
					
367

  
368
			// draw the text fields
369
			if (textBounds.length > 0 && texts!=null) {
370
				for (int i = 0; i < textBounds.length && i < texts.length; i++) {
371
					getTextSymbol().setText(texts[i]);
372
					Rectangle2D textFieldArea = textBounds[i];
373
					xy[0] = textFieldArea.getX();
374
					xy[1] = textFieldArea.getY();
375
					relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
376
					int x = (int) Math.round(xy[0]);
377
					int y = (int) Math.round(xy[1]);
378
					
379
					xy[0] = textFieldArea.getMaxX();
380
					xy[1] = textFieldArea.getMaxY();
381
					relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
382
					int width = (int) Math.round(xy[0]) -x;
383
					int height = (int) Math.round(xy[1] - y) ;
384

  
385
					Rectangle textRect = new Rectangle(x, y, width, height);
386
					Shape oldClip = graphics.getClip();
387
					graphics.setClip(textRect);
388
					getTextSymbol().drawInsideRectangle(graphics, null, textRect);
389
					graphics.setClip(oldClip);
390
				}
391
			}
392
		} else {
393
			
394
			if (texts != null && texts.length>0) 
395
				getTextSymbol().setText(texts[0]);
396
			getTextSymbol().drawInsideRectangle(graphics, null, bounds);
397
		}
398
	}
399
	
400
	public int getPriority() {
401
		return priority;
402
	}
403
	
404
	public void setPriority(int priority) {
405
		this.priority = priority;
406
	}
407

  
408
	public FShape getShape(LabelLocationMetrics llm) {
409
		Point2D anchor = llm.getAnchor();
410
		FPoint2D p = new FPoint2D(anchor);
411
		double theta = llm.getRotation();
412

  
413
		// 2. calculate the container shape
414
		FShape returnedValue;
415
		Rectangle bounds = getBounds();
416
		
417
		AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
418
		at.concatenate(AffineTransform.getRotateInstance(theta));
419
		returnedValue = new FPolygon2D(new GeneralPathX(bounds));
420

  
421
		returnedValue.transform(at);
422
		return returnedValue;
423
	}
424

  
425
	public String getClassName() {
426
		return getClass().getName();
427
	}
428

  
429
	public XMLEntity getXMLEntity() {
430
		XMLEntity xml = new XMLEntity();
431
		xml.putProperty("className", getClassName());
432
		xml.putProperty("isVisible", isVisible);
433
		xml.putProperty("name", name);
434
		xml.putProperty("labelExpression", labelExpression);
435
		xml.putProperty("unit", getUnit());
436
		xml.putProperty("referenceSystem", getReferenceSystem());
437
		xml.putProperty("priority", getPriority());
438
		
439
		if (texts != null) {
440
			xml.putProperty("texts", texts);
441
		}
442
		if (sqlQuery != null) {
443
			xml.putProperty("sqlQuery", getSQLQuery());
444
		}
445
		
446
		if (labelStyle!=null) {
447
			XMLEntity labelStyleXML = labelStyle.getXMLEntity();
448
			labelStyleXML.putProperty("id", "labelStyle");
449
			xml.addChild(labelStyleXML);
450
		}
451
		
452
		XMLEntity textSymXML = getTextSymbol().getXMLEntity();
453
		textSymXML.putProperty("id", "TextSymbol");
454
		xml.addChild(textSymXML);
455
		
456
		return xml;
457
	}
458

  
459
	public void setXMLEntity(XMLEntity xml) {
460
		isVisible = xml.getBooleanProperty("isVisible");
461
		name = xml.getStringProperty("name");
462
		labelExpression = xml.getStringProperty("labelExpression");
463
		setUnit(xml.getIntProperty("unit"));
464
		setReferenceSystem(xml.getIntProperty("referenceSystem"));
465
		setTextSymbol(
466
				(ITextSymbol) SymbologyFactory.
467
				createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
468
		
469
		if (xml.contains("texts")) {
470
			setTexts(xml.getStringArrayProperty("texts"));
471
		}
472
		if (xml.contains("sqlQuery")) {
473
			setSQLQuery(xml.getStringProperty("sqlQuery"));
474
		}
475
		
476
		if (xml.contains("priority")) {
477
			setPriority(xml.getIntProperty("priority"));
478
		}
479
		// labelStyle
480
		XMLEntity aux = xml.firstChild("id", "labelStyle");
481
		if (aux!= null) {
482
			setLabelStyle((ILabelStyle) SymbologyFactory.
483
							createStyleFromXML(aux, "labelStyle"));
484
		}
485
	}
486

  
487
	private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
488
	private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
489
	
490
	public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
491
		Dimension sz = getSize();
492
		double width = sz.getWidth();
493
		double height = sz.getHeight();
494
		return CartographicSupportToolkit.
495
			getCartographicLength(this,
496
							  Math.max(width, height),
497
							  viewPort,
498
							  dpi);
499
	}
500

  
501
	public int getReferenceSystem() {
502
		return referenceSystem;
503
	}
504

  
505
	public int getUnit() {
506
		return unit;
507
	}
508

  
509
	public void setCartographicSize(double cartographicSize, FShape shp) {
510
		Dimension sz = getSize();
511
		double width = sz.getWidth();
512
		double height = sz.getHeight();
513
		if (width >= height) {
514
			scale = cartographicSize / width;
515
		} else {
516
			scale = cartographicSize / height;
517
		}
518
	}
519

  
520
	public void setReferenceSystem(int referenceSystem) {
521
		this.referenceSystem = referenceSystem;
522
		if (textSymbol != null && textSymbol instanceof CartographicSupport) {
523
			((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
524
		}
525
	}
526

  
527
	public void setUnit(int unitIndex) {
528
		this.unit = unitIndex;
529
		if (textSymbol != null && textSymbol instanceof CartographicSupport) {
530
			((CartographicSupport) textSymbol).setUnit(unitIndex);
531
		}
532
	}
533

  
534
	public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
535
		setCartographicSize(getCartographicSize(
536
								viewPort,
537
								dpi,
538
								shp),
539
							shp);
540
		return 0;
541
	}
542

  
543
	public Rectangle getBounds() {
544
		Dimension cBounds = getSize();
545
		return new Rectangle(
546
				0,
547
				0,
548
				(int) Math.round(cBounds.width*scale),
549
				(int) Math.round(cBounds.height*scale));
550
	}
551

  
552
	public String getSQLQuery() {
553
		if (sqlQuery == null) sqlQuery = "";
554
		return sqlQuery;
555
	}
556

  
557
	public void setSQLQuery(String sqlQuery) {
558
		this.sqlQuery = sqlQuery;
559
	}
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

  
42
/* CVS MESSAGES:
43
 *
44
 * $Id: LabelClass.java 13953 2007-09-21 12:26:04Z jaume $
45
 * $Log$
46
 * Revision 1.14  2007-09-21 12:26:04  jaume
47
 * cancellation support extended down to the IGeometry and ISymbol level
48
 *
49
 * Revision 1.13  2007/09/17 14:16:11  jaume
50
 * multilayer symbols sizing bug fixed
51
 *
52
 * Revision 1.12  2007/08/22 09:48:13  jvidal
53
 * javadoc
54
 *
55
 * Revision 1.11  2007/05/09 11:04:58  jaume
56
 * refactored legend hierarchy
57
 *
58
 * Revision 1.10  2007/05/08 08:47:40  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.9  2007/04/26 11:41:00  jaume
62
 * attempting to let defining size in world units
63
 *
64
 * Revision 1.8  2007/04/18 15:35:11  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.7  2007/04/12 14:28:43  jaume
68
 * basic labeling support for lines
69
 *
70
 * Revision 1.6  2007/04/11 16:01:08  jaume
71
 * maybe a label placer refactor
72
 *
73
 * Revision 1.5  2007/04/10 16:34:01  jaume
74
 * towards a styled labeling
75
 *
76
 * Revision 1.4  2007/04/05 16:07:14  jaume
77
 * Styled labeling stuff
78
 *
79
 * Revision 1.3  2007/04/02 16:34:56  jaume
80
 * Styled labeling (start commiting)
81
 *
82
 * Revision 1.2  2007/03/09 08:33:43  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.1.2.6  2007/02/15 16:23:44  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.1.2.5  2007/02/09 07:47:05  jaume
89
 * Isymbol moved
90
 *
91
 * Revision 1.1.2.4  2007/02/02 16:21:24  jaume
92
 * start commiting labeling stuff
93
 *
94
 * Revision 1.1.2.3  2007/02/01 17:46:49  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.1.2.2  2007/02/01 11:42:47  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.1.2.1  2007/01/30 18:10:45  jaume
101
 * start commiting labeling stuff
102
 *
103
 *
104
 */
105
package com.iver.cit.gvsig.fmap.rendering.styling.labeling;
106

  
107
import java.awt.Color;
108
import java.awt.Dimension;
109
import java.awt.Graphics2D;
110
import java.awt.Rectangle;
111
import java.awt.Shape;
112
import java.awt.geom.AffineTransform;
113
import java.awt.geom.Point2D;
114
import java.awt.geom.Rectangle2D;
115

  
116
import com.iver.cit.gvsig.fmap.ViewPort;
117
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
118
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
119
import com.iver.cit.gvsig.fmap.core.FPoint2D;
120
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
121
import com.iver.cit.gvsig.fmap.core.FShape;
122
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
123
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
124
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
125
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
126
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
127
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
128
import com.iver.utiles.IPersistence;
129
import com.iver.utiles.XMLEntity;
130

  
131
/**
132
 *
133
 * LabelClass is the model of the label in the new simbology of gvSIG. In this
134
 * class is contained its definition, the expresion that defines the text which
135
 * is going to be showed, if it will be visible or not, the text symbol that is
136
 * going to paint the label and the style for its background.
137
 *
138
 * @author jaume dominguez faus - jaume.dominguez@iver.es
139
 */
140
public class LabelClass implements IPersistence, CartographicSupport {
141
	private String name;
142
	private ITextSymbol textSymbol;
143
	private String[] labelExpressions;
144
	private boolean isVisible = true;
145
	private ILabelStyle labelStyle;
146
	private String[] texts;
147
	private int priority;
148
	private double scale = 1;
149
	private String sqlQuery;
150

  
151
	/**
152
	 * Returns true if the label will be showed in the map
153
	 *
154
	 * @return isVisible boolean
155
	 */
156
	public boolean isVisible() {
157
		return isVisible;
158
	}
159

  
160
	/**
161
	 * Sets the visibility of the label in the map.
162
	 *
163
	 * @param isVisible boolean
164
	 */
165
	public void setVisible(boolean isVisible) {
166
		this.isVisible = isVisible;
167
	}
168

  
169
	/**
170
	 * Returns the expression that defines the text which will be showed in
171
	 * the label
172
	 *
173
	 * @return labelExpressions String[]
174
	 */
175
	public String[] getLabelExpressions() {
176
		return labelExpressions;
177
	}
178
	/**
179
	 * Returns the expression that defines the text which will be showed in
180
	 * the label ready to be used for the label parser
181
	 *
182
	 * @return labelExpression String
183
	 */
184
	public String getStringLabelExpression(){
185
		String expr = "";
186

  
187
		if(labelExpressions != null && labelExpressions.length > 0){
188

  
189
			for (int i = 0; i < labelExpressions.length-1; i++) {
190
				expr += (String) labelExpressions[i] +  ":";//EOFIELD
191
			}
192
			expr += labelExpressions[labelExpressions.length - 1] +";";//EOEXPRESSION
193

  
194
		}
195
		else expr = ";";
196
		return expr;
197
	}
198

  
199

  
200
	/**
201
	 * Stablishes the expresion that, when it is evaluated, returns the text
202
	 * which will be showed by the label.
203
	 *
204
	 * @param labelExpression String
205
	 */
206
	public void setLabelExpressions(String[] labelExpressions) {
207
		this.labelExpressions = labelExpressions;
208
	}
209

  
210
	/**
211
	 * Returns the text symbol that is being used for the text(the font,
212
	 * size,style,aligment)
213
	 *
214
	 * @return label ITextSymbol
215
	 */
216
	public ITextSymbol getTextSymbol() {
217
		if (textSymbol == null) {
218
			textSymbol = new SimpleTextSymbol();
219
		}
220
		return textSymbol;
221
	}
222

  
223
	private Dimension getSize() {
224
		if (labelStyle == null) {
225
			if (texts!=null && texts.length >0) {
226
				String t = "";
227
				for (int i = 0; i < texts.length; i++) {
228
					t += texts[i];
229
				}
230
				getTextSymbol().setText(t);
231
			}
232

  
233
			Rectangle bounds = getTextSymbol().getBounds();
234
			bounds.setLocation(
235
					(int) Math.round(bounds.getX()),
236
					(int) Math.round(bounds.getY()+bounds.getHeight()));
237
			return new Dimension(bounds.width, bounds.height);
238
		} else {
239
			labelStyle.setTextFields(texts);
240
			return labelStyle.getSize();
241
		}
242
	}
243
	/**
244
	 * Stablishes the text symbol that is going to be used for the text(the
245
	 * font,size,style,aligment)
246
	 *
247
	 * @param textSymbol ITextSymbol
248
	 */
249
	public void setTextSymbol(ITextSymbol textSymbol) {
250
		this.textSymbol = textSymbol;
251
		if (textSymbol == null) {
252
			this.textSymbol = new SimpleTextSymbol();
253
		}
254
		setReferenceSystem(referenceSystem);
255
		setUnit(unit);
256
	}
257

  
258
	/**
259
	 * Stablishes the style for the label.
260
	 *
261
	 * @param labelStyle ILabelStyle
262
	 */
263
	public void setLabelStyle(ILabelStyle labelStyle) {
264
		this.labelStyle = labelStyle;
265
	}
266

  
267
	/**
268
	 * Returns the style of the label
269
	 *
270
	 */
271
	public ILabelStyle getLabelStyle() {
272
		return this.labelStyle;
273
	}
274

  
275
	/**
276
	 * Returns the name of the label
277
	 *
278
	 */
279
	public String getName() {
280
		return name;
281
	}
282

  
283
	/**
284
	 * Stablishes the name of the label
285
	 * @param name
286
	 */
287
	public void setName(String name) {
288
		this.name = name;
289
	}
290

  
291
	public String toString() {
292
		// for debugging
293
//		return name+"{label expression="+labelExpression+", visible="+isVisible+", priority="+priority+"}";
294
		return getName();
295
	}
296

  
297
	/**
298
	 * Sets the text for the label
299
	 *
300
	 * @param texts String[]
301
	 */
302
	public void setTexts(String[] texts) {
303
		this.texts = texts;
304

  
305
	}
306

  
307

  
308
	/**
309
	 * <p>
310
	 * LabelLocationMetrics, contains the anchor point, rotation, and some
311
	 * other geometric calculations computed by the PlacementManager.
312
	 * </p>
313
	 *
314
	 * <p>
315
	 * The shp argument is passed as an accessory for subclasses of this
316
	 * class in case they need futher geometric calculations
317
	 * </p>
318
	 * @param graphics, graphics to use to paint the label.
319
	 * @param llm, concrete settings of the placement of this layer
320
	 * @param shp, the Shape over whose the label is painted
321
	 */
322
	public void draw(Graphics2D graphics, LabelLocationMetrics llm, FShape shp) {
323
		if (scale == 0)
324
			return;
325
		if (!isVisible)
326
			return;
327
		Dimension size = getSize();
328
		int width = (int) Math.round(size.getWidth()*scale);
329
		if (width  < 1)
330
			return;
331

  
332
		int height = (int) Math.round(size.getHeight()*scale);
333
		if (height < 1)
334
			return;
335

  
336
		Rectangle r = new Rectangle(0,0, width, height);
337
		FPoint2D anchor = new FPoint2D(llm.getAnchor());
338
		double xAnchor = anchor.getX();
339
		double yAnchor = anchor.getY();
340
		double theta = llm.getRotation();
341

  
342

  
343
		graphics.translate(xAnchor, yAnchor);
344
		graphics.rotate(theta);
345
		try {
346
			drawInsideRectangle(graphics, r);
347
		} catch (SymbolDrawingException e) {
348
			// TODO Auto-generated catch block
349
			e.printStackTrace();
350
		}
351
		graphics.rotate(-theta);
352
		graphics.translate(-xAnchor, -yAnchor);
353
	}
354

  
355

  
356
	private void relativeToAbsolute(double[] xy, Rectangle r, Dimension labelSz, double ratioLabel, double ratioViewPort) {
357
		int x;
358
		int y;
359
		if (ratioViewPort > ratioLabel) {
360
			// size is defined by the viewport height
361
			y = (int) (r.height*xy[1]);
362
			x = (int) ((0.5*r.width) - (0.5-xy[0])*(ratioLabel*r.height));
363
		} else {
364
			// size is defined by the viewport width
365
			x = (int) (r.width * xy[0]);
366
			y = (int) ((0.5 * r.height) - (0.5-xy[1])*(r.width/ratioLabel));
367
		}
368
		xy[0] = x;
369
		xy[1] = y;
370
	}
371

  
372
	/**
373
	 * Useful to render a Label with size inside little rectangles.
374
	 *
375
	 * @param graphics Graphics2D
376
	 * @param bounds Rectangle
377
	 * @throws SymbolDrawingException
378
	 */
379
	public void drawInsideRectangle(Graphics2D graphics, Rectangle bounds) throws SymbolDrawingException {
380
		if (labelStyle != null) {
381
			labelStyle.drawInsideRectangle(graphics, bounds);
382
			Rectangle2D[] textBounds = labelStyle.getTextBounds();
383
			Dimension labelSz = getSize();
384
			final double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
385
			final double ratioViewPort = bounds.getWidth() / bounds.getHeight();
386
			final double[] xy = new double[2];
387

  
388

  
389
			// draw the text fields
390
			if (textBounds.length > 0 && texts!=null) {
391
				for (int i = 0; i < textBounds.length && i < texts.length; i++) {
392
					getTextSymbol().setText(texts[i]);
393
					Rectangle2D textFieldArea = textBounds[i];
394
					xy[0] = textFieldArea.getX();
395
					xy[1] = textFieldArea.getY();
396
					relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
397
					int x = (int) Math.round(xy[0]);
398
					int y = (int) Math.round(xy[1]);
399

  
400
					xy[0] = textFieldArea.getMaxX();
401
					xy[1] = textFieldArea.getMaxY();
402
					relativeToAbsolute(xy, bounds, labelSz, ratioLabel, ratioViewPort);
403
					int width = (int) Math.round(xy[0]) -x;
404
					int height = (int) Math.round(xy[1] - y) ;
405

  
406
					Rectangle textRect = new Rectangle(x, y, width, height);
407
					Shape oldClip = graphics.getClip();
408
					graphics.setClip(textRect);
409
					getTextSymbol().drawInsideRectangle(graphics, null, textRect);
410
					graphics.setClip(oldClip);
411
				}
412
			}
413
		} else {
414

  
415
			if (texts != null && texts.length>0)
416
				getTextSymbol().setText(texts[0]);
417
			getTextSymbol().drawInsideRectangle(graphics, null, bounds);
418
		}
419
	}
420

  
421
	public int getPriority() {
422
		return priority;
423
	}
424

  
425
	public void setPriority(int priority) {
426
		this.priority = priority;
427
	}
428

  
429
	public FShape getShape(LabelLocationMetrics llm) {
430
		Point2D anchor = llm.getAnchor();
431
		FPoint2D p = new FPoint2D(anchor);
432
		double theta = llm.getRotation();
433

  
434
		// 2. calculate the container shape
435
		FShape returnedValue;
436
		Rectangle bounds = getBounds();
437

  
438
		AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
439
		at.concatenate(AffineTransform.getRotateInstance(theta));
440
		returnedValue = new FPolygon2D(new GeneralPathX(bounds));
441

  
442
		returnedValue.transform(at);
443
		return returnedValue;
444
	}
445

  
446
	public String getClassName() {
447
		return getClass().getName();
448
	}
449

  
450
	public XMLEntity getXMLEntity() {
451
		XMLEntity xml = new XMLEntity();
452
		xml.putProperty("className", getClassName());
453
		xml.putProperty("isVisible", isVisible);
454
		xml.putProperty("name", name);
455
		if(labelExpressions != null)
456
			xml.putProperty("labelExpressions", labelExpressions);
457
		xml.putProperty("unit", getUnit());
458
		xml.putProperty("referenceSystem", getReferenceSystem());
459
		xml.putProperty("priority", getPriority());
460

  
461
		if (texts != null) {
462
			xml.putProperty("texts", texts);
463
		}
464
		if (sqlQuery != null) {
465
			xml.putProperty("sqlQuery", getSQLQuery());
466
		}
467

  
468
		if (labelStyle!=null) {
469
			XMLEntity labelStyleXML = labelStyle.getXMLEntity();
470
			labelStyleXML.putProperty("id", "labelStyle");
471
			xml.addChild(labelStyleXML);
472
		}
473

  
474
		XMLEntity textSymXML = getTextSymbol().getXMLEntity();
475
		textSymXML.putProperty("id", "TextSymbol");
476
		xml.addChild(textSymXML);
477

  
478
		return xml;
479
	}
480

  
481
	public void setXMLEntity(XMLEntity xml) {
482
		isVisible = xml.getBooleanProperty("isVisible");
483
		name = xml.getStringProperty("name");
484
		if(xml.contains("labelExpressions"))
485
			labelExpressions = xml.getStringArrayProperty("labelExpressions");
486
		setUnit(xml.getIntProperty("unit"));
487
		setReferenceSystem(xml.getIntProperty("referenceSystem"));
488
		setTextSymbol(
489
				(ITextSymbol) SymbologyFactory.
490
				createSymbolFromXML(xml.firstChild("id", "TextSymbol"), null));
491

  
492
		if (xml.contains("texts")) {
493
			setTexts(xml.getStringArrayProperty("texts"));
494
		}
495
		if (xml.contains("sqlQuery")) {
496
			setSQLQuery(xml.getStringProperty("sqlQuery"));
497
		}
498

  
499
		if (xml.contains("priority")) {
500
			setPriority(xml.getIntProperty("priority"));
501
		}
502
		// labelStyle
503
		XMLEntity aux = xml.firstChild("id", "labelStyle");
504
		if (aux!= null) {
505
			setLabelStyle((ILabelStyle) SymbologyFactory.
506
							createStyleFromXML(aux, "labelStyle"));
507
		}
508
	}
509

  
510
	private int unit = CartographicSupportToolkit.DefaultMeasureUnit;
511
	private int referenceSystem = CartographicSupportToolkit.DefaultReferenceSystem;
512

  
513
	public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
514
		Dimension sz = getSize();
515
		double width = sz.getWidth();
516
		double height = sz.getHeight();
517
		return CartographicSupportToolkit.
518
			getCartographicLength(this,
519
							  Math.max(width, height),
520
							  viewPort,
521
							  dpi);
522
	}
523

  
524
	public int getReferenceSystem() {
525
		return referenceSystem;
526
	}
527

  
528
	public int getUnit() {
529
		return unit;
530
	}
531

  
532
	public void setCartographicSize(double cartographicSize, FShape shp) {
533
		Dimension sz = getSize();
534
		double width = sz.getWidth();
535
		double height = sz.getHeight();
536
		if (width >= height) {
537
			scale = cartographicSize / width;
538
		} else {
539
			scale = cartographicSize / height;
540
		}
541
	}
542

  
543
	public void setReferenceSystem(int referenceSystem) {
544
		this.referenceSystem = referenceSystem;
545
		if (textSymbol != null && textSymbol instanceof CartographicSupport) {
546
			((CartographicSupport) textSymbol).setReferenceSystem(referenceSystem);
547
		}
548
	}
549

  
550
	public void setUnit(int unitIndex) {
551
		this.unit = unitIndex;
552
		if (textSymbol != null && textSymbol instanceof CartographicSupport) {
553
			((CartographicSupport) textSymbol).setUnit(unitIndex);
554
		}
555
	}
556

  
557
	public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
558
		setCartographicSize(getCartographicSize(
559
								viewPort,
560
								dpi,
561
								shp),
562
							shp);
563
		return 0;
564
	}
565

  
566
	public Rectangle getBounds() {
567
		Dimension cBounds = getSize();
568
		return new Rectangle(
569
				0,
570
				0,
571
				(int) Math.round(cBounds.width*scale),
572
				(int) Math.round(cBounds.height*scale));
573
	}
574

  
575
	public String getSQLQuery() {
576
		if (sqlQuery == null) sqlQuery = "";
577
		return sqlQuery;
578
	}
579

  
580
	public void setSQLQuery(String sqlQuery) {
581
		this.sqlQuery = sqlQuery;
582
	}
560 583
}

Also available in: Unified diff