Statistics
| Revision:

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

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

    
86
import java.awt.Color;
87
import java.awt.Graphics2D;
88
import java.awt.geom.AffineTransform;
89
import java.awt.geom.Rectangle2D;
90
import java.awt.image.BufferedImage;
91
import java.util.Hashtable;
92
import java.util.logging.Level;
93
import java.util.logging.Logger;
94

    
95
import javax.print.attribute.PrintRequestAttributeSet;
96
import javax.print.attribute.standard.PrintQuality;
97

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

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

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

    
139
        public ILabelingMethod getLabelingMethod() {
140
                return method;
141
        }
142

    
143
        public void setLabelingMethod(ILabelingMethod method) {
144
                this.method = method;
145
        }
146

    
147
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
148
                                         Cancellable cancel)
149
        throws ReadDriverException {
150

    
151
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
152

    
153
                 if (method == null) {
154
                         Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn");
155
                         return;
156
                 }
157

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

    
170
                         ReadableVectorial rv = layer.getSource();
171
                         SelectableDataSource sds = rv.getRecordset();
172

    
173
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
174
                                 IGeometry geom;
175
                                 Integer index = new Integer(i);
176

    
177
                                 String[] texts = getText(index, sds);
178
                                 geom = rv.getShape(i);
179
                                 FShape labelShape = lc.getShape(g, geom);
180
                                 if (labelShape != null) {
181
                                         FShape[] places = placementConstraints.getLocationsFor(
182
                                                         geom, labelShape,
183
                                                         null);
184

    
185
                                         for (int j = 0; j < places.length; j++) {
186
                                                 places[j].transform(viewPort.getAffineTransform());
187
                                                 lc.drawInsideRectangle(g, places[j].getBounds(), texts);
188
                                                 g.setColor(Color.BLUE);
189
                                                 g.draw(places[j]);
190

    
191
                                         }
192
                                 }
193
                         }
194
                 } catch (ExpansionFileReadException e) {
195
                         // TODO Auto-generated catch block
196
                         logger.log(Level.SEVERE, e.getMessage());
197
                 } catch (ReadDriverException e) {
198
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
199
                 } catch (VisitorException e) {
200
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
201
                 }
202
        }
203

    
204
        private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
205
                String text = (String) texts.get(index);
206
                if (text == null) {
207
                        String expr = method.getDefaultLabelClass().getLabelExpression();
208

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

    
213
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
214
                        texts.put(index, text);
215
                }
216
                return new String[] { text };
217
        }
218

    
219
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
220
                TextPath tp = (TextPath) textPaths.get(index);
221
                if (tp == null) {
222
                        tp = new TextPath(g, shp, text);
223
                        textPaths.put(index, tp);
224
                }
225
                return tp;
226
        }
227

    
228
        public String getClassName() {
229
                return getClass().getName();
230
        }
231

    
232
        public XMLEntity getXMLEntity() {
233
                // TODO Implement it
234
//                throw new Error("Not yet implemented!");
235
                return new XMLEntity();
236
        }
237

    
238
        public void setXMLEntity(XMLEntity xml) {
239
                // TODO Implement it
240
//                throw new Error("Not yet implemented!");
241

    
242
        }
243

    
244
        public IPlacementConstraints getPlacementConstraints() {
245
                return placementConstraints;
246
        }
247

    
248
        public void setPlacementConstraints(IPlacementConstraints constraints) {}
249

    
250
        public IZoomConstraints getZoomConstraints() {
251
                return zoomConstraints;
252
        }
253

    
254
        public void setZoomConstraints(IZoomConstraints constraints) {
255
                this.zoomConstraints = constraints;
256
        }
257

    
258
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException {
259
//                double originalScaleFactor = unitFactor;
260
//                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
261
//                if (pq.equals(PrintQuality.NORMAL)){
262
//                        unitFactor *= (double) 300/72;
263
//                }else if (pq.equals(PrintQuality.HIGH)){
264
//                        unitFactor *= (double) 600/72;
265
//                }else if (pq.equals(PrintQuality.DRAFT)){
266
//                        //        unitFactor *= 72; (which is the same than doing nothing)
267
//                }
268
//                draw(null, g, viewPort, cancel);
269
//                unitFactor = originalScaleFactor;
270
        }
271
}