Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / Attributes.java @ 28368

History | View | Annotate | Download (30.1 KB)

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

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

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

    
65
import com.iver.andami.PluginServices;
66
import com.iver.cit.gvsig.fmap.MapContext;
67
import com.iver.utiles.XMLEntity;
68

    
69

    
70
/**
71
 * Attributes of Layout.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class Attributes {
76
        /** Array of doubles containg the change factro from <b>CENTIMETERS</b> to KILOMETERS, METERS, CENTIMETERS, MILLIMETERS, MILES, YARDS, FEET, INCHES, DECIMAL DEGREES*/
77
         /* Do not alter the order and the values of this array, if you need append values.*/
78
        public static final double[] CHANGE = {
79
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54, 8.983152841195214E-6
80
                };
81
        public static final String DEGREES="Grados";
82
        /* Do not alter the order and the values of this array, if you need append values.*/
83
        public static final double[] UNIT = {
84
                        0.0000025, 0.0025, 0.25, 2.5, 0.0000025, 0.025, 0.025, 0.25, 8.8E-7
85
                };
86
        public static final int HIGH = 0;
87
        public static final int NORMAL = 1;
88
        public static final int DRAFT = 2;
89

    
90
        //        Para impresi?n
91
        public final static double PULGADA = 2.54;
92

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

    
127
//        static {
128
//                new Attributes();
129
//        }
130

    
131
        private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
132
        private MediaSizeName m_type = MediaSizeName.ISO_A4;
133
        private boolean m_isLandSel;
134
        private OrientationRequested m_Orientation;
135
        private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
136
        private String m_NameUnit;
137
        private Double m_numX=null;
138
        private Double m_numY=null;
139
        private double m_unitX = 0;
140
        private double m_unitY = 0;
141
        private boolean hasmargin;
142
        private int m_resolutionSel = NORMAL;
143
        private int m_typeSel = PRINT;
144
        private int m_selTypeUnit = 2;
145
        public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
146
                        METRIC_A4_PAPER_SIZE.getAncho());
147
        public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
148
        private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
149
        private Size m_sizeinUnits = m_sizePaper;
150
        private PrintQuality m_resolution=PrintQuality.NORMAL;
151
        /**
152
         * Create a new object of Attributes.
153
         *
154
         */
155
        public Attributes() {
156
                m_NameUnit = PluginServices.getText(this,"Centimetros");
157
                m_type = MediaSizeName.ISO_A4;
158
                m_isLandSel = true;
159
                setSizeinUnits(m_isLandSel);
160

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

    
169
                setType(m_typeSel);
170

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

    
177
        /**
178
         * Inserts the Layout's properties to print.
179
         *
180
         * @param typeSel Type of sheet.
181
         * @param units Units.
182
         * @param isLand True if the sheet is horizontal.
183
         * @param margin True if the sheet has margin.
184
         * @param resolution Type of quality of resolution.
185
         * @param area Printing area.
186
         */
187
        public void setSelectedOptions(int typeSel, int units, boolean isLand,
188
                boolean margin, int resolution, double[] area) {
189
                setType(typeSel);
190
                setUnit(units);
191
                setIsLandScape(isLand);
192
                hasmargin = margin;
193
                setResolution(resolution);
194
                m_area = area;
195
        }
196

    
197
        /**
198
         * Returns a PageFormat with the properties of printing.
199
         *
200
         * @return PageFormat
201
         */
202
        public PageFormat getPageFormat() {
203
                PageFormat pf1 = new PageFormat();
204
                Paper paper = pf1.getPaper();
205

    
206
                if (isLandSpace()) {
207
                        pf1.setOrientation(0);
208
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
209
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
210

    
211
                        double aux = m_area[0];
212
                        m_area[0] = m_area[3];
213
                        m_area[3] = m_area[1];
214
                        m_area[1] = m_area[2];
215
                        m_area[2] = aux;
216
                        paper.setImageableArea(getInPixels(m_area[2]),
217
                                getInPixels(m_area[0]),
218
                                getInPixels(m_sizeinUnits.getAncho() - m_area[2] - m_area[3]),
219
                                getInPixels(m_sizeinUnits.getAlto() - m_area[0] - m_area[1]));
220
                } else {
221
                        pf1.setOrientation(1);
222
                        paper.setSize((m_sizePaper.getAlto() * DPISCREEN) / PULGADA,
223
                                (m_sizePaper.getAncho() * DPISCREEN) / PULGADA);
224
                        paper.setImageableArea(getInPixels(m_area[2]),
225
                                getInPixels(m_area[0]),
226
                                getInPixels(m_sizeinUnits.getAlto() - m_area[2] - m_area[3]),
227
                                getInPixels(m_sizeinUnits.getAncho() - m_area[0] - m_area[1]));
228
                }
229

    
230
                pf1.setPaper(paper);
231

    
232
                return pf1;
233
        }
234

    
235
        /**
236
         * Changes centimeters to pixels.
237
         *
238
         * @param d Centimeters.
239
         *
240
         * @return Pixels.
241
         */
242
        private double getInPixels(double d) {
243
                return d * (DPISCREEN / PULGADA);
244
        }
245

    
246
        /**
247
         * Changes pixels to centimeters.
248
         *
249
         * @param d Pixel.
250
         *
251
         * @return Centimeters.
252
         */
253
        private double getInCM(double d) {
254
                return d / (DPISCREEN / PULGADA);
255
        }
256

    
257
        /**
258
         * Returns the printing area.
259
         *
260
         * @return Rectangle Area.
261
         */
262
        public Rectangle2D getArea() {
263
                Rectangle2D.Double rect = new Rectangle2D.Double();
264
                rect.setRect(m_area[0], m_area[1],
265
                        m_sizePaper.getAncho() - m_area[0] - m_area[2],
266
                        m_sizePaper.getAlto() - m_area[1] - m_area[3]);
267

    
268
                return rect;
269
        }
270

    
271
        /**
272
         * Inserts the attributes with a PageFormat.
273
         *
274
         * @param pf PageFormat.
275
         */
276
        public void setPageFormat(PageFormat pf) {
277
                Size size = null;
278

    
279
                if (pf.getOrientation() == 0) {
280
                        setIsLandScape(true);
281
                        m_Orientation = OrientationRequested.LANDSCAPE;
282
                        size = new Size(pf.getHeight(), pf.getWidth());
283
                } else {
284
                        setIsLandScape(false);
285
                        m_Orientation = OrientationRequested.PORTRAIT;
286
                        size = new Size(pf.getHeight(), pf.getWidth());
287
                }
288

    
289
                attributes.add(m_Orientation);
290

    
291
                //area
292
                m_area[0] = getInCM(pf.getImageableY());
293
                m_area[2] = getInCM(pf.getImageableX());
294
                m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
295
                        m_area[0];
296
                m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
297
                        m_area[2];
298

    
299
                //        tipo formato
300
                if (isLandSpace()) {
301
                        //double aux = m_area[0];
302
                        //m_area[0] = m_area[3];
303
                        //m_area[3] = m_area[1];
304
                        //m_area[1] = m_area[2];
305
                        //m_area[2] = aux;
306
                        attributes.add(new MediaPrintableArea((float) (m_area[2] * 10),
307
                                        (float) (m_area[0] * 10),
308
                                        (float) (m_sizeinUnits.getAlto() - m_area[0] - m_area[1]) * 10,
309
                                        (float) (m_sizeinUnits.getAncho() - m_area[2] - m_area[3]) * 10,
310
                                        MediaPrintableArea.MM));
311
                } else {
312
                        attributes.add(new MediaPrintableArea((float) (m_area[0] * 10),
313
                                        (float) (m_area[1] * 10),
314
                                        (float) (getInCM(pf.getImageableWidth()) * 10),
315
                                        (float) (getInCM(pf.getImageableHeight()) * 10),
316
                                        MediaPrintableArea.MM));
317
                }
318

    
319
                setType(getTypePaper(size));
320
        }
321

    
322
        /**
323
         * Returns a PrintRequestAttributeSet with all properties to the PrintDialog.
324
         *
325
         * @return PrintRequestAttributesSet.
326
         */
327
        public PrintRequestAttributeSet toPrintAttributes() {
328
                HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
329

    
330
                setType(m_typeSel);
331

    
332
                resul.add(m_type);
333

    
334
                //units, no hace falta a?adirlo a attributes
335
                resul.add(m_Orientation);
336
                setArea(m_area);
337
                resul.add(new MediaPrintableArea(0, 0,
338
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
339
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
340
                                MediaPrintableArea.MM));
341

    
342
                resul.add(m_resolution);
343
                //resul.add(new PrinterResolution(DPI,DPI,PrinterResolution.DPI));
344
                return resul;
345
        }
346

    
347
        /**
348
         * Inserts printing area.
349
         *
350
         * @param area Printing area.
351
         */
352
        private void setArea(double[] area) {
353
                if (!isLandSpace()) {
354
                        attributes.add(new MediaPrintableArea((float) (area[2] * 10),
355
                                        (float) (area[0] * 10),
356
                                        (float) ((m_sizePaper.getAncho() - area[2] - area[3]) * 10),
357
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
358
                                        MediaPrintableArea.MM));
359

    
360
                        clipRect.setRect((area[2] / PULGADA * DPI),
361
                                area[0] / PULGADA * DPI,
362
                                (m_sizePaper.getAncho() - area[2] - area[3]) / PULGADA * DPI,
363
                                (m_sizePaper.getAlto() - area[0] - area[1]) / PULGADA * DPI);
364
                } else {
365
                        attributes.add(new MediaPrintableArea((float) (area[0] * 10),
366
                                        (float) (area[3] * 10),
367
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
368
                                        (float) ((m_sizePaper.getAncho() - area[3] - area[2]) * 10),
369
                                        MediaPrintableArea.MM));
370

    
371
                        clipRect.setRect((area[1] / PULGADA * DPI),
372
                                area[2] / PULGADA * DPI,
373
                                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
374
                                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
375
                }
376
        }
377

    
378
        /**
379
         * Returns true if the margin should be used.
380
         *
381
         * @return True if margin should be used.
382
         */
383
        public boolean isMargin() {
384
                return hasmargin;
385
        }
386

    
387
        /**
388
         * Returns the resolution of properties to print.
389
         *
390
         * @return Resolution.
391
         */
392
        public int getResolution() {
393
                return m_resolutionSel;
394
        }
395

    
396
        /**
397
         * Inserts the resolution to apply at printing.
398
         *
399
         * @param i Type of resolution.
400
         *
401
         * The type of resolution can be:
402
         *
403
         *  DRAFT:         27 dpi.
404
         *  NORMAL: 300 dpi.
405
         *  HIGH:         600 dpi.
406
         */
407
        private void setResolution(int i) {
408
                m_resolutionSel = i;
409

    
410
                switch (i) {
411
                        case (HIGH):
412

    
413
                                m_resolution=PrintQuality.HIGH;
414
                                DPI = 600;
415

    
416
                                break;
417

    
418
                        case (NORMAL):
419

    
420
                                m_resolution=PrintQuality.NORMAL;
421
                                DPI = 300;
422

    
423
                                break;
424

    
425
                        case (DRAFT):
426

    
427
                                m_resolution=PrintQuality.DRAFT;
428
                                DPI = 72;
429

    
430
                                break;
431
                }
432
                attributes.add(m_resolution);
433
        }
434

    
435
        /**
436
         * Returns attributes of PrintDialog.
437
         *
438
         * @return Attributes of printing.
439
         */
440
        public PrintRequestAttributeSet getAttributes() {
441
                return attributes;
442
        }
443

    
444
        /**
445
         * Returns True if the position of sheet is horizontal or vertical.
446
         *
447
         * @return True if position is horizontal.
448
         */
449
        public boolean isLandSpace() {
450
                return m_isLandSel;
451
        }
452

    
453
        /**
454
         * Returns the format of sheet.
455
         *
456
         * @return Format of sheet.
457
         */
458
        public int getType() {
459
                return m_typeSel;
460
        }
461

    
462
        /**
463
         * Inserts the number of units to define the coords X grid.
464
         *
465
         * @param d number of units to define the grid.
466
         */
467
        public void setNumUnitsX(double d) {
468
                m_numX = new Double(d);
469
        }
470

    
471
        /**
472
         * Returns the number of pixels that represents a unit of coords X.
473
         *
474
         * @return Number of pixels coords X.
475
         */
476

    
477
        public double getNumUnitsX() {
478
                if (m_numX == null)
479
                        m_numX = new Double(getDefaultGridGap().getX());
480
                return m_numX.doubleValue();
481
        }
482

    
483
        /**
484
         * Returns the number of pixels that represents a unit of coords Y.
485
         *
486
         * @return Number of pixels coords Y.
487
         */
488

    
489
        public double getNumUnitsY() {
490
                if (m_numY == null)
491
                        m_numY = new Double(getDefaultGridGap().getY());
492
                return m_numY.doubleValue();
493
        }
494

    
495
        /**
496
         * Inserts the number of units to define the coords X grid.
497
         *
498
         * @param d number of units to define the grid.
499
         */
500
        public void setNumUnitsY(double d) {
501
                m_numY = new Double(d);
502
        }
503

    
504
        /**
505
         * Inserts true if the sheet is horizontal or false if is vertical.
506
         *
507
         * @param b True if sheet is horizontal.
508
         */
509
        public void setIsLandScape(boolean b) {
510
                m_isLandSel = b;
511
                setType(m_typeSel);
512

    
513
                if (m_isLandSel) {
514
                        m_Orientation = OrientationRequested.LANDSCAPE;
515
                } else {
516
                        m_Orientation = OrientationRequested.PORTRAIT;
517
                }
518
        }
519

    
520
        /**
521
         * Inserts the type of sheet to show.
522
         *
523
         * @param t Type of sheet.
524
         */
525
        public void setType(int t) {
526
                m_typeSel = t;
527

    
528
                //m_sizePaper=getSizePaper(t);
529
                switch (t) {
530
                        case (PRINT):
531
                                m_type = ((MediaSizeName) attributes.get(Media.class));
532

    
533
                                if (isLandSpace()) {
534
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
535
                                                        METRIC_A4_PAPER_SIZE.getAlto());
536
                                } else {
537
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
538
                                }
539

    
540
                                break;
541

    
542
                        case (A4):
543
                                m_type = MediaSizeName.ISO_A4;
544

    
545
                                if (isLandSpace()) {
546
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
547
                                                        METRIC_A4_PAPER_SIZE.getAlto());
548
                                } else {
549
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
550
                                }
551

    
552
                                break;
553

    
554
                        case (A3):
555
                                m_type = MediaSizeName.ISO_A3;
556

    
557
                                if (isLandSpace()) {
558
                                        m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
559
                                                        METRIC_A3_PAPER_SIZE.getAlto());
560
                                } else {
561
                                        m_sizePaper = METRIC_A3_PAPER_SIZE;
562
                                }
563

    
564
                                break;
565

    
566
                        case (A2):
567
                                m_type = MediaSizeName.ISO_A2;
568

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

    
576
                                break;
577

    
578
                        case (A1):
579
                                m_type = MediaSizeName.ISO_A1;
580

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

    
588
                                break;
589

    
590
                        case (A0):
591
                                m_type = MediaSizeName.ISO_A0;
592

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

    
600
                                break;
601

    
602
                        case (CUSTOM):
603
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
604

    
605
//                        if (isLandSpace()) {
606
//                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit,
607
//                                        CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit);
608
//                } else {
609
//                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit,
610
//                                        CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit);
611
//                }
612

    
613
                                break;
614
                }
615
                attributes.add(m_type);
616
                setSizeinUnits(isLandSpace());
617
                m_sizeinUnits = getSizeInUnits();
618
        }
619

    
620
        /**
621
         * Returns the size of sheet.
622
         *
623
         * @param isLand True if is horizontal and false if is vertical.
624
         * @param type Type of sheet.
625
         *
626
         * @return Size of sheet with these properties.
627
         */
628
        public Size getSizeinUnits(boolean isLand, int type) {
629
                Size size = null;
630

    
631
                switch (type) {
632
                        case (PRINT):
633

    
634
                                if (isLand) {
635
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
636
                                                        METRIC_A4_PAPER_SIZE.getAlto());
637
                                } else {
638
                                        size = METRIC_A4_PAPER_SIZE;
639
                                }
640

    
641
                                break;
642

    
643
                        case (A4):
644

    
645
                                if (isLand) {
646
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
647
                                                        METRIC_A4_PAPER_SIZE.getAlto());
648
                                } else {
649
                                        size = METRIC_A4_PAPER_SIZE;
650
                                }
651

    
652
                                break;
653

    
654
                        case (A3):
655

    
656
                                if (isLand) {
657
                                        size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
658
                                                        METRIC_A3_PAPER_SIZE.getAlto());
659
                                } else {
660
                                        size = METRIC_A3_PAPER_SIZE;
661
                                }
662

    
663
                                break;
664

    
665
                        case (A2):
666

    
667
                                if (isLand) {
668
                                        size = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
669
                                                        METRIC_A2_PAPER_SIZE.getAlto());
670
                                } else {
671
                                        size = METRIC_A2_PAPER_SIZE;
672
                                }
673

    
674
                                break;
675

    
676
                        case (A1):
677

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

    
685
                                break;
686

    
687
                        case (A0):
688

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

    
696
                                break;
697

    
698
                        case (CUSTOM):
699

    
700
//                                if (isLand) {
701
                                size = new Size(CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit,
702
                                                CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit);
703
//                        } else {
704
//                                size = new Size(CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit,
705
//                                                CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit);
706
//                        }
707
                                break;
708
                }
709

    
710
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
711
                                size.getAncho() / m_TypeUnit);
712

    
713
                return m_sizeinUnits;
714
        }
715

    
716
        /**
717
         * Retunrs the type of units is in use.
718
         *
719
         * @return Type of units.
720
         */
721
        public int getSelTypeUnit() {
722
                return m_selTypeUnit;
723
        }
724

    
725
        /**
726
         * Returns the name of units.
727
         *
728
         * @return String Name of units.
729
         */
730
        public String getNameUnit() {
731
                return m_NameUnit;
732
        }
733

    
734
        /**
735
         * Selecting the type of unit that is utilized in the Layout, by default is in centimetres.
736
         *
737
         * @param sel Type of unit.
738
         */
739
        public void setUnit(int sel) {
740
                m_selTypeUnit = sel;
741
                m_TypeUnit = CHANGE[sel];
742
                m_NameUnit = PluginServices.getText(this,MapContext.getDistanceNames()[sel]);
743
                m_numX = m_numY = new Double(UNIT[sel]);
744
        }
745

    
746
        /**
747
         * Actualize to date m_sizeinUnits with the height and width of the sheet
748
         * in the units of mediated selected.
749
         *
750
         * @param b True if is horizontal.
751
         */
752
        public void setSizeinUnits(boolean b) {
753
                if (b) {
754
                        m_sizeinUnits = new Size(m_sizePaper.getAlto() / m_TypeUnit,
755
                                        m_sizePaper.getAncho() / m_TypeUnit);
756
                } else {
757
                        m_sizeinUnits = new Size(m_sizePaper.getAncho() / m_TypeUnit,
758
                                        m_sizePaper.getAlto() / m_TypeUnit);
759
                }
760
        }
761

    
762
        /**
763
         * Returns a double in the unit of measure selected from the double in centimeters.
764
         *
765
         * @param d Distance in centimeters.
766
         *
767
         * @return Distance in the unit measure selected.
768
         */
769
        public double toUnits(double d) {
770
                NumberFormat nf = NumberFormat.getInstance();
771
                nf.setMaximumFractionDigits(2);
772

    
773
                String s = String.valueOf(nf.format(d));
774
                s = s.replace(',', '.');
775

    
776
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
777
        }
778

    
779
        /**
780
         * Returns a double in centimeters from the the unit of measure selected.
781
         *
782
         * @param d Distance in the unit measure selected.
783
         *
784
         * @return Distance in centimeters.
785
         */
786
        public double fromUnits(double d) {
787
                return (d * m_TypeUnit);
788
        }
789

    
790
        /**
791
         * Returns one centimeter in pixels on screen in this moment.
792
         *
793
         * @param rect Rectangle of sheet.
794
         *
795
         * @return Pixels from centimeter.
796
         */
797
        public double getPixXCm(Rectangle2D rect) {
798
                double value = m_sizePaper.getAncho();
799
                double cm = CHANGE[2];
800
                double unidades = 0;
801
                unidades = ((rect.getMaxX() - rect.getMinX()) /((value / cm)));
802
                return unidades;
803
        }
804

    
805
        /**
806
         * Inserts the number of pixels of one centimeter of X.
807
         *
808
         * @param rect Rectangle of sheet.
809
         */
810
        public void setDistanceUnitX(Rectangle2D rect) {
811
                double value = m_sizePaper.getAncho();
812
                double unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsX()));
813
                m_unitX = unidades;
814
        }
815

    
816
        /**
817
         * Inserts the number of pixels of one centimeter of Y.
818
         *
819
         * @param rect Rectangle of sheet.
820
         */
821
        public void setDistanceUnitY(Rectangle2D rect) {
822
                double value = m_sizePaper.getAncho();
823
                double unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsY()));
824
                m_unitY = unidades;
825
        }
826

    
827
        /**
828
         * Obtains the rectangle that represents the sheet with the characteristics
829
         * that contains attributes
830
         *
831
         * @param rect rectangle.
832
         *
833
         */
834
        public void obtainRect(boolean isPrint, Rectangle2D rect, int w, int h) {
835
                double value1 = 0;
836
                double value2 = 0;
837

    
838
                if (!isPrint) {
839
                        if (isLandSpace()) {
840
                                anchoXalto = m_sizePaper.getAncho() / m_sizePaper.getAlto();
841
                                rect = getRectangleLandscape(rect, w, h);
842
                        } else {
843
                                anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
844
                                rect = getRectanglePortrait(rect, w, h);
845
                        }
846
                } else {
847
                        value1 = m_sizePaper.getAncho();
848
                        value2 = m_sizePaper.getAlto();
849
                        rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
850
                                ((value2 / PULGADA) * DPI));
851
                }
852

    
853
                setDistanceUnitX(rect);
854
                setDistanceUnitY(rect);
855
        }
856

    
857
        /**
858
         * It obtains the rect that is adjusted to the size of the window,
859
         * to see the full extent of horizontal layout.
860
         *
861
         * @param rect Rectangle sheet.
862
         * @param w Width of Layout.
863
         * @param h Height of Layout.
864
         *
865
         * @return Rectangle modified.
866
         */
867
        public Rectangle2D.Double getRectangleLandscape(Rectangle2D rect,
868
                int w, int h) {
869
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
870
                int x0 = (int) rect.getMinX();
871
                int y0 = (int) rect.getMinY();
872
                int y1;
873
                int x1;
874
                y1 = (h - (2 * y0));
875
                x1 = (int) (y1 * anchoXalto);
876

    
877
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
878
                        x1 = ((w) - (2 * x0));
879
                        y1 = (int) (x1 / anchoXalto);
880
                }
881

    
882
                rectaux.setRect(x0, y0, x1, y1);
883

    
884
                return rectaux;
885
        }
886

    
887
        /**
888
         * Returns the size of sheet in the units of measure selected.
889
         *
890
         * @return Size of sheet.
891
         */
892
        public Size getSizeInUnits() {
893
                return m_sizeinUnits;
894
        }
895

    
896
        /**
897
         * It obtains the rect that is adjusted to the size of the window,
898
         * to see the full extent of vertical layout.
899
         *
900
         * @param rect Rectangle sheet.
901
         * @param w Width of Layout.
902
         * @param h Height of Layout.
903
         *
904
         * @return Rectangle modified.
905
         */
906
        public Rectangle2D.Double getRectanglePortrait(Rectangle2D rect,
907
                int w, int h) {
908
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
909
                int x0 = (int) rect.getMinX();
910
                int y0 = (int) rect.getMinY();
911
                int y1;
912
                int x1;
913
                x1 = (w - (2 * x0));
914
                y1 = (int) (x1 * anchoXalto);
915

    
916
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
917
                        y1 = (h - (2 * y0));
918
                        x1 = (int) (y1 / anchoXalto);
919
                }
920

    
921
                rectaux.setRect(x0, y0, x1, y1);
922

    
923
                return rectaux;
924
        }
925

    
926
        /**
927
         * It obtains the type of format from the size.
928
         *
929
         * @param size Size of sheet.
930
         *
931
         * @return Type of sheet.
932
         */
933
        private int getTypePaper(Size size) {
934
                int tol = 1;
935
                Size auxSize = size;
936

    
937
                if (isLandSpace()) {
938
                        auxSize = new Size(size.getAncho(), size.getAlto());
939
                }
940

    
941
                if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
942
                                METRIC_A4_PAPER_SIZE.getAncho())) &&
943
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
944
                                METRIC_A4_PAPER_SIZE.getAlto()))) {
945
                        return A4;
946
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
947
                                METRIC_A3_PAPER_SIZE.getAncho())) &&
948
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
949
                                METRIC_A3_PAPER_SIZE.getAlto()))) {
950
                        return A3;
951
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
952
                                METRIC_A2_PAPER_SIZE.getAncho())) &&
953
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
954
                                METRIC_A2_PAPER_SIZE.getAlto()))) {
955
                        return A2;
956
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
957
                                METRIC_A1_PAPER_SIZE.getAncho())) &&
958
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
959
                                METRIC_A1_PAPER_SIZE.getAlto()))) {
960
                        return A1;
961
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
962
                                METRIC_A0_PAPER_SIZE.getAncho())) &&
963
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
964
                                METRIC_A0_PAPER_SIZE.getAlto()))) {
965
                        return A0;
966
                }
967

    
968
                return A4;
969
        }
970

    
971
        /**
972
         * It returns an Object XMLEntity with the information the necessary attributes
973
         * to be able later to create again the original object.
974
         *
975
         * @return XMLEntity.
976
         *
977
         */
978
        public XMLEntity getXMLEntity() {
979
                XMLEntity xml = new XMLEntity();
980
                xml.putProperty("className",this.getClass().getName());
981
                xml.putProperty("DPI", DPI);
982
                xml.putProperty("DPISCREEN", DPISCREEN);
983
                xml.putProperty("m_TypeUnit", m_TypeUnit);
984
                xml.putProperty("m_numX", m_numX);
985
                xml.putProperty("m_numY", m_numY);
986
                xml.putProperty("m_unitX", m_unitX);
987
                xml.putProperty("m_unitY", m_unitY);
988
                xml.putProperty("hasmargin", hasmargin);
989
                xml.putProperty("m_resolutionSel", m_resolutionSel);
990
                xml.putProperty("m_typeSel", m_typeSel);
991
                xml.putProperty("m_selTypeUnit", m_selTypeUnit);
992
                xml.addChild(m_sizePaper.getXMLEntity());
993
                xml.putProperty("m_area", m_area);
994
                xml.putProperty("anchoXalto", anchoXalto);
995
                xml.addChild(m_sizeinUnits.getXMLEntity());
996

    
997
                // Landscape
998
                xml.putProperty("m_isLandSel", m_isLandSel);
999

    
1000
                return xml;
1001
        }
1002

    
1003
        /**
1004
         * It returns an Object of this class from a XMLEntity.
1005
         *
1006
         *
1007
         * @param xml XMLEntity
1008
         *
1009
         * @return Object of Attributes.
1010
         */
1011
        public static Attributes createAtributes(XMLEntity xml) {
1012
                Attributes atri = new Attributes();
1013
                DPI = xml.getIntProperty("DPI");
1014
                if (DPI==300){
1015
                        atri.m_resolution=PrintQuality.NORMAL;
1016
                }else if (DPI==600){
1017
                        atri.m_resolution=PrintQuality.HIGH;
1018
                }else if (DPI==72){
1019
                        atri.m_resolution=PrintQuality.DRAFT;
1020
                }
1021
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1022
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1023
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1024
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1025
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1026
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1027
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1028
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1029
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1030
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1031
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1032

    
1033
                if (atri.hasmargin) {
1034
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1035
                }
1036

    
1037
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1038
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1039
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1040
                atri.setIsLandScape(atri.m_isLandSel);
1041
                return atri;
1042
        }
1043

    
1044
        /**
1045
         * It returns an Object of this class from a XMLEntity.
1046
         *
1047
         *
1048
         * @param xml XMLEntity
1049
         *
1050
         * @return Object of Attributes.
1051
         */
1052
        public static Attributes createAtributes03(XMLEntity xml) {
1053
                Attributes atri = new Attributes();
1054
                DPI = xml.getIntProperty("DPI");
1055
                if (DPI==300){
1056
                        atri.m_resolution=PrintQuality.NORMAL;
1057
                }else if (DPI==600){
1058
                        atri.m_resolution=PrintQuality.HIGH;
1059
                }else if (DPI==72){
1060
                        atri.m_resolution=PrintQuality.DRAFT;
1061
                }
1062
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1063
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1064
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1065
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1066
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1067
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1068
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1069
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1070
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1071
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1072
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1073

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

    
1078
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1079
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1080
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1081
                atri.setIsLandScape(atri.m_isLandSel);
1082
                return atri;
1083
        }
1084
        /**
1085
         * Inserts the default number of pixels that represents a unit of grid.
1086
         *
1087
         * @param hGap Horizontal distance.
1088
         * @param vGap Vertical distance.
1089
         */
1090
        public static void setDefaultGridGap(double hGap, double vGap) {
1091
                defaultGridGap = new Point2D.Double(hGap, vGap);
1092
        }
1093
        /**
1094
         * Returns the default number of pixels that represents a unit of grid.
1095
         *
1096
         * @return Number of pixels of grid.
1097
         */
1098
        public static Point2D getDefaultGridGap() {
1099
                if (defaultGridGap == null){
1100
                        XMLEntity xml = PluginServices.getPluginServices("com.iver.cit.gvsig").getPersistentXML();
1101
                        double hGap = xml.contains("DefaultLayoutGridHorizontalGapX") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapX"): 0.25;
1102
                        double vGap = xml.contains("DefaultLayoutGridHorizontalGapY") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapY"): 0.25;
1103
                        defaultGridGap = new Point2D.Double(hGap, vGap);
1104
                }
1105
                return defaultGridGap;
1106
        }
1107

    
1108
        /**
1109
         * Devuelve el n?mero de pixels de longitud en el eje Y que representa a la unidad  del mapa que est?
1110
         * seleccionada, suelen ser cent?metros.
1111
         *
1112
         * @return n?mero de pixels que representa la unidad seleccionada.
1113
         */
1114
        public double getUnitInPixelsY() {
1115
                return m_unitY;
1116
        }
1117

    
1118
        /**
1119
         * Devuelve el n?mero de pixels de longitud en el eje X que representa a la unidad del mapa que est?
1120
         * seleccionada, suelen ser cent?metros.
1121
         *
1122
         * @return n?mero de pixels que representa la unidad seleccionada.
1123
         */
1124
        public double getUnitInPixelsX() {
1125
                return m_unitX;
1126
        }
1127

    
1128
}