svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLabelStyle.java @ 11051
History | View | Annotate | Download (4.84 KB)
1 | 10679 | jaume | /* 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$
|
||
45 | * $Log$
|
||
46 | 11051 | jaume | * Revision 1.6 2007-04-04 15:41:05 jaume
|
47 | * *** empty log message ***
|
||
48 | *
|
||
49 | * Revision 1.5 2007/04/02 16:34:56 jaume
|
||
50 | 11009 | jaume | * Styled labeling (start commiting)
|
51 | *
|
||
52 | * Revision 1.4 2007/04/02 00:10:04 jaume
|
||
53 | 10977 | jaume | * *** empty log message ***
|
54 | *
|
||
55 | 10995 | jaume | * Revision 1.3 2007/03/29 16:02:01 jaume
|
56 | * *** empty log message ***
|
||
57 | *
|
||
58 | 10977 | jaume | * Revision 1.2 2007/03/09 11:20:56 jaume
|
59 | 10679 | jaume | * Advanced symbology (start committing)
|
60 | *
|
||
61 | * Revision 1.1.2.2 2007/02/15 16:23:44 jaume
|
||
62 | * *** empty log message ***
|
||
63 | *
|
||
64 | * Revision 1.1.2.1 2007/02/09 07:47:04 jaume
|
||
65 | * Isymbol moved
|
||
66 | *
|
||
67 | *
|
||
68 | */
|
||
69 | package com.iver.cit.gvsig.fmap.core.styles; |
||
70 | |||
71 | 11051 | jaume | import java.awt.Color; |
72 | import java.awt.Dimension; |
||
73 | 10679 | jaume | import java.awt.Graphics2D; |
74 | 11051 | jaume | import java.awt.Point; |
75 | 10679 | jaume | import java.awt.Rectangle; |
76 | 11051 | jaume | import java.awt.geom.AffineTransform; |
77 | import java.awt.geom.Point2D; |
||
78 | 11009 | jaume | import java.awt.geom.Rectangle2D; |
79 | 10679 | jaume | |
80 | 11051 | jaume | import com.iver.cit.gvsig.fmap.Messages; |
81 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
82 | import com.iver.utiles.XMLEntity; |
||
83 | |||
84 | |||
85 | /**
|
||
86 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
87 | */
|
||
88 | 10995 | jaume | public class SimpleLabelStyle extends SVGStyle implements ILabelStyle{ |
89 | 10679 | jaume | private String text; |
90 | 11051 | jaume | private Point2D markerPoint = new Point2D.Double(); |
91 | 10679 | jaume | |
92 | public int getFieldCount() { |
||
93 | return 1; |
||
94 | } |
||
95 | |||
96 | public void setTextFields(String[] texts) { |
||
97 | StringBuffer sb = new StringBuffer(); |
||
98 | for (int i = 0; i < texts.length; i++) { |
||
99 | sb.append(texts[i]); |
||
100 | if (i<texts.length)
|
||
101 | sb.append(" ");
|
||
102 | } |
||
103 | this.text = sb.toString();
|
||
104 | } |
||
105 | |||
106 | public String getTextAt(int i) { |
||
107 | return text;
|
||
108 | } |
||
109 | |||
110 | public boolean isSuitableFor(ISymbol symbol) { |
||
111 | return true; |
||
112 | 11009 | jaume | } |
113 | 10679 | jaume | |
114 | public String getClassName() { |
||
115 | 10995 | jaume | return getClass().getName();
|
116 | 10679 | jaume | } |
117 | |||
118 | public XMLEntity getXMLEntity() {
|
||
119 | 10995 | jaume | XMLEntity xml = super.getXMLEntity();
|
120 | xml.putProperty("className", getClassName());
|
||
121 | xml.putProperty("desc", getDescription());
|
||
122 | xml.putProperty("text", text);
|
||
123 | return xml;
|
||
124 | 10679 | jaume | } |
125 | |||
126 | public void setXMLEntity(XMLEntity xml) { |
||
127 | 10995 | jaume | super.setXMLEntity(xml);
|
128 | setDescription(xml.getStringProperty("desc"));
|
||
129 | text = xml.getStringProperty("text");
|
||
130 | 10679 | jaume | } |
131 | 10977 | jaume | |
132 | 11009 | jaume | public Rectangle[] getTextBounds() { |
133 | return new Rectangle[] {new Rectangle(0,0,300,-60)}; |
||
134 | 10977 | jaume | } |
135 | 11009 | jaume | |
136 | public void drawInsideRectangle(Graphics2D g, Rectangle r) { |
||
137 | g.translate(r.getX(), r.getY()); |
||
138 | super.drawInsideRectangle(g, r);
|
||
139 | g.translate(-r.getX(), -r.getY()); |
||
140 | } |
||
141 | |||
142 | public Rectangle getBounds() { |
||
143 | return super.getBounds(); |
||
144 | } |
||
145 | 11051 | jaume | |
146 | public Point2D getMarkerPoint() { |
||
147 | return markerPoint;
|
||
148 | } |
||
149 | |||
150 | public void setMarkerPoint(Point2D p) throws IllegalArgumentException { |
||
151 | if (p.getX()<0 || p.getX()>1) |
||
152 | throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")"); |
||
153 | if (p.getY()<0 || p.getY()>1) |
||
154 | throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")"); |
||
155 | // the marker represents the point labeled in relative percent units
|
||
156 | this.markerPoint = p;
|
||
157 | } |
||
158 | |||
159 | public void drawOutline(Graphics2D g, Rectangle r) { |
||
160 | super.drawOutline(g, r);
|
||
161 | Rectangle labelSz = getBounds();
|
||
162 | |||
163 | double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
|
||
164 | double ratioViewPort = r.getWidth() / r.getWidth();
|
||
165 | |||
166 | int x;
|
||
167 | int y;
|
||
168 | if (ratioViewPort > ratioLabel) {
|
||
169 | // size is defined by the viewport height
|
||
170 | y = (int) (r.height*markerPoint.getY());
|
||
171 | x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height)); |
||
172 | } else {
|
||
173 | // size is defined by the viewport width
|
||
174 | x = (int) (r.width * markerPoint.getX());
|
||
175 | y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel)); |
||
176 | } |
||
177 | y = r.height - y; |
||
178 | |||
179 | int size = 3; |
||
180 | g.setColor(Color.RED);
|
||
181 | g.fillOval(x, y, size, size); |
||
182 | g.drawString(Messages.getString("labeled_point"), x + size + 5, y); |
||
183 | } |
||
184 | |||
185 | public Dimension getPreferredSize() { |
||
186 | return new Dimension(getBounds().width, getBounds().height); |
||
187 | } |
||
188 | 10679 | jaume | } |