Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / styling / LabelingStrategyImpl.java @ 11741

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

    
104
import java.awt.Graphics2D;
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

    
113
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
114
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
115
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
116
import com.iver.cit.gvsig.fmap.ViewPort;
117
import com.iver.cit.gvsig.fmap.core.IGeometry;
118
import com.iver.cit.gvsig.fmap.layers.FBitSet;
119
import com.iver.cit.gvsig.fmap.layers.FLayer;
120
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
121
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
122
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
123
import com.iver.utiles.XMLEntity;
124
import com.iver.utiles.swing.threads.Cancellable;
125

    
126
/**
127
 * This is the default and simplest (therefore fastest) labeling strategy.
128
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
129
 */
130
public class LabelingStrategyImpl implements ILabelingStrategy {
131
        private static Logger logger = Logger.getAnonymousLogger();
132
        private ILabelingMethod method;
133
        private IPlacementConstraints placementConstraints;
134
        private FLyrVect layer;
135
        private IZoomConstraints zoomConstraints;
136
        private Hashtable texts = new Hashtable();
137

    
138
        protected LabelingStrategyImpl(FLayer layer) throws ReadDriverException {
139
                FLyrVect l = (FLyrVect) layer;
140
                this.layer = l;
141
                if (layer instanceof FLyrVect) {
142
                        FLyrVect lyrVect = (FLyrVect) layer;
143
                        placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect);
144
                } else {
145
                        throw new Error("Layer type not yet supported");
146
                }
147
        }
148

    
149
        public ILabelingMethod getLabelingMethod() {
150
                return method;
151
        }
152

    
153
        public void setLabelingMethod(ILabelingMethod method) {
154
                this.method = method;
155
        }
156

    
157
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
158
                                         Cancellable cancel)
159
        throws ReadDriverException {
160

    
161
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
162

    
163
                 if (method == null) {
164
                         Logger.getAnonymousLogger().warning("Layer '"+layer.getName()+"'. No labeling method was set, labels will not be drawn");
165
                         return;
166
                 }
167

    
168
                 if (zoomConstraints != null) {
169
                         double scale = viewPort.getScale();
170
                         if (scale < zoomConstraints.getMinScale() ||
171
                             scale > zoomConstraints.getMaxScale())
172
                                 return;
173
                 }
174
                 FBitSet bs;
175
                 int i = -1;
176
                 try {
177
                         bs = layer.queryByRect(theExtent);
178
                         LabelClass lc = method.getDefaultLabelClass();
179

    
180
                         ReadableVectorial rv = layer.getSource();
181
                         SelectableDataSource sds = rv.getRecordset();
182

    
183
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
184
                                 IGeometry geom;
185
                                 Integer index = new Integer(i);
186

    
187
                                 geom = rv.getShape(i);
188
                                 lc.setTexts(getText(index, sds));
189
                                 // refactor
190
//                                  if (labelShape is big enough)
191
                                 {
192
//                                         String[] texts = getText(index, sds);
193

    
194
                                         placementConstraints.placeLabel(g, geom, lc, null, viewPort.getAffineTransform());
195
                                 }
196

    
197

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

    
220
        private String[] getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
221
                String text = (String) texts.get(index);
222
                if (text == null) {
223
                        String expr = method.getDefaultLabelClass().getLabelExpression();
224

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

    
229
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
230
                        texts.put(index, text);
231
                }
232
                return new String[] { text };
233
        }
234

    
235

    
236
        public String getClassName() {
237
                return getClass().getName();
238
        }
239

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

    
246
        public void setXMLEntity(XMLEntity xml) {
247
                // TODO Implement it
248
//                throw new Error("Not yet implemented!");
249

    
250
        }
251

    
252
        public IPlacementConstraints getPlacementConstraints() {
253
                return placementConstraints;
254
        }
255

    
256
        public void setPlacementConstraints(IPlacementConstraints constraints) {
257
                this.placementConstraints = constraints;
258
        }
259

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

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

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

    
282
        public String[] getUsedFields() {
283
//                // TODO Implement it
284
//                throw new Error("Not yet implemented!");
285
//
286
                // TODO
287

    
288
                return new String[0];
289
        }
290
}