Statistics
| Revision:

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

History | View | Annotate | Download (30.4 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.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.awt.print.PageFormat;
51
import java.awt.print.Paper;
52
import java.text.NumberFormat;
53

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

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

    
68

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

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

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

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

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

    
150
        /**
151
         * 050211, jmorell: Se a?aden los Grados como unidades de mapa.
152
         * Creates a new Attributes object.
153
         */
154
        public Attributes() {
155
                m_NameUnit = NAMES[2];
156
                m_type = MediaSizeName.ISO_A4;
157
                m_isLandSel = true;
158
                setSizeinUnits(m_isLandSel);
159

    
160
                hasmargin = false;
161
                m_Orientation = OrientationRequested.LANDSCAPE;
162
                attributes.add(new Copies(1));
163
                attributes.add(MediaSizeName.ISO_A4);
164
                attributes.add(Sides.ONE_SIDED);
165
                attributes.add(Fidelity.FIDELITY_FALSE);
166
                attributes.add(PrintQuality.NORMAL);
167

    
168
                setType(m_typeSel);
169

    
170
                attributes.add(new MediaPrintableArea(0, 0,
171
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
172
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
173
                                MediaPrintableArea.MM));
174
        }
175

    
176
        /**
177
         * Seleccionar el tipo de unidad que representa el Layout.
178
         *
179
         * @param s entero.
180
         */
181
        public void setUnits(int s) {
182
                m_TypeUnit = CHANGE[s];
183
                m_NameUnit = NAMES[s];
184
        }
185

    
186
        /**
187
         * Rellena las propiedades del Layout.
188
         *
189
         * @param typeSel tipo de folio.
190
         * @param units unidad de medida.
191
         * @param isLand si es horizontal o vertical.
192
         * @param margin si se deben de coger los margenes que marca la impresora o
193
         *                   no.
194
         * @param resolution tipo de resoluci?n a aplicar.
195
         * @param area area imprimible.
196
         */
197
        public void setSelectedOptions(int typeSel, int units, boolean isLand,
198
                boolean margin, int resolution, double[] area) {
199
                setType(typeSel);
200
                setUnit(units);
201
                setIsLandScape(isLand);
202
                hasmargin = margin;
203
                setResolution(resolution);
204
                m_area = area;
205
        }
206

    
207
        /**
208
         * Devuelve un PageFormat con las propiedades seleccionadas.
209
         *
210
         * @return PageFormat
211
         */
212
        public PageFormat getPageFormat() {
213
                PageFormat pf1 = new PageFormat();
214
                Paper paper = pf1.getPaper();
215

    
216
                if (isLandSpace()) {
217
                        pf1.setOrientation(0);
218
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
219
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
220

    
221
                        double aux = m_area[0];
222
                        m_area[0] = m_area[3];
223
                        m_area[3] = m_area[1];
224
                        m_area[1] = m_area[2];
225
                        m_area[2] = aux;
226
                        paper.setImageableArea(getInPixels(m_area[2]),
227
                                getInPixels(m_area[0]),
228
                                getInPixels(m_sizeinUnits.getAncho() - m_area[2] - m_area[3]),
229
                                getInPixels(m_sizeinUnits.getAlto() - m_area[0] - m_area[1]));
230
                } else {
231
                        pf1.setOrientation(1);
232
                        paper.setSize((m_sizePaper.getAlto() * DPISCREEN) / PULGADA,
233
                                (m_sizePaper.getAncho() * DPISCREEN) / PULGADA);
234
                        paper.setImageableArea(getInPixels(m_area[2]),
235
                                getInPixels(m_area[0]),
236
                                getInPixels(m_sizeinUnits.getAlto() - m_area[2] - m_area[3]),
237
                                getInPixels(m_sizeinUnits.getAncho() - m_area[0] - m_area[1]));
238
                }
239

    
240
                pf1.setPaper(paper);
241

    
242
                return pf1;
243
        }
244

    
245
        /**
246
         * Devuelve en Pixels los cent?metros que se le pasan como par?metro.
247
         *
248
         * @param d cent?metros.
249
         *
250
         * @return Pixels.
251
         */
252
        private double getInPixels(double d) {
253
                return d * (DPISCREEN / PULGADA);
254
        }
255

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

    
267
        /**
268
         * Devuelve un ret?ngulo que representa el ?rea imprimible.
269
         *
270
         * @return Rect?ngulo.
271
         */
272
        public Rectangle2D.Double getArea() {
273
                Rectangle2D.Double rect = new Rectangle2D.Double();
274
                rect.setRect(m_area[0], m_area[1],
275
                        m_sizePaper.getAncho() - m_area[0] - m_area[2],
276
                        m_sizePaper.getAlto() - m_area[1] - m_area[3]);
277

    
278
                return rect;
279
        }
280

    
281
        /**
282
         * Se rellenan los atributos a partir de un PageFormat que se introduce
283
         * como parametro.
284
         *
285
         * @param pf PageFormat
286
         */
287
        public void setPageFormat(PageFormat pf) {
288
                Size size = null;
289

    
290
                if (pf.getOrientation() == 0) {
291
                        setIsLandScape(true);
292
                        m_Orientation = OrientationRequested.LANDSCAPE;
293
                        size = new Size(pf.getHeight(), pf.getWidth());
294
                } else {
295
                        setIsLandScape(false);
296
                        m_Orientation = OrientationRequested.PORTRAIT;
297
                        size = new Size(pf.getHeight(), pf.getWidth());
298
                }
299

    
300
                attributes.add(m_Orientation);
301

    
302
                //area
303
                m_area[0] = getInCM(pf.getImageableY());
304
                m_area[2] = getInCM(pf.getImageableX());
305
                m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
306
                        m_area[0];
307
                m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
308
                        m_area[2];
309

    
310
                //        tipo formato
311
                if (isLandSpace()) {
312
                        //double aux = m_area[0];
313
                        //m_area[0] = m_area[3];
314
                        //m_area[3] = m_area[1];
315
                        //m_area[1] = m_area[2];
316
                        //m_area[2] = aux;
317
                        attributes.add(new MediaPrintableArea((float) (m_area[2] * 10),
318
                                        (float) (m_area[0] * 10),
319
                                        (float) (m_sizeinUnits.getAlto() - m_area[0] - m_area[1]) * 10,
320
                                        (float) (m_sizeinUnits.getAncho() - m_area[2] - m_area[3]) * 10,
321
                                        MediaPrintableArea.MM));
322
                } else {
323
                        attributes.add(new MediaPrintableArea((float) (m_area[0] * 10),
324
                                        (float) (m_area[1] * 10),
325
                                        (float) (getInCM(pf.getImageableWidth()) * 10),
326
                                        (float) (getInCM(pf.getImageableHeight()) * 10),
327
                                        MediaPrintableArea.MM));
328
                }
329

    
330
                setType(getTypePaper(size));
331
        }
332

    
333
        /**
334
         * Preparamos un PrintRequestAttributeSet con nuestras opciones para
335
         * pasarselo como parametro al PrintDialog.
336
         *
337
         * @return
338
         */
339
        public PrintRequestAttributeSet toPrintAttributes() {
340
                HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
341

    
342
                setType(m_typeSel);
343

    
344
                resul.add(m_type);
345

    
346
                //units, no hace falta a?adirlo a attributes
347
                resul.add(m_Orientation);
348
                setArea(m_area);
349
                resul.add(new MediaPrintableArea(0, 0,
350
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
351
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
352
                                MediaPrintableArea.MM));
353

    
354
                resul.add(m_resolution);
355
                //resul.add(new PrinterResolution(DPI,DPI,PrinterResolution.DPI));
356
                return resul;
357
        }
358

    
359
        /**
360
         * rellenar el ?rea imprimible.
361
         *
362
         * @param area ?rea imprimible.
363
         */
364
        private void setArea(double[] area) {
365
                if (!isLandSpace()) {
366
                        attributes.add(new MediaPrintableArea((float) (area[2] * 10),
367
                                        (float) (area[0] * 10),
368
                                        (float) ((m_sizePaper.getAncho() - area[2] - area[3]) * 10),
369
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
370
                                        MediaPrintableArea.MM));
371

    
372
                        clipRect.setRect((area[2] / PULGADA * DPI),
373
                                area[0] / PULGADA * DPI,
374
                                (m_sizePaper.getAncho() - area[2] - area[3]) / PULGADA * DPI,
375
                                (m_sizePaper.getAlto() - area[0] - area[1]) / PULGADA * DPI);
376
                } else {
377
                        attributes.add(new MediaPrintableArea((float) (area[0] * 10),
378
                                        (float) (area[3] * 10),
379
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
380
                                        (float) ((m_sizePaper.getAncho() - area[3] - area[2]) * 10),
381
                                        MediaPrintableArea.MM));
382

    
383
                        clipRect.setRect((area[1] / PULGADA * DPI),
384
                                area[2] / PULGADA * DPI,
385
                                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
386
                                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
387
                }
388
        }
389

    
390
        /**
391
         * Devuelve true si est? seleccionada la opci?n de utilizar los margenes de
392
         * impresi?n.
393
         *
394
         * @return true si est? seleccionada la opci?n de utilizar margenes.
395
         */
396
        public boolean isMargin() {
397
                return hasmargin;
398
        }
399

    
400
        /**
401
         * Devuelve la resoluci?n para imprimir.
402
         *
403
         * @return Resoluci?n seleccionada.
404
         */
405
        public int getResolution() {
406
                return m_resolutionSel;
407
        }
408

    
409
        /**
410
         * Selecciona la resoluci?n a aplicar.
411
         *
412
         * @param i tipo de resoluci?n.
413
         */
414
        private void setResolution(int i) {
415
                m_resolutionSel = i;
416

    
417
                switch (i) {
418
                        case (RALTO):
419

    
420
                                m_resolution=PrintQuality.HIGH;
421
                                DPI = 600;
422

    
423
                                break;
424

    
425
                        case (RNORMAL):
426

    
427
                                m_resolution=PrintQuality.NORMAL;
428
                                DPI = 300;
429

    
430
                                break;
431

    
432
                        case (RBAJO):
433

    
434
                                m_resolution=PrintQuality.DRAFT;
435
                                DPI = 72;
436

    
437
                                break;
438
                }
439
        }
440

    
441
        /**
442
         * Devuelve los Attributes que se pasan como parametro al PrintDialog.
443
         *
444
         * @return Attributes de impresi?n.
445
         */
446
        public PrintRequestAttributeSet getAttributes() {
447
                return attributes;
448
        }
449

    
450
        /**
451
         * Devuelve true si la posici?n elegida para el folio es horizontal y false
452
         * si es vertical.
453
         *
454
         * @return true si la posici?n elegida es horizontal.
455
         */
456
        public boolean isLandSpace() {
457
                return m_isLandSel;
458
        }
459

    
460
        /**
461
         * Devuelve el formato de folio seleccionado.
462
         *
463
         * @return entero que representa el tipo de folio seleccionado.
464
         */
465
        public int getType() {
466
                return m_typeSel;
467
        }
468

    
469
        /**
470
         * Devuelve el n?mero de pixels que representa a la unidad que est?
471
         * seleccionada.
472
         *
473
         * @return n?mero de pixels que representa la unidad seleccionada.
474
         */
475
        public double getUnitInPixelsY() {
476
                return m_unitY;
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 getUnitInPixelsX() {
486
                return m_unitX;
487
        }
488

    
489
        /**
490
         * Establece el n?mero de unidades para el grid.
491
         *
492
         * @param d n?mero que representa la distancia entre los puntos del grid
493
         *                   del Layout.
494
         */
495
        public void setNumUnitsX(double d) {
496
                m_numX = new Double(d);
497
        }
498

    
499
        /**
500
         * Devuelve el n?mero de unidades del espacio de cuadr?cula horizontal en
501
         * la medida que est? seleccionada,  por defecto en cent?metros.
502
         *
503
         * @return double
504
         */
505
        public double getNumUnitsX() {
506
                if (m_numX == null)
507
                        m_numX = new Double(getDefaultGridGap().getX());
508
                return m_numX.doubleValue();
509
        }
510

    
511
        /**
512
         * Devuelve el n?mero de unidades del espacio de cuadr?cula vertical en la
513
         * medida que est? seleccionada,  por defecto en cent?metros.
514
         *
515
         * @return double
516
         */
517
        public double getNumUnitsY() {
518
                if (m_numY == null)
519
                        m_numY = new Double(getDefaultGridGap().getY());
520
                return m_numY.doubleValue();
521
        }
522

    
523
        /**
524
         * Establece el n?mero de unidades para el grid.
525
         *
526
         * @param d n?mero que representa la distancia entre los puntos del grid
527
         *                   del Layout.
528
         */
529
        public void setNumUnitsY(double d) {
530
                m_numY = new Double(d);
531
        }
532

    
533
        /**
534
         * Rellena la forma de visualizaci?n del folio, tru si es horizontal y
535
         * false si es vertical.
536
         *
537
         * @param b true si se quiere a horizontal.
538
         */
539
        public void setIsLandScape(boolean b) {
540
                m_isLandSel = b;
541
                setType(m_typeSel);
542

    
543
                if (m_isLandSel) {
544
                        m_Orientation = OrientationRequested.LANDSCAPE;
545
                } else {
546
                        m_Orientation = OrientationRequested.PORTRAIT;
547
                }
548
        }
549

    
550
        /**
551
         * Selecci?n del tipo de folio a mostrar.
552
         *
553
         * @param t tipo de folio.
554
         */
555
        public void setType(int t) {
556
                m_typeSel = t;
557

    
558
                //m_sizePaper=getSizePaper(t);
559
                switch (t) {
560
                        case (PRINT):
561
                                m_type = ((MediaSizeName) attributes.get(Media.class));
562

    
563
                                if (isLandSpace()) {
564
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
565
                                                        METRIC_A4_PAPER_SIZE.getAlto());
566
                                } else {
567
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
568
                                }
569

    
570
                                break;
571

    
572
                        case (A4):
573
                                m_type = MediaSizeName.ISO_A4;
574

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

    
582
                                break;
583

    
584
                        case (A3):
585
                                m_type = MediaSizeName.ISO_A3;
586

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

    
594
                                break;
595

    
596
                        case (A2):
597
                                m_type = MediaSizeName.ISO_A2;
598

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

    
606
                                break;
607

    
608
                        case (A1):
609
                                m_type = MediaSizeName.ISO_A1;
610

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

    
618
                                break;
619

    
620
                        case (A0):
621
                                m_type = MediaSizeName.ISO_A0;
622

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

    
630
                                break;
631

    
632
                        case (CUSTOM):
633
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
634

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

    
642
                                break;
643
                }
644

    
645
                setSizeinUnits(isLandSpace());
646
                m_sizeinUnits = getSizeInUnits();
647
        }
648

    
649
        /**
650
         * Selecci?n del tipo de folio a mostrar.
651
         *
652
         * @param isLand tipo de folio.
653
         * @param type Tipo de folio.
654
         *
655
         * @return Tama?o del folio seleccionado.
656
         */
657
        public Size getSizeinUnits(boolean isLand, int type) {
658
                Size size = null;
659

    
660
                switch (type) {
661
                        case (PRINT):
662

    
663
                                if (isLand) {
664
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
665
                                                        METRIC_A4_PAPER_SIZE.getAlto());
666
                                } else {
667
                                        size = METRIC_A4_PAPER_SIZE;
668
                                }
669

    
670
                                break;
671

    
672
                        case (A4):
673

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

    
681
                                break;
682

    
683
                        case (A3):
684

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

    
692
                                break;
693

    
694
                        case (A2):
695

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

    
703
                                break;
704

    
705
                        case (A1):
706

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

    
714
                                break;
715

    
716
                        case (A0):
717

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

    
725
                                break;
726

    
727
                        case (CUSTOM):
728

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

    
736
                                break;
737
                }
738

    
739
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
740
                                size.getAncho() / m_TypeUnit);
741

    
742
                return m_sizeinUnits;
743
        }
744

    
745
        /**
746
         * Obtiene el entero que representa el tipo de unidad de medida
747
         * seleccionada.
748
         *
749
         * @return tipo de unidad de medida seleccionada.
750
         */
751
        public int getSelTypeUnit() {
752
                return m_selTypeUnit;
753
        }
754

    
755
        /**
756
         * Devuelve el nombre de la unidad de medida seleccionada.
757
         *
758
         * @return String
759
         */
760
        public String getNameUnit() {
761
                return m_NameUnit;
762
        }
763

    
764
        /**
765
         * Selecci?n de la unidad de medida.
766
         *
767
         * @param sel tipo de unidad de medida.
768
         */
769
        public void setUnit(int sel) {
770
                m_selTypeUnit = sel;
771
                setUnits(sel);
772
                m_numX = m_numY = new Double(UNIT[sel]);
773
        }
774

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

    
791
        /**
792
         * Devuelve un double en la unidad de medida seleccionada a partir del
793
         * double en cent?metros que se le pasa como parametro.
794
         *
795
         * @param d distancia en cent?metros.
796
         *
797
         * @return distancia en la unidad de medida seleccionada.
798
         */
799
        public double toUnits(double d) {
800
                NumberFormat nf = NumberFormat.getInstance();
801
                nf.setMaximumFractionDigits(2);
802

    
803
                String s = String.valueOf(nf.format(d));
804
                s = s.replace(',', '.');
805

    
806
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
807
        }
808

    
809
        /**
810
         * Devuelve un double en cent?metros a partir del double que se pasa como
811
         * par?metro en la unidad de medida establecida.
812
         *
813
         * @param d distancia en unidad de medida seleccionada.
814
         *
815
         * @return distancia en cent?metros.
816
         */
817
        public double fromUnits(double d) {
818
                return (d * m_TypeUnit);
819
        }
820

    
821
        /**
822
         * Devuelve los pixels por cent?metro.
823
         *
824
         * @param rect Rect?ngulo.
825
         *
826
         * @return Pixels por cent?metro.
827
         */
828
        public double getPixXCm(Rectangle2D.Double rect) {
829
                double value = m_sizePaper.getAncho();
830
                double cm = CHANGE[2];
831
                double unidades = 0;
832
                unidades = ((rect.getMaxX() - rect.getMinX()) /((value / cm)));
833

    
834
                return unidades;
835
        }
836

    
837
        /**
838
         * Establece el n?mero de pixels que representan un cent?metro.
839
         *
840
         * @param rect rect?ngulo que representa el folio en pixels.
841
         */
842
        public void setDistanceUnitX(Rectangle2D.Double rect) {
843
                double value = m_sizePaper.getAncho();
844
                double unidades = 0;
845
                unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsX()));
846
                m_unitX = unidades;
847
        }
848

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

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

    
879
                if (!isPrint) {
880
                        if (isLandSpace()) {
881
                                anchoXalto = m_sizePaper.getAncho() / m_sizePaper.getAlto();
882
                                rect = getRectangleLandscape(rect, w, h);
883
                        } else {
884
                                anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
885
                                rect = getRectanglePortrait(rect, w, h);
886
                        }
887
                } else {
888
                        value1 = m_sizePaper.getAncho();
889
                        value2 = m_sizePaper.getAlto();
890
                        rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
891
                                ((value2 / PULGADA) * DPI));
892
                }
893

    
894
                setDistanceUnitX(rect);
895
                setDistanceUnitY(rect);
896

    
897
                return rect;
898
        }
899

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

    
920
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
921
                        x1 = ((w) - (2 * x0));
922
                        y1 = (int) (x1 / anchoXalto);
923
                }
924

    
925
                rectaux.setRect(x0, y0, x1, y1);
926

    
927
                return rectaux;
928
        }
929

    
930
        /**
931
         * Devuelve un Size con las medidas del folio en las unidades de mediada
932
         * seleccionada.
933
         *
934
         * @return Tama?o del folio en las unidades seleccionadas.
935
         */
936
        public Size getSizeInUnits() {
937
                return m_sizeinUnits;
938
        }
939

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

    
959
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
960
                        y1 = (h - (2 * y0));
961
                        x1 = (int) (y1 / anchoXalto);
962
                }
963

    
964
                rectaux.setRect(x0, y0, x1, y1);
965

    
966
                return rectaux;
967
        }
968

    
969
        /**
970
         * Obtiene el tipo de los diferentes formatos de papel.
971
         *
972
         * @param size tipo de paper.
973
         *
974
         * @return Medidas del papel seleccionado.
975
         */
976
        private int getTypePaper(Size size) {
977
                int tol = 1;
978
                Size auxSize = size;
979

    
980
                if (isLandSpace()) {
981
                        auxSize = new Size(size.getAncho(), size.getAlto());
982
                }
983

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

    
1011
                return A4;
1012
        }
1013

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

    
1039
                // Landscape
1040
                xml.putProperty("m_isLandSel", m_isLandSel);
1041

    
1042
                return xml;
1043
        }
1044

    
1045
        /**
1046
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1047
         *
1048
         * @param xml XMLEntity
1049
         *
1050
         * @return Objeto de esta clase.
1051
         */
1052
        public static Attributes createAtributes(XMLEntity xml) {
1053
                Attributes atri = new Attributes();
1054
                DPI = xml.getIntProperty("DPI");
1055
                if (DPI==300){
1056
                        atri.m_resolution=PrintQuality.NORMAL;
1057
                }else if (DPI==600){
1058
                        atri.m_resolution=PrintQuality.HIGH;
1059
                }else if (DPI==72){
1060
                        atri.m_resolution=PrintQuality.DRAFT;
1061
                }
1062
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1063
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1064
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1065
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1066
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1067
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1068
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1069
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1070
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1071
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1072
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1073

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

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

    
1085
        /**
1086
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1087
         *
1088
         * @param xml XMLEntity
1089
         *
1090
         * @return Objeto de esta clase.
1091
         */
1092
        public static Attributes createAtributes03(XMLEntity xml) {
1093
                Attributes atri = new Attributes();
1094
                DPI = xml.getIntProperty("DPI");
1095
                if (DPI==300){
1096
                        atri.m_resolution=PrintQuality.NORMAL;
1097
                }else if (DPI==600){
1098
                        atri.m_resolution=PrintQuality.HIGH;
1099
                }else if (DPI==72){
1100
                        atri.m_resolution=PrintQuality.DRAFT;
1101
                }
1102
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1103
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1104
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1105
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1106
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1107
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1108
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1109
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1110
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1111
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1112
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1113

    
1114
                if (atri.hasmargin) {
1115
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1116
                }
1117

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

    
1125
        public static void setDefaultGridGap(double hGap, double vGap) {
1126
                defaultGridGap = new Point2D.Double(hGap, vGap);
1127
        }
1128

    
1129
        public static Point2D getDefaultGridGap() {
1130
                if (defaultGridGap == null){
1131
                        XMLEntity xml = PluginServices.getPluginServices("com.iver.cit.gvsig").getPersistentXML();
1132
                        double hGap = xml.contains("DefaultLayoutGridHorizontalGapX") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapX"): 0.25;
1133
                        double vGap = xml.contains("DefaultLayoutGridHorizontalGapY") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapY"): 0.25;
1134
                        defaultGridGap = new Point2D.Double(hGap, vGap);
1135
                }
1136
                return defaultGridGap;
1137
        }
1138
}