Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameGrid.java @ 324

History | View | Annotate | Download (29.3 KB)

1 5 jldominguez
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout.fframes;
23
24
import java.awt.Color;
25
import java.awt.Font;
26
import java.awt.Graphics2D;
27
import java.awt.font.FontRenderContext;
28
import java.awt.font.TextLayout;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Point2D;
31
import java.awt.geom.Rectangle2D;
32
import java.awt.image.BufferedImage;
33 274 cmartinez
import java.text.DecimalFormat;
34
import java.text.DecimalFormatSymbols;
35 5 jldominguez
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.compat.print.PrintAttributes;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.Geometry.TYPES;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.GeometryManager;
43
import org.gvsig.fmap.geom.exception.CreateGeometryException;
44
import org.gvsig.fmap.geom.primitive.Curve;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46 85 nbrodin
import org.gvsig.fmap.geom.primitive.Point;
47 5 jldominguez
import org.gvsig.fmap.mapcontext.MapContextLocator;
48
import org.gvsig.fmap.mapcontext.ViewPort;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
51 274 cmartinez
import org.gvsig.math.intervals.IntervalUtils;
52 5 jldominguez
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
53
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dynobject.DynStruct;
56
import org.gvsig.tools.locator.LocatorException;
57
import org.gvsig.tools.persistence.PersistenceManager;
58
import org.gvsig.tools.persistence.PersistentState;
59
import org.gvsig.tools.persistence.exception.PersistenceException;
60
61
/**
62
 * FFrame para introducir una cuadr?cula sobre una vista en el Layout.
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class FFrameGrid extends AbstractFFrameViewDependence implements
67
    IFFrameViewDependence {
68
    private static final GeometryManager GEOMETRY_MANAGER = GeometryLocator.getGeometryManager();
69
70
    private static final int OFFSET_IN_PIXELS = 5;
71
72
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGrid";
73
74
    private static final String SYMBOLLINE_FIELD = "symbolLine";
75
    private static final String SYMBOLPOINT_FIELD = "symbolPoint";
76
    private static final String FONT_FIELD = "font";
77
    private static final String INTERVALX_FIELD = "intervalX";
78
    private static final String INTERVALY_FIELD = "intervalY";
79
    private static final String ISLINE_FIELD = "isLine";
80
    private static final String FONTSYZE_FIELD = "fontsize";
81
    private static final String TEXTCOLOR_FIELD = "textColor";
82 274 cmartinez
    private static final String USE_NUM_DIVISIONS_FIELD = "useNumDivisions";
83
    private static final String NUM_DIV_HORIZ_FIELD = "numDivHoriz";
84
    private static final String NUM_DIV_VERT_FIELD = "numDivVret";
85
    private static final String LABEL_FORMAT_FIELD = "labelFormat";
86
    private static final String HORIZ_LABEL_ROTATION_FIELD = "horizLabelRotation";
87
    private static final String VERT_LABEL_ROTATION_FIELD = "vertLabelRotation";
88
89
    /**
90
     * Use a fixed distance (intervalX and intervalY)
91
     * to calculate the position of grid lines
92
     */
93
    public static final int FIXED_DISTANCE_MODE = 0;
94
    /**
95
     * Use a specific number of horizontal divisions in order
96
     * to calculate the position of grid lines
97
     */
98
    public static final int NUM_DIVISIONS_HORIZ_MODE = 1;
99
    /**
100
     * Use a specific number of vertical divisions in order to calculate
101
     * the position of grid lines
102
     */
103
    public static final int NUM_DIVISIONS_VERT_MODE = 2;
104 5 jldominguez
105 274 cmartinez
    private double defaultInterval = 100000;
106
    private Double intervalX = null;
107
    private Double intervalY = null;
108 5 jldominguez
109
    private Color textColor = Color.black;
110
    private boolean isLine;
111
    private int sizeFont = 8;
112 274 cmartinez
        private int useNumDivisions = FIXED_DISTANCE_MODE;
113
        private int numDivHoriz = 4;
114
        private int numDivVert = 4;
115
        private DecimalFormat labelFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance());
116
        private double horizLabelRotation = 0.0;
117
        private double vertLabelRotation = 0.0;
118 5 jldominguez
119
    // private boolean print=false;
120
    private SymbolManager symbolManager = MapContextLocator.getSymbolManager();
121
    private ILineSymbol symbolLine = (ILineSymbol) symbolManager
122
        .createSymbol(ILineSymbol.SYMBOL_NAME);
123
    private IMarkerSymbol symbolPoint = (IMarkerSymbol) symbolManager
124
        .createSymbol(IMarkerSymbol.SYMBOL_NAME);
125
    private Font font = new Font("Arial", Font.PLAIN, sizeFont);
126
127
    private AffineTransform emptyTransform = new AffineTransform();
128
129
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
130
        BufferedImage imgBase) {
131
        ViewPort vp = fframeViewDependence.getMapContext().getViewPort();
132
133
        Rectangle2D.Double r = getBoundingBox(at);
134
        Rectangle2D rView = fframeViewDependence.getBoundingBox(at);
135 267 cmartinez
        if (r==null || rView==null) {
136
                //extent may be null, for instance if no layer has been loaded on the view
137
                return;
138 274 cmartinez
        }
139 5 jldominguez
        Envelope envelope = vp.getAdjustedEnvelope();
140 274 cmartinez
        updateIntervals(envelope);
141
        double intervalX = getIntervalX();
142
        double intervalY = getIntervalY();
143 5 jldominguez
144 274 cmartinez
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y + (r.height / 2));
145
146 5 jldominguez
        double minX = envelope.getMinimum(0);
147
        double minY = envelope.getMinimum(1);
148 267 cmartinez
149
        // init point, in map coordinates
150
        double initPointGridWCX = Math.floor(minX / intervalX)*intervalX;
151
        double initPointGridWCY = Math.floor(minY / intervalY)*intervalY;
152 5 jldominguez
153 267 cmartinez
        // shift from the beginning of the map till the first point of the grid
154
        double shiftXMap = initPointGridWCX-minX;
155
        double shiftYMap = initPointGridWCY-minY;
156
157
        // init point, in screen coordinates (pixels)
158
        double initPointGridPxX = rView.getMinX() + (rView.getWidth() * shiftXMap) /  envelope.getLength(0);
159
        double initPointGridPxY = rView.getMinY() + (rView.getHeight() * shiftYMap) /  envelope.getLength(1);
160 85 nbrodin
        double intervalPxWidth = (rView.getWidth() * intervalX) /  envelope.getLength(0);
161
        double intervalPxHeight = (rView.getHeight() * intervalY) /  envelope.getLength(1);
162
163 5 jldominguez
        ISymbol symbolForMargins = null;
164
        if (isLine){
165
            symbolForMargins = symbolLine;
166
        }else{
167
            try {
168
                symbolForMargins = (ISymbol)symbolLine.clone();
169
                symbolForMargins.setColor(symbolPoint.getColor());
170
            } catch (CloneNotSupportedException e) {
171
                LOG.error("Symbol desn't support the clone method", e);
172
            }
173
        }
174
175
        //Draw the box
176
        drawBox(rView, g, symbolForMargins);
177
178
        //Draw the margins
179
        double myScale = at.getScaleX() * 0.0234;
180
        int scaledFontSize = (int) (myScale * sizeFont);
181 85 nbrodin
        Font font = new Font(
182
                        this.font.getFamily(),
183
                        this.font.getStyle(),
184 5 jldominguez
                scaledFontSize);
185
186 267 cmartinez
        drawMargins(rView,
187
                        initPointGridWCX,
188
                        initPointGridWCY,
189
                        intervalX,
190
                        intervalY,
191 85 nbrodin
                        initPointGridPxX,
192
                        initPointGridPxY,
193
                        intervalPxWidth,
194 267 cmartinez
                        intervalPxHeight,
195 85 nbrodin
                        g,
196
                        font,
197
                        symbolForMargins);
198 5 jldominguez
199
        //Draw the lines or the points
200
        if (isLine) {
201 85 nbrodin
            drawLines(rView,
202
                            initPointGridPxX,
203
                            initPointGridPxY,
204
                            intervalPxWidth,
205
                            intervalPxHeight,
206
                            vp, g);
207 5 jldominguez
        } else {
208 85 nbrodin
            drawPoints(rView,
209
                            initPointGridPxX,
210
                            initPointGridPxY,
211
                            intervalPxWidth,
212
                            intervalPxHeight,
213
                            vp, g);
214 5 jldominguez
        }
215
216
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
217 274 cmartinez
            + (r.height / 2));
218 5 jldominguez
    }
219
220 85 nbrodin
    private void drawPoints(Rectangle2D rView,
221
                    double initPointGridPxX,
222
                    double initPointGridPxY,
223
                    double intervalPxX,
224
                    double intervalPxY,
225
                    ViewPort viewPort,
226
                    Graphics2D g) {
227
        double minX = rView.getMinX();
228 267 cmartinez
        double maxX = rView.getMaxX();
229 85 nbrodin
        double minY = rView.getMinY();
230 267 cmartinez
        double maxY = rView.getMaxY();
231
        double x = initPointGridPxX;
232
        double y = initPointGridPxY;
233 5 jldominguez
234 267 cmartinez
        while (x <= maxX) {
235 85 nbrodin
            if (x > minX) {
236 267 cmartinez
                while (y <= maxY) {
237
                    if (y > minY) {
238 85 nbrodin
                        Point2D p = new Point2D.Double(x, y);
239
                        drawPoint(p, g);
240
                    }
241
                    y = y + intervalPxY;
242
                }
243 267 cmartinez
                y = initPointGridPxY;
244 85 nbrodin
            }
245
            x = x + intervalPxX;
246
        }
247
    }
248
249
    private void drawLines(Rectangle2D rView,
250
                    double initPointGridPxX,
251
                    double initPointGridPxY,
252
                    double intervalPxX,
253
                    double intervalPxY,
254
                    ViewPort viewPort,
255
                    Graphics2D g) {
256
        double minX = rView.getMinX();
257 267 cmartinez
        double maxX = rView.getMaxX();
258 85 nbrodin
        double minY = rView.getMinY();
259 267 cmartinez
        double maxY = rView.getMaxY();
260
        double x = initPointGridPxX;
261
        double y = initPointGridPxY;
262 5 jldominguez
263 85 nbrodin
        //Draw the vertical lines
264 267 cmartinez
        while (x < maxX) {
265
                if (x>minX) {
266
                        Point2D antPoint = new Point2D.Double(x, minY - 5);
267
                        Point2D p = new Point2D.Double(x, maxY + 5);
268
                        drawLine(antPoint, p, g, symbolLine);
269
                        antPoint = (Point2D) p.clone();
270
                }
271 85 nbrodin
            x = x + intervalPxX;
272
        }
273
        //Draw the horizontal lines
274 267 cmartinez
        while (y <= maxY) {
275
                if (y > minY) {
276
                        Point2D antPoint = new Point2D.Double(minX, y);
277
                        Point2D p = new Point2D.Double(rView.getMaxX(), y);
278
                        drawLine(antPoint, p, g, symbolLine);
279
                        antPoint = (Point2D) p.clone();
280
                }
281 85 nbrodin
            y = y + intervalPxY;
282
        }
283
    }
284
285 267 cmartinez
    private void drawMargins(Rectangle2D rView,
286
                    double initPointGridMapX,
287
                    double initPointGridMapY,
288
                    double intervalMapX,
289
                    double intervalMapY,
290 85 nbrodin
                    double initPointGridPxX,
291
                    double initPointGridPxY,
292
                    double intervalPxX,
293
                    double intervalPxY,
294
                    Graphics2D g,
295 267 cmartinez
                    Font font,
296 85 nbrodin
                    ISymbol
297
                    symbolForMargins) {
298 267 cmartinez
            FontRenderContext frc = g.getFontRenderContext();
299
300 85 nbrodin
        double minX = rView.getMinX();
301 267 cmartinez
        double maxX = rView.getMaxX();
302 85 nbrodin
        double minY = rView.getMinY();
303 267 cmartinez
        double maxY = rView.getMaxY();
304
        double xPx = initPointGridPxX;
305
        double xMap = initPointGridMapX;
306 85 nbrodin
307 267 cmartinez
        // Draw vertical ticks and labels
308
        while (xPx < maxX) {
309
            if (xPx > minX) {
310
                Point2D p2 = new Point2D.Double(xPx, minY);
311
                Point2D p1 = new Point2D.Double(xPx, maxY);
312 5 jldominguez
313
                drawLine(p1.getX(), p1.getY() - OFFSET_IN_PIXELS, p1.getX() , p1.getY(), g, symbolForMargins);
314
                drawLine(p2.getX(), p2.getY(), p2.getX() , p2.getY() + OFFSET_IN_PIXELS, g, symbolForMargins);
315 267 cmartinez
316 274 cmartinez
                TextLayout textaux = new TextLayout(this.labelFormat.format(xMap), font, frc);
317
                g.setColor(textColor);
318 5 jldominguez
                double w = textaux.getBounds().getWidth();
319
                double h = textaux.getBounds().getHeight();
320 274 cmartinez
321
                double labelY = (p1.getY() + h) + 8;
322
                g.rotate(Math.toRadians(getVertLabelRotation()), p1.getX(), (labelY - (h / 2.0)));
323 5 jldominguez
                textaux.draw(g, (int) (p1.getX() - w / 2),
324 274 cmartinez
                    (int) labelY);
325
                g.rotate(Math.toRadians(-getVertLabelRotation()),  p1.getX(), (labelY - (h / 2.0)));
326
327
                labelY = (p2.getY() - h);
328
                g.rotate(Math.toRadians(getVertLabelRotation()), p1.getX(), (labelY - (h / 2.0)));
329 5 jldominguez
                textaux.draw(g, (int) (p2.getX() - w / 2),
330 274 cmartinez
                    (int) labelY);
331
                g.rotate(Math.toRadians(-getVertLabelRotation()),  p1.getX(), (labelY - (h / 2.0)));
332 5 jldominguez
            }
333 267 cmartinez
            xMap = xMap + intervalMapX;
334
            xPx = xPx + intervalPxX;
335 5 jldominguez
        }
336 267 cmartinez
        double yPx = initPointGridPxY;
337
        double yMap = initPointGridMapY;
338 5 jldominguez
339 267 cmartinez
        // horizontal ticks and labels
340
        while (yPx < maxY) {
341
            if (yPx > minY) {
342
                Point2D p1 = new Point2D.Double(minX, yPx);
343
                Point2D p2 = new Point2D.Double(rView.getMaxX(), yPx);
344 5 jldominguez
345
                drawLine(p1.getX() - OFFSET_IN_PIXELS, p1.getY(), p1.getX() , p1.getY(), g, symbolForMargins);
346
                drawLine(p2.getX(), p2.getY(), p2.getX() + OFFSET_IN_PIXELS, p2.getY(), g, symbolForMargins);
347
348 274 cmartinez
                TextLayout textaux = new TextLayout(this.labelFormat.format(yMap), font, frc);
349 85 nbrodin
350 5 jldominguez
                double w = textaux.getBounds().getWidth();
351
                double h = textaux.getBounds().getHeight();
352
                g.setColor(textColor);
353 274 cmartinez
354
                double rotationX = p1.getX()-10-w/2.0;
355
                g.rotate(Math.toRadians(getHorizLabelRotation()), rotationX, p1.getY());
356 85 nbrodin
                textaux.draw(g,
357
                        (int) (p1.getX() - w - 10),
358 5 jldominguez
                    (int) (p1.getY() + h / 2));
359 274 cmartinez
                g.rotate(Math.toRadians(-getHorizLabelRotation()), rotationX, p1.getY());
360
361
                rotationX = p2.getX()+10+w/2.0;
362
                g.rotate(Math.toRadians(getHorizLabelRotation()), rotationX, p2.getY());
363 85 nbrodin
                textaux.draw(g,
364
                        (int) p2.getX() + 10,
365
                        (int) (p2.getY() + h / 2));
366 274 cmartinez
                g.rotate(Math.toRadians(-getHorizLabelRotation()), rotationX, p2.getY());
367 5 jldominguez
            }
368 267 cmartinez
            yMap = yMap + intervalMapY;
369
            yPx = yPx + intervalPxY;
370 5 jldominguez
        }
371
    }
372
373
    private void drawBox(Rectangle2D mapRectangle, Graphics2D g, ISymbol symbol){
374
        try {
375
            Curve curve = (Curve) GEOMETRY_MANAGER.create(TYPES.CURVE, SUBTYPES.GEOM2D);
376
            curve.addMoveToVertex(GEOMETRY_MANAGER.createPoint(mapRectangle.getMinX(), mapRectangle.getMinY(), SUBTYPES.GEOM2D));
377
            curve.addVertex(GEOMETRY_MANAGER.createPoint(mapRectangle.getMinX(), mapRectangle.getMaxY()-1, SUBTYPES.GEOM2D));
378
            curve.addVertex(GEOMETRY_MANAGER.createPoint(mapRectangle.getMaxX()-1, mapRectangle.getMaxY()-1, SUBTYPES.GEOM2D));
379
            curve.addVertex(GEOMETRY_MANAGER.createPoint(mapRectangle.getMaxX()-1, mapRectangle.getMinY(), SUBTYPES.GEOM2D));
380
            curve.addVertex(GEOMETRY_MANAGER.createPoint(mapRectangle.getMinX(), mapRectangle.getMinY(), SUBTYPES.GEOM2D));
381
            symbol.draw(g, null, curve, null, null);
382
        } catch (CreateGeometryException e) {
383
            LOG.error("Error drawing the box", e);
384
        }
385
    }
386
387
    private void drawPoint(Point2D point, Graphics2D g){
388
        try {
389
            Geometry geom =
390
                createPoint((int) point.getX(), (int) point.getY());
391
            symbolPoint.draw(g, emptyTransform, geom, null, null);
392
        }  catch (LocatorException e) {
393
            LOG.error("Error drawing the grid", e);
394
        } catch (CreateGeometryException e) {
395
            LOG.error("Error drawing the grid", e);
396
        }
397
    }
398
399
    private void drawLine(double startX, double startY, double endX, double endY, Graphics2D g, ISymbol symbolLine){
400
        try {
401
            Geometry geom =
402
                createLine((int) startX, (int)startY, (int) endX, (int) endY);
403
            symbolLine.draw(g, null, geom, null, null);
404
        } catch (LocatorException e) {
405
            LOG.error("Error drawing the grid", e);
406
        } catch (CreateGeometryException e) {
407
            LOG.error("Error drawing the grid", e);
408
        }
409
    }
410
411
    private void drawLine(Point2D startPoint, Point2D endPoint, Graphics2D g,  ISymbol symbolLine){
412
        drawLine(startPoint.getX(), startPoint.getY(), endPoint.getX(), endPoint.getY(), g, symbolLine);
413
    }
414
415
    private Geometry createLine(int i, int j, int k, int l)
416
        throws LocatorException, CreateGeometryException {
417
        Curve curve = (Curve)GEOMETRY_MANAGER.create(Geometry.TYPES.CURVE, SUBTYPES.GEOM2D);
418
        curve.addMoveToVertex(GEOMETRY_MANAGER.createPoint(i, j, SUBTYPES.GEOM2D));
419
        curve.addVertex(GEOMETRY_MANAGER.createPoint(k, l, SUBTYPES.GEOM2D));
420
        return curve;
421
    }
422
423
    private Geometry createPoint(int i, int j) throws LocatorException,
424
        CreateGeometryException {
425
        return GeometryLocator.getGeometryManager().createPoint(i, j,
426
            SUBTYPES.GEOM2D);
427
    }
428
429
    public String getNameFFrame() {
430
        return PluginServices.getText(this, "cuadricula") + num;
431
    }
432
433
    public String getName() {
434
        return PERSISTENCE_DEFINITION_NAME;
435
    }
436
437
    public void setFFrameDependence(IFFrame f) {
438
        super.setFFrameDependence(f);
439
        fframeViewDependence.refresh();
440
    }
441
442
    public void setIntervalX(double d) {
443
        intervalX = d;
444
    }
445
446
    public void setIntervalY(double d) {
447
        intervalY = d;
448
    }
449
450
    public double getIntervalX() {
451 274 cmartinez
            if (intervalX!=null) {
452
                    return intervalX;
453
            }
454
            intervalX = intervalY = getReasonableInterval();
455
            return intervalX;
456 5 jldominguez
    }
457 274 cmartinez
458
    private double getReasonableInterval() {
459
            if (fframeViewDependence!=null
460 289 cmartinez
                            && fframeViewDependence.getMapContext()!=null
461 274 cmartinez
                            && fframeViewDependence.getMapContext().getViewPort().getAdjustedEnvelope()!=null) {
462
                    Envelope env = fframeViewDependence.getMapContext().getViewPort().getAdjustedEnvelope();
463
                    return IntervalUtils.roundIntervalDivision(env.getLength(0), 5);
464
            }
465
            return defaultInterval;
466
    }
467
468 5 jldominguez
469
    public double getIntervalY() {
470 274 cmartinez
            if (intervalX!=null) {
471
                    return intervalX;
472
            }
473
            intervalX = intervalY = new Double(getReasonableInterval());
474
            return intervalY;
475 5 jldominguez
    }
476
477
    public void setTextColor(Color textcolor) {
478
        textColor = textcolor;
479
    }
480
481
    public void setIsLine(boolean b) {
482
        isLine = b;
483
    }
484
485
    public boolean isLine() {
486
        return isLine;
487
    }
488
489
    public Color getFontColor() {
490
        return textColor;
491
    }
492
493
    public int getSizeFont() {
494
        return sizeFont;
495
    }
496
497
    public void setSizeFont(int sizeFont) {
498
        this.sizeFont = sizeFont;
499
    }
500
501
    public Rectangle2D.Double getBoundingBox(AffineTransform at) {
502
        if ((fframeViewDependence == null) ||  (fframeViewDependence.getMapContext() == null)){
503
            return super.getBoundingBox(at);
504
        }
505
        Rectangle2D.Double r = fframeViewDependence.getBoundingBox(at);
506
        Envelope extent =
507
            fframeViewDependence.getMapContext().getViewPort()
508
                .getAdjustedEnvelope();
509 267 cmartinez
        if (extent==null) {
510
                //extent may be null, for instance if no layer has been loaded on the view
511
                return null;
512
        }
513 5 jldominguez
        double extentX = extent.getMaximum(0);
514
        double extentY = extent.getMaximum(1);
515
        int lengthX = String.valueOf((long) extentX).length();
516
        double myScale = at.getScaleX() * 0.0234;
517
        int scaledFontSize = (int) (myScale * sizeFont);
518
        int pixelsX = (int) (lengthX * scaledFontSize * 0.7);
519
        int lengthY = String.valueOf((long) extentY).length();
520
        int pixelsY = (lengthY * scaledFontSize);
521
        return new Rectangle2D.Double(r.getMinX() - pixelsX, r.getMinY() - pixelsY,
522
                r.getWidth() + pixelsX * 2, r.getHeight() + pixelsY * 2);
523
    }
524
525
    public Rectangle2D getMovieRect(int difx, int dify) {
526
        return this.getBoundingBox(null);
527
    }
528
529
    public void refreshDependence(IFFrame fant, IFFrame fnew) {
530
        if (fframeViewDependence.equals(fant)) {
531
            fframeViewDependence = (FFrameView) fnew;
532
            fframeViewDependence.refresh();
533
        }
534
    }
535
536
    public void drawHandlers(Graphics2D g) {
537
        g.setColor(Color.gray);
538
        super.drawHandlers(g);
539
        g.setColor(Color.black);
540
    }
541
542
    public Rectangle2D getLastMoveRect() {
543
        return getBoundBox();
544
    }
545
546
    public ISymbol getSymbolLine() {
547
        return symbolLine;
548
    }
549
550
    public void setSymbolLine(ISymbol symbolLine) {
551
        this.symbolLine = (ILineSymbol) symbolLine;
552
    }
553
554
    public ISymbol getSymbolPoint() {
555
        return symbolPoint;
556
    }
557
558
    public void setSymbolPoint(ISymbol symbolPoint) {
559
        this.symbolPoint = (IMarkerSymbol) symbolPoint;
560
    }
561
562
    public Font getFont() {
563
        return font;
564
    }
565
566
    public void setFont(Font m_font) {
567
        this.font = m_font;
568
    }
569
570
    public int getFontSize() {
571 274 cmartinez
        return font.getSize();
572 5 jldominguez
    }
573
574 274 cmartinez
    /**
575
     * Defines whether a number of divisions is used to calculate the
576
     * location of grid lines,
577
     * or a distance (intervalX and intervalY) is used to
578
     * calculate them.
579
     *
580
     * @param useNumDivisions One of {@link #FIXED_DISTANCE_MODE},
581
     * {@link #NUM_DIVISIONS_HORIZ_MODE} or {@link #NUM_DIVISIONS_HORIZ_MODE}.
582
     * @return
583
     */
584
    public void setUseNumDivisions(int mode) {
585
            this.useNumDivisions  = mode;
586
    }
587
588
    protected void updateIntervals() {
589
            if (fframeViewDependence.getMapContext()!=null
590
                            && fframeViewDependence.getMapContext().getViewPort()!=null) {
591
                    Envelope env = fframeViewDependence.getMapContext().getViewPort().getAdjustedEnvelope();
592
                    updateIntervals(env);
593
            }
594
    }
595
596
    protected void updateIntervals(Envelope env) {
597
            if (useNumDivisions==NUM_DIVISIONS_HORIZ_MODE) {
598
                    double division = IntervalUtils.roundIntervalDivision(env.getLength(0), numDivHoriz);
599
                    intervalX = division;
600
                    intervalY = division;
601
            }
602
            else if (useNumDivisions==NUM_DIVISIONS_VERT_MODE) {
603
                    double division = IntervalUtils.roundIntervalDivision(env.getLength(1), numDivVert);
604
                    intervalX = division;
605
                    intervalY = division;
606
            }
607
    }
608
609
    /**
610
     * Returns true if a number of divisions is used to calculate the
611
     * location of grid lines,
612
     * or false if a distance (intervalX and intervalY) is used to
613
     * calculate them.
614
     *
615
     * @return  One of {@link #FIXED_DISTANCE_MODE},
616
     * {@link #NUM_DIVISIONS_HORIZ_MODE} or {@link #NUM_DIVISIONS_HORIZ_MODE}.
617
     */
618
    public int getUseNumDivisions() {
619
            return this.useNumDivisions;
620
    }
621
622
    public void setNumDivisionsHoriz(int nx) {
623
            this.numDivHoriz = nx;
624
    }
625
626
    public void setNumDivisionsVert(int ny) {
627
            this.numDivVert = ny;
628
    }
629
630
    public int getNumDivisionHoriz() {
631
            return this.numDivHoriz;
632
    }
633
634
    public int getNumDivisionsVert() {
635
            return this.numDivVert;
636
    }
637
638
    public void setLabelFormat(DecimalFormat f) {
639
            this.labelFormat  = f;
640
    }
641
642
    public DecimalFormat  getLabelFormat() {
643
            return this.labelFormat;
644
    }
645
646
    /**
647
     * Sets the rotation of the labels corresponding to horizontal lines
648
     *
649
     * @param labelRotation Label rotation, measured in arc degrees
650
     */
651
    public void setHorizLabelRotation(double labelRotation) {
652
            this.horizLabelRotation  = labelRotation;
653
    }
654
655
    /**
656
     * Sets the rotation of the labels corresponding to vertical lines
657
     *
658
     * @param labelRotation Label rotation, measured in arc degrees
659
     */
660
    public void setVertLabelRotation(double labelRotation) {
661
            this.vertLabelRotation  = labelRotation;
662
    }
663
664
    /**
665
     * Gets the rotation of the labels corresponding to horizontal lines
666
     *
667
     * @param labelRotation Label rotation, measured in arc degrees
668
     */
669
    public double getHorizLabelRotation() {
670
            return this.horizLabelRotation;
671
    }
672
673
674
    /**
675
     * Gets the rotation of the labels corresponding to vertical lines
676
     *
677
     * @param labelRotation Label rotation, measured in arc degrees
678
     */
679
    public double getVertLabelRotation() {
680
            return this.vertLabelRotation;
681
    }
682
683 5 jldominguez
    public void print(Graphics2D g, AffineTransform at, Geometry shape,
684
        PrintAttributes properties) {
685
        draw(g, at, null, null);
686
    }
687
688
    public static void registerPersistent() {
689
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
690
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
691
            DynStruct definition =
692
                manager.addDefinition(FFrameGrid.class,
693
                    PERSISTENCE_DEFINITION_NAME,
694
                    "FFrameGrid persistence definition", null, null);
695
696
            definition
697
                .extend(manager
698
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
699
700
            definition.addDynFieldObject(SYMBOLLINE_FIELD)
701
                .setClassOfValue(ILineSymbol.class).setMandatory(true);
702
            definition.addDynFieldObject(SYMBOLPOINT_FIELD)
703
                .setClassOfValue(IMarkerSymbol.class).setMandatory(true);
704
            definition.addDynFieldObject(FONT_FIELD)
705
                .setClassOfValue(Font.class).setMandatory(true);
706
            definition.addDynFieldDouble(INTERVALX_FIELD).setMandatory(true);
707
            definition.addDynFieldDouble(INTERVALY_FIELD).setMandatory(true);
708
            definition.addDynFieldBoolean(ISLINE_FIELD).setMandatory(true);
709
            definition.addDynFieldInt(FONTSYZE_FIELD).setMandatory(true);
710
            definition.addDynFieldObject(TEXTCOLOR_FIELD)
711
                .setClassOfValue(Color.class).setMandatory(true);
712 274 cmartinez
            definition.addDynFieldInt(USE_NUM_DIVISIONS_FIELD).setMandatory(false);
713
            definition.addDynFieldInt(NUM_DIV_HORIZ_FIELD).setMandatory(false);
714
            definition.addDynFieldInt(NUM_DIV_VERT_FIELD).setMandatory(false);
715
            definition.addDynFieldObject(LABEL_FORMAT_FIELD)
716
            .setClassOfValue(DecimalFormat.class).setMandatory(false);
717
            definition.addDynFieldDouble(VERT_LABEL_ROTATION_FIELD).setMandatory(false);
718
            definition.addDynFieldDouble(HORIZ_LABEL_ROTATION_FIELD).setMandatory(false);
719 5 jldominguez
        }
720
    }
721
722
    @Override
723
    public void loadFromState(PersistentState state)
724
        throws PersistenceException {
725
        super.loadFromState(state);
726
        symbolLine = (ILineSymbol) state.get(SYMBOLLINE_FIELD);
727
        symbolPoint = (IMarkerSymbol) state.get(SYMBOLPOINT_FIELD);
728
        font = (Font) state.get(FONT_FIELD);
729
        intervalX = state.getDouble(INTERVALX_FIELD);
730
        intervalY = state.getDouble(INTERVALY_FIELD);
731
        isLine = state.getBoolean(ISLINE_FIELD);
732
        sizeFont = state.getInt(FONTSYZE_FIELD);
733
        textColor = (Color) state.get(TEXTCOLOR_FIELD);
734 274 cmartinez
        if (state.hasValue(USE_NUM_DIVISIONS_FIELD)) {
735
                useNumDivisions = state.getInt(USE_NUM_DIVISIONS_FIELD);
736
        }
737
        if (state.hasValue(NUM_DIV_HORIZ_FIELD)) {
738
                numDivHoriz = state.getInt(NUM_DIV_HORIZ_FIELD);
739
        }
740
        if (state.hasValue(NUM_DIV_VERT_FIELD)) {
741
                numDivVert = state.getInt(NUM_DIV_VERT_FIELD);
742
        }
743
        if (state.hasValue(LABEL_FORMAT_FIELD)) {
744
                labelFormat = (DecimalFormat) state.get(LABEL_FORMAT_FIELD);
745
        }
746
        if (state.hasValue(VERT_LABEL_ROTATION_FIELD)) {
747
                vertLabelRotation = state.getDouble(VERT_LABEL_ROTATION_FIELD);
748
        }
749
        if (state.hasValue(HORIZ_LABEL_ROTATION_FIELD)) {
750
                horizLabelRotation = state.getDouble(HORIZ_LABEL_ROTATION_FIELD);
751
        }
752 5 jldominguez
    }
753
754
    @Override
755
    public void saveToState(PersistentState state) throws PersistenceException {
756
        super.saveToState(state);
757
        state.set(SYMBOLLINE_FIELD, symbolLine);
758
        state.set(SYMBOLPOINT_FIELD, symbolPoint);
759
        state.set(FONT_FIELD, font);
760
        state.set(INTERVALX_FIELD, intervalX);
761
        state.set(INTERVALY_FIELD, intervalY);
762
        state.set(ISLINE_FIELD, isLine);
763
        state.set(FONTSYZE_FIELD, sizeFont);
764
        state.set(TEXTCOLOR_FIELD, textColor);
765 274 cmartinez
        state.set(USE_NUM_DIVISIONS_FIELD, useNumDivisions);
766
        state.set(NUM_DIV_HORIZ_FIELD, numDivHoriz);
767
        state.set(NUM_DIV_VERT_FIELD, numDivVert);
768
        state.set(LABEL_FORMAT_FIELD, labelFormat);
769
        state.set(VERT_LABEL_ROTATION_FIELD, vertLabelRotation);
770
        state.set(HORIZ_LABEL_ROTATION_FIELD, horizLabelRotation);
771
772 5 jldominguez
    }
773
}