Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Attributes.java @ 1729

History | View | Annotate | Download (28.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
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
49

    
50
import com.iver.utiles.XMLEntity;
51

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

    
57
import java.text.NumberFormat;
58

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

    
70

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

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

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

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

    
129
        private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
130
        private MediaSizeName m_type = MediaSizeName.ISO_A4;
131
        private boolean m_isLandSel;
132
        private boolean m_LastLandSel = true;
133
        private OrientationRequested m_Orientation;
134
        private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
135
        private String m_NameUnit;
136
        //private double m_numX = 0.25;
137
        //private double m_numY = 0.25;
138
        private double m_unitX = 0;
139
        private double m_unitY = 0;
140
        private boolean hasmargin;
141
        private int m_resolutionSel = RNORMAL;
142
        private PrintQuality m_resolution;
143
        private int m_typeSel = 0;
144
        private int m_selTypeUnit = 2;
145
        public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
146
                        METRIC_A4_PAPER_SIZE.getAncho());
147
        public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
148
        private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
149
        private Size m_sizeinUnits = m_sizePaper;
150
        private CADToolAdapter cta;
151
        /**
152
         * Creates a new Attributes object.
153
         */
154
        public Attributes() {
155
                if (NAMES == null) {
156
                        int i = 0;
157
                        NAMES = new String[8];
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
                }
167

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

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

    
181
                setType(m_typeSel);
182

    
183
                attributes.add(new MediaPrintableArea((float) 0, (float) 0,
184
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
185
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
186
                                MediaPrintableArea.MM));
187
        }
188
        public void setCADToolAdapter(CADToolAdapter cta){
189
                this.cta=cta;
190
                setNumUnitsX(1);
191
                setNumUnitsY(1);
192
        }
193
        /**
194
         * Seleccionar el tipo de unidad que representa el Layout.
195
         *
196
         * @param s entero.
197
         */
198
        public void setUnits(int s) {
199
                m_TypeUnit = CHANGE[s];
200
                m_NameUnit = NAMES[s];
201
        }
202

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

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

    
233
                if (isLandSpace()) {
234
                        pf1.setOrientation(0);
235
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
236
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
237

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

    
257
                pf1.setPaper(paper);
258

    
259
                return pf1;
260
        }
261

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

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

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

    
295
                return rect;
296
        }
297

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

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

    
317
                attributes.add(m_Orientation);
318

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

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

    
347
                setType(getTypePaper(size));
348
        }
349

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

    
359
                setType(m_typeSel);
360

    
361
                resul.add(m_type);
362

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

    
371
                //resul.add(MediaSize.Other.PERSONAL_ENVELOPE);
372
                //attributes.add(m_resolution);
373
                return resul;
374
        }
375

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

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

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

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

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

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

    
434
                switch (i) {
435
                        case (RALTO):
436

    
437
                                //m_resolution=PrintQuality.HIGH;
438
                                DPI = 600;
439

    
440
                                break;
441

    
442
                        case (RNORMAL):
443

    
444
                                //m_resolution=PrintQuality.NORMAL;
445
                                DPI = 300;
446

    
447
                                break;
448

    
449
                        case (RBAJO):
450

    
451
                                //m_resolution=PrintQuality.DRAFT;
452
                                DPI = 72;
453

    
454
                                break;
455
                }
456
        }
457

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

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

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

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

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

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

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

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

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

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

    
557
                if (m_isLandSel) {
558
                        m_Orientation = OrientationRequested.LANDSCAPE;
559
                } else {
560
                        m_Orientation = OrientationRequested.PORTRAIT;
561
                }
562
        }
563

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

    
572
                //m_sizePaper=getSizePaper(t);
573
                switch (t) {
574
                        case (PRINT):
575
                                m_type = ((MediaSizeName) attributes.get(Media.class));
576

    
577
                                if (isLandSpace()) {
578
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
579
                                                        METRIC_A4_PAPER_SIZE.getAlto());
580
                                } else {
581
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
582
                                }
583

    
584
                                break;
585

    
586
                        case (A4):
587
                                m_type = MediaSizeName.ISO_A4;
588

    
589
                                if (isLandSpace()) {
590
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
591
                                                        METRIC_A4_PAPER_SIZE.getAlto());
592
                                } else {
593
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
594
                                }
595

    
596
                                break;
597

    
598
                        case (A3):
599
                                m_type = MediaSizeName.ISO_A3;
600

    
601
                                if (isLandSpace()) {
602
                                        m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
603
                                                        METRIC_A3_PAPER_SIZE.getAlto());
604
                                } else {
605
                                        m_sizePaper = METRIC_A3_PAPER_SIZE;
606
                                }
607

    
608
                                break;
609

    
610
                        case (A2):
611
                                m_type = MediaSizeName.ISO_A2;
612

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

    
620
                                break;
621

    
622
                        case (A1):
623
                                m_type = MediaSizeName.ISO_A1;
624

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

    
632
                                break;
633

    
634
                        case (A0):
635
                                m_type = MediaSizeName.ISO_A0;
636

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

    
644
                                break;
645

    
646
                        case (CUSTOM):
647
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
648

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

    
656
                                break;
657
                }
658

    
659
                setSizeinUnits(isLandSpace());
660
                m_sizeinUnits = getSizeInUnits();
661
        }
662

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

    
674
                switch (type) {
675
                        case (PRINT):
676

    
677
                                if (isLand) {
678
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
679
                                                        METRIC_A4_PAPER_SIZE.getAlto());
680
                                } else {
681
                                        size = METRIC_A4_PAPER_SIZE;
682
                                }
683

    
684
                                break;
685

    
686
                        case (A4):
687

    
688
                                if (isLand) {
689
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
690
                                                        METRIC_A4_PAPER_SIZE.getAlto());
691
                                } else {
692
                                        size = METRIC_A4_PAPER_SIZE;
693
                                }
694

    
695
                                break;
696

    
697
                        case (A3):
698

    
699
                                if (isLand) {
700
                                        size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
701
                                                        METRIC_A3_PAPER_SIZE.getAlto());
702
                                } else {
703
                                        size = METRIC_A3_PAPER_SIZE;
704
                                }
705

    
706
                                break;
707

    
708
                        case (A2):
709

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

    
717
                                break;
718

    
719
                        case (A1):
720

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

    
728
                                break;
729

    
730
                        case (A0):
731

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

    
739
                                break;
740

    
741
                        case (CUSTOM):
742

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

    
750
                                break;
751
                }
752

    
753
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
754
                                size.getAncho() / m_TypeUnit);
755

    
756
                return m_sizeinUnits;
757
        }
758

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

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

    
778
        /**
779
         * Selecci?n de la unidad de medida.
780
         *
781
         * @param sel tipo de unidad de medida.
782
         */
783
        public void setUnit(int sel) {
784
                m_selTypeUnit = sel;
785
                setUnits(sel);
786
                setNumUnitsX(UNIT[sel]);
787
                setNumUnitsY(UNIT[sel]);
788
        }
789

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

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

    
818
                String s = String.valueOf(nf.format(d));
819
                s = s.replace(',', '.');
820

    
821
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
822
        }
823

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

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

    
849
                return unidades;
850
        }
851

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

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

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

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

    
909
                setDistanceUnitX(rect);
910
                setDistanceUnitY(rect);
911

    
912
                return rect;
913
        }
914

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

    
935
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
936
                        x1 = (int) ((w) - (2 * x0));
937
                        y1 = (int) (x1 / anchoXalto);
938
                }
939

    
940
                rectaux.setRect(x0, y0, x1, y1);
941

    
942
                return rectaux;
943
        }
944

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

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

    
974
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
975
                        y1 = (int) (h - (2 * y0));
976
                        x1 = (int) (y1 / anchoXalto);
977
                }
978

    
979
                rectaux.setRect(x0, y0, x1, y1);
980

    
981
                return rectaux;
982
        }
983

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

    
995
                if (isLandSpace()) {
996
                        auxSize = new Size(size.getAncho(), size.getAlto());
997
                }
998

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

    
1026
                return A4;
1027
        }
1028

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

    
1054
                // Landscape
1055
                xml.putProperty("m_isLandSel", m_isLandSel);
1056

    
1057
                return xml;
1058
        }
1059

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

    
1083
                if (atri.hasmargin) {
1084
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1085
                }
1086

    
1087
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1088
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1089
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1090

    
1091
                return atri;
1092
        }
1093
}