Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v06 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameScaleBar.java @ 4811

History | View | Annotate | Download (38.4 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.fmap.DriverException;
50
import com.iver.cit.gvsig.fmap.FMap;
51
import com.iver.cit.gvsig.gui.layout.Attributes;
52
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
53
import com.iver.cit.gvsig.gui.layout.Layout;
54
import com.iver.cit.gvsig.gui.project.SaveException;
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.FontMetrics;
63
import java.awt.Graphics;
64
import java.awt.Graphics2D;
65
import java.awt.geom.AffineTransform;
66
import java.awt.geom.Point2D;
67
import java.awt.geom.Rectangle2D;
68
import java.awt.image.BufferedImage;
69

    
70
import java.text.NumberFormat;
71
import java.util.ArrayList;
72

    
73

    
74

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

    
116
    /**
117
     * Creates a new FFrameScaleBar object.
118
     */
119
    public FFrameScaleBar() {
120
    }
121

    
122
    /**
123
     * Inserta el FFrameView sobre el que obtener la escala gr?fica.
124
     *
125
     * @param f FFrameView sobre  el que obtener la escala.
126
     */
127
    public void setFFrameDependence(IFFrame f) {
128
        fframeview = (FFrameView) f;
129
        setMapUnits(fframeview.getMapUnits());
130
        setHeight(f.getBoundBox().height);
131
        description = "escala 1:" + String.valueOf(fframeview.getScale());
132
    }
133

    
134
    /**
135
     * Devuelve el FFrameView que se representa con la escala.
136
     *
137
     * @return FFrameView a representar.
138
     */
139
    public IFFrame getFFrameDependence() {
140
        return fframeview;
141
    }
142

    
143
    /**
144
     * Devuelve el FMap de la vista que se representa en la escala.
145
     *
146
     * @return FMap.
147
     */
148
    public FMap getFMap() {
149
        return fframeview.getFMap();
150
    }
151

    
152
    /**
153
     * Rellenar el estilo de barra de escala que se elige.
154
     *
155
     * @param s entero que representa el tipo de barra seleccionada.
156
     */
157
    public void setStyle(int s) {
158
        m_style = s;
159
    }
160

    
161
    /**
162
     * Devuelve el tipo de escala a dibujar.
163
     *
164
     * @return entero.
165
     */
166
    public int getStyle() {
167
        return m_style;
168
    }
169

    
170
    /**
171
     * Rellenar el tama?o de un intervalo.
172
     *
173
     * @param s tama?o de un intervalo.
174
     */
175
    public void setInterval(double s) {
176
        m_interval = s;
177

    
178
        if (m_numleft != 0) {
179
            m_hasleft = 1;
180
        }
181

    
182
        Rectangle2D.Double rect = getBoundBox();
183
        double difL = (rect.width / DIFL);
184
        double difR = (rect.width / DIFR);
185
        double n = (rect.width - difL - difR);
186
        m_numUnit = (m_interval * m_typeUnit) / (double)getScaleView();
187
        m_numinterval = (int) (n / m_numUnit) - m_hasleft;
188
        m_dif = 1;
189
    }
190

    
191
    /**
192
     * Devuelve el valor del intervalo.
193
     *
194
     * @return long.
195
     */
196
    public String obtainInterval() {
197
            if (fframeview != null) {
198
            Rectangle2D.Double rect = getBoundBox();
199

    
200
            if (m_numleft != 0) {
201
                m_hasleft = 1;
202
            }
203

    
204
            double difL = (rect.width / DIFL);
205
            double difR = (rect.width / DIFR);
206
            double n = (rect.width - difL - difR);
207
            m_numUnit = n / (m_numinterval + m_hasleft);
208

    
209
            double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
210
            m_dif = getExact(scaleXunit);
211
            m_numUnit = m_numUnit * m_dif;
212
            m_interval=scaleXunit;
213
            ///m_interval = (scaleXunit * m_dif);
214

    
215
            return format(m_interval);
216
        }
217

    
218
        return "0";
219
    }
220

    
221
    /**
222
     * Rellenar el n?mero de intervalos.
223
     *
224
     * @param s n?mero de intervalos.
225
     */
226
    public void setNumInterval(int s) {
227
        m_numinterval = s;
228
        
229
        if (m_numleft != 0) {
230
            m_hasleft = 1;
231
        }
232

    
233
        Rectangle2D.Double rect = getBoundBox();
234
        double difL = (rect.width / DIFL);
235
        double difR = (rect.width / DIFR);
236
        double n = (rect.width - difL - difR);
237
        m_numUnit = n / (m_numinterval + m_hasleft);
238

    
239
        double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
240
        m_dif = getExact(scaleXunit);
241
        m_numUnit = m_numUnit * m_dif;
242
        m_interval = (scaleXunit * m_dif);
243
    }
244

    
245
    /**
246
     * Devuelve el n?mero de intervalos por encima del cero.
247
     *
248
     * @return entero.
249
     */
250
    public int getNumInterval() {
251
        return m_numinterval;
252
    }
253

    
254
    /**
255
     * Rellenar el n?mero de intervalos a la izquierda del cero.
256
     *
257
     * @param s n?mero de intervalos a la izquierda.
258
     */
259
    public void setNumLeft(int s) {
260
        m_numleft = s;
261
    }
262

    
263
    /**
264
     * Devuelve el n?mero de intervalos  en los que se quiere particionar el
265
     * intervalo  que se queda por debajo del cero.
266
     *
267
     * @return entero.
268
     */
269
    public int getNumLeft() {
270
        return m_numleft;
271
    }
272

    
273
    /**
274
     * Seleccionar ,a true, si se quiere o, a false, si no mantener los
275
     * intervalos.
276
     *
277
     * @param b boolean a true si se quiere mantener los intervalos.
278
     */
279
    public void setIntervalSet(boolean b) {
280
        m_bIntervalSet = b;
281
    }
282

    
283
    /**
284
     * Seleccionar la unidad de medida a representar en la barra de escala.
285
     *
286
     * @param s entero que representa la unidad de medida que se selecciona.
287
     */
288
    public void setUnits(int s) {
289
        m_units = s;
290
        m_typeUnit = Attributes.CHANGE[s];
291
        m_nameUnit = Attributes.NAMES[s];
292
    }
293

    
294
    /**
295
     * Devuelve un entero que representa el tipo de unidades que representamos.
296
     *
297
     * @return entero.
298
     */
299
    public int getUnits() {
300
        return m_units;
301
    }
302

    
303
    /**
304
     * Devuelve el long que representa el intervalo.
305
     *
306
     * @return Intervalo.
307
     */
308
    public String getInterval() {
309
        return format(m_interval);
310
    }
311

    
312
    /**
313
     * Rellenar el rect?ngulo de la vista sobre la que se obtiene la escala.
314
     *
315
     * @param d Rect?ngulo.
316
     */
317
    public void setHeight(double d) {
318
        m_height = d;
319
    }
320

    
321
    /**
322
     * Rellena la unidad de medida en la que est? la vista.
323
     *
324
     * @param i entero que representa la unidad de medida de la vista.
325
     */
326
    public void setMapUnits(int i) {
327
        m_mapUnits = i;
328
    }
329

    
330
    /**
331
     * Devuelve la escala de la vista seleccionada.
332
     *
333
     * @return la escala de la vista.
334
     */
335
    private long getScaleView() {
336
            if (fframeview==null)return 1;
337
        return fframeview.getScale();
338
    }
339

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

    
356
        if (intersects(rv, r)) {
357
            if ((fframeview == null) || (fframeview.getFMap() == null)) {
358
                drawEmpty(g);
359
            } else {
360
                switch (m_style) {
361
                    case (NUMERO):
362
                            double scalex = r.getWidth() / (8);
363
                        if (scalex>r.getHeight()/(8)){
364
                                scalex=r.getHeight()/(2);
365
                        }
366
                            g.setColor(textcolor);
367

    
368
                        if (m_f != null) {
369
                            m_f = new Font(m_f.getFontName(), m_f.getStyle(),
370
                                    (int) (scalex));
371
                            g.setFont(m_f);
372
                        }
373
                        FontMetrics fm=g.getFontMetrics();
374
                        double d=r.getWidth();
375
                        String scale=" 1:" + getScaleView();
376
                       if (fm.stringWidth(String.valueOf(m_interval))>(d*0.8)){
377
                                    double dif=fm.stringWidth(String.valueOf(m_interval))/(d*0.8);
378
                                    m_f=new Font(m_f.getName(),m_f.getStyle(),(int)(m_f.getSize()/dif));
379
                                    g.setFont(m_f);
380
                            }
381
                        
382
                        g.drawString(scale, (int) r.x,
383
                            (int) (r.y + (r.height / 2)));
384

    
385
                        break;
386

    
387
                    case (BARRA1):
388
                    case (BARRA2):
389
                    case (BARRA3):
390
                    case (BARRA4):
391
                        drawBar(m_style, g, at);
392

    
393
                        break;
394
                }
395
            }
396
        }
397

    
398
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2),
399
            r.y + (r.height / 2));
400
    }
401

    
402
    /**
403
     * Rellena con el rect?ngulo que se pasa como par?metro el boundBox(en
404
     * cent?metros) del fframe del cual con una transformaci?n se podr?
405
     * calcular el BoundingBox (en pixels).
406
     *
407
     * @param r Rect?ngulo en cent?metros.
408
     */
409
    public void setBoundBox(Rectangle2D.Double r) {
410
        if ((m_numUnit < 1) || (fframeview == null) ||
411
                (fframeview.getFMap() == null)) {
412
            super.setBoundBox(r);
413

    
414
            return;
415
        }
416

    
417
        double difL = (r.width / DIFL);
418
        double difR = (r.width / DIFR);
419

    
420
        if (m_bIntervalSet) {
421
            m_numinterval = (int) (((r.width - (difL + difR)) * m_dif) / m_numUnit) -
422
                m_hasleft;
423
        }
424

    
425
        if (m_numinterval < 1) {
426
            m_numinterval = 1;
427
            r.width = ((m_numinterval + m_hasleft) * m_numUnit) + difL + difR;
428
        }
429

    
430
        getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
431
    }
432

    
433
    /**
434
     * Dibuja sobre el Graphics la escala gr?fica.
435
     *
436
     * @param type Tipo de barra.
437
     * @param g Graphics sobre el que dibujar.
438
     * @param at Matriz de transformaci?n.
439
     */
440
    private void drawBar(int type, Graphics2D g, AffineTransform at) {
441
        Rectangle2D.Double rect = getBoundBox();
442
        Rectangle2D.Double r = getBoundingBox(at);
443
        double numleft = m_numleft;
444
        initDistances();
445

    
446
        double difDown = (rect.height / DIFDOWN);
447
        double difUp = (rect.height / DIFUP);
448
        double difL = (rect.width / DIFL);
449
        double difR = (rect.width / DIFR);
450
        double n = (rect.width - difL - difR);
451
        //setDescripcion("escala 1:" + String.valueOf(fframeview.getScale()));
452
        g.setStroke(new BasicStroke(0));
453

    
454
        if (!m_bIntervalSet) {
455
            m_numUnit = n / (m_numinterval + m_hasleft);
456

    
457
            double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
458
            m_dif = getExact(scaleXunit);
459
            m_numUnit = m_numUnit * m_dif;
460
            m_interval = (scaleXunit * m_dif);
461
        }
462

    
463
        if (m_bIntervalSet) {
464
            m_numUnit = (m_interval * m_typeUnit) / (m_dif * getScaleView());
465
            m_numinterval = (int) (((rect.width - (difL + difR)) * m_dif) / m_numUnit) -
466
                m_hasleft;
467
        }
468

    
469
        if (m_numinterval < 1) {
470
            m_numinterval = 1;
471
            rect.width = ((m_numinterval + m_hasleft) * m_numUnit) + difL +
472
                difR;
473
        }
474

    
475
        double h = 0;
476

    
477
        if (type == BARRA1) {
478
            h = (rect.height - (difUp + difDown));
479
        } else if ((type == BARRA2) || (type == BARRA3)) {
480
            h = (rect.height - (difUp + difDown)) / 2;
481
        }
482

    
483
        //Dibujar el rect?ngulo que bordea todo.
484
        Rectangle2D.Double rectotal = (FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
485
                    difL), (rect.y + difUp),
486
                    m_numUnit * (m_hasleft + m_numinterval), h), at));
487
        g.setColor(Color.white);
488
        g.fillRect((int) rectotal.x, (int) rectotal.y, (int) (rectotal.width),
489
            (int) rectotal.height);
490
        g.setColor(barcolor);
491
        
492
        if (m_f != null) {
493
            m_f = new Font(m_f.getFontName(), m_f.getStyle(),
494
                    (int) (r.getHeight() / 4));
495
        } else {
496
            m_f = new Font("SansSerif", Font.PLAIN, (int) (r.getHeight() / 4));
497
        }
498
        g.setFont(m_f);
499
        
500
        FontMetrics fm=g.getFontMetrics();
501
        String formatInterval=format(m_interval);
502
        double d=rectotal.getWidth()/m_numinterval+m_hasleft;
503
        double difpos=((r.getHeight() / 4) * formatInterval.length()) / 4;
504
            if (fm.stringWidth(formatInterval)>(d*0.7)){
505
                    double dif=fm.stringWidth(formatInterval)/(d*0.7);
506
                    difpos=(d*0.7)/2;
507
                    m_f=new Font(m_f.getName(),m_f.getStyle(),(int)(m_f.getSize()/dif));
508
                    g.setFont(m_f);
509
            }
510
        
511
        //Derecha del cero
512
        for (int i = 0; i < m_numinterval; i++) {
513
            Rectangle2D.Double recder = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
514
                        (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
515
                        (rect.y + (difUp)), (m_numUnit),
516
                        (rect.height - (difUp + difDown)) / 2), at);
517
            Rectangle2D.Double recderB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
518
                        (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
519
                        (rect.y + difUp) +
520
                        ((rect.height - (difUp + difDown)) / 2), (m_numUnit),
521
                        ((rect.height - (difUp + difDown)) / 2)), at);
522

    
523
            //                        Correcci?n cuando la altura en pixels del rect?ngulo es impar.
524
            rectotal.y = recder.y;
525
            recderB.height = rectotal.height - recder.height + 0.5;
526
            /**
527
             * if (i == (m_numinterval - 1)) { if (recder.getMaxX() !=
528
             * rectotal.getMaxX()) { recder.width = (recder.width +
529
             * recder.getMaxX()) - rectotal.getMaxX(); } if (recderB.getMaxX()
530
             * != rectotal.getMaxX()) { recderB.width = (recderB.width +
531
             * recderB.getMaxX()) - rectotal.getMaxX(); }} if
532
             * (recderB.getMaxY() != rectotal.getMaxY()) { recderB.height =
533
             * (recderB.height + recderB.getMaxY()) - rectotal.getMaxY(); }
534
             */
535
            if ((m_numleft % 2) == 0) {
536
                if ((i % 2) == 0) {
537
                    if ((type == BARRA1) || (type == BARRA2)) {
538
                        fillRect(g, recder);
539
                    } else if (type == BARRA3) {
540
                        g.drawRect((int) recder.x, (int) recder.y,
541
                            (int) recder.width, (int) recder.height);
542
                    }
543
                } else if (type == BARRA1) {
544
                    fillRect(g, recderB);
545
                }
546
            } else {
547
                if ((i % 2) != 0) {
548
                    if ((type == BARRA1) || (type == BARRA2)) {
549
                        fillRect(g, recder);
550
                    } else if (type == BARRA3) {
551
                        g.drawRect((int) recderB.x, (int) recderB.y,
552
                            (int) recderB.width, (int) recderB.height);
553
                    }
554
                } else if (type == BARRA1) {
555
                    fillRect(g, recderB);
556
                }
557
            }
558
           
559
            String interString = format(m_interval * i);
560
            Point2D.Double p = null;
561

    
562
            if (isAboveIntervals()) {
563
                p = new Point2D.Double(recder.x - difpos,
564
                        r.getMinY() + (r.getHeight() / DIFUP));
565
            } else {
566
                p = new Point2D.Double(recder.x - difpos,
567
                        ((r.getHeight() / 4) + r.getMaxY()) -
568
                        (r.getHeight() / DIFDOWN));
569
            }
570
            //Para dibujar el 0 centrado en su sitio.
571
            if (i==0){
572
                    double dif0=recder.x-(fm.stringWidth(interString)/2);
573
                    p=new Point2D.Double(dif0,p.getY());
574
            }
575
            drawInterval(g, interString, p);
576
        }
577

    
578
        //?ltimo n?mero a dibujar.
579
        String interString = format(m_interval * m_numinterval);
580
      
581
        Point2D.Double p = null;
582

    
583
        if (isAboveIntervals()) {
584
            p = new Point2D.Double(rectotal.getMaxX() - difpos,
585
                    r.getMinY() + (r.getHeight() / DIFUP));
586
        } else {
587
            p = new Point2D.Double(rectotal.getMaxX() - difpos,
588
                    ((r.getHeight() / 4) + r.getMaxY()) -
589
                    (r.getHeight() / DIFDOWN));
590
        }
591

    
592
        drawInterval(g, interString, p);
593

    
594
        //Izquierda del cero
595
        for (int i = 0; i < m_numleft; i++) {
596
            Rectangle2D.Double reciz = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
597
                        difL + ((m_numUnit / m_numleft) * i)),
598
                        (rect.y + difUp), (m_numUnit / numleft),
599
                        (rect.height - (difUp + difDown)) / 2), at);
600
            Rectangle2D.Double recizB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
601
                        difL + ((m_numUnit / m_numleft) * i)),
602
                        (rect.y + difUp) +
603
                        ((rect.height - (difUp + difDown)) / 2),
604
                        (m_numUnit / numleft),
605
                        ((rect.height - (difUp + difDown)) / 2)), at);
606

    
607
            //Correcci?n cuando la altura en pixels del rectangulo es impar.
608
            reciz.y = rectotal.y;
609
            recizB.height = rectotal.height - reciz.height + 0.5;
610

    
611
            if ((i % 2) == 0) {
612
                if ((type == BARRA1) || (type == BARRA2)) {
613
                    fillRect(g, reciz);
614
                } else if (type == BARRA3) {
615
                    g.drawRect((int) reciz.x, (int) reciz.y, (int) reciz.width,
616
                        (int) reciz.height);
617
                }
618
            } else if (type == BARRA1) {
619
                fillRect(g, recizB);
620
            }
621
        }
622

    
623
        if (m_numleft > 0) {
624
            interString  = format(m_interval);
625
          
626
            if (isAboveIntervals()) {
627
                p = new Point2D.Double(rectotal.x - difpos,
628
                        r.getMinY() + (r.getHeight() / DIFUP));
629
            } else {
630
                p = new Point2D.Double(rectotal.x - difpos,
631
                        ((r.getHeight() / 4) + r.getMaxY()) -
632
                        (r.getHeight() / DIFDOWN));
633
            }
634

    
635
            drawInterval(g, interString, p);
636
        }
637

    
638
        //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.
639
        if (m_numleft == 0) {
640
            Rectangle2D.Double recAux = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
641
                        difL + ((m_numUnit / 1) * 0)), (rect.y + difUp),
642
                        (m_numUnit / 1), (rect.height - (difUp + difDown)) / 2),
643
                    at);
644
            rectotal.x = rectotal.x + recAux.width;
645
            rectotal.width = rectotal.width - recAux.width;
646
        }
647

    
648
        //Se dibuja el rect?ngulo que bordea toda la escala gr?fica.
649
        g.drawRect((int) rectotal.x, (int) rectotal.y, (int) (rectotal.width),
650
            (int) rectotal.height);
651

    
652
        drawNameUnits(g, r);
653
        drawDescription(g, r);
654
    }
655

    
656
    /**
657
     * Inicializa las distancias de la barra de escala.
658
     */
659
    private void initDistances() {
660
        int numUp = 0;
661
        int numDown = 0;
662

    
663
        DIFDOWN = 30;
664
        DIFL = 40;
665
        DIFR = 40;
666
        DIFUP = 30;
667

    
668
        if (isAboveName()) {
669
            numUp++;
670
        } else {
671
            numDown++;
672
        }
673

    
674
        if (isAboveIntervals()) {
675
            numUp++;
676
        } else {
677
            numDown++;
678
        }
679

    
680
        if (isAboveDescription()) {
681
            numUp++;
682
        } else {
683
            numDown++;
684
        }
685

    
686
        if (numDown == 1) {
687
            DIFDOWN = 3;
688
        } else if (numDown == 2) {
689
            DIFDOWN = 2;
690
        } else if (numDown == 3) {
691
            DIFDOWN = 1.2;
692
        }
693

    
694
        if (numUp == 1) {
695
            DIFUP = 3;
696
        } else if (numUp == 2) {
697
            DIFUP = 2;
698
        } else if (numUp == 3) {
699
            DIFUP = 1.2;
700
        }
701
    }
702

    
703
    /**
704
     * Dibuja sobre el Graphics el nombre de la unidad de medida de la escala.
705
     *
706
     * @param g Graphics sobre el que se dibuja.
707
     * @param rec Rect?ngulo que ocupa la escala gr?fica.
708
     * @param at Matriz de transformaci?n.
709
     */
710
    private void drawNameUnits(Graphics g, Rectangle2D.Double rec) {
711
        //        Nombre de las unidades utilizadas.
712
        Point2D.Double p = null;
713
        FontMetrics fm=g.getFontMetrics();
714
        if (isShowNameUnits()) {
715
            if (isAboveName()) {
716
                p = new Point2D.Double(rec.getCenterX() - (fm.stringWidth(m_nameUnit)/2),
717
                                (rec.getMinY() + (rec.getHeight() / 3)+(fm.getHeight()/2)));
718
            } else {
719
                p = new Point2D.Double(rec.getCenterX() - (fm.stringWidth(m_nameUnit)/2),
720
                        (rec.getMaxY() - (rec.getHeight() / 9)+(fm.getHeight()/2)));
721
            }
722
            g.setColor(textcolor);
723
            g.drawString(m_nameUnit, (int) p.x, (int) p.y);
724
        }
725
    }
726

    
727
    /**
728
     * Dibuja sobre el Graphics la descripci?n que por defecto sera la escala.
729
     *
730
     * @param g Graphics sobre el que se dibuja.
731
     * @param rec Rect?ngulo que ocupa la escala gr?fica.
732
     * @param at Matriz de transformaci?n.
733
     */
734
    private void drawDescription(Graphics g, Rectangle2D.Double rec) {
735
        Point2D.Double p = null;
736
        FontMetrics fm=g.getFontMetrics();
737
        if (isShowDescription()) {
738
            if (isAboveDescription()) {
739
                p = new Point2D.Double(rec.getCenterX() - (fm.stringWidth(getDescripcion())/2),
740
                                (rec.getMinY() + (rec.getHeight() / 9)+(fm.getHeight()/2)));
741
            } else {
742
                p = new Point2D.Double(rec.getCenterX() - (fm.stringWidth(getDescripcion())/2),
743
                                (rec.getMaxY() - (rec.getHeight() / 3)+(fm.getHeight()/2)));
744
            }
745
            g.setColor(textcolor);
746
            g.drawString(getDescripcion(), (int) p.x, (int) p.y);
747
        }
748
    }
749

    
750
    /**
751
     * Rellena la fuente utilizada para dibujar los intervalos y la unidad de
752
     * medida utilizada.
753
     *
754
     * @param f fuente a utilizar.
755
     */
756
    public void setFont(Font f) {
757
        m_f = f;
758
    }
759

    
760
    /**
761
     * Devuelve la fuente con la que se est? dibujando sobre el graphics.
762
     *
763
     * @return fuente utilizada.
764
     */
765
    public Font getFont() {
766
        return new Font(m_f.getFontName(), m_f.getStyle(), 9);
767
    }
768

    
769
    /**
770
     * Devuelve si el intervalo es variable o si por el contrario es fijo.
771
     *
772
     * @return true si el intervalo es fijo.
773
     */
774
    public boolean isbIntervalSet() {
775
        return m_bIntervalSet;
776
    }
777

    
778
    /**
779
     * Especificar si el intervalo se debe mantener o es variable.
780
     *
781
     * @param b si se quiere mantener el intervalo especificado.
782
     */
783
    public void setbIntervalSet(boolean b) {
784
        m_bIntervalSet = b;
785
    }
786

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

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

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

    
835
    /**
836
     * @throws SaveException 
837
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
838
     */
839
    public XMLEntity getXMLEntity() throws SaveException {
840
        XMLEntity xml = new XMLEntity();
841
        try{
842
        xml.putProperty("className", this.getClass().getName());
843
        xml.putProperty("m_name", m_name);
844
        xml.putProperty("x", getBoundBox().x);
845
        xml.putProperty("y", getBoundBox().y);
846
        xml.putProperty("w", getBoundBox().width);
847
        xml.putProperty("h", getBoundBox().height);
848
        xml.putProperty("m_Selected", m_Selected);
849
        xml.putProperty("type", Layout.RECTANGLESCALEBAR);
850
        xml.putProperty("m_bIntervalSet", m_bIntervalSet);
851
        xml.putProperty("m_dif", m_dif);
852
        xml.putProperty("m_hasleft", m_hasleft);
853
        xml.putProperty("m_nameUnit", m_nameUnit);
854
        xml.putProperty("m_numUnit", m_numUnit);
855

    
856
        xml.putProperty("m_height", m_height);
857

    
858
        xml.putProperty("m_style", m_style);
859
        xml.putProperty("m_units", m_units);
860
        xml.putProperty("m_interval", m_interval);
861
        xml.putProperty("m_numinterval", m_numinterval);
862
        xml.putProperty("m_numleft", m_numleft);
863
        xml.putProperty("m_mapUnits", m_mapUnits);
864
        xml.putProperty("fontName", m_f.getFontName());
865
        xml.putProperty("fontStyle", m_f.getStyle());
866
        xml.putProperty("tag", getTag());
867
        xml.putProperty("m_rotation", getRotation());
868
        xml.putProperty("numDec",numDec);
869
        xml.putProperty("m_units",m_units);
870
        if (fframeview != null){
871
        Layout layout = fframeview.getLayout();
872
        IFFrame[] fframes = layout.getAllFFrames();
873

    
874
        for (int i = 0; i < fframes.length; i++) {
875
            if (fframeview.getName().equals(fframes[i].getName())) {
876
                xml.putProperty("index", i);
877
            }
878
        }
879
        }
880
        xml.putProperty("barcolor", StringUtilities.color2String(barcolor));
881
        xml.putProperty("textcolor", StringUtilities.color2String(textcolor));
882
        xml.putProperty("showNameUnits", showNameUnits);
883
        xml.putProperty("showDescription", showDescription);
884
        xml.putProperty("aboveName", aboveName);
885
        xml.putProperty("aboveIntervals", aboveIntervals);
886
        xml.putProperty("aboveDescription", aboveDescription);
887
        xml.putProperty("description", description);
888
        }catch (Exception e) {
889
                        throw new SaveException(e,this.getClass().getName());
890
                }
891
        return xml;
892
    }
893

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

    
905
        this.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
906
        this.m_dif = xml.getDoubleProperty("m_dif");
907
        this.m_hasleft = xml.getIntProperty("m_hasleft");
908
        this.m_nameUnit = xml.getStringProperty("m_nameUnit");
909
        this.m_numUnit = xml.getDoubleProperty("m_numUnit");
910

    
911
        this.m_height = xml.getDoubleProperty("m_height");
912

    
913
        this.m_style = xml.getIntProperty("m_style");
914
        this.m_interval = xml.getLongProperty("m_interval");
915
        this.m_numinterval = xml.getIntProperty("m_numinterval");
916
        this.m_numleft = xml.getIntProperty("m_numleft");
917
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
918
        this.m_f = new Font(xml.getStringProperty("fontName"),
919
                xml.getIntProperty("fontStyle"), 9);
920

    
921
        ArrayList fframes = l.getFFrames();
922
        fframeview = (FFrameView) fframes.get(xml.getIntProperty("index"));
923

    
924
        if (xml.contains("description")) { //Comprobar que es de la versi?n que cambia el di?logo.
925
            this.barcolor = StringUtilities.string2Color(xml.getStringProperty(
926
                        "barcolor"));
927
            this.textcolor = StringUtilities.string2Color(xml.getStringProperty(
928
                        "textcolor"));
929
            this.showNameUnits = xml.getBooleanProperty("showNameUnits");
930
            this.showDescription = xml.getBooleanProperty("showDescription");
931
            this.aboveName = xml.getBooleanProperty("aboveName");
932
            this.aboveIntervals = xml.getBooleanProperty("aboveIntervals");
933
            this.aboveDescription = xml.getBooleanProperty("aboveDescription");
934
            this.description = xml.getStringProperty("description");
935
        }
936
    }
937

    
938
    /**
939
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
940
     *      com.iver.cit.gvsig.project.Project)
941
     */
942
    public void setXMLEntity(XMLEntity xml) {
943
        if (xml.getIntProperty("m_Selected") != 0) {
944
            this.setSelected(true);
945
        } else {
946
            this.setSelected(false);
947
        }
948

    
949
        this.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
950
        this.m_dif = xml.getDoubleProperty("m_dif");
951
        this.m_hasleft = xml.getIntProperty("m_hasleft");
952
        this.m_nameUnit = xml.getStringProperty("m_nameUnit");
953
        this.m_numUnit = xml.getDoubleProperty("m_numUnit");
954

    
955
        this.m_height = xml.getDoubleProperty("m_height");
956

    
957
        this.m_style = xml.getIntProperty("m_style");
958
        this.m_interval = xml.getDoubleProperty("m_interval");
959
        this.m_numinterval = xml.getIntProperty("m_numinterval");
960
        this.m_numleft = xml.getIntProperty("m_numleft");
961
        this.m_mapUnits = xml.getIntProperty("m_mapUnits");
962
        this.m_f = new Font(xml.getStringProperty("fontName"),
963
                xml.getIntProperty("fontStyle"), 9);
964
        setRotation(xml.getDoubleProperty("m_rotation"));
965
        if (xml.contains("m_units")){//Comprobaci?n por versi?n 0.5
966
                setUnits(xml.getIntProperty("m_units"));
967
        }
968
        if (xml.contains("index")) {
969
            dependenceIndex = xml.getIntProperty("index");
970
        }
971

    
972
        if (xml.contains("description")) { //Comprobar que es de la versi?n que cambia el di?logo.
973
            this.barcolor = StringUtilities.string2Color(xml.getStringProperty(
974
                        "barcolor"));
975
            this.textcolor = StringUtilities.string2Color(xml.getStringProperty(
976
                        "textcolor"));
977
            this.showNameUnits = xml.getBooleanProperty("showNameUnits");
978
            this.showDescription = xml.getBooleanProperty("showDescription");
979
            this.aboveName = xml.getBooleanProperty("aboveName");
980
            this.aboveIntervals = xml.getBooleanProperty("aboveIntervals");
981
            this.aboveDescription = xml.getBooleanProperty("aboveDescription");
982
            this.description = xml.getStringProperty("description");
983
        }
984
        if (xml.contains("numDec")){
985
                setNumDec(xml.getIntProperty("numDec"));
986
        }
987
    }
988

    
989
    /**
990
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
991
     */
992
    public String getNameFFrame() {
993
        return PluginServices.getText(this, "escala") + num;
994
    }
995

    
996
    /**
997
     * Inserta el color de la escala gr?fica.
998
     *
999
     * @param color Color de la escala gr?fica.
1000
     */
1001
    public void setBarColor(Color color) {
1002
        barcolor = color;
1003
    }
1004

    
1005
    /**
1006
     * Inserta el color del texto.
1007
     *
1008
     * @param color Color del texto.
1009
     */
1010
    public void setTextColor(Color color) {
1011
        textcolor = color;
1012
    }
1013

    
1014
    /**
1015
     * Devuelve el color de la escala gr?fica.
1016
     *
1017
     * @return Color de la escala gr?fica.
1018
     */
1019
    public Color getBarColor() {
1020
        return barcolor;
1021
    }
1022

    
1023
    /**
1024
     * Devuelve el color del texto.
1025
     *
1026
     * @return Color del texto.
1027
     */
1028
    public Color getTextColor() {
1029
        return textcolor;
1030
    }
1031

    
1032
    /**
1033
     * Devuelve true si se debe mostrar el nombre de las unidades de medida de
1034
     * la escala gr?fica.
1035
     *
1036
     * @return True si se muestra las unidades de medida.
1037
     */
1038
    public boolean isShowNameUnits() {
1039
        return showNameUnits;
1040
    }
1041

    
1042
    /**
1043
     * Inserta si se debe de mostrar el nombre de las unidades de medida o no.
1044
     *
1045
     * @param showNameUnits True si se muestra el nombre de las unidades de
1046
     *        medida.
1047
     */
1048
    public void setShowNameUnits(boolean showNameUnits) {
1049
        this.showNameUnits = showNameUnits;
1050
    }
1051

    
1052
    /**
1053
     * Devuelve true si se muestra la descripci?n a la parte de arriba de la
1054
     * escala gr?fica.
1055
     *
1056
     * @return True si se muestra arriba de la escala gr?fica.
1057
     */
1058
    public boolean isAboveDescription() {
1059
        return aboveDescription;
1060
    }
1061

    
1062
    /**
1063
     * Introduce true si se muestra arriba de la escala gr?fica la descripci?n.
1064
     *
1065
     * @param aboveDescription True si se muestra arriba la descripci?n.
1066
     */
1067
    public void setAboveDescription(boolean aboveDescription) {
1068
        this.aboveDescription = aboveDescription;
1069
    }
1070

    
1071
    /**
1072
     * Devuelve true si se muestran a la parte de arriba los valores de los
1073
     * intervalos.
1074
     *
1075
     * @return True si se muestran arriba de la escala gr?fica.
1076
     */
1077
    public boolean isAboveIntervals() {
1078
        return aboveIntervals;
1079
    }
1080

    
1081
    /**
1082
     * Inserta si se muestran los valores de los intervalos a la parte de
1083
     * arriba de la escala o debajo.
1084
     *
1085
     * @param aboveIntervals True si se muestran los valores de los intervalos
1086
     *        a la parte de arriba de la escala gr?fica.
1087
     */
1088
    public void setAboveIntervals(boolean aboveIntervals) {
1089
        this.aboveIntervals = aboveIntervals;
1090
    }
1091

    
1092
    /**
1093
     * Devuelve si se muestra a la parte de arriba de la escala gr?fica el
1094
     * nombre de las unidades de medida o debajo.
1095
     *
1096
     * @return True si se muestra a la parte de arriba de la escala gr?fica.
1097
     */
1098
    public boolean isAboveName() {
1099
        return aboveName;
1100
    }
1101

    
1102
    /**
1103
     * Inserta si el nombre se muestra a la parte de arriba de la escala
1104
     * gr?fica o a la parte de abajo.
1105
     *
1106
     * @param aboveName True si se muestra a la parte de arriba de la escala
1107
     *        gr?fica.
1108
     */
1109
    public void setAboveName(boolean aboveName) {
1110
        this.aboveName = aboveName;
1111
    }
1112

    
1113
    /**
1114
     * Devuelve si se debe mostrar la descripci?n o no.
1115
     *
1116
     * @return True si se muestra la descripci?n.
1117
     */
1118
    public boolean isShowDescription() {
1119
        return showDescription;
1120
    }
1121

    
1122
    /**
1123
     * Inserta si se muestra o no la descripci?n.
1124
     *
1125
     * @param showDescription True si se muestra la descripci?n.
1126
     */
1127
    public void setShowDescription(boolean showDescription) {
1128
        this.showDescription = showDescription;
1129
    }
1130

    
1131
    /**
1132
     * Devuelve la descripci?n de la escala.
1133
     *
1134
     * @return Descripci?n de la escala.
1135
     */
1136
    public String getDescripcion() {
1137
        return description;
1138
    }
1139

    
1140
    /**
1141
     * Inserta la descripci?n de la escala.
1142
     *
1143
     * @param descripcion Descripci?n de la escala.
1144
     */
1145
    public void setDescripcion(String descripcion) {
1146
        this.description = descripcion;
1147
    }
1148

    
1149
    /**
1150
     * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
1151
     *      java.awt.geom.AffineTransform)
1152
     */
1153
    public void print(Graphics2D g, AffineTransform at)
1154
        throws DriverException {
1155
        draw(g, at, null, null);
1156
    }
1157

    
1158
    /**
1159
     * Update the dependences that have this FFrame with the other FFrame.
1160
     *
1161
     * @param fframes Other FFrames.
1162
     */
1163
    public void initDependence(IFFrame[] fframes) {
1164
            if (dependenceIndex!=-1 && fframes[dependenceIndex] instanceof FFrameView)
1165
        fframeview = (FFrameView) fframes[dependenceIndex];
1166
    }
1167
    public String format(double d) {
1168
            NumberFormat nf = NumberFormat.getInstance();
1169
            if ((d % (long)d )!=0){
1170
                    nf.setMaximumFractionDigits(getNumDec());
1171
            }else{
1172
                    nf.setMaximumFractionDigits(0);
1173
            }
1174
                //String s = String.valueOf(nf.format(d));
1175
                //s=s.replace('.','*');
1176
                //s = s.replace(',', '.');
1177
                //s=s.replace('*',',');
1178

    
1179
                return nf.format(d);//(Double.valueOf(s).doubleValue());
1180
        }
1181

    
1182
        public int getNumDec() {
1183
                return numDec;
1184
        }
1185

    
1186
        public void setNumDec(int numDec) {
1187
                this.numDec = numDec;
1188
        }
1189

    
1190
        public void initialize() {
1191
                // TODO Auto-generated method stub
1192
                
1193
        }
1194
}