Statistics
| Revision:

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

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

    
77
import java.awt.Graphics2D;
78
import java.awt.geom.AffineTransform;
79
import java.awt.geom.Rectangle2D;
80
import java.awt.image.BufferedImage;
81
import java.util.Hashtable;
82
import java.util.logging.Level;
83
import java.util.logging.Logger;
84

    
85
import javax.print.attribute.PrintRequestAttributeSet;
86
import javax.print.attribute.standard.PrintQuality;
87

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

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

    
118
        protected SimpleLabeling(FLayer layer) throws ReadDriverException {
119
                FLyrVect l = (FLyrVect) layer;
120
                this.layer = l;
121
                if (layer instanceof FLyrVect) {
122
                        FLyrVect lyrVect = (FLyrVect) layer;
123
                        placementConstraints = LabelingFactory.createPlacementConstraints(lyrVect);
124
                } else {
125
                        throw new Error("Layer type not yet supported");
126
                }
127

    
128
                method = new DefaultLabelingMethod();
129
        }
130

    
131
        public ILabelingMethod getLabelingMethod() {
132
                return method;
133
        }
134

    
135
        public void setLabelingMethod(ILabelingMethod method) {}
136

    
137
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
138
                                         Cancellable cancel)
139
        throws ReadDriverException {
140

    
141
                 Rectangle2D theExtent = viewPort.getAdjustedExtent();
142

    
143
                 if (zoomConstraints != null) {
144
                         double scale = viewPort.getScale();
145
                         if (scale < zoomConstraints.getMinScale() ||
146
                             scale > zoomConstraints.getMaxScale())
147
                                 return;
148
                 }
149
                 FBitSet bs;
150
                 int i = -1;
151
                 try {
152
                         bs = layer.queryByRect(theExtent);
153
                         LabelClass lc = method.getDefaultLabelClass();
154

    
155
                         ReadableVectorial rv = layer.getSource();
156
                         SelectableDataSource sds = rv.getRecordset();
157

    
158
                         ITextSymbol sym = lc.getLabelSymbol();
159
                         for(i=bs.nextSetBit(0); i>=0 && !cancel.isCanceled(); i=bs.nextSetBit(i+1)) {
160
                                 IGeometry geom;
161
                                 Integer index = new Integer(i);
162

    
163
                                 String text = getText(index, sds);
164
                                 sym.setText(text);
165

    
166
                                 geom = rv.getShape(i);
167
                                 FShape[] shp = placementConstraints.getLocationsFor(geom, sym, null);
168
                                 AffineTransform at = viewPort.getAffineTransform();
169
                                 for (int j = 0; j < shp.length; j++) {
170
                                         sym.draw(g, at, shp[i]);
171
                                 }
172

    
173
                         }
174
                 } catch (ExpansionFileReadException e) {
175
                         // TODO Auto-generated catch block
176
                         logger.log(Level.SEVERE, e.getMessage());
177
                 } catch (ReadDriverException e) {
178
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
179
                 } catch (VisitorException e) {
180
                         logger.log(Level.SEVERE, "Getting label text for "+i+"th shape of "+layer.getName());
181
                 }
182
        }
183

    
184
        private String getText(Integer index, SelectableDataSource sds) throws ReadDriverException {
185
                String text = (String) texts.get(index);
186
                if (text == null) {
187
                        String expr = method.getDefaultLabelClass().getLabelExpression();
188

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

    
193
                        text = sds.getFieldValue(index.longValue(), sds.getFieldIndexByName(fieldName)).toString();
194
                        texts.put(index, text);
195
                }
196
                return text;
197
        }
198

    
199
        private TextPath getTreePath(Integer index, Graphics2D g, FShape shp, char[] text) {
200
                TextPath tp = (TextPath) textPaths.get(index);
201
                if (tp == null) {
202
                        tp = new TextPath(g, shp, text);
203
                        textPaths.put(index, tp);
204
                }
205
                return tp;
206
        }
207

    
208

    
209

    
210
        public String getClassName() {
211
                return getClass().getName();
212
        }
213

    
214
        public XMLEntity getXMLEntity() {
215
                // TODO Implement it
216
//                throw new Error("Not yet implemented!");
217
                return new XMLEntity();
218
        }
219

    
220
        public void setXMLEntity(XMLEntity xml) {
221
                // TODO Implement it
222
//                throw new Error("Not yet implemented!");
223

    
224
        }
225

    
226
        public IPlacementConstraints getPlacementConstraints() {
227
                return placementConstraints;
228
        }
229

    
230
        public void setPlacementConstraints(IPlacementConstraints constraints) {}
231

    
232
        public IZoomConstraints getZoomConstraints() {
233
                return zoomConstraints;
234
        }
235

    
236
        public void setZoomConstraints(IZoomConstraints constraints) {
237
                this.zoomConstraints = constraints;
238
        }
239

    
240
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties) throws ReadDriverException {
241
//                double originalScaleFactor = unitFactor;
242
//                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
243
//                if (pq.equals(PrintQuality.NORMAL)){
244
//                        unitFactor *= (double) 300/72;
245
//                }else if (pq.equals(PrintQuality.HIGH)){
246
//                        unitFactor *= (double) 600/72;
247
//                }else if (pq.equals(PrintQuality.DRAFT)){
248
//                        //        unitFactor *= 72; (which is the same than doing nothing)
249
//                }
250
//                draw(null, g, viewPort, cancel);
251
//                unitFactor = originalScaleFactor;
252
        }
253
}