Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / SimpleLabelingStrategy.java @ 10671

History | View | Annotate | Download (6.49 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: SimpleLabelingStrategy.java 10671 2007-03-09 08:33:43Z jaume $
45
* $Log$
46
* Revision 1.1  2007-03-09 08:33:43  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.5  2007/02/21 07:34:08  jaume
50
* labeling starts working
51
*
52
* Revision 1.1.2.4  2007/02/15 16:23:44  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.3  2007/02/09 07:47:05  jaume
56
* Isymbol moved
57
*
58
* Revision 1.1.2.2  2007/02/02 16:21:24  jaume
59
* start commiting labeling stuff
60
*
61
* Revision 1.1.2.1  2007/02/01 17:46:49  jaume
62
* *** empty log message ***
63
*
64
*
65
*/
66
package com.iver.cit.gvsig.fmap.rendering.styling;
67

    
68
import java.awt.Graphics2D;
69
import java.awt.geom.AffineTransform;
70
import java.awt.geom.Rectangle2D;
71
import java.awt.image.BufferedImage;
72
import java.util.Hashtable;
73
import java.util.logging.Level;
74
import java.util.logging.Logger;
75

    
76
import com.iver.cit.gvsig.fmap.DriverException;
77
import com.iver.cit.gvsig.fmap.ViewPort;
78
import com.iver.cit.gvsig.fmap.core.FPoint2D;
79
import com.iver.cit.gvsig.fmap.core.FShape;
80
import com.iver.cit.gvsig.fmap.core.IGeometry;
81
import com.iver.cit.gvsig.fmap.core.TextPath;
82
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
83
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
84
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
85
import com.iver.cit.gvsig.fmap.layers.FBitSet;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
88
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
89
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
90
import com.iver.utiles.XMLEntity;
91
import com.iver.utiles.swing.threads.Cancellable;
92

    
93
/**
94
 * This is the default and simplest (therefore fastest) labeling strategy.
95
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
96
 */
97
public class SimpleLabelingStrategy implements ILabelingStrategy {
98
        private static Logger logger = Logger.getAnonymousLogger();
99
        private ILabelingMethod method;
100
        private IPlacementConstraints placementConstraints;
101
        private FLyrVect layer;
102
        private IZoomConstraints zoomConstraints;
103
        private Hashtable textPaths = new Hashtable(), texts = new Hashtable();
104

    
105
        protected SimpleLabelingStrategy(FLayer layer) throws DriverException {
106
                FLyrVect l = (FLyrVect) layer;
107
                this.layer = l;
108
                if (layer instanceof FLyrVect) {
109
                        FLyrVect lyrVect = (FLyrVect) layer;
110
                        placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect);
111
                } else {
112
                        throw new Error("Layer type not yet supported");
113
                }
114

    
115
                method = new EquallyLabeled();
116
        }
117

    
118
        public ILabelingMethod getLabelingMethod() {
119
                return method;
120
        }
121

    
122
        public void setLabelingMethod(ILabelingMethod method) {}
123

    
124
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel) throws DriverException {
125
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
126

    
127
                 if (zoomConstraints != null) {
128
                         double scale = viewPort.getScale();
129
                         if (scale < zoomConstraints.getMinScale() ||
130
                             scale > zoomConstraints.getMaxScale())
131
                                 return;
132
                 }
133
                 FBitSet bs = layer.queryByRect(theExtent);
134
                 LabelClass lc = method.getDefaultLabelClass();
135

    
136
                 ReadableVectorial rv = layer.getSource();
137
                 SelectableDataSource sds = rv.getRecordset();
138

    
139
                 ITextSymbol sym = lc.getLabelSymbol();
140
                 for(int i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
141
                         IGeometry geom;
142
                         try {
143
                                 Integer index = new Integer(i);
144

    
145
                                 String text = getText(index, sds);
146
                                 sym.setText(text);
147

    
148
                                 geom = rv.getShape(i);
149
                                 FShape[] shp = placementConstraints.getLocationsFor(geom, sym, null);
150
                                 AffineTransform at = viewPort.getAffineTransform();
151
                                 for (int j = 0; j < shp.length; j++) {
152
                                         sym.draw(g, at, shp[i]);
153
                                 }
154

    
155
                         } catch (DriverIOException e) {
156
                                 logger.log(Level.SEVERE, "Draw label for "+i+"th shape of "+layer.getName());
157

    
158
                         } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
159
                                 logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
160

    
161
                        }
162

    
163
                 }
164
        }
165

    
166
        private String getText(Integer index, SelectableDataSource sds) throws com.hardcode.gdbms.engine.data.driver.DriverException {
167
                String text = (String) texts.get(index);
168
                if (text == null) {
169
                        String expr = method.getDefaultLabelClass().getLabelExpression();
170

    
171
                        // TODO el analizador sint?ctico de momento s?lo pilla un valor del combo por
172
                        // defecto
173
                        String fieldName = expr.replaceAll("\\[", "").replaceAll("\\]", "").trim();
174

    
175
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
176
                        texts.put(index, text);
177
                }
178
                return text;
179
        }
180

    
181
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
182
                TextPath tp = (TextPath) textPaths.get(index);
183
                if (tp == null) {
184
                        tp = new TextPath(g, shp, text);
185
                        textPaths.put(index, tp);
186
                }
187
                return tp;
188
        }
189

    
190

    
191

    
192
        public String getClassName() {
193
                return getClass().getName();
194
        }
195

    
196
        public XMLEntity getXMLEntity() {
197
                // TODO Implement it
198
//                throw new Error("Not yet implemented!");
199
                return new XMLEntity();
200
        }
201

    
202
        public void setXMLEntity(XMLEntity xml) {
203
                // TODO Implement it
204
//                throw new Error("Not yet implemented!");
205

    
206
        }
207

    
208
        public IPlacementConstraints getPlacementConstraints() {
209
                return placementConstraints;
210
        }
211

    
212
        public void setPlacementConstraints(IPlacementConstraints constraints) {}
213

    
214
        public IZoomConstraints getZoomConstraints() {
215
                return zoomConstraints;
216
        }
217

    
218
        public void setZoomConstraints(IZoomConstraints constraints) {
219
                this.zoomConstraints = constraints;
220
        }
221
}