Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Attributes.java @ 8202

History | View | Annotate | Download (30.6 KB)

1
/*
2
 * Created on 25-jun-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.gui.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.cit.gvsig.project.documents.layout.Size;
67
import com.iver.utiles.XMLEntity;
68

    
69

    
70
/**
71
 * Atributos del Layout.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class Attributes {
76
        /** KILOMETROS,METROS,CENTIMETROS,MILIMETROS,MILLAS,YARDAS,PIES,PULGADAS */
77
        public static final double[] CHANGE = {
78
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
79
                };
80
        public static final String[] NAMES= {"Kilometros","Metros","Centimetros","Milimetros","Millas","Yardas","Pies","Pulgadas"};
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 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
                /*if (NAMES == null) {
156
                        int i = 0;
157
                        NAMES = new String[8];
158
                        NAMES[i++] = PluginServices.getText(this, "Kilometros");
159
                        NAMES[i++] = PluginServices.getText(this, "Metros");
160
                        NAMES[i++] = PluginServices.getText(this, "Centimetros");
161
                        NAMES[i++] = PluginServices.getText(this, "Milimetros");
162
                        NAMES[i++] = PluginServices.getText(this, "Millas");
163
                        NAMES[i++] = PluginServices.getText(this, "Yardas");
164
                        NAMES[i++] = PluginServices.getText(this, "Pies");
165
                        NAMES[i++] = PluginServices.getText(this, "Pulgadas");
166
                        //NAMES[i++] = PluginServices.getText(this, "Grados");
167
                }
168
*/
169
                m_NameUnit = NAMES[2];
170
                m_type = MediaSizeName.ISO_A4;
171
                m_isLandSel = true;
172
                setSizeinUnits(m_isLandSel);
173

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

    
182
                setType(m_typeSel);
183

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

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

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

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

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

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

    
254
                pf1.setPaper(paper);
255

    
256
                return pf1;
257
        }
258

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

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

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

    
292
                return rect;
293
        }
294

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

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

    
314
                attributes.add(m_Orientation);
315

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

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

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

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

    
356
                setType(m_typeSel);
357

    
358
                resul.add(m_type);
359

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

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

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

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

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

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

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

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

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

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

    
437
                                break;
438

    
439
                        case (RNORMAL):
440

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

    
444
                                break;
445

    
446
                        case (RBAJO):
447

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

    
451
                                break;
452
                }
453
        }
454

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

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

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

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

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

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

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

    
525
        /**
526
         * Devuelve el n?mero de unidades del espacio de cuadr?cula vertical en la
527
         * medida que est? seleccionada,  por defecto en cent?metros.
528
         *
529
         * @return double
530
         */
531
        public double getNumUnitsY() {
532
                if (m_numY == null)
533
                        m_numY = new Double(getDefaultGridGap().getY());
534
                return m_numY.doubleValue();
535
        }
536

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

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

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

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

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

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

    
584
                                break;
585

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

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

    
596
                                break;
597

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

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

    
608
                                break;
609

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

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

    
620
                                break;
621

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

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

    
632
                                break;
633

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

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

    
644
                                break;
645

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

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

    
656
                                break;
657
                }
658

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

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

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

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

    
684
                                break;
685

    
686
                        case (A4):
687

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

    
695
                                break;
696

    
697
                        case (A3):
698

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

    
706
                                break;
707

    
708
                        case (A2):
709

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

    
717
                                break;
718

    
719
                        case (A1):
720

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

    
728
                                break;
729

    
730
                        case (A0):
731

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

    
739
                                break;
740

    
741
                        case (CUSTOM):
742

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

    
750
                                break;
751
                }
752

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

    
756
                return m_sizeinUnits;
757
        }
758

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

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

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

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

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

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

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

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

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

    
848
                return unidades;
849
        }
850

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

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

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

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

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

    
911
                return rect;
912
        }
913

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

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

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

    
941
                return rectaux;
942
        }
943

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

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

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

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

    
980
                return rectaux;
981
        }
982

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

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

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

    
1025
                return A4;
1026
        }
1027

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

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

    
1056
                return xml;
1057
        }
1058

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

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

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

    
1092
        /**
1093
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
1094
         *
1095
         * @param xml XMLEntity
1096
         *
1097
         * @return Objeto de esta clase.
1098
         */
1099
        public static Attributes createAtributes03(XMLEntity xml) {
1100
                Attributes atri = new Attributes();
1101
                DPI = xml.getIntProperty("DPI");
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
}