Statistics
| Revision:

root / tags / V10_RC2 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / Attributes.java @ 8040

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.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 String[] NAMES;
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
         * 050211, jmorell: Se a?aden los Grados como unidades de mapa.
151
         * Creates a new Attributes object.
152
         */
153
        public Attributes() {
154
                if (NAMES == null) {
155
                        int i = 0;
156
                        NAMES = new String[8];
157
                        NAMES[i++] = PluginServices.getText(this, "Kilometros");
158
                        NAMES[i++] = PluginServices.getText(this, "Metros");
159
                        NAMES[i++] = PluginServices.getText(this, "Centimetros");
160
                        NAMES[i++] = PluginServices.getText(this, "Milimetros");
161
                        NAMES[i++] = PluginServices.getText(this, "Millas");
162
                        NAMES[i++] = PluginServices.getText(this, "Yardas");
163
                        NAMES[i++] = PluginServices.getText(this, "Pies");
164
                        NAMES[i++] = PluginServices.getText(this, "Pulgadas");
165
                        //NAMES[i++] = PluginServices.getText(this, "Grados");
166
                }
167

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

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

    
181
                setType(m_typeSel);
182

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

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

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

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

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

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

    
253
                pf1.setPaper(paper);
254

    
255
                return pf1;
256
        }
257

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

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

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

    
291
                return rect;
292
        }
293

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

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

    
313
                attributes.add(m_Orientation);
314

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

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

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

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

    
355
                setType(m_typeSel);
356

    
357
                resul.add(m_type);
358

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

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

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

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

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

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

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

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

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

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

    
436
                                break;
437

    
438
                        case (RNORMAL):
439

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

    
443
                                break;
444

    
445
                        case (RBAJO):
446

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

    
450
                                break;
451
                }
452
        }
453

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

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

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

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

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

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

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

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

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

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

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

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

    
571
                //m_sizePaper=getSizePaper(t);
572
                switch (t) {
573
                        case (PRINT):
574
                                m_type = ((MediaSizeName) attributes.get(Media.class));
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 (A4):
586
                                m_type = MediaSizeName.ISO_A4;
587

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

    
595
                                break;
596

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

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

    
607
                                break;
608

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

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

    
619
                                break;
620

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

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

    
631
                                break;
632

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

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

    
643
                                break;
644

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

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

    
655
                                break;
656
                }
657

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

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

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

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

    
683
                                break;
684

    
685
                        case (A4):
686

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

    
694
                                break;
695

    
696
                        case (A3):
697

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

    
705
                                break;
706

    
707
                        case (A2):
708

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

    
716
                                break;
717

    
718
                        case (A1):
719

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

    
727
                                break;
728

    
729
                        case (A0):
730

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

    
738
                                break;
739

    
740
                        case (CUSTOM):
741

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

    
749
                                break;
750
                }
751

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

    
755
                return m_sizeinUnits;
756
        }
757

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

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

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

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

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

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

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

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

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

    
847
                return 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 setDistanceUnitX(Rectangle2D.Double rect) {
856
                double value = m_sizePaper.getAncho();
857
                double unidades = 0;
858
                unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsX()));
859
                m_unitX = unidades;
860
        }
861

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

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

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

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

    
910
                return rect;
911
        }
912

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

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

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

    
940
                return rectaux;
941
        }
942

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

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

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

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

    
979
                return rectaux;
980
        }
981

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

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

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

    
1024
                return A4;
1025
        }
1026

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

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

    
1055
                return xml;
1056
        }
1057

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

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

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

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

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

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

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

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