Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Attributes.java @ 2183

History | View | Annotate | Download (29.7 KB)

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

    
47
import com.iver.andami.PluginServices;
48

    
49
import com.iver.utiles.XMLEntity;
50

    
51
import java.awt.Rectangle;
52
import java.awt.geom.Rectangle2D;
53
import java.awt.print.PageFormat;
54
import java.awt.print.Paper;
55

    
56
import java.text.NumberFormat;
57

    
58
import javax.print.attribute.HashPrintRequestAttributeSet;
59
import javax.print.attribute.PrintRequestAttributeSet;
60
import javax.print.attribute.standard.Copies;
61
import javax.print.attribute.standard.Fidelity;
62
import javax.print.attribute.standard.Media;
63
import javax.print.attribute.standard.MediaPrintableArea;
64
import javax.print.attribute.standard.MediaSizeName;
65
import javax.print.attribute.standard.OrientationRequested;
66
import javax.print.attribute.standard.PrintQuality;
67
import javax.print.attribute.standard.Sides;
68

    
69

    
70
/**
71
 * Atributos del Layout.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class Attributes {
76
        /** KILOMETROS,METROS,CENTIMETROS,MILIMETROS,MILLAS,YARDAS,PIES,PULGADAS */
77
        public static final double[] CHANGE = {
78
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
79
                };
80
        public static String[] NAMES;
81
        public static final double[] UNIT = {
82
                        0.0000025, 0.0025, 0.25, 2.5, 0.0000025, 0.025, 0.025, 0.25
83
                };
84
        public static final int RALTO = 0;
85
        public static final int RNORMAL = 1;
86
        public static final int RBAJO = 2;
87

    
88
        //        Para impresi?n
89
        public final static double PULGADA = 2.54;
90

    
91
        //public final static Size CUSTOM_PAPER_SIZE = new Size(8.5, 11.0);
92
        public final static Size STANDARD_LETTER_PAPER_SIZE = new Size(8.5, 11.0);
93
        public final static Size STANDARD_FOLIO_PAPER_SIZE = new Size(8.5, 13.0);
94
        public final static Size STANDARD_LEGAL_PAPER_SIZE = new Size(8.5, 14.0);
95
        public final static Size STANDARD_TABLOID_PAPER_SIZE = new Size(11.0, 17.0);
96
        public final static Size METRIC_A0_PAPER_SIZE = new Size(118.9, 84.1);
97
        public final static Size METRIC_A1_PAPER_SIZE = new Size(84.1, 59.4);
98
        public final static Size METRIC_A2_PAPER_SIZE = new Size(59.4, 42.0);
99
        public final static Size METRIC_A3_PAPER_SIZE = new Size(42.0, 29.7);
100
        public final static Size METRIC_A4_PAPER_SIZE = new Size(29.7, 21.0);
101
        public final static Size METRIC_A5_PAPER_SIZE = new Size(21.0, 14.8);
102
        public final static Size ANSI_ENG_A_PAPER_SIZE = new Size(11.0, 8.5);
103
        public final static Size ANSI_ENG_B_PAPER_SIZE = new Size(17.0, 11.0);
104
        public final static Size ANSI_ENG_C_PAPER_SIZE = new Size(22.0, 17.0);
105
        public final static Size ANSI_ENG_D_PAPER_SIZE = new Size(34.0, 22.0);
106
        public final static Size ANSI_ENG_E_PAPER_SIZE = new Size(44.0, 34.0);
107
        public final static Size ANSI_ARCH_A_PAPER_SIZE = new Size(12.0, 9.0);
108
        public final static Size ANSI_ARCH_B_PAPER_SIZE = new Size(18.0, 12.0);
109
        public final static Size ANSI_ARCH_C_PAPER_SIZE = new Size(24.0, 18.0);
110
        public final static Size ANSI_ARCH_D_PAPER_SIZE = new Size(36.0, 24.0);
111
        public final static Size ANSI_ARCH_E_PAPER_SIZE = new Size(42.0, 30.0);
112
        public static Size CUSTOM_PAPER_SIZE = new Size(100.0, 100.0);
113
        public final static int PRINT = 0;
114
        public final static int CUSTOM = 6;
115
        public final static int A0 = 5;
116
        public final static int A1 = 4;
117
        public final static int A2 = 3;
118
        public final static int A3 = 2;
119
        public final static int A4 = 1;
120
        public static int DPI = 300;
121
        public static int DPISCREEN = 72;
122
        public static Rectangle clipRect = new Rectangle();
123

    
124
        static {
125
                new Attributes();
126
        }
127

    
128
        private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
129
        private MediaSizeName m_type = MediaSizeName.ISO_A4;
130
        private boolean m_isLandSel;
131
        private boolean m_LastLandSel = true;
132
        private OrientationRequested m_Orientation;
133
        private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
134
        private String m_NameUnit;
135
        private double m_numX = 0.25;
136
        private double m_numY = 0.25;
137
        private double m_unitX = 0;
138
        private double m_unitY = 0;
139
        private boolean hasmargin;
140
        private int m_resolutionSel = RNORMAL;
141
        private PrintQuality m_resolution;
142
        private int m_typeSel = 0;
143
        private int m_selTypeUnit = 2;
144
        public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
145
                        METRIC_A4_PAPER_SIZE.getAncho());
146
        public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
147
        private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
148
        private Size m_sizeinUnits = m_sizePaper;
149

    
150
        /**
151
         * 050211, jmorell: Se a?aden los Grados como unidades de mapa.
152
         * Creates a new Attributes object.
153
         */
154
        public Attributes() {
155
                if (NAMES == null) {
156
                        int i = 0;
157
                        NAMES = new String[9];
158
                        NAMES[i++] = PluginServices.getText(this, "Kilometros");
159
                        NAMES[i++] = PluginServices.getText(this, "Metros");
160
                        NAMES[i++] = PluginServices.getText(this, "Centimetros");
161
                        NAMES[i++] = PluginServices.getText(this, "Milimetros");
162
                        NAMES[i++] = PluginServices.getText(this, "Millas");
163
                        NAMES[i++] = PluginServices.getText(this, "Yardas");
164
                        NAMES[i++] = PluginServices.getText(this, "Pies");
165
                        NAMES[i++] = PluginServices.getText(this, "Pulgadas");
166
                        NAMES[i++] = PluginServices.getText(this, "Grados");
167
                }
168

    
169
                m_NameUnit = NAMES[2];
170
                m_type = MediaSizeName.ISO_A4;
171
                m_isLandSel = true;
172
                setSizeinUnits(m_isLandSel);
173

    
174
                hasmargin = false;
175
                m_Orientation = OrientationRequested.LANDSCAPE;
176
                attributes.add(new Copies(1));
177
                attributes.add(MediaSizeName.ISO_A4);
178
                attributes.add(Sides.ONE_SIDED);
179
                attributes.add(Fidelity.FIDELITY_FALSE);
180
                attributes.add(PrintQuality.NORMAL);
181

    
182
                setType(m_typeSel);
183

    
184
                attributes.add(new MediaPrintableArea((float) 0, (float) 0,
185
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
186
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
187
                                MediaPrintableArea.MM));
188
        }
189

    
190
        /**
191
         * Seleccionar el tipo de unidad que representa el Layout.
192
         *
193
         * @param s entero.
194
         */
195
        public void setUnits(int s) {
196
                m_TypeUnit = CHANGE[s];
197
                m_NameUnit = NAMES[s];
198
        }
199

    
200
        /**
201
         * Rellena las propiedades del Layout.
202
         *
203
         * @param typeSel tipo de folio.
204
         * @param units unidad de medida.
205
         * @param isLand si es horizontal o vertical.
206
         * @param margin si se deben de coger los margenes que marca la impresora o
207
         *                   no.
208
         * @param resolution tipo de resoluci?n a aplicar.
209
         * @param area area imprimible.
210
         */
211
        public void setSelectedOptions(int typeSel, int units, boolean isLand,
212
                boolean margin, int resolution, double[] area) {
213
                setType(typeSel);
214
                setUnit(units);
215
                setIsLandScape(isLand);
216
                hasmargin = margin;
217
                setResolution(resolution);
218
                m_area = area;
219
        }
220

    
221
        /**
222
         * Devuelve un PageFormat con las propiedades seleccionadas.
223
         *
224
         * @return PageFormat
225
         */
226
        public PageFormat getPageFormat() {
227
                PageFormat pf1 = new PageFormat();
228
                Paper paper = pf1.getPaper();
229

    
230
                if (isLandSpace()) {
231
                        pf1.setOrientation(0);
232
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
233
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
234

    
235
                        double aux = m_area[0];
236
                        m_area[0] = m_area[3];
237
                        m_area[3] = m_area[1];
238
                        m_area[1] = m_area[2];
239
                        m_area[2] = aux;
240
                        paper.setImageableArea(getInPixels(m_area[2]),
241
                                getInPixels(m_area[0]),
242
                                getInPixels(m_sizeinUnits.getAncho() - m_area[2] - m_area[3]),
243
                                getInPixels(m_sizeinUnits.getAlto() - m_area[0] - m_area[1]));
244
                } else {
245
                        pf1.setOrientation(1);
246
                        paper.setSize((m_sizePaper.getAlto() * DPISCREEN) / PULGADA,
247
                                (m_sizePaper.getAncho() * DPISCREEN) / PULGADA);
248
                        paper.setImageableArea(getInPixels(m_area[2]),
249
                                getInPixels(m_area[0]),
250
                                getInPixels(m_sizeinUnits.getAlto() - m_area[2] - m_area[3]),
251
                                getInPixels(m_sizeinUnits.getAncho() - m_area[0] - m_area[1]));
252
                }
253

    
254
                pf1.setPaper(paper);
255

    
256
                return pf1;
257
        }
258

    
259
        /**
260
         * Devuelve en Pixels los cent?metros que se le pasan como par?metro.
261
         *
262
         * @param d cent?metros.
263
         *
264
         * @return Pixels.
265
         */
266
        private double getInPixels(double d) {
267
                return d * (DPISCREEN / PULGADA);
268
        }
269

    
270
        /**
271
         * Devuelve en cent?metros los pixels que se le pasan como par?metro.
272
         *
273
         * @param d Pixel.
274
         *
275
         * @return Cent?metros.
276
         */
277
        private double getInCM(double d) {
278
                return d / (DPISCREEN / PULGADA);
279
        }
280

    
281
        /**
282
         * Devuelve un ret?ngulo que representa el ?rea imprimible.
283
         *
284
         * @return Rect?ngulo.
285
         */
286
        public Rectangle2D.Double getArea() {
287
                Rectangle2D.Double rect = new Rectangle2D.Double();
288
                rect.setRect(m_area[0], m_area[1],
289
                        m_sizePaper.getAncho() - m_area[0] - m_area[2],
290
                        m_sizePaper.getAlto() - m_area[1] - m_area[3]);
291

    
292
                return rect;
293
        }
294

    
295
        /**
296
         * Se rellenan los atributos a partir de un PageFormat que se introduce
297
         * como parametro.
298
         *
299
         * @param pf PageFormat
300
         */
301
        public void setPageFormat(PageFormat pf) {
302
                Size size = null;
303

    
304
                if (pf.getOrientation() == 0) {
305
                        setIsLandScape(true);
306
                        m_Orientation = OrientationRequested.LANDSCAPE;
307
                        size = new Size(pf.getHeight(), pf.getWidth());
308
                } else {
309
                        setIsLandScape(false);
310
                        m_Orientation = OrientationRequested.PORTRAIT;
311
                        size = new Size(pf.getHeight(), pf.getWidth());
312
                }
313

    
314
                attributes.add(m_Orientation);
315

    
316
                //area
317
                m_area[0] = getInCM(pf.getImageableY());
318
                m_area[2] = getInCM(pf.getImageableX());
319
                m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
320
                        m_area[0];
321
                m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
322
                        m_area[2];
323

    
324
                //        tipo formato
325
                if (isLandSpace()) {
326
                        //double aux = m_area[0];
327
                        //m_area[0] = m_area[3];
328
                        //m_area[3] = m_area[1];
329
                        //m_area[1] = m_area[2];
330
                        //m_area[2] = aux;
331
                        attributes.add(new MediaPrintableArea((float) (m_area[2] * 10),
332
                                        (float) (m_area[0] * 10),
333
                                        (float) (m_sizeinUnits.getAlto() - m_area[0] - m_area[1]) * 10,
334
                                        (float) (m_sizeinUnits.getAncho() - m_area[2] - m_area[3]) * 10,
335
                                        MediaPrintableArea.MM));
336
                } else {
337
                        attributes.add(new MediaPrintableArea((float) (m_area[0] * 10),
338
                                        (float) (m_area[1] * 10),
339
                                        (float) (getInCM(pf.getImageableWidth()) * 10),
340
                                        (float) (getInCM(pf.getImageableHeight()) * 10),
341
                                        MediaPrintableArea.MM));
342
                }
343

    
344
                setType(getTypePaper(size));
345
        }
346

    
347
        /**
348
         * Preparamos un PrintRequestAttributeSet con nuestras opciones para
349
         * pasarselo como parametro al PrintDialog.
350
         *
351
         * @return
352
         */
353
        public PrintRequestAttributeSet toPrintAttributes() {
354
                HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
355

    
356
                setType(m_typeSel);
357

    
358
                resul.add(m_type);
359

    
360
                //units, no hace falta a?adirlo a attributes
361
                resul.add(m_Orientation);
362
                setArea(m_area);
363
                resul.add(new MediaPrintableArea((float) 0, (float) 0,
364
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
365
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
366
                                MediaPrintableArea.MM));
367

    
368
                //resul.add(MediaSize.Other.PERSONAL_ENVELOPE);
369
                //attributes.add(m_resolution);
370
                return resul;
371
        }
372

    
373
        /**
374
         * rellenar el ?rea imprimible.
375
         *
376
         * @param area ?rea imprimible.
377
         */
378
        private void setArea(double[] area) {
379
                if (!isLandSpace()) {
380
                        attributes.add(new MediaPrintableArea((float) (area[2] * 10),
381
                                        (float) (area[0] * 10),
382
                                        (float) ((m_sizePaper.getAncho() - area[2] - area[3]) * 10),
383
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
384
                                        MediaPrintableArea.MM));
385

    
386
                        clipRect.setRect((area[2] / PULGADA * DPI),
387
                                area[0] / PULGADA * DPI,
388
                                (m_sizePaper.getAncho() - area[2] - area[3]) / PULGADA * DPI,
389
                                (m_sizePaper.getAlto() - area[0] - area[1]) / PULGADA * DPI);
390
                } else {
391
                        attributes.add(new MediaPrintableArea((float) (area[0] * 10),
392
                                        (float) (area[3] * 10),
393
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
394
                                        (float) ((m_sizePaper.getAncho() - area[3] - area[2]) * 10),
395
                                        MediaPrintableArea.MM));
396

    
397
                        clipRect.setRect((area[1] / PULGADA * DPI),
398
                                area[2] / PULGADA * DPI,
399
                                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
400
                                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
401
                }
402
        }
403

    
404
        /**
405
         * Devuelve true si est? seleccionada la opci?n de utilizar los margenes de
406
         * impresi?n.
407
         *
408
         * @return true si est? seleccionada la opci?n de utilizar margenes.
409
         */
410
        public boolean isMargin() {
411
                return hasmargin;
412
        }
413

    
414
        /**
415
         * Devuelve la resoluci?n para imprimir.
416
         *
417
         * @return Resoluci?n seleccionada.
418
         */
419
        public int getResolution() {
420
                return m_resolutionSel;
421
        }
422

    
423
        /**
424
         * Selecciona la resoluci?n a aplicar.
425
         *
426
         * @param i tipo de resoluci?n.
427
         */
428
        private void setResolution(int i) {
429
                m_resolutionSel = i;
430

    
431
                switch (i) {
432
                        case (RALTO):
433

    
434
                                //m_resolution=PrintQuality.HIGH;
435
                                DPI = 600;
436

    
437
                                break;
438

    
439
                        case (RNORMAL):
440

    
441
                                //m_resolution=PrintQuality.NORMAL;
442
                                DPI = 300;
443

    
444
                                break;
445

    
446
                        case (RBAJO):
447

    
448
                                //m_resolution=PrintQuality.DRAFT;
449
                                DPI = 72;
450

    
451
                                break;
452
                }
453
        }
454

    
455
        /**
456
         * Devuelve los Attributes que se pasan como parametro al PrintDialog.
457
         *
458
         * @return Attributes de impresi?n.
459
         */
460
        public PrintRequestAttributeSet getAttributes() {
461
                return attributes;
462
        }
463

    
464
        /**
465
         * Devuelve true si la posici?n elegida para el folio es horizontal y false
466
         * si es vertical.
467
         *
468
         * @return true si la posici?n elegida es horizontal.
469
         */
470
        public boolean isLandSpace() {
471
                return m_isLandSel;
472
        }
473

    
474
        /**
475
         * Devuelve el formato de folio seleccionado.
476
         *
477
         * @return entero que representa el tipo de folio seleccionado.
478
         */
479
        public int getType() {
480
                return m_typeSel;
481
        }
482

    
483
        /**
484
         * Devuelve el n?mero de pixels que representa a la unidad que est?
485
         * seleccionada.
486
         *
487
         * @return n?mero de pixels que representa la unidad seleccionada.
488
         */
489
        public double getUnitInPixelsY() {
490
                return m_unitY;
491
        }
492

    
493
        /**
494
         * Devuelve el n?mero de pixels que representa a la unidad que est?
495
         * seleccionada.
496
         *
497
         * @return n?mero de pixels que representa la unidad seleccionada.
498
         */
499
        public double getUnitInPixelsX() {
500
                return m_unitX;
501
        }
502

    
503
        /**
504
         * Establece el n?mero de unidades para el grid.
505
         *
506
         * @param d n?mero que representa la distancia entre los puntos del grid
507
         *                   del Layout.
508
         */
509
        public void setNumUnitsX(double d) {
510
                m_numX = d;
511
        }
512

    
513
        /**
514
         * Devuelve el n?mero de unidades del espacio de cuadr?cula horizontal en
515
         * la medida que est? seleccionada,  por defecto en cent?metros.
516
         *
517
         * @return double
518
         */
519
        public double getNumUnitsX() {
520
                return m_numX;
521
        }
522

    
523
        /**
524
         * Devuelve el n?mero de unidades del espacio de cuadr?cula vertical en la
525
         * medida que est? seleccionada,  por defecto en cent?metros.
526
         *
527
         * @return double
528
         */
529
        public double getNumUnitsY() {
530
                return m_numY;
531
        }
532

    
533
        /**
534
         * Establece el n?mero de unidades para el grid.
535
         *
536
         * @param d n?mero que representa la distancia entre los puntos del grid
537
         *                   del Layout.
538
         */
539
        public void setNumUnitsY(double d) {
540
                m_numY = d;
541
        }
542

    
543
        /**
544
         * Rellena la forma de visualizaci?n del folio, tru si es horizontal y
545
         * false si es vertical.
546
         *
547
         * @param b true si se quiere a horizontal.
548
         */
549
        public void setIsLandScape(boolean b) {
550
                m_LastLandSel = m_isLandSel;
551
                m_isLandSel = b;
552
                setType(m_typeSel);
553

    
554
                if (m_isLandSel) {
555
                        m_Orientation = OrientationRequested.LANDSCAPE;
556
                } else {
557
                        m_Orientation = OrientationRequested.PORTRAIT;
558
                }
559
        }
560

    
561
        /**
562
         * Selecci?n del tipo de folio a mostrar.
563
         *
564
         * @param t tipo de folio.
565
         */
566
        public void setType(int t) {
567
                m_typeSel = t;
568

    
569
                //m_sizePaper=getSizePaper(t);
570
                switch (t) {
571
                        case (PRINT):
572
                                m_type = ((MediaSizeName) attributes.get(Media.class));
573

    
574
                                if (isLandSpace()) {
575
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
576
                                                        METRIC_A4_PAPER_SIZE.getAlto());
577
                                } else {
578
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
579
                                }
580

    
581
                                break;
582

    
583
                        case (A4):
584
                                m_type = MediaSizeName.ISO_A4;
585

    
586
                                if (isLandSpace()) {
587
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
588
                                                        METRIC_A4_PAPER_SIZE.getAlto());
589
                                } else {
590
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
591
                                }
592

    
593
                                break;
594

    
595
                        case (A3):
596
                                m_type = MediaSizeName.ISO_A3;
597

    
598
                                if (isLandSpace()) {
599
                                        m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
600
                                                        METRIC_A3_PAPER_SIZE.getAlto());
601
                                } else {
602
                                        m_sizePaper = METRIC_A3_PAPER_SIZE;
603
                                }
604

    
605
                                break;
606

    
607
                        case (A2):
608
                                m_type = MediaSizeName.ISO_A2;
609

    
610
                                if (isLandSpace()) {
611
                                        m_sizePaper = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
612
                                                        METRIC_A2_PAPER_SIZE.getAlto());
613
                                } else {
614
                                        m_sizePaper = METRIC_A2_PAPER_SIZE;
615
                                }
616

    
617
                                break;
618

    
619
                        case (A1):
620
                                m_type = MediaSizeName.ISO_A1;
621

    
622
                                if (isLandSpace()) {
623
                                        m_sizePaper = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
624
                                                        METRIC_A1_PAPER_SIZE.getAlto());
625
                                } else {
626
                                        m_sizePaper = METRIC_A1_PAPER_SIZE;
627
                                }
628

    
629
                                break;
630

    
631
                        case (A0):
632
                                m_type = MediaSizeName.ISO_A0;
633

    
634
                                if (isLandSpace()) {
635
                                        m_sizePaper = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
636
                                                        METRIC_A0_PAPER_SIZE.getAlto());
637
                                } else {
638
                                        m_sizePaper = METRIC_A0_PAPER_SIZE;
639
                                }
640

    
641
                                break;
642

    
643
                        case (CUSTOM):
644
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
645

    
646
                                if (isLandSpace()) {
647
                                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAncho(),
648
                                                        CUSTOM_PAPER_SIZE.getAlto());
649
                                } else {
650
                                        m_sizePaper = CUSTOM_PAPER_SIZE;
651
                                }
652

    
653
                                break;
654
                }
655

    
656
                setSizeinUnits(isLandSpace());
657
                m_sizeinUnits = getSizeInUnits();
658
        }
659

    
660
        /**
661
         * Selecci?n del tipo de folio a mostrar.
662
         *
663
         * @param isLand tipo de folio.
664
         * @param type Tipo de folio.
665
         *
666
         * @return Tama?o del folio seleccionado.
667
         */
668
        public Size getSizeinUnits(boolean isLand, int type) {
669
                Size size = null;
670

    
671
                switch (type) {
672
                        case (PRINT):
673

    
674
                                if (isLand) {
675
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
676
                                                        METRIC_A4_PAPER_SIZE.getAlto());
677
                                } else {
678
                                        size = METRIC_A4_PAPER_SIZE;
679
                                }
680

    
681
                                break;
682

    
683
                        case (A4):
684

    
685
                                if (isLand) {
686
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
687
                                                        METRIC_A4_PAPER_SIZE.getAlto());
688
                                } else {
689
                                        size = METRIC_A4_PAPER_SIZE;
690
                                }
691

    
692
                                break;
693

    
694
                        case (A3):
695

    
696
                                if (isLand) {
697
                                        size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
698
                                                        METRIC_A3_PAPER_SIZE.getAlto());
699
                                } else {
700
                                        size = METRIC_A3_PAPER_SIZE;
701
                                }
702

    
703
                                break;
704

    
705
                        case (A2):
706

    
707
                                if (isLand) {
708
                                        size = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
709
                                                        METRIC_A2_PAPER_SIZE.getAlto());
710
                                } else {
711
                                        size = METRIC_A2_PAPER_SIZE;
712
                                }
713

    
714
                                break;
715

    
716
                        case (A1):
717

    
718
                                if (isLand) {
719
                                        size = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
720
                                                        METRIC_A1_PAPER_SIZE.getAlto());
721
                                } else {
722
                                        size = METRIC_A1_PAPER_SIZE;
723
                                }
724

    
725
                                break;
726

    
727
                        case (A0):
728

    
729
                                if (isLand) {
730
                                        size = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
731
                                                        METRIC_A0_PAPER_SIZE.getAlto());
732
                                } else {
733
                                        size = METRIC_A0_PAPER_SIZE;
734
                                }
735

    
736
                                break;
737

    
738
                        case (CUSTOM):
739

    
740
                                if (isLand) {
741
                                        size = new Size(CUSTOM_PAPER_SIZE.getAncho(),
742
                                                        CUSTOM_PAPER_SIZE.getAlto());
743
                                } else {
744
                                        size = CUSTOM_PAPER_SIZE;
745
                                }
746

    
747
                                break;
748
                }
749

    
750
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
751
                                size.getAncho() / m_TypeUnit);
752

    
753
                return m_sizeinUnits;
754
        }
755

    
756
        /**
757
         * Obtiene el entero que representa el tipo de unidad de medida
758
         * seleccionada.
759
         *
760
         * @return tipo de unidad de medida seleccionada.
761
         */
762
        public int getSelTypeUnit() {
763
                return m_selTypeUnit;
764
        }
765

    
766
        /**
767
         * Devuelve el nombre de la unidad de medida seleccionada.
768
         *
769
         * @return String
770
         */
771
        public String getNameUnit() {
772
                return m_NameUnit;
773
        }
774

    
775
        /**
776
         * Selecci?n de la unidad de medida.
777
         *
778
         * @param sel tipo de unidad de medida.
779
         */
780
        public void setUnit(int sel) {
781
                m_selTypeUnit = sel;
782
                setUnits(sel);
783
                m_numX = m_numY = UNIT[sel];
784
        }
785

    
786
        /**
787
         * Actualiza m_sizeinUnits con la altura y anchura del folio en las
788
         * unidades de mediada seleccionada.
789
         *
790
         * @param b True si es en horizontal.
791
         */
792
        public void setSizeinUnits(boolean b) {
793
                if (b) {
794
                        m_sizeinUnits = new Size(m_sizePaper.getAlto() / m_TypeUnit,
795
                                        m_sizePaper.getAncho() / m_TypeUnit);
796
                } else {
797
                        m_sizeinUnits = new Size(m_sizePaper.getAncho() / m_TypeUnit,
798
                                        m_sizePaper.getAlto() / m_TypeUnit);
799
                }
800
        }
801

    
802
        /**
803
         * Devuelve un double en la unidad de medida seleccionada a partir del
804
         * double en cent?metros que se le pasa como parametro.
805
         *
806
         * @param d distancia en cent?metros.
807
         *
808
         * @return distancia en la unidad de medida seleccionada.
809
         */
810
        public double toUnits(double d) {
811
                NumberFormat nf = NumberFormat.getInstance();
812
                nf.setMaximumFractionDigits(2);
813

    
814
                String s = String.valueOf(nf.format(d));
815
                s = s.replace(',', '.');
816

    
817
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
818
        }
819

    
820
        /**
821
         * Devuelve un double en cent?metros a partir del double que se pasa como
822
         * par?metro en la unidad de medida establecida.
823
         *
824
         * @param d distancia en unidad de medida seleccionada.
825
         *
826
         * @return distancia en cent?metros.
827
         */
828
        public double fromUnits(double d) {
829
                return (d * m_TypeUnit);
830
        }
831

    
832
        /**
833
         * Devuelve los pixels por cent?metro.
834
         *
835
         * @param rect Rect?ngulo.
836
         *
837
         * @return Pixels por cent?metro.
838
         */
839
        public double getPixXCm(Rectangle2D.Double rect) {
840
                double value = m_sizePaper.getAncho();
841
                double cm = CHANGE[2];
842
                double unidades = 0;
843
                unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / cm)));
844

    
845
                return unidades;
846
        }
847

    
848
        /**
849
         * Establece el n?mero de pixels que representan un cent?metro.
850
         *
851
         * @param rect rect?ngulo que representa el folio en pixels.
852
         */
853
        public void setDistanceUnitX(Rectangle2D.Double rect) {
854
                double value = m_sizePaper.getAncho();
855
                double unidades = 0;
856
                unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / m_TypeUnit) / m_numX));
857
                m_unitX = unidades;
858
        }
859

    
860
        /**
861
         * Establece el n?mero de pixels que representan un cent?metro.
862
         *
863
         * @param rect rect?ngulo que representa el folio en pixels.
864
         */
865
        public void setDistanceUnitY(Rectangle2D.Double rect) {
866
                double value = m_sizePaper.getAncho();
867
                double unidades = 0;
868
                unidades = ((double) (rect.getMaxX() - rect.getMinX()) / (double) ((value / m_TypeUnit) / m_numY));
869
                m_unitY = unidades;
870
        }
871

    
872
        /**
873
         * Obtiene el rect?ngulo que representa el folio con las caracter?sticas
874
         * que contiene attributes y diferenciando si es para visualizar en
875
         * pantalla o para imprimir.
876
         *
877
         * @param isPrint si es para imprimir.
878
         * @param rect rect?ngulo a modificar.
879
         * @param w anchura
880
         * @param h altura
881
         *
882
         * @return Rect?ngulo que representa el folio en horizontal o vertical
883
         *                    seg?n se haya seleccionado.
884
         */
885
        public Rectangle2D.Double obtainRect(boolean isPrint,
886
                Rectangle2D.Double rect, int w, int h) {
887
                double value1 = 0;
888
                double value2 = 0;
889

    
890
                if (!isPrint) {
891
                        if (isLandSpace()) {
892
                                anchoXalto = m_sizePaper.getAncho() / m_sizePaper.getAlto();
893
                                rect = getRectangleLandscape(rect, w, h);
894
                        } else {
895
                                anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
896
                                rect = getRectanglePortrait(rect, w, h);
897
                        }
898
                } else {
899
                        value1 = m_sizePaper.getAncho();
900
                        value2 = m_sizePaper.getAlto();
901
                        rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
902
                                ((value2 / PULGADA) * DPI));
903
                }
904

    
905
                setDistanceUnitX(rect);
906
                setDistanceUnitY(rect);
907

    
908
                return rect;
909
        }
910

    
911
        /**
912
         * Modifica el rect?ngulo que representa al folio en una posici?n
913
         * horizontal.
914
         *
915
         * @param rect Rect?ngulo que representa el folio en pixels.
916
         * @param w anchura del Layout.
917
         * @param h altura del Layout.
918
         *
919
         * @return Rect?ngulo modificado.
920
         */
921
        public Rectangle2D.Double getRectangleLandscape(Rectangle2D.Double rect,
922
                int w, int h) {
923
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
924
                int x0 = (int) rect.getMinX();
925
                int y0 = (int) rect.getMinY();
926
                int y1;
927
                int x1;
928
                y1 = (int) (h - (2 * y0));
929
                x1 = (int) (y1 * anchoXalto);
930

    
931
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
932
                        x1 = (int) ((w) - (2 * x0));
933
                        y1 = (int) (x1 / anchoXalto);
934
                }
935

    
936
                rectaux.setRect(x0, y0, x1, y1);
937

    
938
                return rectaux;
939
        }
940

    
941
        /**
942
         * Devuelve un Size con las medidas del folio en las unidades de mediada
943
         * seleccionada.
944
         *
945
         * @return Tama?o del folio en las unidades seleccionadas.
946
         */
947
        public Size getSizeInUnits() {
948
                return m_sizeinUnits;
949
        }
950

    
951
        /**
952
         * Modifica el rect?ngulo que representa al folio en una posici?n vertical.
953
         *
954
         * @param rect Rect?ngulo que representa el folio en pixels.
955
         * @param w anchura del Layout.
956
         * @param h altura del Layout.
957
         *
958
         * @return Rect?ngulo modificado.
959
         */
960
        public Rectangle2D.Double getRectanglePortrait(Rectangle2D.Double rect,
961
                int w, int h) {
962
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
963
                int x0 = (int) rect.getMinX();
964
                int y0 = (int) rect.getMinY();
965
                int y1;
966
                int x1;
967
                x1 = (int) (w - (2 * x0));
968
                y1 = (int) (x1 * anchoXalto);
969

    
970
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
971
                        y1 = (int) (h - (2 * y0));
972
                        x1 = (int) (y1 / anchoXalto);
973
                }
974

    
975
                rectaux.setRect(x0, y0, x1, y1);
976

    
977
                return rectaux;
978
        }
979

    
980
        /**
981
         * Obtiene el tipo de los diferentes formatos de papel.
982
         *
983
         * @param size tipo de paper.
984
         *
985
         * @return Medidas del papel seleccionado.
986
         */
987
        private int getTypePaper(Size size) {
988
                int tol = 1;
989
                Size auxSize = size;
990

    
991
                if (isLandSpace()) {
992
                        auxSize = new Size(size.getAncho(), size.getAlto());
993
                }
994

    
995
                if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
996
                                METRIC_A4_PAPER_SIZE.getAncho())) &&
997
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
998
                                METRIC_A4_PAPER_SIZE.getAlto()))) {
999
                        return A4;
1000
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1001
                                METRIC_A3_PAPER_SIZE.getAncho())) &&
1002
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1003
                                METRIC_A3_PAPER_SIZE.getAlto()))) {
1004
                        return A3;
1005
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1006
                                METRIC_A2_PAPER_SIZE.getAncho())) &&
1007
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1008
                                METRIC_A2_PAPER_SIZE.getAlto()))) {
1009
                        return A2;
1010
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1011
                                METRIC_A1_PAPER_SIZE.getAncho())) &&
1012
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1013
                                METRIC_A1_PAPER_SIZE.getAlto()))) {
1014
                        return A1;
1015
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
1016
                                METRIC_A0_PAPER_SIZE.getAncho())) &&
1017
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
1018
                                METRIC_A0_PAPER_SIZE.getAlto()))) {
1019
                        return A0;
1020
                }
1021

    
1022
                return A4;
1023
        }
1024

    
1025
        /**
1026
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
1027
         * para poder despu?s volver a crear el objeto original.
1028
         *
1029
         * @return XMLEntity.
1030
         */
1031
        public XMLEntity getXMLEntity() {
1032
                XMLEntity xml = new XMLEntity();
1033
                xml.putProperty("className",this.getClass().getName());
1034
                xml.putProperty("DPI", DPI);
1035
                xml.putProperty("DPISCREEN", DPISCREEN);
1036
                xml.putProperty("m_TypeUnit", m_TypeUnit);
1037
                xml.putProperty("m_numX", m_numX);
1038
                xml.putProperty("m_numY", m_numY);
1039
                xml.putProperty("m_unitX", m_unitX);
1040
                xml.putProperty("m_unitY", m_unitY);
1041
                xml.putProperty("hasmargin", hasmargin);
1042
                xml.putProperty("m_resolutionSel", m_resolutionSel);
1043
                xml.putProperty("m_typeSel", m_typeSel);
1044
                xml.putProperty("m_selTypeUnit", m_selTypeUnit);
1045
                xml.addChild(m_sizePaper.getXMLEntity());
1046
                xml.putProperty("m_area", m_area);
1047
                xml.putProperty("anchoXalto", anchoXalto);
1048
                xml.addChild(m_sizeinUnits.getXMLEntity());
1049

    
1050
                // Landscape
1051
                xml.putProperty("m_isLandSel", m_isLandSel);
1052

    
1053
                return xml;
1054
        }
1055

    
1056
        /**
1057
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1058
         *
1059
         * @param xml XMLEntity
1060
         *
1061
         * @return Objeto de esta clase.
1062
         */
1063
        public static Attributes createAtributes(XMLEntity xml) {
1064
                Attributes atri = new Attributes();
1065
                DPI = xml.getIntProperty("DPI");
1066
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1067
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1068
                atri.m_numX = xml.getDoubleProperty("m_numX");
1069
                atri.m_numY = xml.getDoubleProperty("m_numY");
1070
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1071
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1072
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1073
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1074
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1075
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1076
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1077

    
1078
                if (atri.hasmargin) {
1079
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1080
                }
1081

    
1082
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1083
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1084
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1085
                atri.setIsLandScape(atri.m_isLandSel);
1086
                return atri;
1087
        }
1088

    
1089
        /**
1090
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1091
         *
1092
         * @param xml XMLEntity
1093
         *
1094
         * @return Objeto de esta clase.
1095
         */
1096
        public static Attributes createAtributes03(XMLEntity xml) {
1097
                Attributes atri = new Attributes();
1098
                DPI = xml.getIntProperty("DPI");
1099
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1100
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1101
                atri.m_numX = xml.getDoubleProperty("m_numX");
1102
                atri.m_numY = xml.getDoubleProperty("m_numY");
1103
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1104
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1105
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1106
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1107
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1108
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1109
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1110

    
1111
                if (atri.hasmargin) {
1112
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1113
                }
1114

    
1115
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1116
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1117
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1118
                atri.setIsLandScape(atri.m_isLandSel);
1119
                return atri;
1120
        }
1121
}