Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / Attributes.java @ 7304

History | View | Annotate | Download (29.6 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.project.documents.layout;
46

    
47
import java.awt.Rectangle;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.print.PageFormat;
50
import java.awt.print.Paper;
51
import java.text.NumberFormat;
52

    
53
import javax.print.attribute.HashPrintRequestAttributeSet;
54
import javax.print.attribute.PrintRequestAttributeSet;
55
import javax.print.attribute.standard.Copies;
56
import javax.print.attribute.standard.Fidelity;
57
import javax.print.attribute.standard.Media;
58
import javax.print.attribute.standard.MediaPrintableArea;
59
import javax.print.attribute.standard.MediaSizeName;
60
import javax.print.attribute.standard.OrientationRequested;
61
import javax.print.attribute.standard.PrintQuality;
62
import javax.print.attribute.standard.Sides;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.utiles.XMLEntity;
66

    
67

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

    
86
        //        Para impresi?n
87
        public final static double PULGADA = 2.54;
88

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

    
122
        static {
123
                new Attributes();
124
        }
125

    
126
        private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
127
        private MediaSizeName m_type = MediaSizeName.ISO_A4;
128
        private boolean m_isLandSel;
129
        private OrientationRequested m_Orientation;
130
        private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
131
        private String m_NameUnit;
132
        private double m_numX = 0.25;
133
        private double m_numY = 0.25;
134
        private double m_unitX = 0;
135
        private double m_unitY = 0;
136
        private boolean hasmargin;
137
        private int m_resolutionSel = RNORMAL;
138
        private int m_typeSel = 0;
139
        private int m_selTypeUnit = 2;
140
        public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
141
                        METRIC_A4_PAPER_SIZE.getAncho());
142
        public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
143
        private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
144
        private Size m_sizeinUnits = m_sizePaper;
145
        //private javax.print.attribute.standard.
146
        /**
147
         * 050211, jmorell: Se a?aden los Grados como unidades de mapa.
148
         * Creates a new Attributes object.
149
         */
150
        public Attributes() {
151
                if (NAMES == null) {
152
                        int i = 0;
153
                        NAMES = new String[8];
154
                        NAMES[i++] = PluginServices.getText(this, "Kilometros");
155
                        NAMES[i++] = PluginServices.getText(this, "Metros");
156
                        NAMES[i++] = PluginServices.getText(this, "Centimetros");
157
                        NAMES[i++] = PluginServices.getText(this, "Milimetros");
158
                        NAMES[i++] = PluginServices.getText(this, "Millas");
159
                        NAMES[i++] = PluginServices.getText(this, "Yardas");
160
                        NAMES[i++] = PluginServices.getText(this, "Pies");
161
                        NAMES[i++] = PluginServices.getText(this, "Pulgadas");
162
                        //NAMES[i++] = PluginServices.getText(this, "Grados");
163
                }
164

    
165
                m_NameUnit = NAMES[2];
166
                m_type = MediaSizeName.ISO_A4;
167
                m_isLandSel = true;
168
                setSizeinUnits(m_isLandSel);
169

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

    
178
                setType(m_typeSel);
179

    
180
                attributes.add(new MediaPrintableArea(0, 0,
181
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
182
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
183
                                MediaPrintableArea.MM));
184
        }
185

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

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

    
217
        /**
218
         * Devuelve un PageFormat con las propiedades seleccionadas.
219
         *
220
         * @return PageFormat
221
         */
222
        public PageFormat getPageFormat() {
223
                PageFormat pf1 = new PageFormat();
224
                Paper paper = pf1.getPaper();
225

    
226
                if (isLandSpace()) {
227
                        pf1.setOrientation(0);
228
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
229
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
230

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

    
250
                pf1.setPaper(paper);
251

    
252
                return pf1;
253
        }
254

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

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

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

    
288
                return rect;
289
        }
290

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

    
300
                if (pf.getOrientation() == 0) {
301
                        setIsLandScape(true);
302
                        m_Orientation = OrientationRequested.LANDSCAPE;
303
                        size = new Size(pf.getHeight(), pf.getWidth());
304
                } else {
305
                        setIsLandScape(false);
306
                        m_Orientation = OrientationRequested.PORTRAIT;
307
                        size = new Size(pf.getHeight(), pf.getWidth());
308
                }
309

    
310
                attributes.add(m_Orientation);
311

    
312
                //area
313
                m_area[0] = getInCM(pf.getImageableY());
314
                m_area[2] = getInCM(pf.getImageableX());
315
                m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
316
                        m_area[0];
317
                m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
318
                        m_area[2];
319

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

    
340
                setType(getTypePaper(size));
341
        }
342

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

    
352
                setType(m_typeSel);
353

    
354
                resul.add(m_type);
355

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

    
364
                //resul.add(MediaSize.Other.PERSONAL_ENVELOPE);
365
                //attributes.add(m_resolution);
366
                return resul;
367
        }
368

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

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

    
393
                        clipRect.setRect((area[1] / PULGADA * DPI),
394
                                area[2] / PULGADA * DPI,
395
                                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
396
                                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
397
                }
398
        }
399

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

    
410
        /**
411
         * Devuelve la resoluci?n para imprimir.
412
         *
413
         * @return Resoluci?n seleccionada.
414
         */
415
        public int getResolution() {
416
                return m_resolutionSel;
417
        }
418

    
419
        /**
420
         * Selecciona la resoluci?n a aplicar.
421
         *
422
         * @param i tipo de resoluci?n.
423
         */
424
        private void setResolution(int i) {
425
                m_resolutionSel = i;
426

    
427
                switch (i) {
428
                        case (RALTO):
429

    
430
                                //m_resolution=PrintQuality.HIGH;
431
                                DPI = 600;
432

    
433
                                break;
434

    
435
                        case (RNORMAL):
436

    
437
                                //m_resolution=PrintQuality.NORMAL;
438
                                DPI = 300;
439

    
440
                                break;
441

    
442
                        case (RBAJO):
443

    
444
                                //m_resolution=PrintQuality.DRAFT;
445
                                DPI = 72;
446

    
447
                                break;
448
                }
449
        }
450

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

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

    
470
        /**
471
         * Devuelve el formato de folio seleccionado.
472
         *
473
         * @return entero que representa el tipo de folio seleccionado.
474
         */
475
        public int getType() {
476
                return m_typeSel;
477
        }
478

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

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

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

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

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

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

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

    
549
                if (m_isLandSel) {
550
                        m_Orientation = OrientationRequested.LANDSCAPE;
551
                } else {
552
                        m_Orientation = OrientationRequested.PORTRAIT;
553
                }
554
        }
555

    
556
        /**
557
         * Selecci?n del tipo de folio a mostrar.
558
         *
559
         * @param t tipo de folio.
560
         */
561
        public void setType(int t) {
562
                m_typeSel = t;
563

    
564
                //m_sizePaper=getSizePaper(t);
565
                switch (t) {
566
                        case (PRINT):
567
                                m_type = ((MediaSizeName) attributes.get(Media.class));
568

    
569
                                if (isLandSpace()) {
570
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
571
                                                        METRIC_A4_PAPER_SIZE.getAlto());
572
                                } else {
573
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
574
                                }
575

    
576
                                break;
577

    
578
                        case (A4):
579
                                m_type = MediaSizeName.ISO_A4;
580

    
581
                                if (isLandSpace()) {
582
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
583
                                                        METRIC_A4_PAPER_SIZE.getAlto());
584
                                } else {
585
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
586
                                }
587

    
588
                                break;
589

    
590
                        case (A3):
591
                                m_type = MediaSizeName.ISO_A3;
592

    
593
                                if (isLandSpace()) {
594
                                        m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
595
                                                        METRIC_A3_PAPER_SIZE.getAlto());
596
                                } else {
597
                                        m_sizePaper = METRIC_A3_PAPER_SIZE;
598
                                }
599

    
600
                                break;
601

    
602
                        case (A2):
603
                                m_type = MediaSizeName.ISO_A2;
604

    
605
                                if (isLandSpace()) {
606
                                        m_sizePaper = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
607
                                                        METRIC_A2_PAPER_SIZE.getAlto());
608
                                } else {
609
                                        m_sizePaper = METRIC_A2_PAPER_SIZE;
610
                                }
611

    
612
                                break;
613

    
614
                        case (A1):
615
                                m_type = MediaSizeName.ISO_A1;
616

    
617
                                if (isLandSpace()) {
618
                                        m_sizePaper = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
619
                                                        METRIC_A1_PAPER_SIZE.getAlto());
620
                                } else {
621
                                        m_sizePaper = METRIC_A1_PAPER_SIZE;
622
                                }
623

    
624
                                break;
625

    
626
                        case (A0):
627
                                m_type = MediaSizeName.ISO_A0;
628

    
629
                                if (isLandSpace()) {
630
                                        m_sizePaper = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
631
                                                        METRIC_A0_PAPER_SIZE.getAlto());
632
                                } else {
633
                                        m_sizePaper = METRIC_A0_PAPER_SIZE;
634
                                }
635

    
636
                                break;
637

    
638
                        case (CUSTOM):
639
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
640

    
641
                                if (isLandSpace()) {
642
                                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAncho(),
643
                                                        CUSTOM_PAPER_SIZE.getAlto());
644
                                } else {
645
                                        m_sizePaper = CUSTOM_PAPER_SIZE;
646
                                }
647

    
648
                                break;
649
                }
650

    
651
                setSizeinUnits(isLandSpace());
652
                m_sizeinUnits = getSizeInUnits();
653
        }
654

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

    
666
                switch (type) {
667
                        case (PRINT):
668

    
669
                                if (isLand) {
670
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
671
                                                        METRIC_A4_PAPER_SIZE.getAlto());
672
                                } else {
673
                                        size = METRIC_A4_PAPER_SIZE;
674
                                }
675

    
676
                                break;
677

    
678
                        case (A4):
679

    
680
                                if (isLand) {
681
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
682
                                                        METRIC_A4_PAPER_SIZE.getAlto());
683
                                } else {
684
                                        size = METRIC_A4_PAPER_SIZE;
685
                                }
686

    
687
                                break;
688

    
689
                        case (A3):
690

    
691
                                if (isLand) {
692
                                        size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
693
                                                        METRIC_A3_PAPER_SIZE.getAlto());
694
                                } else {
695
                                        size = METRIC_A3_PAPER_SIZE;
696
                                }
697

    
698
                                break;
699

    
700
                        case (A2):
701

    
702
                                if (isLand) {
703
                                        size = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
704
                                                        METRIC_A2_PAPER_SIZE.getAlto());
705
                                } else {
706
                                        size = METRIC_A2_PAPER_SIZE;
707
                                }
708

    
709
                                break;
710

    
711
                        case (A1):
712

    
713
                                if (isLand) {
714
                                        size = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
715
                                                        METRIC_A1_PAPER_SIZE.getAlto());
716
                                } else {
717
                                        size = METRIC_A1_PAPER_SIZE;
718
                                }
719

    
720
                                break;
721

    
722
                        case (A0):
723

    
724
                                if (isLand) {
725
                                        size = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
726
                                                        METRIC_A0_PAPER_SIZE.getAlto());
727
                                } else {
728
                                        size = METRIC_A0_PAPER_SIZE;
729
                                }
730

    
731
                                break;
732

    
733
                        case (CUSTOM):
734

    
735
                                if (isLand) {
736
                                        size = new Size(CUSTOM_PAPER_SIZE.getAncho(),
737
                                                        CUSTOM_PAPER_SIZE.getAlto());
738
                                } else {
739
                                        size = CUSTOM_PAPER_SIZE;
740
                                }
741

    
742
                                break;
743
                }
744

    
745
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
746
                                size.getAncho() / m_TypeUnit);
747

    
748
                return m_sizeinUnits;
749
        }
750

    
751
        /**
752
         * Obtiene el entero que representa el tipo de unidad de medida
753
         * seleccionada.
754
         *
755
         * @return tipo de unidad de medida seleccionada.
756
         */
757
        public int getSelTypeUnit() {
758
                return m_selTypeUnit;
759
        }
760

    
761
        /**
762
         * Devuelve el nombre de la unidad de medida seleccionada.
763
         *
764
         * @return String
765
         */
766
        public String getNameUnit() {
767
                return m_NameUnit;
768
        }
769

    
770
        /**
771
         * Selecci?n de la unidad de medida.
772
         *
773
         * @param sel tipo de unidad de medida.
774
         */
775
        public void setUnit(int sel) {
776
                m_selTypeUnit = sel;
777
                setUnits(sel);
778
                m_numX = m_numY = UNIT[sel];
779
        }
780

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

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

    
809
                String s = String.valueOf(nf.format(d));
810
                s = s.replace(',', '.');
811

    
812
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
813
        }
814

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

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

    
840
                return unidades;
841
        }
842

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

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

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

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

    
900
                setDistanceUnitX(rect);
901
                setDistanceUnitY(rect);
902

    
903
                return rect;
904
        }
905

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

    
926
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
927
                        x1 = ((w) - (2 * x0));
928
                        y1 = (int) (x1 / anchoXalto);
929
                }
930

    
931
                rectaux.setRect(x0, y0, x1, y1);
932

    
933
                return rectaux;
934
        }
935

    
936
        /**
937
         * Devuelve un Size con las medidas del folio en las unidades de mediada
938
         * seleccionada.
939
         *
940
         * @return Tama?o del folio en las unidades seleccionadas.
941
         */
942
        public Size getSizeInUnits() {
943
                return m_sizeinUnits;
944
        }
945

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

    
965
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
966
                        y1 = (h - (2 * y0));
967
                        x1 = (int) (y1 / anchoXalto);
968
                }
969

    
970
                rectaux.setRect(x0, y0, x1, y1);
971

    
972
                return rectaux;
973
        }
974

    
975
        /**
976
         * Obtiene el tipo de los diferentes formatos de papel.
977
         *
978
         * @param size tipo de paper.
979
         *
980
         * @return Medidas del papel seleccionado.
981
         */
982
        private int getTypePaper(Size size) {
983
                int tol = 1;
984
                Size auxSize = size;
985

    
986
                if (isLandSpace()) {
987
                        auxSize = new Size(size.getAncho(), size.getAlto());
988
                }
989

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

    
1017
                return A4;
1018
        }
1019

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

    
1045
                // Landscape
1046
                xml.putProperty("m_isLandSel", m_isLandSel);
1047

    
1048
                return xml;
1049
        }
1050

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

    
1073
                if (atri.hasmargin) {
1074
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1075
                }
1076

    
1077
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1078
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1079
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1080
                atri.setIsLandScape(atri.m_isLandSel);
1081
                return atri;
1082
        }
1083

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

    
1106
                if (atri.hasmargin) {
1107
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1108
                }
1109

    
1110
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1111
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1112
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1113
                atri.setIsLandScape(atri.m_isLandSel);
1114
                return atri;
1115
        }
1116
}