Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Attributes.java @ 596

History | View | Annotate | Download (33.6 KB)

1
/*
2
 * Created on 25-jun-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;
8

    
9
import java.awt.Rectangle;
10
import java.awt.geom.Rectangle2D;
11
import java.awt.print.PageFormat;
12
import java.awt.print.Paper;
13
import java.text.NumberFormat;
14

    
15
import javax.print.attribute.HashPrintRequestAttributeSet;
16
import javax.print.attribute.PrintRequestAttributeSet;
17
import javax.print.attribute.standard.Copies;
18
import javax.print.attribute.standard.Fidelity;
19
import javax.print.attribute.standard.Media;
20
import javax.print.attribute.standard.MediaPrintableArea;
21
import javax.print.attribute.standard.MediaSizeName;
22
import javax.print.attribute.standard.OrientationRequested;
23
import javax.print.attribute.standard.PrintQuality;
24
import javax.print.attribute.standard.Sides;
25

    
26
import com.iver.andami.PluginServices;
27
import com.iver.andami.Utilities;
28
import com.iver.utiles.XMLEntity;
29

    
30

    
31
/**
32
 * Atributos del Layout.
33
 *
34
 * @author Vicente Caballero Navarro
35
 */
36
public class Attributes {
37
    /** KILOMETROS,METROS,CENTIMETROS,MILIMETROS,MILLAS,YARDAS,PIES,PULGADAS */
38
    public static final double[] CHANGE = {
39
        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
40
    };
41
public static String[] NAMES;
42
    
43
        /*if (NAMES == null) {
44
                int i=0;
45
                NAMES = new String[8];
46
                NAMES[i++] = PluginServices.getText(this, "Kilometros");
47
                NAMES[i++] = PluginServices.getText(this, "Metros");
48
                NAMES[i++] = PluginServices.getText(this, "Centimetros");
49
                NAMES[i++] = PluginServices.getText(this, "Milimetros");
50
                NAMES[i++] = PluginServices.getText(this, "Millas");
51
                NAMES[i++] = PluginServices.getText(this, "Yardas");
52
                NAMES[i++] = PluginServices.getText(this, "Pies");
53
                NAMES[i++] = PluginServices.getText(this, "Pulgadas");
54
        
55
    }
56
    */
57
    /**
58
     * DOCUMENT ME!
59
     */
60
    public static final double[] UNIT = {
61
        0.0000025, 0.0025, 0.25, 2.5, 0.0000025, 0.025, 0.025, 0.25
62
    };
63

    
64
    /** DOCUMENT ME! */
65
    public static final int RALTO = 0;
66

    
67
    /** DOCUMENT ME! */
68
    public static final int RNORMAL = 1;
69

    
70
    /** DOCUMENT ME! */
71
    public static final int RBAJO = 2;
72

    
73
    //        Para impresi?n
74

    
75
    /** DOCUMENT ME! */
76
    public final static double PULGADA = 2.54;
77

    
78
    /** DOCUMENT ME! */
79
    //public final static Size CUSTOM_PAPER_SIZE = new Size(8.5, 11.0);
80

    
81
    /** DOCUMENT ME! */
82
    public final static Size STANDARD_LETTER_PAPER_SIZE = new Size(8.5, 11.0);
83

    
84
    /** DOCUMENT ME! */
85
    public final static Size STANDARD_FOLIO_PAPER_SIZE = new Size(8.5, 13.0);
86

    
87
    /** DOCUMENT ME! */
88
    public final static Size STANDARD_LEGAL_PAPER_SIZE = new Size(8.5, 14.0);
89

    
90
    /** DOCUMENT ME! */
91
    public final static Size STANDARD_TABLOID_PAPER_SIZE = new Size(11.0, 17.0);
92

    
93
    /** DOCUMENT ME! */
94
    public final static Size METRIC_A0_PAPER_SIZE = new Size(118.9, 84.1);
95

    
96
    /** DOCUMENT ME! */
97
    public final static Size METRIC_A1_PAPER_SIZE = new Size(84.1, 59.4);
98

    
99
    /** DOCUMENT ME! */
100
    public final static Size METRIC_A2_PAPER_SIZE = new Size(59.4, 42.0);
101

    
102
    /** DOCUMENT ME! */
103
    public final static Size METRIC_A3_PAPER_SIZE = new Size(42.0, 29.7);
104

    
105
    /** DOCUMENT ME! */
106
    public final static Size METRIC_A4_PAPER_SIZE = new Size(29.7, 21.0);
107

    
108
    /** DOCUMENT ME! */
109
    public final static Size METRIC_A5_PAPER_SIZE = new Size(21.0, 14.8);
110

    
111
    /** DOCUMENT ME! */
112
    public final static Size ANSI_ENG_A_PAPER_SIZE = new Size(11.0, 8.5);
113

    
114
    /** DOCUMENT ME! */
115
    public final static Size ANSI_ENG_B_PAPER_SIZE = new Size(17.0, 11.0);
116

    
117
    /** DOCUMENT ME! */
118
    public final static Size ANSI_ENG_C_PAPER_SIZE = new Size(22.0, 17.0);
119

    
120
    /** DOCUMENT ME! */
121
    public final static Size ANSI_ENG_D_PAPER_SIZE = new Size(34.0, 22.0);
122

    
123
    /** DOCUMENT ME! */
124
    public final static Size ANSI_ENG_E_PAPER_SIZE = new Size(44.0, 34.0);
125

    
126
    /** DOCUMENT ME! */
127
    public final static Size ANSI_ARCH_A_PAPER_SIZE = new Size(12.0, 9.0);
128

    
129
    /** DOCUMENT ME! */
130
    public final static Size ANSI_ARCH_B_PAPER_SIZE = new Size(18.0, 12.0);
131

    
132
    /** DOCUMENT ME! */
133
    public final static Size ANSI_ARCH_C_PAPER_SIZE = new Size(24.0, 18.0);
134

    
135
    /** DOCUMENT ME! */
136
    public final static Size ANSI_ARCH_D_PAPER_SIZE = new Size(36.0, 24.0);
137

    
138
    /** DOCUMENT ME! */
139
    public final static Size ANSI_ARCH_E_PAPER_SIZE = new Size(42.0, 30.0);
140
    public static Size CUSTOM_PAPER_SIZE=new Size(100.0,100.0);
141
    /** DOCUMENT ME! */
142
    public final static int PRINT = 0;
143
    public final static int CUSTOM=6;
144
    /** DOCUMENT ME! */
145
    public final static int A0 = 5;
146

    
147
    /** DOCUMENT ME! */
148
    public final static int A1 = 4;
149

    
150
    /** DOCUMENT ME! */
151
    public final static int A2 = 3;
152

    
153
    /** DOCUMENT ME! */
154
    public final static int A3 = 2;
155

    
156
    /** DOCUMENT ME! */
157
    public final static int A4 = 1;
158

    
159
    /** DOCUMENT ME! */
160
    public static int DPI = 300;
161

    
162
    /** DOCUMENT ME! */
163
    public static int DPISCREEN = 72;
164

    
165
    /** DOCUMENT ME! */
166
    public static Rectangle clipRect = new Rectangle();
167

    
168
       private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
169
    private MediaSizeName m_type = MediaSizeName.ISO_A4;
170
    private boolean m_isLandSel;
171
    private boolean m_LastLandSel = true;
172
    private OrientationRequested m_Orientation;
173
    private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
174
    private String m_NameUnit;
175
    private double m_numX = 0.25;
176
    private double m_numY = 0.25;
177
    private double m_unitX = 0;
178
    private double m_unitY = 0;
179
    private boolean hasmargin;
180
    private int m_resolutionSel = RNORMAL;
181
    private PrintQuality m_resolution;
182
    private int m_typeSel = 0;
183
    private int m_selTypeUnit = 2;
184

    
185
    /** DOCUMENT ME! */
186
    public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
187
            METRIC_A4_PAPER_SIZE.getAncho());
188

    
189
    /** DOCUMENT ME! */
190
    public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
191
    private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
192
    private Size m_sizeinUnits = m_sizePaper;
193
    static{
194
            new Attributes();
195
    }
196
                
197
   /**
198
     * Creates a new Attributes object.
199
     */
200
    public Attributes() {
201
            if (NAMES == null) {
202
                    int i=0;
203
                    NAMES = new String[8];
204
                    NAMES[i++] = PluginServices.getText(this, "Kilometros");
205
                    NAMES[i++] = PluginServices.getText(this, "Metros");
206
                    NAMES[i++] = PluginServices.getText(this, "Centimetros");
207
                    NAMES[i++] = PluginServices.getText(this, "Milimetros");
208
                    NAMES[i++] = PluginServices.getText(this, "Millas");
209
                    NAMES[i++] = PluginServices.getText(this, "Yardas");
210
                    NAMES[i++] = PluginServices.getText(this, "Pies");
211
                    NAMES[i++] = PluginServices.getText(this, "Pulgadas");
212
            }
213
            m_NameUnit = NAMES[2];
214
        m_type = MediaSizeName.ISO_A4;
215
        m_isLandSel = true;
216
        setSizeinUnits(m_isLandSel);
217
        
218
        hasmargin = false;
219
        m_Orientation = OrientationRequested.LANDSCAPE;
220
        attributes.add(new Copies(1));
221
        attributes.add(MediaSizeName.ISO_A4);
222
        attributes.add(Sides.ONE_SIDED);
223
        attributes.add(Fidelity.FIDELITY_FALSE);
224
        attributes.add(PrintQuality.NORMAL);
225
        
226
        setType(m_typeSel);
227
        
228
        attributes.add(new MediaPrintableArea((float) 0, (float) 0,
229
                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
230
                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
231
                MediaPrintableArea.MM));
232
    }
233

    
234
    /**
235
     * Seleccionar el tipo de unidad que representa el Layout.
236
     *
237
     * @param s entero.
238
     */
239
    public void setUnits(int s) {
240
        m_TypeUnit = CHANGE[s];
241
        m_NameUnit = NAMES[s];
242
    }
243

    
244
    /**
245
     * Rellena las propiedades del Layout.
246
     *
247
     * @param typeSel tipo de folio.
248
     * @param units unidad de medida.
249
     * @param isLand si es horizontal o vertical.
250
     * @param margin si se deben de coger los margenes que marca la impresora o
251
     *        no.
252
     * @param resolution tipo de resoluci?n a aplicar.
253
     * @param area area imprimible.
254
     */
255
    public void setSelectedOptions(int typeSel, int units, boolean isLand,
256
        boolean margin, int resolution, double[] area) {
257
            setType(typeSel);
258
        setUnit(units);
259
        setIsLandScape(isLand);
260
        hasmargin = margin;
261
        setResolution(resolution);
262
        m_area = area;
263
    }
264

    
265
    /**
266
     * Devuelve un PageFormat con las propiedades seleccionadas.
267
     *
268
     * @return PageFormat
269
     */
270
    public PageFormat getPageFormat() {
271
        PageFormat pf1 = new PageFormat();
272
        Paper paper = pf1.getPaper();
273
        
274

    
275
        if (isLandSpace()) {
276
            pf1.setOrientation(0);
277
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
278
                                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
279

    
280
            double aux = m_area[0];
281
            m_area[0] = m_area[3];
282
            m_area[3] = m_area[1];
283
            m_area[1] = m_area[2];
284
            m_area[2] = aux;
285
            paper.setImageableArea(getInPixels(m_area[2]),
286
                getInPixels(m_area[0]),
287
                getInPixels(m_sizeinUnits.getAncho() - m_area[2] - m_area[3]),
288
                getInPixels(m_sizeinUnits.getAlto() - m_area[0] - m_area[1]));
289
        } else {
290
            pf1.setOrientation(1);
291
                        paper.setSize((m_sizePaper.getAlto() * DPISCREEN) / PULGADA,
292
                                                (m_sizePaper.getAncho() * DPISCREEN) / PULGADA);
293
            paper.setImageableArea(getInPixels(m_area[2]),
294
                getInPixels(m_area[0]),
295
                getInPixels(m_sizeinUnits.getAlto() - m_area[2] - m_area[3]),
296
                getInPixels(m_sizeinUnits.getAncho() - m_area[0] - m_area[1]));
297
        }
298

    
299
        pf1.setPaper(paper);
300

    
301
        return pf1;
302
    }
303

    
304
    /**
305
     * Devuelve en Pixels los cent?metros que se le pasan como par?metro.
306
     *
307
     * @param d cent?metros.
308
     *
309
     * @return Pixels.
310
     */
311
    private double getInPixels(double d) {
312
        return d * (DPISCREEN / PULGADA);
313
    }
314

    
315
    /**
316
     * Devuelve en cent?metros los pixels que se le pasan como par?metro.
317
     *
318
     * @param d Pixel.
319
     *
320
     * @return Cent?metros.
321
     */
322
    private double getInCM(double d) {
323
        return d / (DPISCREEN / PULGADA);
324
    }
325

    
326
    /**
327
     * Devuelve un ret?ngulo que representa el ?rea imprimible.
328
     *
329
     * @return Rect?ngulo.
330
     */
331
    public Rectangle2D.Double getArea() {
332
        Rectangle2D.Double rect = new Rectangle2D.Double();
333
        rect.setRect(m_area[0], m_area[1],
334
            m_sizePaper.getAncho() - m_area[0] - m_area[2],
335
            m_sizePaper.getAlto() - m_area[1] - m_area[3]);
336

    
337
        return rect;
338
    }
339

    
340
    /**
341
     * Se rellenan los atributos a partir de un PageFormat que se introduce
342
     * como parametro.
343
     *
344
     * @param pf PageFormat
345
     */
346
    public void setPageFormat(PageFormat pf) {
347
        Size size = null;
348

    
349
        if (pf.getOrientation() == 0) {
350
            setIsLandScape(true);
351
            m_Orientation = OrientationRequested.LANDSCAPE;
352
            size = new Size(pf.getHeight(), pf.getWidth());
353
        } else {
354
            setIsLandScape(false);
355
            m_Orientation = OrientationRequested.PORTRAIT;
356
            size = new Size(pf.getHeight(), pf.getWidth());
357
        }
358

    
359
        attributes.add(m_Orientation);
360

    
361
        //area
362
        m_area[0] = getInCM(pf.getImageableY());
363
        m_area[2] = getInCM(pf.getImageableX());
364
        m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
365
            m_area[0];
366
        m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
367
            m_area[2];
368

    
369
        //        tipo formato
370
        if (isLandSpace()) {
371
            //double aux = m_area[0];
372
            //m_area[0] = m_area[3];
373
            //m_area[3] = m_area[1];
374
            //m_area[1] = m_area[2];
375
            //m_area[2] = aux;
376
            attributes.add(new MediaPrintableArea((float) (m_area[2] * 10),
377
                    (float) (m_area[0] * 10),
378
                    (float) (m_sizeinUnits.getAlto() - m_area[0] - m_area[1]) * 10,
379
                    (float) (m_sizeinUnits.getAncho() - m_area[2] - m_area[3]) * 10,
380
                    MediaPrintableArea.MM));
381
        } else {
382
            attributes.add(new MediaPrintableArea((float) (m_area[0] * 10),
383
                    (float) (m_area[1] * 10),
384
                    (float) (getInCM(pf.getImageableWidth()) * 10),
385
                    (float) (getInCM(pf.getImageableHeight()) * 10),
386
                    MediaPrintableArea.MM));
387
        }
388

    
389
        setType(getTypePaper(size));
390
    }
391

    
392
    
393
    /**
394
     * Preparamos un PrintRequestAttributeSet con nuestras opciones para pasarselo
395
     * como parametro al PrintDialog.
396
     */
397
    public PrintRequestAttributeSet toPrintAttributes() {
398
            HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
399
            
400
            setType(m_typeSel);
401
            
402
            resul.add(m_type);
403

    
404
        //units, no hace falta a?adirlo a attributes
405
        resul.add(m_Orientation);
406
        setArea(m_area);
407
        resul.add(new MediaPrintableArea((float) 0, (float) 0,
408
                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
409
                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
410
                MediaPrintableArea.MM));
411
        //resul.add(MediaSize.Other.PERSONAL_ENVELOPE);
412

    
413
        //attributes.add(m_resolution);
414
        return resul;
415
    }
416

    
417

    
418
    /**
419
     * rellenar el area imprimible.
420
     *
421
     * @param area area imprimible.
422
     */
423
    private void setArea(double[] area) {
424
        if (!isLandSpace()) {
425
            attributes.add(new MediaPrintableArea((float) (area[2] * 10),
426
                    (float) (area[0] * 10),
427
                    (float) ((m_sizePaper.getAncho() - area[2] - area[3]) * 10),
428
                    (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
429
                    MediaPrintableArea.MM));
430

    
431
            clipRect.setRect((area[2] / PULGADA * DPI),
432
                area[0] / PULGADA * DPI,
433
                (m_sizePaper.getAncho() - area[2] - area[3]) / PULGADA * DPI,
434
                (m_sizePaper.getAlto() - area[0] - area[1]) / PULGADA * DPI);
435
        } else {
436
                 attributes.add(new MediaPrintableArea((float) (area[0] * 10),
437
                    (float) (area[3] * 10),
438
                    (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
439
                    (float) ((m_sizePaper.getAncho() - area[3] - area[2]) * 10),
440
                    MediaPrintableArea.MM));
441

    
442
            clipRect.setRect((area[1] / PULGADA * DPI),
443
                area[2] / PULGADA * DPI,
444
                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
445
                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
446
        }
447
    }
448

    
449
    /**
450
     * Devuelve true si est? seleccionada la opci?n de utilizar los margenes de
451
     * impresi?n.
452
     *
453
     * @return true si est? seleccionada la opci?n de utilizar margenes.
454
     */
455
    public boolean isMargin() {
456
        return hasmargin;
457
    }
458

    
459
        public int getResolution(){
460
                return m_resolutionSel;
461
        }
462
    /**
463
     * Selecciona la resoluci?n a aplicar.
464
     *
465
     * @param i tipo de resoluci?n.
466
     */
467
    private void setResolution(int i) {
468
        m_resolutionSel = i;
469

    
470
        switch (i) {
471
            case (RALTO):
472

    
473
                //m_resolution=PrintQuality.HIGH;
474
                DPI = 600;
475

    
476
                break;
477

    
478
            case (RNORMAL):
479

    
480
                //m_resolution=PrintQuality.NORMAL;
481
                DPI = 300;
482

    
483
                break;
484

    
485
            case (RBAJO):
486

    
487
                //m_resolution=PrintQuality.DRAFT;
488
                DPI = 72;
489

    
490
                break;
491
        }
492
    }
493

    
494
    /**
495
     * Devuelve los Attributes que se pasan como parametro al PrintDialog.
496
     *
497
     * @return Attributes de impresi?n.
498
     */
499
    public PrintRequestAttributeSet getAttributes() {
500
        return attributes;
501
    }
502

    
503
    /**
504
     * Devuelve true si la posici?n elegida para el folio es horizontal y false
505
     * si es vertical.
506
     *
507
     * @return true si la posici?n elegida es horizontal.
508
     */
509
    public boolean isLandSpace() {
510
        return m_isLandSel;
511
    }
512

    
513
    /**
514
     * Devuelve el formato de folio seleccionado.
515
     *
516
     * @return entero que representa el tipo de folio seleccionado.
517
     */
518
    public int getType() {
519
        return m_typeSel;
520
    }
521

    
522
    /**
523
     * Devuelve el n?mero de pixels que representa a la unidad que est?
524
     * seleccionada.
525
     *
526
     * @return n?mero de pixels que representa la unidad seleccionada.
527
     */
528
    public double getUnitInPixelsY() {
529
        return m_unitY;
530
    }
531

    
532
    /**
533
     * Devuelve el n?mero de pixels que representa a la unidad que est?
534
     * seleccionada.
535
     *
536
     * @return n?mero de pixels que representa la unidad seleccionada.
537
     */
538
    public double getUnitInPixelsX() {
539
        return m_unitX;
540
    }
541

    
542
    /**
543
     * Establece el n?mero de unidades para el grid.
544
     *
545
     * @param d n?mero que representa la distancia entre los puntos del grid
546
     *        del Layout.
547
     */
548
    public void setNumUnitsX(double d) {
549
        m_numX = d;
550
    }
551

    
552
    /**
553
     * Devuelve el n?mero de unidades del espacio de cuadr?cula horizontal en
554
     * la medida que est? seleccionada,  por defecto en cent?metros.
555
     *
556
     * @return double
557
     */
558
    public double getNumUnitsX() {
559
        return m_numX;
560
    }
561

    
562
    /**
563
     * Devuelve el n?mero de unidades del espacio de cuadr?cula vertical en la
564
     * medida que est? seleccionada,  por defecto en cent?metros.
565
     *
566
     * @return double
567
     */
568
    public double getNumUnitsY() {
569
        return m_numY;
570
    }
571

    
572
    /**
573
     * Establece el n?mero de unidades para el grid.
574
     *
575
     * @param d n?mero que representa la distancia entre los puntos del grid
576
     *        del Layout.
577
     */
578
    public void setNumUnitsY(double d) {
579
        m_numY = d;
580
    }
581

    
582
    /**
583
     * Rellena la forma de visualizaci?n del folio, tru si es horizontal y
584
     * false si es vertical.
585
     *
586
     * @param b true si se quiere a horizontal.
587
     */
588
    public void setIsLandScape(boolean b) {
589
        m_LastLandSel = m_isLandSel;
590
        m_isLandSel = b;
591
        setType(m_typeSel);
592

    
593
        if (m_isLandSel) {
594
            m_Orientation = OrientationRequested.LANDSCAPE;
595
        } else {
596
            m_Orientation = OrientationRequested.PORTRAIT;
597
        }
598
    }
599

    
600
    /**
601
     * Selecci?n del tipo de folio a mostrar.
602
     *
603
     * @param t tipo de folio.
604
     */
605
    public void setType(int t) {
606
        m_typeSel = t;
607

    
608
        //m_sizePaper=getSizePaper(t);
609
        switch (t) {
610
            case (PRINT):
611
                m_type = ((MediaSizeName) attributes.get(Media.class));
612

    
613
                if (isLandSpace()) {
614
                    m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
615
                            METRIC_A4_PAPER_SIZE.getAlto());
616
                } else {
617
                    m_sizePaper = METRIC_A4_PAPER_SIZE;
618
                }
619

    
620
                break;
621

    
622
            case (A4):
623
                m_type = MediaSizeName.ISO_A4;
624

    
625
                if (isLandSpace()) {
626
                    m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
627
                            METRIC_A4_PAPER_SIZE.getAlto());
628
                } else {
629
                    m_sizePaper = METRIC_A4_PAPER_SIZE;
630
                }
631

    
632
                break;
633

    
634
            case (A3):
635
                m_type = MediaSizeName.ISO_A3;
636

    
637
                if (isLandSpace()) {
638
                    m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
639
                            METRIC_A3_PAPER_SIZE.getAlto());
640
                } else {
641
                    m_sizePaper = METRIC_A3_PAPER_SIZE;
642
                }
643

    
644
                break;
645

    
646
            case (A2):
647
                m_type = MediaSizeName.ISO_A2;
648

    
649
                if (isLandSpace()) {
650
                    m_sizePaper = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
651
                            METRIC_A2_PAPER_SIZE.getAlto());
652
                } else {
653
                    m_sizePaper = METRIC_A2_PAPER_SIZE;
654
                }
655

    
656
                break;
657

    
658
            case (A1):
659
                m_type = MediaSizeName.ISO_A1;
660

    
661
                if (isLandSpace()) {
662
                    m_sizePaper = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
663
                            METRIC_A1_PAPER_SIZE.getAlto());
664
                } else {
665
                    m_sizePaper = METRIC_A1_PAPER_SIZE;
666
                }
667

    
668
                break;
669

    
670
            case (A0):
671
                m_type = MediaSizeName.ISO_A0;
672

    
673
                if (isLandSpace()) {
674
                    m_sizePaper = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
675
                            METRIC_A0_PAPER_SIZE.getAlto());
676
                } else {
677
                    m_sizePaper = METRIC_A0_PAPER_SIZE;
678
                }
679

    
680
                break;
681

    
682
            case (CUSTOM):
683
                m_type = MediaSizeName.PERSONAL_ENVELOPE;
684
            if (isLandSpace()) {
685
                m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAncho(),
686
                        CUSTOM_PAPER_SIZE.getAlto());
687
            } else {
688
                m_sizePaper = CUSTOM_PAPER_SIZE;
689
            }
690
                break;
691
        }
692

    
693
        setSizeinUnits(isLandSpace());
694
        m_sizeinUnits = getSizeInUnits();
695
    }
696

    
697
    /**
698
     * Selecci?n del tipo de folio a mostrar.
699
     *
700
     * @param isLand tipo de folio.
701
     * @param type DOCUMENT ME!
702
     *
703
     * @return DOCUMENT ME!
704
     */
705
    public Size getSizeinUnits(boolean isLand, int type) {
706
        Size size = null;
707

    
708
        switch (type) {
709
            case (PRINT):
710

    
711
                if (isLand) {
712
                    size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
713
                            METRIC_A4_PAPER_SIZE.getAlto());
714
                } else {
715
                    size = METRIC_A4_PAPER_SIZE;
716
                }
717

    
718
                break;
719

    
720
            case (A4):
721

    
722
                if (isLand) {
723
                    size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
724
                            METRIC_A4_PAPER_SIZE.getAlto());
725
                } else {
726
                    size = METRIC_A4_PAPER_SIZE;
727
                }
728

    
729
                break;
730

    
731
            case (A3):
732

    
733
                if (isLand) {
734
                    size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
735
                            METRIC_A3_PAPER_SIZE.getAlto());
736
                } else {
737
                    size = METRIC_A3_PAPER_SIZE;
738
                }
739

    
740
                break;
741

    
742
            case (A2):
743

    
744
                if (isLand) {
745
                    size = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
746
                            METRIC_A2_PAPER_SIZE.getAlto());
747
                } else {
748
                    size = METRIC_A2_PAPER_SIZE;
749
                }
750

    
751
                break;
752

    
753
            case (A1):
754

    
755
                if (isLand) {
756
                    size = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
757
                            METRIC_A1_PAPER_SIZE.getAlto());
758
                } else {
759
                    size = METRIC_A1_PAPER_SIZE;
760
                }
761

    
762
                break;
763

    
764
            case (A0):
765

    
766
                if (isLand) {
767
                    size = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
768
                            METRIC_A0_PAPER_SIZE.getAlto());
769
                } else {
770
                    size = METRIC_A0_PAPER_SIZE;
771
                }
772

    
773
                break;
774

    
775
            case (CUSTOM):
776
                    if (isLand) {
777
                    size = new Size(CUSTOM_PAPER_SIZE.getAncho(),
778
                                    CUSTOM_PAPER_SIZE.getAlto());
779
                } else {
780
                    size = CUSTOM_PAPER_SIZE;
781
                }
782
                break;
783
        }
784

    
785
        m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
786
                size.getAncho() / m_TypeUnit);
787

    
788
        return m_sizeinUnits;
789
    }
790

    
791
    /**
792
     * Obtiene el entero que representa el tipo de unidad de medida
793
     * seleccionada.
794
     *
795
     * @return tipo de unidad de medida seleccionada.
796
     */
797
    public int getSelTypeUnit() {
798
        return m_selTypeUnit;
799
    }
800

    
801
    /**
802
     * Devuelve el nombre de la unidad de medida seleccionada.
803
     *
804
     * @return String
805
     */
806
    public String getNameUnit() {
807
        return m_NameUnit;
808
    }
809

    
810
    /**
811
     * Selecci?n de la unidad de medida.
812
     *
813
     * @param sel tipo de unidad de medida.
814
     */
815
    public void setUnit(int sel) {
816
        m_selTypeUnit = sel;
817
        setUnits(sel);
818
        m_numX = m_numY = UNIT[sel];
819
    }
820

    
821
    /**
822
     * Actualiza m_sizeinUnits con la altura y anchura del folio en las
823
     * unidades de mediada seleccionada.
824
     *
825
     * @param b DOCUMENT ME!
826
     */
827
    public void setSizeinUnits(boolean b) {
828
        if (b) {
829
            m_sizeinUnits = new Size(m_sizePaper.getAlto() / m_TypeUnit,
830
                    m_sizePaper.getAncho() / m_TypeUnit);
831
        } else {
832
            m_sizeinUnits = new Size(m_sizePaper.getAncho() / m_TypeUnit,
833
                    m_sizePaper.getAlto() / m_TypeUnit);
834
        }
835
    }
836

    
837
    /**
838
     * Devuelve un double en la unidad de medida seleccionada a partir del
839
     * double en cent?metros que se le pasa como parametro.
840
     *
841
     * @param d distancia en cent?metros.
842
     *
843
     * @return distancia en la unidad de medida seleccionada.
844
     */
845
    public double toUnits(double d) {
846
        NumberFormat nf = NumberFormat.getInstance();
847
        nf.setMaximumFractionDigits(2);
848

    
849
        String s = String.valueOf(nf.format(d));
850
        s = s.replace(',', '.');
851

    
852
        return (Double.valueOf(s).doubleValue() / m_TypeUnit);
853
    }
854

    
855
    /**
856
     * Devuelve un double en cent?metros a partir del double que se pasa como
857
     * par?metro en la unidad de medida establecida.
858
     *
859
     * @param d distancia en unidad de medida seleccionada.
860
     *
861
     * @return distancia en cent?metros.
862
     */
863
    public double fromUnits(double d) {
864
        return (d * m_TypeUnit);
865
    }
866
        public double getPixXCm(Rectangle2D.Double rect){
867
                double value = m_sizePaper.getAncho();
868
                double cm = CHANGE[2];
869
                double unidades = 0;
870
                unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / cm)));
871
                return unidades;
872
        }
873
    /**
874
     * Establece el n?mero de pixels que representan un cent?metro.
875
     *
876
     * @param rect rect?ngulo que representa el folio en pixels.
877
     */
878
    public void setDistanceUnitX(Rectangle2D.Double rect) {
879
        double value = m_sizePaper.getAncho();
880
        double unidades = 0;
881
        unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / m_TypeUnit) / m_numX));
882
        m_unitX = unidades;
883
    }
884

    
885
    /**
886
     * Establece el n?mero de pixels que representan un cent?metro.
887
     *
888
     * @param rect rect?ngulo que representa el folio en pixels.
889
     */
890
    public void setDistanceUnitY(Rectangle2D.Double rect) {
891
        double value = m_sizePaper.getAncho();
892
        double unidades = 0;
893
        unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / m_TypeUnit) / m_numY));
894
        m_unitY = unidades;
895
    }
896

    
897
    /**
898
     * Obtiene el rect?ngulo que representa el folio con las caracter?sticas
899
     * que contiene attributes y diferenciando si es para visualizar en
900
     * pantalla o para imprimir.
901
     *
902
     * @param isPrint si es para imprimir.
903
     * @param rect rect?ngulo a modificar.
904
     * @param w anchura
905
     * @param h altura
906
     *
907
     * @return Rect?ngulo que representa el folio en horizontal o vertical
908
     *         seg?n se haya seleccionado.
909
     */
910
    public Rectangle2D.Double obtainRect(boolean isPrint,
911
        Rectangle2D.Double rect, int w, int h) {
912
        double value1 = 0;
913
        double value2 = 0;
914

    
915
        if (!isPrint) {
916
            if (isLandSpace()) {
917
                anchoXalto = m_sizePaper.getAncho() / m_sizePaper.getAlto();
918
                rect = getRectangleLandscape(rect, w, h);
919
            } else {
920
                anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
921
                rect = getRectanglePortrait(rect, w, h);
922
            }
923
        } else {
924
            value1 = m_sizePaper.getAncho();
925
            value2 = m_sizePaper.getAlto();
926
            rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
927
                ((value2 / PULGADA) * DPI));
928
        }
929

    
930
        setDistanceUnitX(rect);
931
        setDistanceUnitY(rect);
932

    
933
        return rect;
934
    }
935

    
936
    /**
937
     * Modifica el rect?ngulo que representa al folio en una posici?n
938
     * horizontal.
939
     *
940
     * @param rect Rect?ngulo que representa el folio en pixels.
941
     * @param w anchura del Layout.
942
     * @param h altura del Layout.
943
     *
944
     * @return Rect?ngulo modificado.
945
     */
946
    public Rectangle2D.Double getRectangleLandscape(Rectangle2D.Double rect,
947
        int w, int h) {
948
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
949
        int x0 = (int) rect.getMinX();
950
        int y0 = (int) rect.getMinY();
951
        int y1;
952
        int x1;
953
        y1 = (int) (h - (2 * y0));
954
        x1 = (int) (y1 * anchoXalto);
955

    
956
        if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
957
            x1 = (int) ((w) - (2 * x0));
958
            y1 = (int) (x1 / anchoXalto);
959
        }
960

    
961
        rectaux.setRect(x0, y0, x1, y1);
962

    
963
        return rectaux;
964
    }
965

    
966
    /**
967
     * Devuelve un Size con las medidas del folio en las unidades de mediada
968
     * seleccionada.
969
     *
970
     * @return DOCUMENT ME!
971
     */
972
    public Size getSizeInUnits() {
973
        return m_sizeinUnits;
974
    }
975

    
976
    /**
977
     * Modifica el rect?ngulo que representa al folio en una posici?n vertical.
978
     *
979
     * @param rect Rect?ngulo que representa el folio en pixels.
980
     * @param w anchura del Layout.
981
     * @param h altura del Layout.
982
     *
983
     * @return Rect?ngulo modificado.
984
     */
985
    public Rectangle2D.Double getRectanglePortrait(Rectangle2D.Double rect,
986
        int w, int h) {
987
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
988
        int x0 = (int) rect.getMinX();
989
        int y0 = (int) rect.getMinY();
990
        int y1;
991
        int x1;
992
        x1 = (int) (w - (2 * x0));
993
        y1 = (int) (x1 * anchoXalto);
994

    
995
        if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
996
            y1 = (int) (h - (2 * y0));
997
            x1 = (int) (y1 / anchoXalto);
998
        }
999

    
1000
        rectaux.setRect(x0, y0, x1, y1);
1001

    
1002
        return rectaux;
1003
    }
1004

    
1005
    /**
1006
     * Obtiene el tipo de los diferentes formatos de papel.
1007
     *
1008
     * @param size tipo de paper.
1009
     *
1010
     * @return Medidas del papel seleccionado.
1011
     */
1012
    private int getTypePaper(Size size) {
1013
        int tol = 1;
1014
        Size auxSize = size;
1015

    
1016
        if (isLandSpace()) {
1017
            auxSize = new Size(size.getAncho(), size.getAlto());
1018
        }
1019

    
1020
        if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1021
                METRIC_A4_PAPER_SIZE.getAncho())) &&
1022
                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1023
                METRIC_A4_PAPER_SIZE.getAlto()))) {
1024
            return A4;
1025
        } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1026
                METRIC_A3_PAPER_SIZE.getAncho())) &&
1027
                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1028
                METRIC_A3_PAPER_SIZE.getAlto()))) {
1029
            return A3;
1030
        } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1031
                METRIC_A2_PAPER_SIZE.getAncho())) &&
1032
                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1033
                METRIC_A2_PAPER_SIZE.getAlto()))) {
1034
            return A2;
1035
        } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1036
                METRIC_A1_PAPER_SIZE.getAncho())) &&
1037
                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1038
                METRIC_A1_PAPER_SIZE.getAlto()))) {
1039
            return A1;
1040
        } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1041
                METRIC_A0_PAPER_SIZE.getAncho())) &&
1042
                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1043
                METRIC_A0_PAPER_SIZE.getAlto()))) {
1044
            return A0;
1045
        }
1046

    
1047
        return A4;
1048
    }
1049

    
1050
    /**
1051
     * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
1052
     * para poder despu?s volver a crear el objeto original.
1053
     *
1054
     * @return DOCUMENT XMLEntity.
1055
     */
1056
    public XMLEntity getXMLEntity() {
1057
        XMLEntity xml = new XMLEntity();
1058
        xml.putProperty("DPI", DPI);
1059
        xml.putProperty("DPISCREEN", DPISCREEN);
1060
        xml.putProperty("m_TypeUnit", m_TypeUnit);
1061
        xml.putProperty("m_numX", m_numX);
1062
        xml.putProperty("m_numY", m_numY);
1063
        xml.putProperty("m_unitX", m_unitX);
1064
        xml.putProperty("m_unitY", m_unitY);
1065
        xml.putProperty("hasmargin", hasmargin);
1066
        xml.putProperty("m_resolutionSel", m_resolutionSel);
1067
        xml.putProperty("m_typeSel", m_typeSel);
1068
        xml.putProperty("m_selTypeUnit", m_selTypeUnit);
1069
        xml.addChild(m_sizePaper.getXMLEntity());
1070
        xml.putProperty("m_area", m_area);
1071
        xml.putProperty("anchoXalto", anchoXalto);
1072
        xml.addChild(m_sizeinUnits.getXMLEntity());
1073
        // Landscape
1074
        xml.putProperty("m_isLandSel", m_isLandSel);
1075

    
1076
        return xml;
1077
    }
1078

    
1079
    /**
1080
     * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1081
     *
1082
     * @param xml XMLEntity
1083
     *
1084
     * @return Objeto de esta clase.
1085
     */
1086
    public static Attributes createAtributes(XMLEntity xml) {
1087
        Attributes atri = new Attributes();
1088
        DPI = xml.getIntProperty("DPI");
1089
        DPISCREEN = xml.getIntProperty("DPISCREEN");
1090
        atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1091
        atri.m_numX = xml.getDoubleProperty("m_numX");
1092
        atri.m_numY = xml.getDoubleProperty("m_numY");
1093
        atri.m_unitX = xml.getDoubleProperty("m_unitX");
1094
        atri.m_unitY = xml.getDoubleProperty("m_unitY");
1095
        atri.hasmargin = xml.getBooleanProperty("hasmargin");
1096
        atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1097
        atri.m_typeSel = xml.getIntProperty("m_typeSel");
1098
        atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1099
        atri.m_sizePaper = Size.createSize(xml.getChild(0));
1100
        if (atri.hasmargin){
1101
                atri.m_area = xml.getDoubleArrayProperty("m_area");
1102
        }
1103
        atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1104
        atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1105
        atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1106

    
1107
        return atri;
1108
    }
1109
}