Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / Attributes.java @ 28304

History | View | Annotate | Download (30.1 KB)

1 9392 caballero
/*
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 20994 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
66
67 9392 caballero
import com.iver.andami.PluginServices;
68
import com.iver.utiles.XMLEntity;
69
70
71
/**
72 10007 caballero
 * Attributes of Layout.
73 9392 caballero
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class Attributes {
77 10910 jaume
        /** Array of doubles containg the change factro from <b>CENTIMETERS</b> to KILOMETERS, METERS, CENTIMETERS, MILLIMETERS, MILES, YARDS, FEET, INCHES, DECIMAL DEGREES*/
78
         /* Do not alter the order and the values of this array, if you need append values.*/
79 9392 caballero
        public static final double[] CHANGE = {
80 9956 caballero
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54, 8.983152841195214E-6
81 9392 caballero
                };
82 27366 vcaballero
        public static final String DEGREES="Grados";
83 10910 jaume
        /* Do not alter the order and the values of this array, if you need append values.*/
84 9392 caballero
        public static final double[] UNIT = {
85 27366 vcaballero
                        0.0000025, 0.0025, 0.25, 2.5, 0.0000025, 0.025, 0.025, 0.25, 8.8E-7
86 9392 caballero
                };
87 10007 caballero
        public static final int HIGH = 0;
88
        public static final int NORMAL = 1;
89
        public static final int DRAFT = 2;
90 9392 caballero
91
        //        Para impresi?n
92
        public final static double PULGADA = 2.54;
93
94
        //public final static Size CUSTOM_PAPER_SIZE = new Size(8.5, 11.0);
95
        public final static Size STANDARD_LETTER_PAPER_SIZE = new Size(8.5, 11.0);
96
        public final static Size STANDARD_FOLIO_PAPER_SIZE = new Size(8.5, 13.0);
97
        public final static Size STANDARD_LEGAL_PAPER_SIZE = new Size(8.5, 14.0);
98
        public final static Size STANDARD_TABLOID_PAPER_SIZE = new Size(11.0, 17.0);
99
        public final static Size METRIC_A0_PAPER_SIZE = new Size(118.9, 84.1);
100
        public final static Size METRIC_A1_PAPER_SIZE = new Size(84.1, 59.4);
101
        public final static Size METRIC_A2_PAPER_SIZE = new Size(59.4, 42.0);
102
        public final static Size METRIC_A3_PAPER_SIZE = new Size(42.0, 29.7);
103
        public final static Size METRIC_A4_PAPER_SIZE = new Size(29.7, 21.0);
104
        public final static Size METRIC_A5_PAPER_SIZE = new Size(21.0, 14.8);
105
        public final static Size ANSI_ENG_A_PAPER_SIZE = new Size(11.0, 8.5);
106
        public final static Size ANSI_ENG_B_PAPER_SIZE = new Size(17.0, 11.0);
107
        public final static Size ANSI_ENG_C_PAPER_SIZE = new Size(22.0, 17.0);
108
        public final static Size ANSI_ENG_D_PAPER_SIZE = new Size(34.0, 22.0);
109
        public final static Size ANSI_ENG_E_PAPER_SIZE = new Size(44.0, 34.0);
110
        public final static Size ANSI_ARCH_A_PAPER_SIZE = new Size(12.0, 9.0);
111
        public final static Size ANSI_ARCH_B_PAPER_SIZE = new Size(18.0, 12.0);
112
        public final static Size ANSI_ARCH_C_PAPER_SIZE = new Size(24.0, 18.0);
113
        public final static Size ANSI_ARCH_D_PAPER_SIZE = new Size(36.0, 24.0);
114
        public final static Size ANSI_ARCH_E_PAPER_SIZE = new Size(42.0, 30.0);
115
        public static Size CUSTOM_PAPER_SIZE = new Size(100.0, 100.0);
116
        public final static int PRINT = 0;
117
        public final static int CUSTOM = 6;
118
        public final static int A0 = 5;
119
        public final static int A1 = 4;
120
        public final static int A2 = 3;
121
        public final static int A3 = 2;
122
        public final static int A4 = 1;
123
        public static int DPI = 300;
124
        public static int DPISCREEN = 72;
125
        public static Rectangle clipRect = new Rectangle();
126
        private static Point2D defaultGridGap;
127
128 10007 caballero
//        static {
129
//                new Attributes();
130
//        }
131 9392 caballero
132
        private PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
133
        private MediaSizeName m_type = MediaSizeName.ISO_A4;
134
        private boolean m_isLandSel;
135
        private OrientationRequested m_Orientation;
136
        private double m_TypeUnit = CHANGE[2]; //CENTIMETROS;
137
        private String m_NameUnit;
138
        private Double m_numX=null;
139
        private Double m_numY=null;
140
        private double m_unitX = 0;
141
        private double m_unitY = 0;
142
        private boolean hasmargin;
143 10007 caballero
        private int m_resolutionSel = NORMAL;
144
        private int m_typeSel = PRINT;
145 9392 caballero
        private int m_selTypeUnit = 2;
146
        public Size m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAlto(),
147
                        METRIC_A4_PAPER_SIZE.getAncho());
148
        public double[] m_area = { PULGADA, PULGADA, PULGADA, PULGADA };
149
        private double anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
150
        private Size m_sizeinUnits = m_sizePaper;
151
        private PrintQuality m_resolution=PrintQuality.NORMAL;
152
        /**
153 10007 caballero
         * Create a new object of Attributes.
154
         *
155 9392 caballero
         */
156
        public Attributes() {
157 20334 vcaballero
                m_NameUnit = PluginServices.getText(this,"Centimetros");
158 9392 caballero
                m_type = MediaSizeName.ISO_A4;
159
                m_isLandSel = true;
160
                setSizeinUnits(m_isLandSel);
161
162
                hasmargin = false;
163
                m_Orientation = OrientationRequested.LANDSCAPE;
164
                attributes.add(new Copies(1));
165
                attributes.add(MediaSizeName.ISO_A4);
166
                attributes.add(Sides.ONE_SIDED);
167
                attributes.add(Fidelity.FIDELITY_FALSE);
168
                attributes.add(PrintQuality.NORMAL);
169
170
                setType(m_typeSel);
171
172
                attributes.add(new MediaPrintableArea(0, 0,
173
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
174
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
175
                                MediaPrintableArea.MM));
176
        }
177
178
        /**
179 10007 caballero
         * Inserts the Layout's properties to print.
180 9392 caballero
         *
181 10007 caballero
         * @param typeSel Type of sheet.
182
         * @param units Units.
183
         * @param isLand True if the sheet is horizontal.
184
         * @param margin True if the sheet has margin.
185
         * @param resolution Type of quality of resolution.
186
         * @param area Printing area.
187 9392 caballero
         */
188
        public void setSelectedOptions(int typeSel, int units, boolean isLand,
189
                boolean margin, int resolution, double[] area) {
190
                setType(typeSel);
191
                setUnit(units);
192
                setIsLandScape(isLand);
193
                hasmargin = margin;
194
                setResolution(resolution);
195
                m_area = area;
196
        }
197
198
        /**
199 10007 caballero
         * Returns a PageFormat with the properties of printing.
200 9392 caballero
         *
201
         * @return PageFormat
202
         */
203
        public PageFormat getPageFormat() {
204
                PageFormat pf1 = new PageFormat();
205
                Paper paper = pf1.getPaper();
206
207
                if (isLandSpace()) {
208
                        pf1.setOrientation(0);
209
                        paper.setSize((m_sizePaper.getAncho() * DPISCREEN) / PULGADA,
210
                                (m_sizePaper.getAlto() * DPISCREEN) / PULGADA);
211
212
                        double aux = m_area[0];
213
                        m_area[0] = m_area[3];
214
                        m_area[3] = m_area[1];
215
                        m_area[1] = m_area[2];
216
                        m_area[2] = aux;
217
                        paper.setImageableArea(getInPixels(m_area[2]),
218
                                getInPixels(m_area[0]),
219
                                getInPixels(m_sizeinUnits.getAncho() - m_area[2] - m_area[3]),
220
                                getInPixels(m_sizeinUnits.getAlto() - m_area[0] - m_area[1]));
221
                } else {
222
                        pf1.setOrientation(1);
223
                        paper.setSize((m_sizePaper.getAlto() * DPISCREEN) / PULGADA,
224
                                (m_sizePaper.getAncho() * DPISCREEN) / PULGADA);
225
                        paper.setImageableArea(getInPixels(m_area[2]),
226
                                getInPixels(m_area[0]),
227
                                getInPixels(m_sizeinUnits.getAlto() - m_area[2] - m_area[3]),
228
                                getInPixels(m_sizeinUnits.getAncho() - m_area[0] - m_area[1]));
229
                }
230
231
                pf1.setPaper(paper);
232
233
                return pf1;
234
        }
235
236
        /**
237 10007 caballero
         * Changes centimeters to pixels.
238 9392 caballero
         *
239 10007 caballero
         * @param d Centimeters.
240 9392 caballero
         *
241
         * @return Pixels.
242
         */
243
        private double getInPixels(double d) {
244
                return d * (DPISCREEN / PULGADA);
245
        }
246
247
        /**
248 10007 caballero
         * Changes pixels to centimeters.
249 9392 caballero
         *
250
         * @param d Pixel.
251
         *
252 10007 caballero
         * @return Centimeters.
253 9392 caballero
         */
254
        private double getInCM(double d) {
255
                return d / (DPISCREEN / PULGADA);
256
        }
257
258
        /**
259 10007 caballero
         * Returns the printing area.
260 9392 caballero
         *
261 10007 caballero
         * @return Rectangle Area.
262 9392 caballero
         */
263 10007 caballero
        public Rectangle2D getArea() {
264 9392 caballero
                Rectangle2D.Double rect = new Rectangle2D.Double();
265
                rect.setRect(m_area[0], m_area[1],
266
                        m_sizePaper.getAncho() - m_area[0] - m_area[2],
267
                        m_sizePaper.getAlto() - m_area[1] - m_area[3]);
268
269
                return rect;
270
        }
271
272
        /**
273 10007 caballero
         * Inserts the attributes with a PageFormat.
274 9392 caballero
         *
275 10007 caballero
         * @param pf PageFormat.
276 9392 caballero
         */
277
        public void setPageFormat(PageFormat pf) {
278
                Size size = null;
279
280
                if (pf.getOrientation() == 0) {
281
                        setIsLandScape(true);
282
                        m_Orientation = OrientationRequested.LANDSCAPE;
283
                        size = new Size(pf.getHeight(), pf.getWidth());
284
                } else {
285
                        setIsLandScape(false);
286
                        m_Orientation = OrientationRequested.PORTRAIT;
287
                        size = new Size(pf.getHeight(), pf.getWidth());
288
                }
289
290
                attributes.add(m_Orientation);
291
292
                //area
293
                m_area[0] = getInCM(pf.getImageableY());
294
                m_area[2] = getInCM(pf.getImageableX());
295
                m_area[1] = m_sizePaper.getAlto() - getInCM(pf.getImageableHeight()) -
296
                        m_area[0];
297
                m_area[3] = m_sizePaper.getAncho() - getInCM(pf.getImageableWidth()) -
298
                        m_area[2];
299
300
                //        tipo formato
301
                if (isLandSpace()) {
302
                        //double aux = m_area[0];
303
                        //m_area[0] = m_area[3];
304
                        //m_area[3] = m_area[1];
305
                        //m_area[1] = m_area[2];
306
                        //m_area[2] = aux;
307
                        attributes.add(new MediaPrintableArea((float) (m_area[2] * 10),
308
                                        (float) (m_area[0] * 10),
309
                                        (float) (m_sizeinUnits.getAlto() - m_area[0] - m_area[1]) * 10,
310
                                        (float) (m_sizeinUnits.getAncho() - m_area[2] - m_area[3]) * 10,
311
                                        MediaPrintableArea.MM));
312
                } else {
313
                        attributes.add(new MediaPrintableArea((float) (m_area[0] * 10),
314
                                        (float) (m_area[1] * 10),
315
                                        (float) (getInCM(pf.getImageableWidth()) * 10),
316
                                        (float) (getInCM(pf.getImageableHeight()) * 10),
317
                                        MediaPrintableArea.MM));
318
                }
319
320
                setType(getTypePaper(size));
321
        }
322
323
        /**
324 10007 caballero
         * Returns a PrintRequestAttributeSet with all properties to the PrintDialog.
325 9392 caballero
         *
326 10007 caballero
         * @return PrintRequestAttributesSet.
327 9392 caballero
         */
328
        public PrintRequestAttributeSet toPrintAttributes() {
329
                HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
330
331
                setType(m_typeSel);
332
333
                resul.add(m_type);
334
335
                //units, no hace falta a?adirlo a attributes
336
                resul.add(m_Orientation);
337
                setArea(m_area);
338
                resul.add(new MediaPrintableArea(0, 0,
339
                                (float) ((m_sizePaper.getAlto() * DPISCREEN) / PULGADA),
340
                                (float) ((m_sizePaper.getAncho() * DPISCREEN) / PULGADA),
341
                                MediaPrintableArea.MM));
342
343
                resul.add(m_resolution);
344
                //resul.add(new PrinterResolution(DPI,DPI,PrinterResolution.DPI));
345
                return resul;
346
        }
347
348
        /**
349 10007 caballero
         * Inserts printing area.
350 9392 caballero
         *
351 10007 caballero
         * @param area Printing area.
352 9392 caballero
         */
353
        private void setArea(double[] area) {
354
                if (!isLandSpace()) {
355
                        attributes.add(new MediaPrintableArea((float) (area[2] * 10),
356
                                        (float) (area[0] * 10),
357
                                        (float) ((m_sizePaper.getAncho() - area[2] - area[3]) * 10),
358
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
359
                                        MediaPrintableArea.MM));
360
361
                        clipRect.setRect((area[2] / PULGADA * DPI),
362
                                area[0] / PULGADA * DPI,
363
                                (m_sizePaper.getAncho() - area[2] - area[3]) / PULGADA * DPI,
364
                                (m_sizePaper.getAlto() - area[0] - area[1]) / PULGADA * DPI);
365
                } else {
366
                        attributes.add(new MediaPrintableArea((float) (area[0] * 10),
367
                                        (float) (area[3] * 10),
368
                                        (float) ((m_sizePaper.getAlto() - area[0] - area[1]) * 10),
369
                                        (float) ((m_sizePaper.getAncho() - area[3] - area[2]) * 10),
370
                                        MediaPrintableArea.MM));
371
372
                        clipRect.setRect((area[1] / PULGADA * DPI),
373
                                area[2] / PULGADA * DPI,
374
                                (m_sizePaper.getAncho() - area[1] - area[0]) / PULGADA * DPI,
375
                                (m_sizePaper.getAlto() - area[2] - area[3]) / PULGADA * DPI);
376
                }
377
        }
378
379
        /**
380 10007 caballero
         * Returns true if the margin should be used.
381 9392 caballero
         *
382 10007 caballero
         * @return True if margin should be used.
383 9392 caballero
         */
384
        public boolean isMargin() {
385
                return hasmargin;
386
        }
387
388
        /**
389 10007 caballero
         * Returns the resolution of properties to print.
390 9392 caballero
         *
391 10007 caballero
         * @return Resolution.
392 9392 caballero
         */
393
        public int getResolution() {
394
                return m_resolutionSel;
395
        }
396
397
        /**
398 10007 caballero
         * Inserts the resolution to apply at printing.
399 9392 caballero
         *
400 10007 caballero
         * @param i Type of resolution.
401
         *
402
         * The type of resolution can be:
403
         *
404
         *  DRAFT:         27 dpi.
405
         *  NORMAL: 300 dpi.
406
         *  HIGH:         600 dpi.
407 9392 caballero
         */
408
        private void setResolution(int i) {
409
                m_resolutionSel = i;
410
411
                switch (i) {
412 10007 caballero
                        case (HIGH):
413 9392 caballero
414
                                m_resolution=PrintQuality.HIGH;
415
                                DPI = 600;
416
417
                                break;
418
419 10007 caballero
                        case (NORMAL):
420 9392 caballero
421
                                m_resolution=PrintQuality.NORMAL;
422
                                DPI = 300;
423
424
                                break;
425
426 10007 caballero
                        case (DRAFT):
427 9392 caballero
428
                                m_resolution=PrintQuality.DRAFT;
429
                                DPI = 72;
430
431
                                break;
432
                }
433 18623 jdominguez
                attributes.add(m_resolution);
434 9392 caballero
        }
435
436
        /**
437 10007 caballero
         * Returns attributes of PrintDialog.
438 9392 caballero
         *
439 10007 caballero
         * @return Attributes of printing.
440 9392 caballero
         */
441
        public PrintRequestAttributeSet getAttributes() {
442
                return attributes;
443
        }
444
445
        /**
446 10007 caballero
         * Returns True if the position of sheet is horizontal or vertical.
447 9392 caballero
         *
448 10007 caballero
         * @return True if position is horizontal.
449 9392 caballero
         */
450
        public boolean isLandSpace() {
451
                return m_isLandSel;
452
        }
453
454
        /**
455 10007 caballero
         * Returns the format of sheet.
456 9392 caballero
         *
457 10007 caballero
         * @return Format of sheet.
458 9392 caballero
         */
459
        public int getType() {
460
                return m_typeSel;
461
        }
462
463
        /**
464 10007 caballero
         * Inserts the number of units to define the coords X grid.
465 9392 caballero
         *
466 10007 caballero
         * @param d number of units to define the grid.
467 9392 caballero
         */
468
        public void setNumUnitsX(double d) {
469
                m_numX = new Double(d);
470
        }
471
472
        /**
473 10007 caballero
         * Returns the number of pixels that represents a unit of coords X.
474 9392 caballero
         *
475 10007 caballero
         * @return Number of pixels coords X.
476 9392 caballero
         */
477 10007 caballero
478 9392 caballero
        public double getNumUnitsX() {
479
                if (m_numX == null)
480
                        m_numX = new Double(getDefaultGridGap().getX());
481
                return m_numX.doubleValue();
482
        }
483
484
        /**
485 10007 caballero
         * Returns the number of pixels that represents a unit of coords Y.
486 9392 caballero
         *
487 10007 caballero
         * @return Number of pixels coords Y.
488 9392 caballero
         */
489 10007 caballero
490 9392 caballero
        public double getNumUnitsY() {
491
                if (m_numY == null)
492
                        m_numY = new Double(getDefaultGridGap().getY());
493
                return m_numY.doubleValue();
494
        }
495
496
        /**
497 10007 caballero
         * Inserts the number of units to define the coords X grid.
498 9392 caballero
         *
499 10007 caballero
         * @param d number of units to define the grid.
500 9392 caballero
         */
501
        public void setNumUnitsY(double d) {
502
                m_numY = new Double(d);
503
        }
504
505
        /**
506 10007 caballero
         * Inserts true if the sheet is horizontal or false if is vertical.
507 9392 caballero
         *
508 10007 caballero
         * @param b True if sheet is horizontal.
509 9392 caballero
         */
510
        public void setIsLandScape(boolean b) {
511
                m_isLandSel = b;
512
                setType(m_typeSel);
513
514
                if (m_isLandSel) {
515
                        m_Orientation = OrientationRequested.LANDSCAPE;
516
                } else {
517
                        m_Orientation = OrientationRequested.PORTRAIT;
518
                }
519
        }
520
521
        /**
522 10007 caballero
         * Inserts the type of sheet to show.
523 9392 caballero
         *
524 10007 caballero
         * @param t Type of sheet.
525 9392 caballero
         */
526
        public void setType(int t) {
527
                m_typeSel = t;
528
529
                //m_sizePaper=getSizePaper(t);
530
                switch (t) {
531
                        case (PRINT):
532
                                m_type = ((MediaSizeName) attributes.get(Media.class));
533
534
                                if (isLandSpace()) {
535
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
536
                                                        METRIC_A4_PAPER_SIZE.getAlto());
537
                                } else {
538
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
539
                                }
540
541
                                break;
542
543
                        case (A4):
544
                                m_type = MediaSizeName.ISO_A4;
545
546
                                if (isLandSpace()) {
547
                                        m_sizePaper = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
548
                                                        METRIC_A4_PAPER_SIZE.getAlto());
549
                                } else {
550
                                        m_sizePaper = METRIC_A4_PAPER_SIZE;
551
                                }
552
553
                                break;
554
555
                        case (A3):
556
                                m_type = MediaSizeName.ISO_A3;
557
558
                                if (isLandSpace()) {
559
                                        m_sizePaper = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
560
                                                        METRIC_A3_PAPER_SIZE.getAlto());
561
                                } else {
562
                                        m_sizePaper = METRIC_A3_PAPER_SIZE;
563
                                }
564
565
                                break;
566
567
                        case (A2):
568
                                m_type = MediaSizeName.ISO_A2;
569
570
                                if (isLandSpace()) {
571
                                        m_sizePaper = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
572
                                                        METRIC_A2_PAPER_SIZE.getAlto());
573
                                } else {
574
                                        m_sizePaper = METRIC_A2_PAPER_SIZE;
575
                                }
576
577
                                break;
578
579
                        case (A1):
580
                                m_type = MediaSizeName.ISO_A1;
581
582
                                if (isLandSpace()) {
583
                                        m_sizePaper = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
584
                                                        METRIC_A1_PAPER_SIZE.getAlto());
585
                                } else {
586
                                        m_sizePaper = METRIC_A1_PAPER_SIZE;
587
                                }
588
589
                                break;
590
591
                        case (A0):
592
                                m_type = MediaSizeName.ISO_A0;
593
594
                                if (isLandSpace()) {
595
                                        m_sizePaper = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
596
                                                        METRIC_A0_PAPER_SIZE.getAlto());
597
                                } else {
598
                                        m_sizePaper = METRIC_A0_PAPER_SIZE;
599
                                }
600
601
                                break;
602
603
                        case (CUSTOM):
604
                                m_type = MediaSizeName.PERSONAL_ENVELOPE;
605
606 13561 caballero
//                        if (isLandSpace()) {
607 16737 vcaballero
//                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit,
608
//                                        CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit);
609 13561 caballero
//                } else {
610
//                        m_sizePaper = new Size(CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit,
611
//                                        CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit);
612
//                }
613 9392 caballero
614
                                break;
615
                }
616 16853 vcaballero
                attributes.add(m_type);
617 9392 caballero
                setSizeinUnits(isLandSpace());
618
                m_sizeinUnits = getSizeInUnits();
619
        }
620
621
        /**
622 10007 caballero
         * Returns the size of sheet.
623 9392 caballero
         *
624 10007 caballero
         * @param isLand True if is horizontal and false if is vertical.
625
         * @param type Type of sheet.
626 9392 caballero
         *
627 10007 caballero
         * @return Size of sheet with these properties.
628 9392 caballero
         */
629
        public Size getSizeinUnits(boolean isLand, int type) {
630
                Size size = null;
631
632
                switch (type) {
633
                        case (PRINT):
634
635
                                if (isLand) {
636
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
637
                                                        METRIC_A4_PAPER_SIZE.getAlto());
638
                                } else {
639
                                        size = METRIC_A4_PAPER_SIZE;
640
                                }
641
642
                                break;
643
644
                        case (A4):
645
646
                                if (isLand) {
647
                                        size = new Size(METRIC_A4_PAPER_SIZE.getAncho(),
648
                                                        METRIC_A4_PAPER_SIZE.getAlto());
649
                                } else {
650
                                        size = METRIC_A4_PAPER_SIZE;
651
                                }
652
653
                                break;
654
655
                        case (A3):
656
657
                                if (isLand) {
658
                                        size = new Size(METRIC_A3_PAPER_SIZE.getAncho(),
659
                                                        METRIC_A3_PAPER_SIZE.getAlto());
660
                                } else {
661
                                        size = METRIC_A3_PAPER_SIZE;
662
                                }
663
664
                                break;
665
666
                        case (A2):
667
668
                                if (isLand) {
669
                                        size = new Size(METRIC_A2_PAPER_SIZE.getAncho(),
670
                                                        METRIC_A2_PAPER_SIZE.getAlto());
671
                                } else {
672
                                        size = METRIC_A2_PAPER_SIZE;
673
                                }
674
675
                                break;
676
677
                        case (A1):
678
679
                                if (isLand) {
680
                                        size = new Size(METRIC_A1_PAPER_SIZE.getAncho(),
681
                                                        METRIC_A1_PAPER_SIZE.getAlto());
682
                                } else {
683
                                        size = METRIC_A1_PAPER_SIZE;
684
                                }
685
686
                                break;
687
688
                        case (A0):
689
690
                                if (isLand) {
691
                                        size = new Size(METRIC_A0_PAPER_SIZE.getAncho(),
692
                                                        METRIC_A0_PAPER_SIZE.getAlto());
693
                                } else {
694
                                        size = METRIC_A0_PAPER_SIZE;
695
                                }
696
697
                                break;
698
699
                        case (CUSTOM):
700
701 13561 caballero
//                                if (isLand) {
702
                                size = new Size(CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit,
703
                                                CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit);
704
//                        } else {
705
//                                size = new Size(CUSTOM_PAPER_SIZE.getAlto()*m_TypeUnit,
706
//                                                CUSTOM_PAPER_SIZE.getAncho()*m_TypeUnit);
707
//                        }
708 9392 caballero
                                break;
709
                }
710
711
                m_sizeinUnits = new Size(size.getAlto() / m_TypeUnit,
712
                                size.getAncho() / m_TypeUnit);
713
714
                return m_sizeinUnits;
715
        }
716
717
        /**
718 10007 caballero
         * Retunrs the type of units is in use.
719 9392 caballero
         *
720 10007 caballero
         * @return Type of units.
721 9392 caballero
         */
722
        public int getSelTypeUnit() {
723
                return m_selTypeUnit;
724
        }
725
726
        /**
727 10007 caballero
         * Returns the name of units.
728 9392 caballero
         *
729 10007 caballero
         * @return String Name of units.
730 9392 caballero
         */
731
        public String getNameUnit() {
732
                return m_NameUnit;
733
        }
734
735
        /**
736 10007 caballero
         * Selecting the type of unit that is utilized in the Layout, by default is in centimetres.
737 9392 caballero
         *
738 10007 caballero
         * @param sel Type of unit.
739 9392 caballero
         */
740
        public void setUnit(int sel) {
741
                m_selTypeUnit = sel;
742 10007 caballero
                m_TypeUnit = CHANGE[sel];
743 20345 vcaballero
                m_NameUnit = PluginServices.getText(this,MapContext.getDistanceNames()[sel]);
744 9392 caballero
                m_numX = m_numY = new Double(UNIT[sel]);
745
        }
746
747
        /**
748 10007 caballero
         * Actualize to date m_sizeinUnits with the height and width of the sheet
749
         * in the units of mediated selected.
750 9392 caballero
         *
751 10007 caballero
         * @param b True if is horizontal.
752 9392 caballero
         */
753
        public void setSizeinUnits(boolean b) {
754
                if (b) {
755
                        m_sizeinUnits = new Size(m_sizePaper.getAlto() / m_TypeUnit,
756
                                        m_sizePaper.getAncho() / m_TypeUnit);
757
                } else {
758
                        m_sizeinUnits = new Size(m_sizePaper.getAncho() / m_TypeUnit,
759
                                        m_sizePaper.getAlto() / m_TypeUnit);
760
                }
761
        }
762
763
        /**
764 10007 caballero
         * Returns a double in the unit of measure selected from the double in centimeters.
765 9392 caballero
         *
766 10007 caballero
         * @param d Distance in centimeters.
767 9392 caballero
         *
768 10007 caballero
         * @return Distance in the unit measure selected.
769 9392 caballero
         */
770
        public double toUnits(double d) {
771
                NumberFormat nf = NumberFormat.getInstance();
772
                nf.setMaximumFractionDigits(2);
773
774
                String s = String.valueOf(nf.format(d));
775
                s = s.replace(',', '.');
776
777
                return (Double.valueOf(s).doubleValue() / m_TypeUnit);
778
        }
779
780
        /**
781 10007 caballero
         * Returns a double in centimeters from the the unit of measure selected.
782 9392 caballero
         *
783 10007 caballero
         * @param d Distance in the unit measure selected.
784 9392 caballero
         *
785 10007 caballero
         * @return Distance in centimeters.
786 9392 caballero
         */
787
        public double fromUnits(double d) {
788
                return (d * m_TypeUnit);
789
        }
790
791
        /**
792 10007 caballero
         * Returns one centimeter in pixels on screen in this moment.
793 9392 caballero
         *
794 10007 caballero
         * @param rect Rectangle of sheet.
795 9392 caballero
         *
796 10007 caballero
         * @return Pixels from centimeter.
797 9392 caballero
         */
798 10007 caballero
        public double getPixXCm(Rectangle2D rect) {
799 9392 caballero
                double value = m_sizePaper.getAncho();
800
                double cm = CHANGE[2];
801
                double unidades = 0;
802
                unidades = ((rect.getMaxX() - rect.getMinX()) /((value / cm)));
803
                return unidades;
804
        }
805
806
        /**
807 10007 caballero
         * Inserts the number of pixels of one centimeter of X.
808 9392 caballero
         *
809 10007 caballero
         * @param rect Rectangle of sheet.
810 9392 caballero
         */
811 10007 caballero
        public void setDistanceUnitX(Rectangle2D rect) {
812 9392 caballero
                double value = m_sizePaper.getAncho();
813 10007 caballero
                double unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsX()));
814 9392 caballero
                m_unitX = unidades;
815
        }
816
817
        /**
818 10007 caballero
         * Inserts the number of pixels of one centimeter of Y.
819 9392 caballero
         *
820 10007 caballero
         * @param rect Rectangle of sheet.
821 9392 caballero
         */
822 10007 caballero
        public void setDistanceUnitY(Rectangle2D rect) {
823 9392 caballero
                double value = m_sizePaper.getAncho();
824 10007 caballero
                double unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / m_TypeUnit) / getNumUnitsY()));
825 9392 caballero
                m_unitY = unidades;
826
        }
827
828
        /**
829 10007 caballero
         * Obtains the rectangle that represents the sheet with the characteristics
830
         * that contains attributes
831 9392 caballero
         *
832 10007 caballero
         * @param rect rectangle.
833 9392 caballero
         *
834
         */
835 13402 caballero
        public void obtainRect(boolean isPrint, Rectangle2D rect, int w, int h) {
836 9392 caballero
                double value1 = 0;
837
                double value2 = 0;
838
839 13402 caballero
                if (!isPrint) {
840
                        if (isLandSpace()) {
841
                                anchoXalto = m_sizePaper.getAncho() / m_sizePaper.getAlto();
842
                                rect = getRectangleLandscape(rect, w, h);
843
                        } else {
844
                                anchoXalto = m_sizePaper.getAlto() / m_sizePaper.getAncho();
845
                                rect = getRectanglePortrait(rect, w, h);
846
                        }
847
                } else {
848 9392 caballero
                        value1 = m_sizePaper.getAncho();
849
                        value2 = m_sizePaper.getAlto();
850
                        rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
851
                                ((value2 / PULGADA) * DPI));
852 13402 caballero
                }
853 9392 caballero
854
                setDistanceUnitX(rect);
855
                setDistanceUnitY(rect);
856
        }
857
858
        /**
859 10007 caballero
         * It obtains the rect that is adjusted to the size of the window,
860
         * to see the full extent of horizontal layout.
861 9392 caballero
         *
862 10007 caballero
         * @param rect Rectangle sheet.
863
         * @param w Width of Layout.
864
         * @param h Height of Layout.
865 9392 caballero
         *
866 10007 caballero
         * @return Rectangle modified.
867 9392 caballero
         */
868 10007 caballero
        public Rectangle2D.Double getRectangleLandscape(Rectangle2D rect,
869 9392 caballero
                int w, int h) {
870
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
871
                int x0 = (int) rect.getMinX();
872
                int y0 = (int) rect.getMinY();
873
                int y1;
874
                int x1;
875
                y1 = (h - (2 * y0));
876
                x1 = (int) (y1 * anchoXalto);
877
878
                if (((int) (((h) - (2 * y0)) * anchoXalto)) > ((w) - (2 * x0))) {
879
                        x1 = ((w) - (2 * x0));
880
                        y1 = (int) (x1 / anchoXalto);
881
                }
882
883
                rectaux.setRect(x0, y0, x1, y1);
884
885
                return rectaux;
886
        }
887
888
        /**
889 10007 caballero
         * Returns the size of sheet in the units of measure selected.
890 9392 caballero
         *
891 10007 caballero
         * @return Size of sheet.
892 9392 caballero
         */
893
        public Size getSizeInUnits() {
894
                return m_sizeinUnits;
895
        }
896
897
        /**
898 10007 caballero
         * It obtains the rect that is adjusted to the size of the window,
899
         * to see the full extent of vertical layout.
900 9392 caballero
         *
901 10007 caballero
         * @param rect Rectangle sheet.
902
         * @param w Width of Layout.
903
         * @param h Height of Layout.
904 9392 caballero
         *
905 10007 caballero
         * @return Rectangle modified.
906 9392 caballero
         */
907 10007 caballero
        public Rectangle2D.Double getRectanglePortrait(Rectangle2D rect,
908 9392 caballero
                int w, int h) {
909
                Rectangle2D.Double rectaux = new Rectangle2D.Double();
910
                int x0 = (int) rect.getMinX();
911
                int y0 = (int) rect.getMinY();
912
                int y1;
913
                int x1;
914
                x1 = (w - (2 * x0));
915
                y1 = (int) (x1 * anchoXalto);
916
917
                if (((int) (((w) - (2 * x0)) * anchoXalto)) > ((h) - (2 * y0))) {
918
                        y1 = (h - (2 * y0));
919
                        x1 = (int) (y1 / anchoXalto);
920
                }
921
922
                rectaux.setRect(x0, y0, x1, y1);
923
924
                return rectaux;
925
        }
926
927
        /**
928 10007 caballero
         * It obtains the type of format from the size.
929 9392 caballero
         *
930 10007 caballero
         * @param size Size of sheet.
931 9392 caballero
         *
932 10007 caballero
         * @return Type of sheet.
933 9392 caballero
         */
934
        private int getTypePaper(Size size) {
935
                int tol = 1;
936
                Size auxSize = size;
937
938
                if (isLandSpace()) {
939
                        auxSize = new Size(size.getAncho(), size.getAlto());
940
                }
941
942
                if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
943
                                METRIC_A4_PAPER_SIZE.getAncho())) &&
944
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
945
                                METRIC_A4_PAPER_SIZE.getAlto()))) {
946
                        return A4;
947
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
948
                                METRIC_A3_PAPER_SIZE.getAncho())) &&
949
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
950
                                METRIC_A3_PAPER_SIZE.getAlto()))) {
951
                        return A3;
952
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
953
                                METRIC_A2_PAPER_SIZE.getAncho())) &&
954
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
955
                                METRIC_A2_PAPER_SIZE.getAlto()))) {
956
                        return A2;
957
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
958
                                METRIC_A1_PAPER_SIZE.getAncho())) &&
959
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
960
                                METRIC_A1_PAPER_SIZE.getAlto()))) {
961
                        return A1;
962
                } else if ((((auxSize.getAncho() * PULGADA) / DPISCREEN) < (tol +
963
                                METRIC_A0_PAPER_SIZE.getAncho())) &&
964
                                (((auxSize.getAlto() * PULGADA) / DPISCREEN) < (tol +
965
                                METRIC_A0_PAPER_SIZE.getAlto()))) {
966
                        return A0;
967
                }
968
969
                return A4;
970
        }
971
972
        /**
973 10007 caballero
         * It returns an Object XMLEntity with the information the necessary attributes
974
         * to be able later to create again the original object.
975 9392 caballero
         *
976
         * @return XMLEntity.
977 10007 caballero
         *
978 9392 caballero
         */
979
        public XMLEntity getXMLEntity() {
980
                XMLEntity xml = new XMLEntity();
981
                xml.putProperty("className",this.getClass().getName());
982
                xml.putProperty("DPI", DPI);
983
                xml.putProperty("DPISCREEN", DPISCREEN);
984
                xml.putProperty("m_TypeUnit", m_TypeUnit);
985
                xml.putProperty("m_numX", m_numX);
986
                xml.putProperty("m_numY", m_numY);
987
                xml.putProperty("m_unitX", m_unitX);
988
                xml.putProperty("m_unitY", m_unitY);
989
                xml.putProperty("hasmargin", hasmargin);
990
                xml.putProperty("m_resolutionSel", m_resolutionSel);
991
                xml.putProperty("m_typeSel", m_typeSel);
992
                xml.putProperty("m_selTypeUnit", m_selTypeUnit);
993
                xml.addChild(m_sizePaper.getXMLEntity());
994
                xml.putProperty("m_area", m_area);
995
                xml.putProperty("anchoXalto", anchoXalto);
996
                xml.addChild(m_sizeinUnits.getXMLEntity());
997
998
                // Landscape
999
                xml.putProperty("m_isLandSel", m_isLandSel);
1000
1001
                return xml;
1002
        }
1003
1004
        /**
1005 10007 caballero
         * It returns an Object of this class from a XMLEntity.
1006 9392 caballero
         *
1007 10007 caballero
         *
1008 9392 caballero
         * @param xml XMLEntity
1009
         *
1010 10007 caballero
         * @return Object of Attributes.
1011 9392 caballero
         */
1012
        public static Attributes createAtributes(XMLEntity xml) {
1013
                Attributes atri = new Attributes();
1014
                DPI = xml.getIntProperty("DPI");
1015
                if (DPI==300){
1016
                        atri.m_resolution=PrintQuality.NORMAL;
1017
                }else if (DPI==600){
1018
                        atri.m_resolution=PrintQuality.HIGH;
1019
                }else if (DPI==72){
1020
                        atri.m_resolution=PrintQuality.DRAFT;
1021
                }
1022
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1023
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1024
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1025
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1026
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1027
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1028
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1029
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1030
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1031
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1032
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1033
1034
                if (atri.hasmargin) {
1035
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1036
                }
1037
1038
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1039
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1040
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1041
                atri.setIsLandScape(atri.m_isLandSel);
1042
                return atri;
1043
        }
1044
1045
        /**
1046 10007 caballero
         * It returns an Object of this class from a XMLEntity.
1047 9392 caballero
         *
1048 10007 caballero
         *
1049 9392 caballero
         * @param xml XMLEntity
1050
         *
1051 10007 caballero
         * @return Object of Attributes.
1052 9392 caballero
         */
1053
        public static Attributes createAtributes03(XMLEntity xml) {
1054
                Attributes atri = new Attributes();
1055
                DPI = xml.getIntProperty("DPI");
1056
                if (DPI==300){
1057
                        atri.m_resolution=PrintQuality.NORMAL;
1058
                }else if (DPI==600){
1059
                        atri.m_resolution=PrintQuality.HIGH;
1060
                }else if (DPI==72){
1061
                        atri.m_resolution=PrintQuality.DRAFT;
1062
                }
1063
                DPISCREEN = xml.getIntProperty("DPISCREEN");
1064
                atri.m_TypeUnit = xml.getDoubleProperty("m_TypeUnit");
1065
                atri.setNumUnitsX(xml.getDoubleProperty("m_numX"));
1066
                atri.setNumUnitsY(xml.getDoubleProperty("m_numY"));
1067
                atri.m_unitX = xml.getDoubleProperty("m_unitX");
1068
                atri.m_unitY = xml.getDoubleProperty("m_unitY");
1069
                atri.hasmargin = xml.getBooleanProperty("hasmargin");
1070
                atri.m_resolutionSel = xml.getIntProperty("m_resolutionSel");
1071
                atri.m_typeSel = xml.getIntProperty("m_typeSel");
1072
                atri.m_selTypeUnit = xml.getIntProperty("m_selTypeUnit");
1073
                atri.m_sizePaper = Size.createSize(xml.getChild(0));
1074
1075
                if (atri.hasmargin) {
1076
                        atri.m_area = xml.getDoubleArrayProperty("m_area");
1077
                }
1078
1079
                atri.anchoXalto = xml.getDoubleProperty("anchoXalto");
1080
                atri.m_sizeinUnits = Size.createSize(xml.getChild(1));
1081
                atri.m_isLandSel = xml.getBooleanProperty("m_isLandSel");
1082
                atri.setIsLandScape(atri.m_isLandSel);
1083
                return atri;
1084
        }
1085 10007 caballero
        /**
1086
         * Inserts the default number of pixels that represents a unit of grid.
1087
         *
1088
         * @param hGap Horizontal distance.
1089
         * @param vGap Vertical distance.
1090
         */
1091 9392 caballero
        public static void setDefaultGridGap(double hGap, double vGap) {
1092
                defaultGridGap = new Point2D.Double(hGap, vGap);
1093
        }
1094 10007 caballero
        /**
1095
         * Returns the default number of pixels that represents a unit of grid.
1096
         *
1097
         * @return Number of pixels of grid.
1098
         */
1099 9392 caballero
        public static Point2D getDefaultGridGap() {
1100
                if (defaultGridGap == null){
1101
                        XMLEntity xml = PluginServices.getPluginServices("com.iver.cit.gvsig").getPersistentXML();
1102
                        double hGap = xml.contains("DefaultLayoutGridHorizontalGapX") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapX"): 0.25;
1103
                        double vGap = xml.contains("DefaultLayoutGridHorizontalGapY") ? xml.getDoubleProperty("DefaultLayoutGridHorizontalGapY"): 0.25;
1104
                        defaultGridGap = new Point2D.Double(hGap, vGap);
1105
                }
1106
                return defaultGridGap;
1107
        }
1108 10851 caballero
1109
        /**
1110 10913 caballero
         * Devuelve el n?mero de pixels de longitud en el eje Y que representa a la unidad  del mapa que est?
1111
         * seleccionada, suelen ser cent?metros.
1112 10851 caballero
         *
1113
         * @return n?mero de pixels que representa la unidad seleccionada.
1114
         */
1115
        public double getUnitInPixelsY() {
1116
                return m_unitY;
1117
        }
1118
1119
        /**
1120 10913 caballero
         * Devuelve el n?mero de pixels de longitud en el eje X que representa a la unidad del mapa que est?
1121
         * seleccionada, suelen ser cent?metros.
1122 10851 caballero
         *
1123
         * @return n?mero de pixels que representa la unidad seleccionada.
1124
         */
1125
        public double getUnitInPixelsX() {
1126
                return m_unitX;
1127
        }
1128
1129 9392 caballero
}