Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameScaleBar.java @ 777

History | View | Annotate | Download (29.6 KB)

1
/*
2
 * Created on 20-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.gui.layout.fframes;
8

    
9
import java.awt.BasicStroke;
10
import java.awt.Color;
11
import java.awt.Font;
12
import java.awt.Graphics;
13
import java.awt.Graphics2D;
14
import java.awt.geom.AffineTransform;
15
import java.awt.geom.Point2D;
16
import java.awt.geom.Rectangle2D;
17
import java.awt.image.BufferedImage;
18
import java.util.ArrayList;
19

    
20
import org.apache.log4j.Logger;
21

    
22
import com.iver.andami.PluginServices;
23
import com.iver.cit.gvsig.LayoutControls;
24
import com.iver.cit.gvsig.fmap.DriverException;
25
import com.iver.cit.gvsig.fmap.FMap;
26
import com.iver.cit.gvsig.gui.layout.Attributes;
27
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
28
import com.iver.cit.gvsig.gui.layout.Layout;
29
import com.iver.cit.gvsig.project.castor.ProjectView;
30
import com.iver.utiles.StringUtilities;
31
import com.iver.utiles.XMLEntity;
32

    
33

    
34
/**
35
 * FFrame para introducir una barra de escala en el Layout.
36
 *
37
 * @author Vicente Caballero Navarro
38
 */
39
public class FFrameScaleBar extends FFrame {
40
        private static Logger logger = Logger.getLogger(FFrameView.class.getName());
41
        private static final int NUMERO = 0;
42
        private static final int BARRA1 = 1;
43
        private static final int BARRA2 = 2;
44
        private static final int BARRA3 = 3;
45
        private static final int BARRA4 = 4;
46
        private double DIFDOWN = 1.5;
47
        private double DIFL = 30;
48
        private double DIFR = 30;
49
        private double DIFUP = 10;
50
        private boolean m_bIntervalSet = false;
51
        private int m_style = NUMERO;
52
        private int m_units = 1; //Metros
53
        private int m_mapUnits = 1; //unidad de medida de la vista(Metros)
54
        private long m_interval = 1;
55
        private int m_numinterval = 3;
56
        private int m_numleft = 2;
57
        private double m_height = 0;
58

    
59
        //private FMap m_fmap = null;
60
        private FFrameView fframeview = null;
61
        private double m_typeUnit = Attributes.CHANGE[1]; //METROS;
62
        private String m_nameUnit = null;
63
        private double m_numUnit = 0;
64
        private double m_dif = 1;
65
        private int m_hasleft = 0;
66
        private Font m_f = new Font("SansSerif", Font.PLAIN, 9);
67
        private ProjectView view = null;
68
        private Color barcolor = Color.black;
69
        private Color textcolor = Color.black;
70
        private boolean showNameUnits = true;
71
        private boolean showDescription = false;
72
        private boolean aboveName = false;
73
        private boolean aboveIntervals = true;
74
        private boolean aboveDescription = false;
75
        private String description = "";
76

    
77
        /**
78
         * Creates a new FFrameScaleBar object.
79
         */
80
        public FFrameScaleBar() {
81
        }
82

    
83
        /**
84
         * DOCUMENT ME!
85
         *
86
         * @param f DOCUMENT ME!
87
         */
88
        public void setFFrameView(FFrameView f) {
89
                fframeview = f;
90
                view = f.getView();
91
                setMapUnits(f.getMapUnits());
92

    
93
                //setFMap(f.getFMap());
94
                setHeight(f.getBoundBox().height);
95
                description = "escala 1:" + String.valueOf(fframeview.getScale());
96

    
97
                /*if (f.getTypeScale()!=0){
98
                   isFixed=true;
99
                   }else{
100
                           isFixed=false;
101
                   }
102
                 */
103
        }
104

    
105
        /**
106
         * Devuelve el FFrameView que se representa con la escala.
107
         *
108
         * @return FFrameView a representar.
109
         */
110
        public FFrameView getFFrameView() {
111
                return fframeview;
112
        }
113

    
114
        /**
115
         * Pone el FMap de la vista que representa a la escala.
116
         *
117
         * @return DOCUMENT ME!
118
         */
119

    
120
        /*  public void setFMap(FMap map) {
121
           m_fmap = map;
122
           }
123
         */
124

    
125
        /**
126
         * Devuelve el FMap de la vista que se representa en la escala.
127
         *
128
         * @return FMap.
129
         */
130
        public FMap getFMap() {
131
                return fframeview.getFMap();
132
        }
133

    
134
        /**
135
         * Rellenar el estilo de barra de escala que se elige.
136
         *
137
         * @param s entero que representa el tipo de barra seleccionada.
138
         */
139
        public void setStyle(int s) {
140
                m_style = s;
141
        }
142

    
143
        /**
144
         * Devuelve el tipo de escala a dibujar.
145
         *
146
         * @return entero.
147
         */
148
        public int getStyle() {
149
                return m_style;
150
        }
151

    
152
        /**
153
         * Rellenar el tama?o de un intervalo.
154
         *
155
         * @param s tama?o de un intervalo.
156
         */
157
        public void setInterval(long s) {
158
                m_interval = s;
159

    
160
                if (m_numleft != 0) {
161
                        m_hasleft = 1;
162
                }
163

    
164
                Rectangle2D.Double rect = getBoundBox();
165
                double difUp = (rect.height / DIFUP);
166
                double difL = (rect.width / DIFL);
167
                double difR = (rect.width / DIFR);
168
                double n = (rect.width - difL - difR);
169
                m_numUnit = (m_interval * m_typeUnit) / getScaleView();
170
                m_numinterval = (int) (n / m_numUnit) - m_hasleft;
171
                m_dif = 1;
172
        }
173

    
174
        /**
175
         * Devuelve el valor del intervalo.
176
         *
177
         * @return long.
178
         */
179
        public long obtainInterval() {
180
                Rectangle2D.Double rect = getBoundBox();
181

    
182
                if (m_numleft != 0) {
183
                        m_hasleft = 1;
184
                }
185

    
186
                double difUp = (rect.height / DIFUP);
187
                double difL = (rect.width / DIFL);
188
                double difR = (rect.width / DIFR);
189
                double n = (rect.width - difL - difR);
190
                m_numUnit = n / (m_numinterval + m_hasleft);
191

    
192
                double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
193
                m_dif = getExact(scaleXunit);
194
                m_numUnit = m_numUnit * m_dif;
195
                m_interval = (long) (scaleXunit * m_dif);
196

    
197
                return m_interval;
198
        }
199

    
200
        /**
201
         * Rellenar el n?mero de intervalos.
202
         *
203
         * @param s n?mero de intervalos.
204
         */
205
        public void setNumInterval(int s) {
206
                m_numinterval = s;
207

    
208
                if (m_numleft != 0) {
209
                        m_hasleft = 1;
210
                }
211

    
212
                Rectangle2D.Double rect = getBoundBox();
213
                double difUp = (rect.height / DIFUP);
214
                double difL = (rect.width / DIFL);
215
                double difR = (rect.width / DIFR);
216
                double n = (rect.width - difL - difR);
217
                m_numUnit = n / (m_numinterval + m_hasleft);
218

    
219
                double scaleXunit = (m_numUnit * getScaleView()) / m_typeUnit;
220
                m_dif = getExact(scaleXunit);
221
                m_numUnit = m_numUnit * m_dif;
222
                m_interval = (long) (scaleXunit * m_dif);
223
        }
224

    
225
        /**
226
         * Devuelve el n?mero de intervalos por encima del cero.
227
         *
228
         * @return entero.
229
         */
230
        public int getNumInterval() {
231
                return m_numinterval;
232
        }
233

    
234
        /**
235
         * Rellenar el n?mero de intervalos a la izquierda del cero.
236
         *
237
         * @param s n?mero de intervalos a la izquierda.
238
         */
239
        public void setNumLeft(int s) {
240
                m_numleft = s;
241
        }
242

    
243
        /**
244
         * Devuelve el n?mero de intervalos  en los que se quiere particionar el
245
         * intervalo  que se queda por debajo del cero.
246
         *
247
         * @return entero.
248
         */
249
        public int getNumLeft() {
250
                return m_numleft;
251
        }
252

    
253
        /**
254
         * Seleccionar ,a true, si se quiere o, a false, si no mantener los
255
         * intervalos.
256
         *
257
         * @param b boolean a true si se quiere mantener los intervalos.
258
         */
259
        public void setIntervalSet(boolean b) {
260
                m_bIntervalSet = b;
261
        }
262

    
263
        /**
264
         * Seleccionar la unidad de medida a representar en la barra de escala.
265
         *
266
         * @param s entero que representa la unidad de medida que se selecciona.
267
         */
268
        public void setUnits(int s) {
269
                m_units = s;
270
                m_typeUnit = Attributes.CHANGE[s];
271
                m_nameUnit = Attributes.NAMES[s];
272
        }
273

    
274
        /**
275
         * Devuelve un entero que representa el tipo de unidades que representamos.
276
         *
277
         * @return entero.
278
         */
279
        public int getUnits() {
280
                return m_units;
281
        }
282

    
283
        /**
284
         * Devuelve el long que representa el intervalo.
285
         *
286
         * @return Intervalo.
287
         */
288
        public long getInterval() {
289
                return m_interval;
290
        }
291

    
292
        /**
293
         * Rellenar el rect?ngulo de la vista sobre la que se obtiene la escala.
294
         *
295
         * @param d Rect?ngulo.
296
         */
297
        public void setHeight(double d) {
298
                m_height = d;
299
        }
300

    
301
        /**
302
         * Rellena la unidad de medida en la que est? la vista.
303
         *
304
         * @param i entero que representa la unidad de medida de la vista.
305
         */
306
        public void setMapUnits(int i) {
307
                m_mapUnits = i;
308
        }
309

    
310
        /*public void setIsFixed(boolean b){
311
           isFixed=b;
312
           }*/
313
        /*public void setScaleView(long l){
314
           m_scaleView=l;
315
           }
316
         */
317

    
318
        /**
319
         * Devuelve la escala de la vista seleccionada.
320
         *
321
         * @return la escala de la vista.
322
         */
323
        private long getScaleView() {
324
                return fframeview.getScale();
325

    
326
                /*        if (isFixed){
327
                   return m_scaleView;
328
                   }
329
                   return FLayoutUtilities.getScaleView(m_fmap, m_height);
330
                 */
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 DOCUMENT ME!
342
         */
343
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
344
                BufferedImage imgBase) {
345
                Rectangle2D.Double r = getBoundingBox(at);
346

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

    
354
                                                double scalex = r.getWidth() / (8);
355
                                                g.setColor(textcolor);
356

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

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

    
366
                                                //System.out.println("Escala real  " + getScaleView());
367
                                                break;
368

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

    
375
                                                break;
376
                                }
377
                        }
378
                }
379
        }
380

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

    
392
                        return;
393
                }
394

    
395
                double difL = (r.width / DIFL);
396
                double difR = (r.width / DIFR);
397

    
398
                if (m_bIntervalSet) {
399
                        m_numinterval = (int) (((r.width - (difL + difR)) * m_dif) / m_numUnit) -
400
                                m_hasleft;
401
                }
402

    
403
                if (m_numinterval < 1) {
404
                        m_numinterval = 1;
405
                        r.width = ((m_numinterval + m_hasleft) * m_numUnit) + difL + difR;
406
                }
407

    
408
                getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
409
        }
410

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

    
424
                double difDown = (rect.height / DIFDOWN);
425
                double difUp = (rect.height / DIFUP);
426
                double difL = (rect.width / DIFL);
427
                double difR = (rect.width / DIFR);
428

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

    
434
                //double scalex = r.getWidth() / (8);
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.getWidth() / 8) / (m_numinterval + m_hasleft)));
479
                   }else{
480
                           m_f=new Font("SansSerif", Font.PLAIN, (int) ((r.getWidth() / 8) / (m_numinterval + m_hasleft)));
481
                   }*/
482
                if (m_f != null) {
483
                        m_f = new Font(m_f.getFontName(), m_f.getStyle(),
484
                                        (int) (r.getHeight() / 4));
485
                } else {
486
                        m_f = new Font("SansSerif", Font.PLAIN, (int) (r.getHeight() / 4));
487
                }
488

    
489
                g.setFont(m_f);
490

    
491
                //Derecha del cero
492
                for (int i = 0; i < m_numinterval; i++) {
493
                        Rectangle2D.Double recder = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
494
                                                (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
495
                                                (rect.y + (difUp)), (m_numUnit),
496
                                                (rect.height - (difUp + difDown)) / 2), at);
497
                        Rectangle2D.Double recderB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
498
                                                (difL) + ((m_numUnit * i) + (m_hasleft * m_numUnit))),
499
                                                (rect.y + difUp) +
500
                                                ((rect.height - (difUp + difDown)) / 2), (m_numUnit),
501
                                                ((rect.height - (difUp + difDown)) / 2)), at);
502

    
503
                        //                        Correcci?n cuando la altura en pixels del rect?ngulo es impar.
504
                        rectotal.y = recder.y;
505
                        recderB.height = rectotal.height - recder.height + 0.5;
506

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

    
540
                        long inter = (long) (m_interval * i);
541
                        Long l = new Long(inter);
542

    
543
                        /* int pos = ((int) (r.getWidth() / (m_numinterval + m_hasleft)) * l.toString()
544
                           .length()) / 24;
545
                         */
546
                        int pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
547

    
548
                        ///Point2D.Double p = new Point2D.Double(recder.x - pos, recder.y);
549
                        Point2D.Double p = null;
550

    
551
                        if (isAboveIntervals()) {
552
                                p = new Point2D.Double(recder.x - pos,
553
                                                r.getMinY() + (r.getHeight() / DIFUP));
554
                        } else {
555
                                p = new Point2D.Double(recder.x - pos,
556
                                                ((r.getHeight() / 4) + r.getMaxY()) -
557
                                                (r.getHeight() / DIFDOWN));
558
                        }
559

    
560
                        drawInterval(g, inter, p);
561
                }
562

    
563
                //?ltimo n?mero a dibujar.
564
                long inter = (long) (m_interval * m_numinterval);
565
                Long l = new Long(inter);
566

    
567
                /* int pos = ((int) (r.getWidth() / (m_numinterval + m_hasleft)) * l.toString()
568
                   .length()) / 24;
569
                 */
570
                int pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
571
                Point2D.Double p = null;
572

    
573
                if (isAboveIntervals()) {
574
                        p = new Point2D.Double(rectotal.getMaxX() - pos,
575
                                        r.getMinY() + (r.getHeight() / DIFUP));
576
                } else {
577
                        p = new Point2D.Double(rectotal.getMaxX() - pos,
578
                                        ((r.getHeight() / 4) + r.getMaxY()) -
579
                                        (r.getHeight() / DIFDOWN));
580
                }
581

    
582
                /* Point2D.Double p = new Point2D.Double(rectotal.getMaxX() - pos,
583
                   rectotal.y);
584
                 */
585
                drawInterval(g, inter, p);
586

    
587
                //Izquierda del cero
588
                for (int i = 0; i < m_numleft; i++) {
589
                        Rectangle2D.Double reciz = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
590
                                                difL + ((m_numUnit / m_numleft) * i)),
591
                                                (rect.y + difUp), (m_numUnit / numleft),
592
                                                (rect.height - (difUp + difDown)) / 2), at);
593
                        Rectangle2D.Double recizB = FLayoutUtilities.fromSheetRect(new Rectangle2D.Double((rect.x +
594
                                                difL + ((m_numUnit / m_numleft) * i)),
595
                                                (rect.y + difUp) +
596
                                                ((rect.height - (difUp + difDown)) / 2),
597
                                                (m_numUnit / numleft),
598
                                                ((rect.height - (difUp + difDown)) / 2)), at);
599

    
600
                        //Correcci?n cuando la altura en pixels del rectangulo es impar.
601
                        reciz.y = rectotal.y;
602
                        recizB.height = rectotal.height - reciz.height + 0.5;
603

    
604
                        if ((i % 2) == 0) {
605
                                if ((type == BARRA1) || (type == BARRA2)) {
606
                                        fillRect(g, reciz);
607
                                } else if (type == BARRA3) {
608
                                        g.drawRect((int) reciz.x, (int) reciz.y, (int) reciz.width,
609
                                                (int) reciz.height);
610
                                }
611
                        } else if (type == BARRA1) {
612
                                fillRect(g, recizB);
613
                        }
614
                }
615

    
616
                if (m_numleft > 0) {
617
                        inter = (long) (m_interval);
618
                        l = new Long(inter);
619

    
620
                        /*pos = ((int) (r.getWidth() / (m_numinterval + m_hasleft)) * l.toString()
621
                           .length()) / 24;
622
                         */
623
                        pos = ((int) (r.getHeight() / 4) * l.toString().length()) / 4;
624

    
625
                        if (isAboveIntervals()) {
626
                                p = new Point2D.Double(rectotal.x - pos,
627
                                                r.getMinY() + (r.getHeight() / DIFUP));
628
                        } else {
629
                                p = new Point2D.Double(rectotal.x - pos,
630
                                                ((r.getHeight() / 4) + r.getMaxY()) -
631
                                                (r.getHeight() / DIFDOWN));
632
                        }
633

    
634
                        /// Point2D.Double p1 = new Point2D.Double(rectotal.x - pos, rectotal.y);
635
                        drawInterval(g, inter, p);
636
                }
637

    
638
                //Se dibuja el rect?ngulo que bordea toda la escala gr?fica.
639
                g.drawRect((int) rectotal.x, (int) rectotal.y, (int) (rectotal.width),
640
                        (int) rectotal.height);
641

    
642
                drawNameUnits(g, rect, at);
643
                drawDescription(g, rect, at);
644
        }
645

    
646
        /**
647
         * DOCUMENT ME!
648
         */
649
        private void initDistances() {
650
                int numUp = 0;
651
                int numDown = 0;
652

    
653
                DIFDOWN = 30;
654
                DIFL = 40;
655
                DIFR = 40;
656
                DIFUP = 30;
657

    
658
                if (isAboveName()) {
659
                        numUp++;
660
                } else {
661
                        numDown++;
662
                }
663

    
664
                if (isAboveIntervals()) {
665
                        numUp++;
666
                } else {
667
                        numDown++;
668
                }
669

    
670
                if (isAboveDescription()) {
671
                        numUp++;
672
                } else {
673
                        numDown++;
674
                }
675

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

    
684
                if (numUp == 1) {
685
                        DIFUP = 3;
686
                } else if (numUp == 2) {
687
                        DIFUP = 2;
688
                } else if (numUp == 3) {
689
                        DIFUP = 1.2;
690
                }
691

    
692
                //DIFDOWN=numDown;
693
                //DIFUP=numUp;
694
        }
695

    
696
        /**
697
         * DOCUMENT ME!
698
         *
699
         * @param g DOCUMENT ME!
700
         * @param rec DOCUMENT ME!
701
         * @param at DOCUMENT ME!
702
         */
703
        private void drawNameUnits(Graphics g, Rectangle2D.Double rec,
704
                AffineTransform at) {
705
                //        Nombre de las unidades utilizadas.
706
                Point2D.Double p = null;
707

    
708
                if (isShowNameUnits()) {
709
                        if (isAboveName()) {
710
                                p = new Point2D.Double(rec.getCenterX() -
711
                                                ((rec.getHeight() * (m_nameUnit.length() / 6)) / 4),
712
                                                
713
                                        //(m_nameUnit.length() / 6),
714
                                        (rec.getMinY() + (rec.getHeight() / (DIFUP))) -
715
                                                (rec.getHeight() / 2));
716
                        } else {
717
                                p = new Point2D.Double(rec.getCenterX() -
718
                                                ((rec.getHeight() * (m_nameUnit.length() / 6)) / 4),
719
                                                (
720
                                        //(m_nameUnit.length() / 6),
721
                                        rec.getHeight() / 1.4) +
722
                                                (rec.getMaxY() - (rec.getHeight() / (DIFDOWN))));
723
                        }
724

    
725
                        p = FLayoutUtilities.fromSheetPoint(p, at);
726
                        g.setColor(textcolor);
727
                        g.drawString(m_nameUnit, (int) p.x, (int) p.y);
728
                }
729
        }
730

    
731
        /**
732
         * DOCUMENT ME!
733
         *
734
         * @param g DOCUMENT ME!
735
         * @param rec DOCUMENT ME!
736
         * @param at DOCUMENT ME!
737
         */
738
        private void drawDescription(Graphics g, Rectangle2D.Double rec,
739
                AffineTransform at) {
740
                Point2D.Double p = null;
741

    
742
                if (isShowDescription()) {
743
                        if (isAboveDescription()) {
744
                                p = new Point2D.Double(rec.getCenterX() -
745
                                                ((rec.getHeight() * (getDescripcion().length() / 6)) / 4),
746
                                                
747
                                        //(getDescripcion().length() / 6),
748
                                        (rec.getMinY() + (rec.getHeight() / (DIFUP))) -
749
                                                (rec.getHeight() / 5));
750
                        } else {
751
                                p = new Point2D.Double(rec.getCenterX() -
752
                                                ((rec.getHeight() * (getDescripcion().length() / 6)) / 4),
753
                                                (
754
                                        //(getDescripcion().length() / 6),
755
                                        rec.getHeight() / 2) +
756
                                                (rec.getMaxY() - (rec.getHeight() / (DIFDOWN))));
757
                        }
758

    
759
                        p = FLayoutUtilities.fromSheetPoint(p, at);
760
                        g.setColor(textcolor);
761
                        g.drawString(getDescripcion(), (int) p.x, (int) p.y);
762
                }
763
        }
764

    
765
        /**
766
         * Rellena la fuente utilizada para dibujar los intervalos y la unidad de
767
         * medida utilizada.
768
         *
769
         * @param f fuente a utilizar.
770
         */
771
        public void setFont(Font f) {
772
                m_f = f;
773
        }
774

    
775
        /**
776
         * Devuelve la fuente con la que se est? dibujando sobre el graphics.
777
         *
778
         * @return fuente utilizada.
779
         */
780
        public Font getFont() {
781
                return new Font(m_f.getFontName(), m_f.getStyle(), 9);
782
        }
783

    
784
        /**
785
         * Devuelve si el intervalo es variable o si por el contrario es fijo.
786
         *
787
         * @return true si el intervalo es fijo.
788
         */
789
        public boolean isbIntervalSet() {
790
                return m_bIntervalSet;
791
        }
792

    
793
        /**
794
         * Especificar si el intervalo se debe mantener o es variable.
795
         *
796
         * @param b si se quiere mantener el intervalo especificado.
797
         */
798
        public void setbIntervalSet(boolean b) {
799
                m_bIntervalSet = b;
800
        }
801

    
802
        /**
803
         * Devuelve el porcentaje por el cual hay que multiplicar  el intervalo
804
         * para conseguir un intervalo redondeado,  de momento con una cifra
805
         * significativas(NUM=1).
806
         *
807
         * @param total intervalo.
808
         *
809
         * @return Porcentaje
810
         */
811
        private double getExact(double total) {
812
                int NUM = 1;
813
                double t = 0;
814
                double dif = 1;
815
                Double d = new Double(total);
816
                Long l = new Long(d.longValue());
817
                int num = l.toString().length();
818
                t = ((long) (total / Math.pow(10, num - NUM)) * Math.pow(10, num - NUM));
819
                dif = t / total;
820

    
821
                return dif;
822
        }
823

    
824
        /**
825
         * Rellena un rect?ngulo.
826
         *
827
         * @param g Graphics sobre el que dibujar.
828
         * @param r Rect?ngulo a rellenar.
829
         */
830
        private void fillRect(Graphics2D g, Rectangle2D.Double r) {
831
                g.fillRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
832
        }
833

    
834
        /**
835
         * Escribe sobre el Graphics y en la posici?n indicada el tama?o del
836
         * intervalo.
837
         *
838
         * @param g Graphics sobre el que dibujar.
839
         * @param inter Valor del intervalo.
840
         * @param p Punto donde dibujarlo.
841
         */
842
        private void drawInterval(Graphics2D g, long inter, Point2D.Double p) {
843
                Long l = new Long(inter);
844
                g.setColor(textcolor);
845
                g.drawString(l.toString(), (int) p.x, (int) p.y);
846
                g.setColor(barcolor);
847
        }
848

    
849
        /**
850
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
851
         */
852
        public XMLEntity getXMLEntity() {
853
                XMLEntity xml = new XMLEntity();
854
                xml.putProperty("nameClass", this.getClass().getName());
855
                xml.putProperty("m_name", m_name);
856
                xml.putProperty("x", getBoundBox().x);
857
                xml.putProperty("y", getBoundBox().y);
858
                xml.putProperty("w", getBoundBox().width);
859
                xml.putProperty("h", getBoundBox().height);
860
                xml.putProperty("m_Selected", m_Selected);
861
                xml.putProperty("type", Layout.RECTANGLESCALEBAR);
862
                xml.putProperty("m_bIntervalSet", m_bIntervalSet);
863
                xml.putProperty("m_dif", m_dif);
864
                xml.putProperty("m_hasleft", m_hasleft);
865
                xml.putProperty("m_nameUnit", m_nameUnit);
866
                xml.putProperty("m_numUnit", m_numUnit);
867

    
868
                xml.putProperty("m_height", m_height);
869

    
870
                xml.putProperty("m_style", m_style);
871
                xml.putProperty("m_units", m_units);
872
                xml.putProperty("m_interval", m_interval);
873
                xml.putProperty("m_numinterval", m_numinterval);
874
                xml.putProperty("m_numleft", m_numleft);
875
                xml.putProperty("m_mapUnits", m_mapUnits);
876
                xml.putProperty("fontName", m_f.getFontName());
877
                xml.putProperty("fontStyle", m_f.getStyle());
878
                xml.putProperty("tag", getTag());
879
/*                xml.addChild(fframeview.getXMLEntity());
880
                
881
                ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
882
                ArrayList views = pe.getProject().getViews();
883

884
                boolean hasIndex = false;
885

886
                for (int i = 0; i < views.size(); i++) {
887
                        if (fframeview.getView() == ((ProjectView) views.get(i))) {
888
                                xml.putProperty("indice", i);
889
                                hasIndex = true;
890
                        }
891
                }
892

893
                if (!hasIndex) {
894
                        xml.putProperty("indice", -1);
895
                }
896
*/
897
                
898
                LayoutControls lc=(LayoutControls)PluginServices.getExtension(LayoutControls.class);
899
                Layout layout=lc.getLayout();
900
                ArrayList fframes=layout.getFFrames();
901
                
902
                for (int i=0;i<fframes.size();i++){
903
                        if (fframes.get(i) instanceof FFrameView){
904
                        if (fframeview.getView().getName().equals(((FFrameView)fframes.get(i)).getView().getName())){
905
                                xml.putProperty("index",i);
906
                        }
907
                        }
908
                }
909
                xml.putProperty("barcolor", StringUtilities.color2String(barcolor));
910
                xml.putProperty("textcolor", StringUtilities.color2String(textcolor));
911
                xml.putProperty("showNameUnits", showNameUnits);
912
                xml.putProperty("showDescription", showDescription);
913
                xml.putProperty("aboveName", aboveName);
914
                xml.putProperty("aboveIntervals", aboveIntervals);
915
                xml.putProperty("aboveDescription", aboveDescription);
916
                xml.putProperty("description", description);
917

    
918
                return xml;
919
        }
920

    
921
        /**
922
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
923
         *
924
         * @param xml XMLEntity
925
         * @param p DOCUMENT ME!
926
         */
927

    
928
        /*  public static FFrameScaleBar createFFrameScaleBar(XMLEntity xml, Project p) {
929
           FFrameScaleBar fframe = new FFrameScaleBar();
930
           if (xml.getIntProperty("m_Selected") != 0) {
931
               fframe.setSelected(true);
932
           } else {
933
               fframe.setSelected(false);
934
           }
935
           fframe.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
936
           fframe.m_dif = xml.getDoubleProperty("m_dif");
937
           fframe.m_hasleft = xml.getIntProperty("m_hasleft");
938
           fframe.m_nameUnit = xml.getStringProperty("m_nameUnit");
939
           fframe.m_numUnit = xml.getDoubleProperty("m_numUnit");
940
           fframe.m_height = xml.getDoubleProperty("m_height");
941
           fframe.m_style = xml.getIntProperty("m_style");
942
           fframe.m_interval = xml.getLongProperty("m_interval");
943
           fframe.m_numinterval = xml.getIntProperty("m_numinterval");
944
           fframe.m_numleft = xml.getIntProperty("m_numleft");
945
           fframe.m_mapUnits = xml.getIntProperty("m_mapUnits");
946
           fframe.m_f = new Font(xml.getStringProperty("fontName"),
947
                   xml.getIntProperty("fontStyle"), 9);
948
           fframe.setFFrameView((FFrameView) FFrameView.createFFrame(xml.getChild(
949
                       0), p));
950
           return fframe;
951
           }
952
         */
953

    
954
        /**
955
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
956
         *                 com.iver.cit.gvsig.project.Project)
957
         */
958
        public void setXMLEntity(XMLEntity xml, Layout l) {
959
                if (xml.getIntProperty("m_Selected") != 0) {
960
                        this.setSelected(true);
961
                } else {
962
                        this.setSelected(false);
963
                }
964

    
965
                this.m_bIntervalSet = xml.getBooleanProperty("m_bIntervalSet");
966
                this.m_dif = xml.getDoubleProperty("m_dif");
967
                this.m_hasleft = xml.getIntProperty("m_hasleft");
968
                this.m_nameUnit = xml.getStringProperty("m_nameUnit");
969
                this.m_numUnit = xml.getDoubleProperty("m_numUnit");
970

    
971
                this.m_height = xml.getDoubleProperty("m_height");
972

    
973
                this.m_style = xml.getIntProperty("m_style");
974
                this.m_interval = xml.getLongProperty("m_interval");
975
                this.m_numinterval = xml.getIntProperty("m_numinterval");
976
                this.m_numleft = xml.getIntProperty("m_numleft");
977
                this.m_mapUnits = xml.getIntProperty("m_mapUnits");
978
                this.m_f = new Font(xml.getStringProperty("fontName"),
979
                                xml.getIntProperty("fontStyle"), 9);
980
                /*FFrameView fv=(FFrameView) FFrameView.createFFrame(xml.getChild(0),
981
                                                p);
982
                int indice = xml.getIntProperty("indice");
983

984
                if (indice != -1) {
985
                        ProjectView view = (ProjectView) p.getViews().get(indice);
986
                        fv.setView((ProjectView) view);
987
                        fv.getFMap().setViewPort(((ProjectView) p.getViews().get(indice)).getMapContext().getViewPort());
988
                }
989
                
990
                this.setFFrameView(fv);
991
*/
992
                ArrayList fframes=l.getFFrames();
993
                fframeview=(FFrameView)fframes.get(xml.getIntProperty("index"));
994
                        if (xml.contains("description")) { //Comprobar que es de la versi?n que cambia el di?logo.
995
                        this.barcolor = StringUtilities.string2Color(xml.getStringProperty(
996
                                                "barcolor"));
997
                        this.textcolor = StringUtilities.string2Color(xml.getStringProperty(
998
                                                "textcolor"));
999
                        this.showNameUnits = xml.getBooleanProperty("showNameUnits");
1000
                        this.showDescription = xml.getBooleanProperty("showDescription");
1001
                        this.aboveName = xml.getBooleanProperty("aboveName");
1002
                        this.aboveIntervals = xml.getBooleanProperty("aboveIntervals");
1003
                        this.aboveDescription = xml.getBooleanProperty("aboveDescription");
1004
                        this.description = xml.getStringProperty("description");
1005
                }
1006
        }
1007

    
1008
        /**
1009
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
1010
         */
1011
        public String getNameFFrame() {
1012
                return PluginServices.getText(this, "escala") + num;
1013
        }
1014

    
1015
        /**
1016
         * DOCUMENT ME!
1017
         *
1018
         * @param color DOCUMENT ME!
1019
         */
1020
        public void setBarColor(Color color) {
1021
                barcolor = color;
1022
        }
1023

    
1024
        /**
1025
         * DOCUMENT ME!
1026
         *
1027
         * @param color DOCUMENT ME!
1028
         */
1029
        public void setTextColor(Color color) {
1030
                textcolor = color;
1031
        }
1032

    
1033
        /**
1034
         * DOCUMENT ME!
1035
         *
1036
         * @return DOCUMENT ME!
1037
         */
1038
        public Color getBarColor() {
1039
                return barcolor;
1040
        }
1041

    
1042
        /**
1043
         * DOCUMENT ME!
1044
         *
1045
         * @return DOCUMENT ME!
1046
         */
1047
        public Color getTextColor() {
1048
                return textcolor;
1049
        }
1050

    
1051
        /**
1052
         * DOCUMENT ME!
1053
         *
1054
         * @return DOCUMENT ME!
1055
         */
1056
        public boolean isShowNameUnits() {
1057
                return showNameUnits;
1058
        }
1059

    
1060
        /**
1061
         * DOCUMENT ME!
1062
         *
1063
         * @param showNameUnits DOCUMENT ME!
1064
         */
1065
        public void setShowNameUnits(boolean showNameUnits) {
1066
                this.showNameUnits = showNameUnits;
1067
        }
1068

    
1069
        /**
1070
         * DOCUMENT ME!
1071
         *
1072
         * @return DOCUMENT ME!
1073
         */
1074
        public boolean isAboveDescription() {
1075
                return aboveDescription;
1076
        }
1077

    
1078
        /**
1079
         * DOCUMENT ME!
1080
         *
1081
         * @param aboveDescription DOCUMENT ME!
1082
         */
1083
        public void setAboveDescription(boolean aboveDescription) {
1084
                this.aboveDescription = aboveDescription;
1085
        }
1086

    
1087
        /**
1088
         * DOCUMENT ME!
1089
         *
1090
         * @return DOCUMENT ME!
1091
         */
1092
        public boolean isAboveIntervals() {
1093
                return aboveIntervals;
1094
        }
1095

    
1096
        /**
1097
         * DOCUMENT ME!
1098
         *
1099
         * @param aboveIntervals DOCUMENT ME!
1100
         */
1101
        public void setAboveIntervals(boolean aboveIntervals) {
1102
                this.aboveIntervals = aboveIntervals;
1103
        }
1104

    
1105
        /**
1106
         * DOCUMENT ME!
1107
         *
1108
         * @return DOCUMENT ME!
1109
         */
1110
        public boolean isAboveName() {
1111
                return aboveName;
1112
        }
1113

    
1114
        /**
1115
         * DOCUMENT ME!
1116
         *
1117
         * @param aboveName DOCUMENT ME!
1118
         */
1119
        public void setAboveName(boolean aboveName) {
1120
                this.aboveName = aboveName;
1121
        }
1122

    
1123
        /**
1124
         * DOCUMENT ME!
1125
         *
1126
         * @return DOCUMENT ME!
1127
         */
1128
        public boolean isShowDescription() {
1129
                return showDescription;
1130
        }
1131

    
1132
        /**
1133
         * DOCUMENT ME!
1134
         *
1135
         * @param showDescription DOCUMENT ME!
1136
         */
1137
        public void setShowDescription(boolean showDescription) {
1138
                this.showDescription = showDescription;
1139
        }
1140

    
1141
        /**
1142
         * DOCUMENT ME!
1143
         *
1144
         * @return DOCUMENT ME!
1145
         */
1146
        public String getDescripcion() {
1147
                return description;
1148
        }
1149

    
1150
        /**
1151
         * DOCUMENT ME!
1152
         *
1153
         * @param descripcion DOCUMENT ME!
1154
         */
1155
        public void setDescripcion(String descripcion) {
1156
                this.description = descripcion;
1157
        }
1158

    
1159
        /**
1160
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
1161
         *                 java.awt.geom.AffineTransform)
1162
         */
1163
        public void print(Graphics2D g, AffineTransform at)
1164
                throws DriverException {
1165
                draw(g, at, null, null);
1166
        }
1167
}