Statistics
| Revision:

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

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

    
101
import java.awt.Color;
102
import java.awt.Font;
103
import java.awt.Graphics2D;
104
import java.awt.geom.AffineTransform;
105
import java.awt.geom.Rectangle2D;
106
import java.awt.image.BufferedImage;
107
import java.util.Hashtable;
108
import java.util.logging.Level;
109
import java.util.logging.Logger;
110

    
111
import javax.print.attribute.PrintRequestAttributeSet;
112
import javax.print.attribute.standard.PrintQuality;
113

    
114
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
115
import com.hardcode.gdbms.engine.data.driver.DriverException;
116
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
117
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
118
import com.iver.cit.gvsig.fmap.ViewPort;
119
import com.iver.cit.gvsig.fmap.core.FShape;
120
import com.iver.cit.gvsig.fmap.core.IGeometry;
121
import com.iver.cit.gvsig.fmap.core.TextPath;
122
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
123
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
124
import com.iver.cit.gvsig.fmap.layers.FBitSet;
125
import com.iver.cit.gvsig.fmap.layers.FLayer;
126
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
127
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
128
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
129
import com.iver.utiles.XMLEntity;
130
import com.iver.utiles.swing.threads.Cancellable;
131

    
132
/**
133
 * This is the default and simplest (therefore fastest) labeling strategy.
134
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
135
 */
136
public class SimpleLabeling implements ILabelingStrategy {
137
        private static Logger logger = Logger.getAnonymousLogger();
138
        private ILabelingMethod method;
139
        private IPlacementConstraints placementConstraints;
140
        private FLyrVect layer;
141
        private IZoomConstraints zoomConstraints;
142
        private Hashtable textPaths = new Hashtable(), texts = new Hashtable();
143

    
144
        protected SimpleLabeling(FLayer layer) throws ReadDriverException {
145
                FLyrVect l = (FLyrVect) layer;
146
                this.layer = l;
147
                if (layer instanceof FLyrVect) {
148
                        FLyrVect lyrVect = (FLyrVect) layer;
149
                        placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect);
150
                } else {
151
                        throw new Error("Layer type not yet supported");
152
                }
153
        }
154

    
155
        public ILabelingMethod getLabelingMethod() {
156
                return method;
157
        }
158

    
159
        public void setLabelingMethod(ILabelingMethod method) {
160
                this.method = method;
161
        }
162

    
163
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
164
                                         Cancellable cancel)
165
        throws ReadDriverException {
166

    
167
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
168

    
169
                 if (method == null) {
170
                         Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn");
171
                         return;
172
                 }
173

    
174
                 if (zoomConstraints != null) {
175
                         double scale = viewPort.getScale();
176
                         if (scale < zoomConstraints.getMinScale() ||
177
                             scale > zoomConstraints.getMaxScale())
178
                                 return;
179
                 }
180
                 FBitSet bs;
181
                 int i = -1;
182
                 try {
183
                         bs = layer.queryByRect(theExtent);
184
                         LabelClass lc = method.getDefaultLabelClass();
185

    
186
                         ReadableVectorial rv = layer.getSource();
187
                         SelectableDataSource sds = rv.getRecordset();
188

    
189
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
190
                                 IGeometry geom;
191
                                 Integer index = new Integer(i);
192

    
193
                                 geom = rv.getShape(i);
194
                                 lc.setTexts(getText(index, sds));
195
                                 // refactor
196
//                                  if (labelShape is big enough)
197
                                 {
198
//                                         String[] texts = getText(index, sds);
199

    
200
                                         placementConstraints.placeLabel(g, geom, lc, null, viewPort.getAffineTransform());
201
                                 }
202

    
203

    
204
//                                        este ?s el cas quan gaste placementConstraints.getLocationsFor(...)
205
//                                  if (labelShape != null) {
206
//                                         FShape[] places = placementConstraints.getLocationsFor(
207
//                                                         geom, labelShape,
208
//                                                         null);
209
//
210
//                                         for (int j = 0; j < places.length; j++) {
211
////                                                 places[j].transform(viewPort.getAffineTransform());
212
//                                                 lc.drawInsideRectangle(g, places[j].getBounds());
213
//
214
//                                         }
215
//                                 }
216
                         }
217
                 } catch (ExpansionFileReadException e) {
218
                         logger.log(Level.SEVERE, e.getMessage());
219
                 } catch (ReadDriverException e) {
220
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
221
                 } catch (VisitorException e) {
222
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
223
                 }
224
        }
225

    
226
        private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
227
                String text = (String) texts.get(index);
228
                if (text == null) {
229
                        String expr = method.getDefaultLabelClass().getLabelExpression();
230

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

    
235
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
236
                        texts.put(index, text);
237
                }
238
                return new String[] { text };
239
        }
240

    
241
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
242
                TextPath tp = (TextPath) textPaths.get(index);
243
                if (tp == null) {
244
                        tp = new TextPath(g, shp, text);
245
                        textPaths.put(index, tp);
246
                }
247
                return tp;
248
        }
249

    
250
        public String getClassName() {
251
                return getClass().getName();
252
        }
253

    
254
        public XMLEntity getXMLEntity() {
255
                // TODO Implement it
256
//                throw new Error("Not yet implemented!");
257
                return new XMLEntity();
258
        }
259

    
260
        public void setXMLEntity(XMLEntity xml) {
261
                // TODO Implement it
262
//                throw new Error("Not yet implemented!");
263

    
264
        }
265

    
266
        public IPlacementConstraints getPlacementConstraints() {
267
                return placementConstraints;
268
        }
269

    
270
        public void setPlacementConstraints(IPlacementConstraints constraints) {
271
                this.placementConstraints = constraints;
272
        }
273

    
274
        public IZoomConstraints getZoomConstraints() {
275
                return zoomConstraints;
276
        }
277

    
278
        public void setZoomConstraints(IZoomConstraints constraints) {
279
                this.zoomConstraints = constraints;
280
        }
281

    
282
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException {
283
//                double originalScaleFactor = unitFactor;
284
//                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
285
//                if (pq.equals(PrintQuality.NORMAL)){
286
//                        unitFactor *= (double) 300/72;
287
//                }else if (pq.equals(PrintQuality.HIGH)){
288
//                        unitFactor *= (double) 600/72;
289
//                }else if (pq.equals(PrintQuality.DRAFT)){
290
//                        //        unitFactor *= 72; (which is the same than doing nothing)
291
//                }
292
//                draw(null, g, viewPort, cancel);
293
//                unitFactor = originalScaleFactor;
294
        }
295

    
296
        public String[] getUsedFields() {
297
//                // TODO Implement it
298
//                throw new Error("Not yet implemented!");
299
//
300
                // TODO
301

    
302
                return new String[0];
303
        }
304
}