Statistics
| Revision:

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

History | View | Annotate | Download (30.5 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,GRADOS DECIMALES **/
76
        public static final double[] CHANGE = {
77
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54, 8.983152841195214E-6
78
                };
79
        public static final String[] NAMES= {"Kilometros","Metros","Centimetros","Milimetros","Millas","Yardas","Pies","Pulgadas","Grados"};
80
        public static final String DEGREES="Coordenadas";
81
        public static final double[] UNIT = {
82
                        0.0000025, 0.0025, 0.25, 2.5, 0.0000025, 0.025, 0.025, 0.25
83
                };
84
        public static final int RALTO = 0;
85
        public static final int RNORMAL = 1;
86
        public static final int RBAJO = 2;
87

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

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

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

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

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

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

    
169
                setType(m_typeSel);
170

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

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

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

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

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

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

    
241
                pf1.setPaper(paper);
242

    
243
                return pf1;
244
        }
245

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

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

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

    
279
                return rect;
280
        }
281

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

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

    
301
                attributes.add(m_Orientation);
302

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

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

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

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

    
343
                setType(m_typeSel);
344

    
345
                resul.add(m_type);
346

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

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

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

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

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

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

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

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

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

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

    
424
                                break;
425

    
426
                        case (RNORMAL):
427

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

    
431
                                break;
432

    
433
                        case (RBAJO):
434

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

    
438
                                break;
439
                }
440
        }
441

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

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

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

    
470
        /**
471
         * Devuelve el n?mero de pixels que representa a la unidad que est?
472
         * seleccionada.
473
         *
474
         * @return n?mero de pixels que representa la unidad seleccionada.
475
         */
476
        public double getUnitInPixelsY() {
477
                return m_unitY;
478
        }
479

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

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

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

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

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

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

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

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

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

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

    
571
                                break;
572

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

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

    
583
                                break;
584

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

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

    
595
                                break;
596

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

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

    
607
                                break;
608

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

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

    
619
                                break;
620

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

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

    
631
                                break;
632

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

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

    
643
                                break;
644
                }
645

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

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

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

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

    
671
                                break;
672

    
673
                        case (A4):
674

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

    
682
                                break;
683

    
684
                        case (A3):
685

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

    
693
                                break;
694

    
695
                        case (A2):
696

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

    
704
                                break;
705

    
706
                        case (A1):
707

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

    
715
                                break;
716

    
717
                        case (A0):
718

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

    
726
                                break;
727

    
728
                        case (CUSTOM):
729

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

    
737
                                break;
738
                }
739

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

    
743
                return m_sizeinUnits;
744
        }
745

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

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

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

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

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

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

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

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

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

    
835
                return unidades;
836
        }
837

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

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

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

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

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

    
898
                return rect;
899
        }
900

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

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

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

    
928
                return rectaux;
929
        }
930

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

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

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

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

    
967
                return rectaux;
968
        }
969

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

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

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

    
1012
                return A4;
1013
        }
1014

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

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

    
1043
                return xml;
1044
        }
1045

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

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

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

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

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

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

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

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