Statistics
| Revision:

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

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

    
83
import java.awt.Graphics2D;
84
import java.awt.geom.AffineTransform;
85
import java.awt.geom.Rectangle2D;
86
import java.awt.image.BufferedImage;
87
import java.util.Hashtable;
88
import java.util.logging.Level;
89
import java.util.logging.Logger;
90

    
91
import javax.print.attribute.PrintRequestAttributeSet;
92
import javax.print.attribute.standard.PrintQuality;
93

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

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

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

    
135
        public ILabelingMethod getLabelingMethod() {
136
                return method;
137
        }
138

    
139
        public void setLabelingMethod(ILabelingMethod method) {
140
                this.method = method;
141
        }
142

    
143
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
144
                                         Cancellable cancel)
145
        throws ReadDriverException {
146

    
147
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
148

    
149
                 if (method == null) {
150
                         Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn");
151
                         return;
152
                 }
153

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

    
166
                         ReadableVectorial rv = layer.getSource();
167
                         SelectableDataSource sds = rv.getRecordset();
168

    
169
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
170
                                 IGeometry geom;
171
                                 Integer index = new Integer(i);
172

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

    
181
                                         for (int j = 0; j < places.length; j++) {
182
                                                 places[j].transform(viewPort.getAffineTransform());
183
                                                 lc.drawInsideRectangle(g, places[j].getBounds(), texts);
184
                                         }
185
                                 }
186
                         }
187
                 } catch (ExpansionFileReadException e) {
188
                         // TODO Auto-generated catch block
189
                         logger.log(Level.SEVERE, e.getMessage());
190
                 } catch (ReadDriverException e) {
191
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
192
                 } catch (VisitorException e) {
193
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
194
                 }
195
        }
196

    
197
        private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
198
                String text = (String) texts.get(index);
199
                if (text == null) {
200
                        String expr = method.getDefaultLabelClass().getLabelExpression();
201

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

    
206
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
207
                        texts.put(index, text);
208
                }
209
                return new String[] { text };
210
        }
211

    
212
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
213
                TextPath tp = (TextPath) textPaths.get(index);
214
                if (tp == null) {
215
                        tp = new TextPath(g, shp, text);
216
                        textPaths.put(index, tp);
217
                }
218
                return tp;
219
        }
220

    
221
        public String getClassName() {
222
                return getClass().getName();
223
        }
224

    
225
        public XMLEntity getXMLEntity() {
226
                // TODO Implement it
227
//                throw new Error("Not yet implemented!");
228
                return new XMLEntity();
229
        }
230

    
231
        public void setXMLEntity(XMLEntity xml) {
232
                // TODO Implement it
233
//                throw new Error("Not yet implemented!");
234

    
235
        }
236

    
237
        public IPlacementConstraints getPlacementConstraints() {
238
                return placementConstraints;
239
        }
240

    
241
        public void setPlacementConstraints(IPlacementConstraints constraints) {}
242

    
243
        public IZoomConstraints getZoomConstraints() {
244
                return zoomConstraints;
245
        }
246

    
247
        public void setZoomConstraints(IZoomConstraints constraints) {
248
                this.zoomConstraints = constraints;
249
        }
250

    
251
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException {
252
//                double originalScaleFactor = unitFactor;
253
//                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
254
//                if (pq.equals(PrintQuality.NORMAL)){
255
//                        unitFactor *= (double) 300/72;
256
//                }else if (pq.equals(PrintQuality.HIGH)){
257
//                        unitFactor *= (double) 600/72;
258
//                }else if (pq.equals(PrintQuality.DRAFT)){
259
//                        //        unitFactor *= 72; (which is the same than doing nothing)
260
//                }
261
//                draw(null, g, viewPort, cancel);
262
//                unitFactor = originalScaleFactor;
263
        }
264
}