Statistics
| Revision:

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

History | View | Annotate | Download (8.42 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: SimpleLabeling.java 11138 2007-04-11 16:01:08Z jaume $
45
* $Log$
46
* Revision 1.6  2007-04-11 16:01:08  jaume
47
* maybe a label placer refactor
48
*
49
* Revision 1.5  2007/04/10 16:34:01  jaume
50
* towards a styled labeling
51
*
52
* Revision 1.4  2007/04/02 16:34:56  jaume
53
* Styled labeling (start commiting)
54
*
55
* Revision 1.3  2007/03/28 16:48:01  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.2  2007/03/26 14:40:38  jaume
59
* added print method (BUT UNIMPLEMENTED)
60
*
61
* Revision 1.1  2007/03/20 16:16:20  jaume
62
* refactored to use ISymbol instead of FSymbol
63
*
64
* Revision 1.2  2007/03/09 11:20:57  jaume
65
* Advanced symbology (start committing)
66
*
67
* Revision 1.1  2007/03/09 08:33:43  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.5  2007/02/21 07:34:08  jaume
71
* labeling starts working
72
*
73
* Revision 1.1.2.4  2007/02/15 16:23:44  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.1.2.3  2007/02/09 07:47:05  jaume
77
* Isymbol moved
78
*
79
* Revision 1.1.2.2  2007/02/02 16:21:24  jaume
80
* start commiting labeling stuff
81
*
82
* Revision 1.1.2.1  2007/02/01 17:46:49  jaume
83
* *** empty log message ***
84
*
85
*
86
*/
87
package com.iver.cit.gvsig.fmap.rendering.styling;
88

    
89
import java.awt.Color;
90
import java.awt.Font;
91
import java.awt.Graphics2D;
92
import java.awt.geom.AffineTransform;
93
import java.awt.geom.Rectangle2D;
94
import java.awt.image.BufferedImage;
95
import java.util.Hashtable;
96
import java.util.logging.Level;
97
import java.util.logging.Logger;
98

    
99
import javax.print.attribute.PrintRequestAttributeSet;
100
import javax.print.attribute.standard.PrintQuality;
101

    
102
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
103
import com.hardcode.gdbms.engine.data.driver.DriverException;
104
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
105
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
106
import com.iver.cit.gvsig.fmap.ViewPort;
107
import com.iver.cit.gvsig.fmap.core.FShape;
108
import com.iver.cit.gvsig.fmap.core.IGeometry;
109
import com.iver.cit.gvsig.fmap.core.TextPath;
110
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
111
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
112
import com.iver.cit.gvsig.fmap.layers.FBitSet;
113
import com.iver.cit.gvsig.fmap.layers.FLayer;
114
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
115
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
116
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
117
import com.iver.utiles.XMLEntity;
118
import com.iver.utiles.swing.threads.Cancellable;
119

    
120
/**
121
 * This is the default and simplest (therefore fastest) labeling strategy.
122
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
123
 */
124
public class SimpleLabeling implements ILabelingStrategy {
125
        private static Logger logger = Logger.getAnonymousLogger();
126
        private ILabelingMethod method;
127
        private IPlacementConstraints placementConstraints;
128
        private FLyrVect layer;
129
        private IZoomConstraints zoomConstraints;
130
        private Hashtable textPaths = new Hashtable(), texts = new Hashtable();
131

    
132
        protected SimpleLabeling(FLayer layer) throws ReadDriverException {
133
                FLyrVect l = (FLyrVect) layer;
134
                this.layer = l;
135
                if (layer instanceof FLyrVect) {
136
                        FLyrVect lyrVect = (FLyrVect) layer;
137
                        placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect);
138
                } else {
139
                        throw new Error("Layer type not yet supported");
140
                }
141
        }
142

    
143
        public ILabelingMethod getLabelingMethod() {
144
                return method;
145
        }
146

    
147
        public void setLabelingMethod(ILabelingMethod method) {
148
                this.method = method;
149
        }
150

    
151
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
152
                                         Cancellable cancel)
153
        throws ReadDriverException {
154

    
155
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
156

    
157
                 if (method == null) {
158
                         Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn");
159
                         return;
160
                 }
161

    
162
                 if (zoomConstraints != null) {
163
                         double scale = viewPort.getScale();
164
                         if (scale < zoomConstraints.getMinScale() ||
165
                             scale > zoomConstraints.getMaxScale())
166
                                 return;
167
                 }
168
                 FBitSet bs;
169
                 int i = -1;
170
                 try {
171
                         bs = layer.queryByRect(theExtent);
172
                         LabelClass lc = method.getDefaultLabelClass();
173

    
174
                         ReadableVectorial rv = layer.getSource();
175
                         SelectableDataSource sds = rv.getRecordset();
176

    
177
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
178
                                 IGeometry geom;
179
                                 Integer index = new Integer(i);
180

    
181
                                 geom = rv.getShape(i);
182
                                 FShape labelShape = lc.getShape(g, geom);
183

    
184
                                 // refactor
185
                                 // if (labelShape is big enough)
186
                                 {
187
                                         String[] texts = getText(index, sds);
188

    
189
                                         placementConstraints.placeLabel(geom, lc, null, viewPort.getAffineTransform());
190
                                 }
191

    
192
                                 /*
193
                                  String[] texts = getText(index, sds);
194

195
                                  if (labelShape != null) {
196
                                         FShape[] places = placementConstraints.getLocationsFor(
197
                                                         geom, labelShape,
198
                                                         null);
199

200
                                         for (int j = 0; j < places.length; j++) {
201
                                                 places[j].transform(viewPort.getAffineTransform());
202
                                                 lc.drawInsideRectangle(g, places[j].getBounds(), texts);
203
                                         }
204
                                 }*/
205
                         }
206
                 } catch (ExpansionFileReadException e) {
207
                         logger.log(Level.SEVERE, e.getMessage());
208
                 } catch (ReadDriverException e) {
209
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
210
                 } catch (VisitorException e) {
211
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
212
                 }
213
        }
214

    
215
        private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
216
                String text = (String) texts.get(index);
217
                if (text == null) {
218
                        String expr = method.getDefaultLabelClass().getLabelExpression();
219

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

    
224
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
225
                        texts.put(index, text);
226
                }
227
                return new String[] { text };
228
        }
229

    
230
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
231
                TextPath tp = (TextPath) textPaths.get(index);
232
                if (tp == null) {
233
                        tp = new TextPath(g, shp, text);
234
                        textPaths.put(index, tp);
235
                }
236
                return tp;
237
        }
238

    
239
        public String getClassName() {
240
                return getClass().getName();
241
        }
242

    
243
        public XMLEntity getXMLEntity() {
244
                // TODO Implement it
245
//                throw new Error("Not yet implemented!");
246
                return new XMLEntity();
247
        }
248

    
249
        public void setXMLEntity(XMLEntity xml) {
250
                // TODO Implement it
251
//                throw new Error("Not yet implemented!");
252

    
253
        }
254

    
255
        public IPlacementConstraints getPlacementConstraints() {
256
                return placementConstraints;
257
        }
258

    
259
        public void setPlacementConstraints(IPlacementConstraints constraints) {}
260

    
261
        public IZoomConstraints getZoomConstraints() {
262
                return zoomConstraints;
263
        }
264

    
265
        public void setZoomConstraints(IZoomConstraints constraints) {
266
                this.zoomConstraints = constraints;
267
        }
268

    
269
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException {
270
//                double originalScaleFactor = unitFactor;
271
//                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
272
//                if (pq.equals(PrintQuality.NORMAL)){
273
//                        unitFactor *= (double) 300/72;
274
//                }else if (pq.equals(PrintQuality.HIGH)){
275
//                        unitFactor *= (double) 600/72;
276
//                }else if (pq.equals(PrintQuality.DRAFT)){
277
//                        //        unitFactor *= 72; (which is the same than doing nothing)
278
//                }
279
//                draw(null, g, viewPort, cancel);
280
//                unitFactor = originalScaleFactor;
281
        }
282
}