Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameScaleBar.java @ 2984

History | View | Annotate | Download (36.6 KB)

1
/*
2
 * Created on 20-feb-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.gui.layout.fframes;
46

    
47
import com.iver.andami.PluginServices;
48

    
49
import com.iver.cit.gvsig.LayoutControls;
50
import com.iver.cit.gvsig.fmap.DriverException;
51
import com.iver.cit.gvsig.fmap.FMap;
52
import com.iver.cit.gvsig.gui.layout.Attributes;
53
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
54
import com.iver.cit.gvsig.gui.layout.Layout;
55

    
56
import com.iver.utiles.StringUtilities;
57
import com.iver.utiles.XMLEntity;
58

    
59
import java.awt.BasicStroke;
60
import java.awt.Color;
61
import java.awt.Font;
62
import java.awt.Graphics;
63
import java.awt.Graphics2D;
64
import java.awt.geom.AffineTransform;
65
import java.awt.geom.Point2D;
66
import java.awt.geom.Rectangle2D;
67
import java.awt.image.BufferedImage;
68

    
69
import java.util.ArrayList;
70

    
71

    
72
/**
73
 * FFrame para introducir una barra de escala en el Layout.
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class FFrameScaleBar extends FFrame implements IFFrameViewDependence{
78
    private static final int NUMERO = 0;
79
    private static final int BARRA1 = 1;
80
    private static final int BARRA2 = 2;
81
    private static final int BARRA3 = 3;
82
    private static final int BARRA4 = 4;
83
    private double DIFDOWN = 1.5;
84
    private double DIFL = 30;
85
    private double DIFR = 30;
86
    private double DIFUP = 10;
87
    private boolean m_bIntervalSet = false;
88
    private int m_style = NUMERO;
89
    private int m_units = 1; //Metros
90
    private int m_mapUnits = 1; //unidad de medida de la vista(Metros)
91
    private long m_interval = 1;
92
    private int m_numinterval = 3;
93
    private int m_numleft = 2;
94
    private double m_height = 0;
95
    private FFrameView fframeview = null;
96
    private double m_typeUnit = Attributes.CHANGE[1]; //METROS;
97
    private String m_nameUnit = null;
98
    private double m_numUnit = 0;
99
    private double m_dif = 1;
100
    private int m_hasleft = 0;
101
    private Font m_f = new Font("SansSerif", Font.PLAIN, 9);
102
    private Color barcolor = Color.black;
103
    private Color textcolor = Color.black;
104
    private boolean showNameUnits = true;
105
    private boolean showDescription = false;
106
    private boolean aboveName = false;
107
    private boolean aboveIntervals = true;
108
    private boolean aboveDescription = false;
109
    private String description = "";
110

    
111
    /**
112
     * Creates a new FFrameScaleBar object.
113
     */
114
    public FFrameScaleBar() {
115
    }
116

    
117
    /**
118
     * Inserta el FFrameView sobre el que obtener la escala gr?fica.
119
     *
120
     * @param f FFrameView sobre  el que obtener la escala.
121
     */
122
    public void setFFrameView(FFrameView f) {
123
        fframeview = f;
124
        setMapUnits(f.getMapUnits());
125
        setHeight(f.getBoundBox().height);
126
        description = "escala 1:" + String.valueOf(fframeview.getScale());
127
    }
128

    
129
    /**
130
     * Devuelve el FFrameView que se representa con la escala.
131
     *
132
     * @return FFrameView a representar.
133
     */
134
    public FFrameView getFFrameView() {
135
        return fframeview;
136
    }
137

    
138
    /**
139
     * Devuelve el FMap de la vista que se representa en la escala.
140
     *
141
     * @return FMap.
142
     */
143
    public FMap getFMap() {
144
        return fframeview.getFMap();
145
    }
146

    
147
    /**
148
     * Rellenar el estilo de barra de escala que se elige.
149
     *
150
     * @param s entero que representa el tipo de barra seleccionada.
151
     */
152
    public void setStyle(int s) {
153
        m_style = s;
154
    }
155

    
156
    /**
157
     * Devuelve el tipo de escala a dibujar.
158
     *
159
     * @return entero.
160
     */
161
    public int getStyle() {
162
        return m_style;
163
    }
164

    
165
    /**
166
     * Rellenar el tama?o de un intervalo.
167
     *
168
     * @param s tama?o de un intervalo.
169
     */
170
    public void setInterval(long s) {
171
        m_interval = s;
172

    
173
        if (m_numleft != 0) {
174
            m_hasleft = 1;
175
        }
176

    
177
        Rectangle2D.Double rect = getBoundBox();
178
        double difL = (rect.width / DIFL);
179
        double difR = (rect.width / DIFR);
180
        double n = (rect.width - difL - difR);
181
        m_numUnit = (m_interval * m_typeUnit) / getScaleView();
182
        m_numinterval = (int) (n / m_numUnit) - m_hasleft;
183
        m_dif = 1;
184
    }
185

    
186
    /**
187
     * Devuelve el valor del intervalo.
188
     *
189
     * @return long.
190
     */
191
    public long obtainInterval() {
192
        if (fframeview != null) {
193
            Rectangle2D.Double rect = getBoundBox();
194

    
195
            if (m_numleft != 0) {
196
                m_hasleft = 1;
197
            }
198

    
199
            double difL = (rect.width / DIFL);
200
            double difR = (rect.width / DIFR);
201
            double n = (rect.width - difL - difR);
202
            m_numUnit = n / (m_numinterval + m_hasleft);
203

    
204
            double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
205
            m_dif = getExact(scaleXunit);
206
            m_numUnit = m_numUnit * m_dif;
207
            m_interval = (long) (scaleXunit * m_dif);
208

    
209
            return m_interval;
210
        }
211

    
212
        return 0;
213
    }
214

    
215
    /**
216
     * Rellenar el n?mero de intervalos.
217
     *
218
     * @param s n?mero de intervalos.
219
     */
220
    public void setNumInterval(int s) {
221
        m_numinterval = s;
222

    
223
        if (m_numleft != 0) {
224
            m_hasleft = 1;
225
        }
226

    
227
        Rectangle2D.Double rect = getBoundBox();
228
        double difL = (rect.width / DIFL);
229
        double difR = (rect.width / DIFR);
230
        double n = (rect.width - difL - difR);
231
        m_numUnit = n / (m_numinterval + m_hasleft);
232

    
233
        double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
234
        m_dif = getExact(scaleXunit);
235
        m_numUnit = m_numUnit * m_dif;
236
        m_interval = (long) (scaleXunit * m_dif);
237
    }
238

    
239
    /**
240
     * Devuelve el n?mero de intervalos por encima del cero.
241
     *
242
     * @return entero.
243
     */
244
    public int getNumInterval() {
245
        return m_numinterval;
246
    }
247

    
248
    /**
249
     * Rellenar el n?mero de intervalos a la izquierda del cero.
250
     *
251
     * @param s n?mero de intervalos a la izquierda.
252
     */
253
    public void setNumLeft(int s) {
254
        m_numleft = s;
255
    }
256

    
257
    /**
258
     * Devuelve el n?mero de intervalos  en los que se quiere particionar el
259
     * intervalo  que se queda por debajo del cero.
260
     *
261
     * @return entero.
262
     */
263
    public int getNumLeft() {
264
        return m_numleft;
265
    }
266

    
267
    /**
268
     * Seleccionar ,a true, si se quiere o, a false, si no mantener los
269
     * intervalos.
270
     *
271
     * @param b boolean a true si se quiere mantener los intervalos.
272
     */
273
    public void setIntervalSet(boolean b) {
274
        m_bIntervalSet = b;
275
    }
276

    
277
    /**
278
     * Seleccionar la unidad de medida a representar en la barra de escala.
279
     *
280
     * @param s entero que representa la unidad de medida que se selecciona.
281
     */
282
    public void setUnits(int s) {
283
        m_units = s;
284
        m_typeUnit = Attributes.CHANGE[s];
285
        m_nameUnit = Attributes.NAMES[s];
286
    }
287

    
288
    /**
289
     * Devuelve un entero que representa el tipo de unidades que representamos.
290
     *
291
     * @return entero.
292
     */
293
    public int getUnits() {
294
        return m_units;
295
    }
296

    
297
    /**
298
     * Devuelve el long que representa el intervalo.
299
     *
300
     * @return Intervalo.
301
     */
302
    public long getInterval() {
303
        return m_interval;
304
    }
305

    
306
    /**
307
     * Rellenar el rect?ngulo de la vista sobre la que se obtiene la escala.
308
     *
309
     * @param d Rect?ngulo.
310
     */
311
    public void setHeight(double d) {
312
        m_height = d;
313
    }
314

    
315
    /**
316
     * Rellena la unidad de medida en la que est? la vista.
317
     *
318
     * @param i entero que representa la unidad de medida de la vista.
319
     */
320
    public void setMapUnits(int i) {
321
        m_mapUnits = i;
322
    }
323

    
324
    /**
325
     * Devuelve la escala de la vista seleccionada.
326
     *
327
     * @return la escala de la vista.
328
     */
329
    private long getScaleView() {
330
        return fframeview.getScale();
331
    }
332

    
333
    /**
334
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
335
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
336
     * de dibujar.
337
     *
338
     * @param g Graphics
339
     * @param at Transformada afin.
340
     * @param rv rect?ngulo sobre el que hacer un clip.
341
     * @param imgBase Image para acelerar el dibujado.
342
     */
343
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
344
        BufferedImage imgBase) {
345
        Rectangle2D.Double r = getBoundingBox(at);
346
        g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2),
347
            r.y + (r.height / 2));
348

    
349
        if (intersects(rv, r)) {
350
            if ((fframeview == null) || (fframeview.getFMap() == null)) {
351
                drawEmpty(g);
352
            } else {
353
                switch (m_style) {
354
                    case (NUMERO):
355

    
356
                        double scalex = r.getWidth() / (8);
357
                        g.setColor(textcolor);
358

    
359
                        if (m_f != null) {
360
                            m_f = new Font(m_f.getFontName(), m_f.getStyle(),
361
                                    (int) (scalex));
362
                            g.setFont(m_f);
363
                        }
364

    
365
                        g.drawString(" 1:" + getScaleView(), (int) r.x,
366
                            (int) (r.y + (r.height / 2)));
367

    
368
                        break;
369

    
370
                    case (BARRA1):
371
                    case (BARRA2):
372
                    case (BARRA3):
373
                    case (BARRA4):
374
                        drawBar(m_style, g, at);
375

    
376
                        break;
377
                }
378
            }
379
        }
380

    
381
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
382
            r.y + (r.height / 2));
383
    }
384

    
385
    /**
386
     * Rellena con el rect?ngulo que se pasa como par?metro el boundBox(en
387
     * cent?metros) del fframe del cual con una transformaci?n se podr?
388
     * calcular el BoundingBox (en pixels).
389
     *
390
     * @param r Rect?ngulo en cent?metros.
391
     */
392
    public void setBoundBox(Rectangle2D.Double r) {
393
        if ((m_numUnit < 1) || (fframeview == null) ||
394
                (fframeview.getFMap() == null)) {
395
            super.setBoundBox(r);
396

    
397
            return;
398
        }
399

    
400
        double difL = (r.width / DIFL);
401
        double difR = (r.width / DIFR);
402

    
403
        if (m_bIntervalSet) {
404
            m_numinterval = (int) (((r.width - (difL + difR)) * m_dif) / m_numUnit) -
405
                m_hasleft;
406
        }
407

    
408
        if (m_numinterval < 1) {
409
            m_numinterval = 1;
410
            r.width = ((m_numinterval + m_hasleft) * m_numUnit) + difL + difR;
411
        }
412

    
413
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
414
    }
415

    
416
    /**
417
     * Dibuja sobre el Graphics la escala gr?fica.
418
     *
419
     * @param type Tipo de barra.
420
     * @param g Graphics sobre el que dibujar.
421
     * @param at Matriz de transformaci?n.
422
     */
423
    private void drawBar(int type, Graphics2D g, AffineTransform at) {
424
        Rectangle2D.Double rect = getBoundBox();
425
        Rectangle2D.Double r = getBoundingBox(at);
426
        double numleft = m_numleft;
427
        initDistances();
428

    
429
        double difDown = (rect.height / DIFDOWN);
430
        double difUp = (rect.height / DIFUP);
431
        double difL = (rect.width / DIFL);
432
        double difR = (rect.width / DIFR);
433
        double n = (rect.width - difL - difR);
434

    
435
        setDescripcion("escala 1:" + String.valueOf(fframeview.getScale()));
436
        g.setStroke(new BasicStroke(0));
437

    
438
        if (!m_bIntervalSet) {
439
            m_numUnit = n / (m_numinterval + m_hasleft);
440

    
441
            double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
442
            m_dif = getExact(scaleXunit);
443
            m_numUnit = m_numUnit * m_dif;
444
            m_interval = (long) (scaleXunit * m_dif);
445
        }
446

    
447
        if (m_bIntervalSet) {
448
            m_numUnit = (m_interval * m_typeUnit) / (m_dif * getScaleView());
449
            m_numinterval = (int) (((rect.width - (difL + difR)) * m_dif) / m_numUnit) -
450
                m_hasleft;
451
        }
452

    
453
        if (m_numinterval < 1) {
454
            m_numinterval = 1;
455
            rect.width = ((m_numinterval + m_hasleft) * m_numUnit) + difL +
456
                difR;
457
        }
458

    
459
        double h = 0;
460

    
461
        if (type == BARRA1) {
462
            h = (rect.height - (difUp + difDown));
463
        } else if ((type == BARRA2) || (type == BARRA3)) {
464
            h = (rect.height - (difUp + difDown)) / 2;
465
        }
466

    
467
        //Dibujar el rect?ngulo que bordea todo.
468
        Rectangle2D.Double rectotal = (FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
469
                    difL), (rect.y + difUp),
470
                    m_numUnit * (m_hasleft + m_numinterval), h), at));
471
        g.setColor(Color.white);
472
        g.fillRect((int) rectotal.x, (int) rectotal.y, (int) (rectotal.width),
473
            (int) rectotal.height);
474
        g.setColor(barcolor);
475

    
476
        if (m_f != null) {
477
            m_f = new Font(m_f.getFontName(), m_f.getStyle(),
478
                    (int) (r.getHeight() / 4));
479
        } else {
480
            m_f = new Font("SansSerif", Font.PLAIN, (int) (r.getHeight() / 4));
481
        }
482

    
483
        g.setFont(m_f);
484

    
485
        //Derecha del cero
486
        for (int i = 0; i < m_numinterval; i++) {
487
            Rectangle2D.Double recder = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
488
                        (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
489
                        (rect.y + (difUp)), (m_numUnit),
490
                        (rect.height - (difUp + difDown)) / 2), at);
491
            Rectangle2D.Double recderB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
492
                        (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
493
                        (rect.y + difUp) +
494
                        ((rect.height - (difUp + difDown)) / 2), (m_numUnit),
495
                        ((rect.height - (difUp + difDown)) / 2)), at);
496

    
497
            //                        Correcci?n cuando la altura en pixels del rect?ngulo es impar.
498
            rectotal.y = recder.y;
499
            recderB.height = rectotal.height - recder.height + 0.5;
500

    
501
            /**
502
             * if (i == (m_numinterval - 1)) { if (recder.getMaxX() !=
503
             * rectotal.getMaxX()) { recder.width = (recder.width +
504
             * recder.getMaxX()) - rectotal.getMaxX(); } if (recderB.getMaxX()
505
             * != rectotal.getMaxX()) { recderB.width = (recderB.width +
506
             * recderB.getMaxX()) - rectotal.getMaxX(); }} if
507
             * (recderB.getMaxY() != rectotal.getMaxY()) { recderB.height =
508
             * (recderB.height + recderB.getMaxY()) - rectotal.getMaxY(); }
509
             */
510
            if ((m_numleft % 2) == 0) {
511
                if ((i % 2) == 0) {
512
                    if ((type == BARRA1) || (type == BARRA2)) {
513
                        fillRect(g, recder);
514
                    } else if (type == BARRA3) {
515
                        g.drawRect((int) recder.x, (int) recder.y,
516
                            (int) recder.width, (int) recder.height);
517
                    }
518
                } else if (type == BARRA1) {
519
                    fillRect(g, recderB);
520
                }
521
            } else {
522
                if ((i % 2) != 0) {
523
                    if ((type == BARRA1) || (type == BARRA2)) {
524
                        fillRect(g, recder);
525
                    } else if (type == BARRA3) {
526
                        g.drawRect((int) recderB.x, (int) recderB.y,
527
                            (int) recderB.width, (int) recderB.height);
528
                    }
529
                } else if (type == BARRA1) {
530
                    fillRect(g, recderB);
531
                }
532
            }
533

    
534
            long inter = (long) (m_interval * i);
535
            Long l = new Long(inter);
536

    
537
            int pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
538

    
539
            Point2D.Double p = null;
540

    
541
            if (isAboveIntervals()) {
542
                p = new Point2D.Double(recder.x - pos,
543
                        r.getMinY() + (r.getHeight() / DIFUP));
544
            } else {
545
                p = new Point2D.Double(recder.x - pos,
546
                        ((r.getHeight() / 4) + r.getMaxY()) -
547
                        (r.getHeight() / DIFDOWN));
548
            }
549

    
550
            drawInterval(g, inter, p);
551
        }
552

    
553
        //?ltimo n?mero a dibujar.
554
        long inter = (long) (m_interval * m_numinterval);
555
        Long l = new Long(inter);
556

    
557
        int pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
558

    
559
        Point2D.Double p = null;
560

    
561
        if (isAboveIntervals()) {
562
            p = new Point2D.Double(rectotal.getMaxX() - pos,
563
                    r.getMinY() + (r.getHeight() / DIFUP));
564
        } else {
565
            p = new Point2D.Double(rectotal.getMaxX() - pos,
566
                    ((r.getHeight() / 4) + r.getMaxY()) -
567
                    (r.getHeight() / DIFDOWN));
568
        }
569

    
570
        drawInterval(g, inter, p);
571

    
572
        //Izquierda del cero
573
        for (int i = 0; i < m_numleft; i++) {
574
            Rectangle2D.Double reciz = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
575
                        difL + ((m_numUnit / m_numleft) * i)),
576
                        (rect.y + difUp), (m_numUnit / numleft),
577
                        (rect.height - (difUp + difDown)) / 2), at);
578
            Rectangle2D.Double recizB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
579
                        difL + ((m_numUnit / m_numleft) * i)),
580
                        (rect.y + difUp) +
581
                        ((rect.height - (difUp + difDown)) / 2),
582
                        (m_numUnit / numleft),
583
                        ((rect.height - (difUp + difDown)) / 2)), at);
584

    
585
            //Correcci?n cuando la altura en pixels del rectangulo es impar.
586
            reciz.y = rectotal.y;
587
            recizB.height = rectotal.height - reciz.height + 0.5;
588

    
589
            if ((i % 2) == 0) {
590
                if ((type == BARRA1) || (type == BARRA2)) {
591
                    fillRect(g, reciz);
592
                } else if (type == BARRA3) {
593
                    g.drawRect((int) reciz.x, (int) reciz.y, (int) reciz.width,
594
                        (int) reciz.height);
595
                }
596
            } else if (type == BARRA1) {
597
                fillRect(g, recizB);
598
            }
599
        }
600

    
601
        if (m_numleft > 0) {
602
            inter = (long) (m_interval);
603
            l = new Long(inter);
604

    
605
            pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
606

    
607
            if (isAboveIntervals()) {
608
                p = new Point2D.Double(rectotal.x - pos,
609
                        r.getMinY() + (r.getHeight() / DIFUP));
610
            } else {
611
                p = new Point2D.Double(rectotal.x - pos,
612
                        ((r.getHeight() / 4) + r.getMaxY()) -
613
                        (r.getHeight() / DIFDOWN));
614
            }
615

    
616
            drawInterval(g, inter, p);
617
        }
618

    
619
        //En el caso de que se pida como n?mro de intervalos a la izquierda del 0, se reajusta el tama?o del rect?ngulo exterior de la escala gr?fica.
620
        if (m_numleft == 0) {
621
            Rectangle2D.Double recAux = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
622
                        difL + ((m_numUnit / 1) * 0)), (rect.y + difUp),
623
                        (m_numUnit / 1), (rect.height - (difUp + difDown)) / 2),
624
                    at);
625
            rectotal.x = rectotal.x + recAux.width;
626
            rectotal.width = rectotal.width - recAux.width;
627
        }
628

    
629
        //Se dibuja el rect?ngulo que bordea toda la escala gr?fica.
630
        g.drawRect((int) rectotal.x, (int) rectotal.y, (int) (rectotal.width),
631
            (int) rectotal.height);
632

    
633
        drawNameUnits(g, rect, at);
634
        drawDescription(g, rect, at);
635
    }
636

    
637
    /**
638
     * Inicializa las distancias de la barra de escala.
639
     */
640
    private void initDistances() {
641
        int numUp = 0;
642
        int numDown = 0;
643

    
644
        DIFDOWN = 30;
645
        DIFL = 40;
646
        DIFR = 40;
647
        DIFUP = 30;
648

    
649
        if (isAboveName()) {
650
            numUp++;
651
        } else {
652
            numDown++;
653
        }
654

    
655
        if (isAboveIntervals()) {
656
            numUp++;
657
        } else {
658
            numDown++;
659
        }
660

    
661
        if (isAboveDescription()) {
662
            numUp++;
663
        } else {
664
            numDown++;
665
        }
666

    
667
        if (numDown == 1) {
668
            DIFDOWN = 3;
669
        } else if (numDown == 2) {
670
            DIFDOWN = 2;
671
        } else if (numDown == 3) {
672
            DIFDOWN = 1.2;
673
        }
674

    
675
        if (numUp == 1) {
676
            DIFUP = 3;
677
        } else if (numUp == 2) {
678
            DIFUP = 2;
679
        } else if (numUp == 3) {
680
            DIFUP = 1.2;
681
        }
682
    }
683

    
684
    /**
685
     * Dibuja sobre el Graphics el nombre de la unidad de medida de la escala.
686
     *
687
     * @param g Graphics sobre el que se dibuja.
688
     * @param rec Rect?ngulo que ocupa la escala gr?fica.
689
     * @param at Matriz de transformaci?n.
690
     */
691
    private void drawNameUnits(Graphics g, Rectangle2D.Double rec,
692
        AffineTransform at) {
693
        //        Nombre de las unidades utilizadas.
694
        Point2D.Double p = null;
695

    
696
        if (isShowNameUnits()) {
697
            if (isAboveName()) {
698
                p = new Point2D.Double(rec.getCenterX() -
699
                        ((rec.getHeight() * (m_nameUnit.length() / 6)) / 4),
700
                        (rec.getMinY() + (rec.getHeight() / (DIFUP))) -
701
                        (rec.getHeight() / 2));
702
            } else {
703
                p = new Point2D.Double(rec.getCenterX() -
704
                        ((rec.getHeight() * (m_nameUnit.length() / 6)) / 4),
705
                        (rec.getHeight() / 1.4) +
706
                        (rec.getMaxY() - (rec.getHeight() / (DIFDOWN))));
707
            }
708

    
709
            p = FLayoutUtilities.fromSheetPoint(p, at);
710
            g.setColor(textcolor);
711
            g.drawString(m_nameUnit, (int) p.x, (int) p.y);
712
        }
713
    }
714

    
715
    /**
716
     * Dibuja sobre el Graphics la descripci?n que por defecto sera la escala.
717
     *
718
     * @param g Graphics sobre el que se dibuja.
719
     * @param rec Rect?ngulo que ocupa la escala gr?fica.
720
     * @param at Matriz de transformaci?n.
721
     */
722
    private void drawDescription(Graphics g, Rectangle2D.Double rec,
723
        AffineTransform at) {
724
        Point2D.Double p = null;
725

    
726
        if (isShowDescription()) {
727
            if (isAboveDescription()) {
728
                p = new Point2D.Double(rec.getCenterX() -
729
                        ((rec.getHeight() * (getDescripcion().length() / 6)) / 4),
730
                        (rec.getMinY() + (rec.getHeight() / (DIFUP))) -
731
                        (rec.getHeight() / 5));
732
            } else {
733
                p = new Point2D.Double(rec.getCenterX() -
734
                        ((rec.getHeight() * (getDescripcion().length() / 6)) / 4),
735
                        (rec.getHeight() / 2) +
736
                        (rec.getMaxY() - (rec.getHeight() / (DIFDOWN))));
737
            }
738

    
739
            p = FLayoutUtilities.fromSheetPoint(p, at);
740
            g.setColor(textcolor);
741
            g.drawString(getDescripcion(), (int) p.x, (int) p.y);
742
        }
743
    }
744

    
745
    /**
746
     * Rellena la fuente utilizada para dibujar los intervalos y la unidad de
747
     * medida utilizada.
748
     *
749
     * @param f fuente a utilizar.
750
     */
751
    public void setFont(Font f) {
752
        m_f = f;
753
    }
754

    
755
    /**
756
     * Devuelve la fuente con la que se est? dibujando sobre el graphics.
757
     *
758
     * @return fuente utilizada.
759
     */
760
    public Font getFont() {
761
        return new Font(m_f.getFontName(), m_f.getStyle(), 9);
762
    }
763

    
764
    /**
765
     * Devuelve si el intervalo es variable o si por el contrario es fijo.
766
     *
767
     * @return true si el intervalo es fijo.
768
     */
769
    public boolean isbIntervalSet() {
770
        return m_bIntervalSet;
771
    }
772

    
773
    /**
774
     * Especificar si el intervalo se debe mantener o es variable.
775
     *
776
     * @param b si se quiere mantener el intervalo especificado.
777
     */
778
    public void setbIntervalSet(boolean b) {
779
        m_bIntervalSet = b;
780
    }
781

    
782
    /**
783
     * Devuelve el porcentaje por el cual hay que multiplicar  el intervalo
784
     * para conseguir un intervalo redondeado,  de momento con una cifra
785
     * significativas(NUM=1).
786
     *
787
     * @param total intervalo.
788
     *
789
     * @return Porcentaje
790
     */
791
    private double getExact(double total) {
792
        int NUM = 1;
793
        double t = 0;
794
        double dif = 1;
795
        Double d = new Double(total);
796
        Long l = new Long(d.longValue());
797
        int num = l.toString().length();
798
        t = ((long) (total / Math.pow(10, num - NUM)) * Math.pow(10, num - NUM));
799
        dif = t / total;
800

    
801
        return dif;
802
    }
803

    
804
    /**
805
     * Rellena un rect?ngulo.
806
     *
807
     * @param g Graphics sobre el que dibujar.
808
     * @param r Rect?ngulo a rellenar.
809
     */
810
    private void fillRect(Graphics2D g, Rectangle2D.Double r) {
811
        g.fillRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
812
    }
813

    
814
    /**
815
     * Escribe sobre el Graphics y en la posici?n indicada el tama?o del
816
     * intervalo.
817
     *
818
     * @param g Graphics sobre el que dibujar.
819
     * @param inter Valor del intervalo.
820
     * @param p Punto donde dibujarlo.
821
     */
822
    private void drawInterval(Graphics2D g, long inter, Point2D.Double p) {
823
        Long l = new Long(inter);
824
        g.setColor(textcolor);
825
        g.drawString(l.toString(), (int) p.x, (int) p.y);
826
        g.setColor(barcolor);
827
    }
828

    
829
    /**
830
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
831
     */
832
    public XMLEntity getXMLEntity() {
833
        XMLEntity xml = new XMLEntity();
834
        xml.putProperty("className", this.getClass().getName());
835
        xml.putProperty("m_name", m_name);
836
        xml.putProperty("x", getBoundBox().x);
837
        xml.putProperty("y", getBoundBox().y);
838
        xml.putProperty("w", getBoundBox().width);
839
        xml.putProperty("h", getBoundBox().height);
840
        xml.putProperty("m_Selected", m_Selected);
841
        xml.putProperty("type", Layout.RECTANGLESCALEBAR);
842
        xml.putProperty("m_bIntervalSet", m_bIntervalSet);
843
        xml.putProperty("m_dif", m_dif);
844
        xml.putProperty("m_hasleft", m_hasleft);
845
        xml.putProperty("m_nameUnit", m_nameUnit);
846
        xml.putProperty("m_numUnit", m_numUnit);
847

    
848
        xml.putProperty("m_height", m_height);
849

    
850
        xml.putProperty("m_style", m_style);
851
        xml.putProperty("m_units", m_units);
852
        xml.putProperty("m_interval", m_interval);
853
        xml.putProperty("m_numinterval", m_numinterval);
854
        xml.putProperty("m_numleft", m_numleft);
855
        xml.putProperty("m_mapUnits", m_mapUnits);
856
        xml.putProperty("fontName", m_f.getFontName());
857
        xml.putProperty("fontStyle", m_f.getStyle());
858
        xml.putProperty("tag", getTag());
859
        xml.putProperty("m_rotation", getRotation());
860

    
861
        LayoutControls lc = (LayoutControls) PluginServices.getExtension(LayoutControls.class);
862
        Layout layout = lc.getLayout();
863
        ArrayList fframes = layout.getFFrames();
864

    
865
        for (int i = 0; i < fframes.size(); i++) {
866
            if (fframes.get(i) instanceof FFrameView) {
867
                if ((fframeview != null) &&
868
                        fframeview.getView().getName().equals(((FFrameView) fframes
869
                                                                   .get(i)).getView()
870
                                                                   .getName())) {
871
                    xml.putProperty("index", i);
872
                }
873
            }
874
        }
875

    
876
        xml.putProperty("barcolor", StringUtilities.color2String(barcolor));
877
        xml.putProperty("textcolor", StringUtilities.color2String(textcolor));
878
        xml.putProperty("showNameUnits", showNameUnits);
879
        xml.putProperty("showDescription", showDescription);
880
        xml.putProperty("aboveName", aboveName);
881
        xml.putProperty("aboveIntervals", aboveIntervals);
882
        xml.putProperty("aboveDescription", aboveDescription);
883
        xml.putProperty("description", description);
884

    
885
        return xml;
886
    }
887

    
888
    /**
889
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
890
     *      com.iver.cit.gvsig.project.Project)
891
     */
892
    public void setXMLEntity03(XMLEntity xml, Layout l) {
893
        if (xml.getIntProperty("m_Selected") != 0) {
894
            this.setSelected(true);
895
        } else {
896
            this.setSelected(false);
897
        }
898

    
899
        this.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
900
        this.m_dif = xml.getDoubleProperty("m_dif");
901
        this.m_hasleft = xml.getIntProperty("m_hasleft");
902
        this.m_nameUnit = xml.getStringProperty("m_nameUnit");
903
        this.m_numUnit = xml.getDoubleProperty("m_numUnit");
904

    
905
        this.m_height = xml.getDoubleProperty("m_height");
906

    
907
        this.m_style = xml.getIntProperty("m_style");
908
        this.m_interval = xml.getLongProperty("m_interval");
909
        this.m_numinterval = xml.getIntProperty("m_numinterval");
910
        this.m_numleft = xml.getIntProperty("m_numleft");
911
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
912
        this.m_f = new Font(xml.getStringProperty("fontName"),
913
                xml.getIntProperty("fontStyle"), 9);
914

    
915
        ArrayList fframes = l.getFFrames();
916
        fframeview = (FFrameView) fframes.get(xml.getIntProperty("index"));
917

    
918
        if (xml.contains("description")) { //Comprobar que es de la versi?n que cambia el di?logo.
919
            this.barcolor = StringUtilities.string2Color(xml.getStringProperty(
920
                        "barcolor"));
921
            this.textcolor = StringUtilities.string2Color(xml.getStringProperty(
922
                        "textcolor"));
923
            this.showNameUnits = xml.getBooleanProperty("showNameUnits");
924
            this.showDescription = xml.getBooleanProperty("showDescription");
925
            this.aboveName = xml.getBooleanProperty("aboveName");
926
            this.aboveIntervals = xml.getBooleanProperty("aboveIntervals");
927
            this.aboveDescription = xml.getBooleanProperty("aboveDescription");
928
            this.description = xml.getStringProperty("description");
929
        }
930
    }
931

    
932
    /**
933
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
934
     *      com.iver.cit.gvsig.project.Project)
935
     */
936
    public void setXMLEntity(XMLEntity xml, Layout l) {
937
        if (xml.getIntProperty("m_Selected") != 0) {
938
            this.setSelected(true);
939
        } else {
940
            this.setSelected(false);
941
        }
942

    
943
        this.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
944
        this.m_dif = xml.getDoubleProperty("m_dif");
945
        this.m_hasleft = xml.getIntProperty("m_hasleft");
946
        this.m_nameUnit = xml.getStringProperty("m_nameUnit");
947
        this.m_numUnit = xml.getDoubleProperty("m_numUnit");
948

    
949
        this.m_height = xml.getDoubleProperty("m_height");
950

    
951
        this.m_style = xml.getIntProperty("m_style");
952
        this.m_interval = xml.getLongProperty("m_interval");
953
        this.m_numinterval = xml.getIntProperty("m_numinterval");
954
        this.m_numleft = xml.getIntProperty("m_numleft");
955
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
956
        this.m_f = new Font(xml.getStringProperty("fontName"),
957
                xml.getIntProperty("fontStyle"), 9);
958
        setRotation(xml.getDoubleProperty("m_rotation"));
959

    
960
        ArrayList fframes = l.getFFrames();
961

    
962
        if (xml.contains("index")) {
963
            fframeview = (FFrameView) fframes.get(xml.getIntProperty("index"));
964
        }
965

    
966
        if (xml.contains("description")) { //Comprobar que es de la versi?n que cambia el di?logo.
967
            this.barcolor = StringUtilities.string2Color(xml.getStringProperty(
968
                        "barcolor"));
969
            this.textcolor = StringUtilities.string2Color(xml.getStringProperty(
970
                        "textcolor"));
971
            this.showNameUnits = xml.getBooleanProperty("showNameUnits");
972
            this.showDescription = xml.getBooleanProperty("showDescription");
973
            this.aboveName = xml.getBooleanProperty("aboveName");
974
            this.aboveIntervals = xml.getBooleanProperty("aboveIntervals");
975
            this.aboveDescription = xml.getBooleanProperty("aboveDescription");
976
            this.description = xml.getStringProperty("description");
977
        }
978
    }
979

    
980
    /**
981
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
982
     */
983
    public String getNameFFrame() {
984
        return PluginServices.getText(this, "escala") + num;
985
    }
986

    
987
    /**
988
     * Inserta el color de la escala gr?fica.
989
     *
990
     * @param color Color de la escala gr?fica.
991
     */
992
    public void setBarColor(Color color) {
993
        barcolor = color;
994
    }
995

    
996
    /**
997
     * Inserta el color del texto.
998
     *
999
     * @param color Color del texto.
1000
     */
1001
    public void setTextColor(Color color) {
1002
        textcolor = color;
1003
    }
1004

    
1005
    /**
1006
     * Devuelve el color de la escala gr?fica.
1007
     *
1008
     * @return Color de la escala gr?fica.
1009
     */
1010
    public Color getBarColor() {
1011
        return barcolor;
1012
    }
1013

    
1014
    /**
1015
     * Devuelve el color del texto.
1016
     *
1017
     * @return Color del texto.
1018
     */
1019
    public Color getTextColor() {
1020
        return textcolor;
1021
    }
1022

    
1023
    /**
1024
     * Devuelve true si se debe mostrar el nombre de las unidades de medida de
1025
     * la escala gr?fica.
1026
     *
1027
     * @return True si se muestra las unidades de medida.
1028
     */
1029
    public boolean isShowNameUnits() {
1030
        return showNameUnits;
1031
    }
1032

    
1033
    /**
1034
     * Inserta si se debe de mostrar el nombre de las unidades de medida o no.
1035
     *
1036
     * @param showNameUnits True si se muestra el nombre de las unidades de
1037
     *        medida.
1038
     */
1039
    public void setShowNameUnits(boolean showNameUnits) {
1040
        this.showNameUnits = showNameUnits;
1041
    }
1042

    
1043
    /**
1044
     * Devuelve true si se muestra la descripci?n a la parte de arriba de la
1045
     * escala gr?fica.
1046
     *
1047
     * @return True si se muestra arriba de la escala gr?fica.
1048
     */
1049
    public boolean isAboveDescription() {
1050
        return aboveDescription;
1051
    }
1052

    
1053
    /**
1054
     * Introduce true si se muestra arriba de la escala gr?fica la descripci?n.
1055
     *
1056
     * @param aboveDescription True si se muestra arriba la descripci?n.
1057
     */
1058
    public void setAboveDescription(boolean aboveDescription) {
1059
        this.aboveDescription = aboveDescription;
1060
    }
1061

    
1062
    /**
1063
     * Devuelve true si se muestran a la parte de arriba los valores de los
1064
     * intervalos.
1065
     *
1066
     * @return True si se muestran arriba de la escala gr?fica.
1067
     */
1068
    public boolean isAboveIntervals() {
1069
        return aboveIntervals;
1070
    }
1071

    
1072
    /**
1073
     * Inserta si se muestran los valores de los intervalos a la parte de
1074
     * arriba de la escala o debajo.
1075
     *
1076
     * @param aboveIntervals True si se muestran los valores de los intervalos
1077
     *        a la parte de arriba de la escala gr?fica.
1078
     */
1079
    public void setAboveIntervals(boolean aboveIntervals) {
1080
        this.aboveIntervals = aboveIntervals;
1081
    }
1082

    
1083
    /**
1084
     * Devuelve si se muestra a la parte de arriba de la escala gr?fica el
1085
     * nombre de las unidades de medida o debajo.
1086
     *
1087
     * @return True si se muestra a la parte de arriba de la escala gr?fica.
1088
     */
1089
    public boolean isAboveName() {
1090
        return aboveName;
1091
    }
1092

    
1093
    /**
1094
     * Inserta si el nombre se muestra a la parte de arriba de la escala
1095
     * gr?fica o a la parte de abajo.
1096
     *
1097
     * @param aboveName True si se muestra a la parte de arriba de la escala
1098
     *        gr?fica.
1099
     */
1100
    public void setAboveName(boolean aboveName) {
1101
        this.aboveName = aboveName;
1102
    }
1103

    
1104
    /**
1105
     * Devuelve si se debe mostrar la descripci?n o no.
1106
     *
1107
     * @return True si se muestra la descripci?n.
1108
     */
1109
    public boolean isShowDescription() {
1110
        return showDescription;
1111
    }
1112

    
1113
    /**
1114
     * Inserta si se muestra o no la descripci?n.
1115
     *
1116
     * @param showDescription True si se muestra la descripci?n.
1117
     */
1118
    public void setShowDescription(boolean showDescription) {
1119
        this.showDescription = showDescription;
1120
    }
1121

    
1122
    /**
1123
     * Devuelve la descripci?n de la escala.
1124
     *
1125
     * @return Descripci?n de la escala.
1126
     */
1127
    public String getDescripcion() {
1128
        return description;
1129
    }
1130

    
1131
    /**
1132
     * Inserta la descripci?n de la escala.
1133
     *
1134
     * @param descripcion Descripci?n de la escala.
1135
     */
1136
    public void setDescripcion(String descripcion) {
1137
        this.description = descripcion;
1138
    }
1139

    
1140
    /**
1141
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
1142
     *      java.awt.geom.AffineTransform)
1143
     */
1144
    public void print(Graphics2D g, AffineTransform at)
1145
        throws DriverException {
1146
        draw(g, at, null, null);
1147
    }
1148
}