Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / styles / SimpleLabelStyle.java @ 11052

History | View | Annotate | Download (4.81 KB)

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: SimpleLabelStyle.java 11052 2007-04-04 15:42:03Z jaume $
45
* $Log$
46
* Revision 1.7  2007-04-04 15:42:03  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.6  2007/04/04 15:41:05  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2007/04/02 16:34:56  jaume
53
* Styled labeling (start commiting)
54
*
55
* Revision 1.4  2007/04/02 00:10:04  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.3  2007/03/29 16:02:01  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.2  2007/03/09 11:20:56  jaume
62
* Advanced symbology (start committing)
63
*
64
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2007/02/09 07:47:04  jaume
68
* Isymbol moved
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.fmap.core.styles;
73

    
74
import java.awt.Color;
75
import java.awt.Dimension;
76
import java.awt.Graphics2D;
77
import java.awt.Point;
78
import java.awt.Rectangle;
79
import java.awt.geom.AffineTransform;
80
import java.awt.geom.Point2D;
81
import java.awt.geom.Rectangle2D;
82

    
83
import com.iver.cit.gvsig.fmap.Messages;
84
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
85
import com.iver.utiles.XMLEntity;
86

    
87

    
88
/**
89
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
90
 */
91
public class SimpleLabelStyle extends SVGStyle implements ILabelStyle{
92
        private String text;
93
        private Point2D markerPoint = new Point2D.Double();
94

    
95
        public int getFieldCount() {
96
                return 1;
97
        }
98

    
99
        public void setTextFields(String[] texts) {
100
                StringBuffer sb = new StringBuffer();
101
                for (int i = 0; i < texts.length; i++) {
102
                        sb.append(texts[i]);
103
                        if (i<texts.length)
104
                                sb.append(" ");
105
                }
106
                this.text = sb.toString();
107
        }
108

    
109
        public String getTextAt(int i) {
110
                return text;
111
        }
112

    
113
        public boolean isSuitableFor(ISymbol symbol) {
114
                return true;
115
        }
116

    
117
        public String getClassName() {
118
                return getClass().getName();
119
        }
120

    
121
        public XMLEntity getXMLEntity() {
122
                XMLEntity xml = super.getXMLEntity();
123
                xml.putProperty("className", getClassName());
124
                xml.putProperty("desc", getDescription());
125
                xml.putProperty("text", text);
126
                return xml;
127
        }
128

    
129
        public void setXMLEntity(XMLEntity xml) {
130
                super.setXMLEntity(xml);
131
                setDescription(xml.getStringProperty("desc"));
132
                text = xml.getStringProperty("text");
133
        }
134

    
135
        public Rectangle[] getTextBounds() {
136
                return new Rectangle[] {new Rectangle(0,0,300,-60)};
137
        }
138

    
139
        public void drawInsideRectangle(Graphics2D g, Rectangle r) {
140
                g.translate(r.getX(), r.getY());
141
                super.drawInsideRectangle(g, r);
142
                g.translate(-r.getX(), -r.getY());
143
        }
144

    
145
        public Rectangle getBounds() {
146
                return super.getBounds();
147
        }
148

    
149
        public Point2D getMarkerPoint() {
150
                return markerPoint;
151
        }
152

    
153
        public void setMarkerPoint(Point2D p) throws IllegalArgumentException {
154
                if (p.getX()<0 || p.getX()>1)
155
                        throw new IllegalArgumentException("X must be >=0 and <=1 ("+p.getX()+")");
156
                if (p.getY()<0 || p.getY()>1)
157
                        throw new IllegalArgumentException("Y must be >=0 and <=1 ("+p.getY()+")");
158
                // the marker represents the point labeled in relative percent units
159
                this.markerPoint = p;
160
        }
161

    
162
        public void drawOutline(Graphics2D g, Rectangle r) {
163
                super.drawOutline(g, r);
164
                Rectangle labelSz = getBounds();
165

    
166
                double ratioLabel = labelSz.getWidth()/labelSz.getHeight();
167
                double ratioViewPort = r.getWidth() / r.getWidth();
168

    
169
                int x;
170
                int y;
171
                if (ratioViewPort > ratioLabel) {
172
                        // size is defined by the viewport height
173
                        y = (int) (r.height*markerPoint.getY());
174
                        x = (int) ((0.5*r.width) - (0.5-markerPoint.getX())*(ratioLabel*r.height));
175
                } else {
176
                        // size is defined by the viewport width
177
                        x = (int) (r.width * markerPoint.getX());
178
                        y = (int) ((0.5 * r.height) - (0.5-markerPoint.getY())*(r.width/ratioLabel));
179
                }
180
                y = r.height - y;
181

    
182
                int size = 3;
183
                g.setColor(Color.RED);
184
                g.fillOval(x, y, size, size);
185
                g.drawString(Messages.getString("labeled_point"), x + size + 5, y);
186
        }
187

    
188
}