Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / Attributes.java @ 1632

History | View | Annotate | Download (46.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2015 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.layout;
25

    
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.awt.print.PageFormat;
29
import java.awt.print.Paper;
30
import java.text.NumberFormat;
31
import java.util.ArrayList;
32

    
33
import javax.print.attribute.DocAttributeSet;
34
import javax.print.attribute.HashDocAttributeSet;
35
import javax.print.attribute.HashPrintRequestAttributeSet;
36
import javax.print.attribute.PrintRequestAttributeSet;
37
import javax.print.attribute.standard.Copies;
38
import javax.print.attribute.standard.Fidelity;
39
import javax.print.attribute.standard.Media;
40
import javax.print.attribute.standard.MediaPrintableArea;
41
import javax.print.attribute.standard.MediaSizeName;
42
import javax.print.attribute.standard.OrientationRequested;
43
import javax.print.attribute.standard.PrintQuality;
44
import javax.print.attribute.standard.Sides;
45

    
46
import org.gvsig.andami.PluginServices;
47
import org.gvsig.andami.PluginsLocator;
48
import org.gvsig.app.extension.LayoutExtension;
49
import org.gvsig.app.gui.preferencespage.PreferenceKeys;
50
import org.gvsig.compat.CompatLocator;
51
import org.gvsig.compat.print.PrintAttributes;
52
import org.gvsig.fmap.mapcontext.MapContext;
53
import org.gvsig.i18n.Messages;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dynobject.DynStruct;
56
import org.gvsig.tools.persistence.PersistenceManager;
57
import org.gvsig.tools.persistence.Persistent;
58
import org.gvsig.tools.persistence.PersistentState;
59
import org.gvsig.tools.persistence.exception.PersistenceException;
60
import org.gvsig.utils.XMLEntity;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63
import org.gvsig.app.project.documents.layout.Size;
64

    
65
/**
66
 * Attributes of Layout.
67
 * 
68
 * @author Vicente Caballero Navarro
69
 * @author jldominguez
70
 */
71
public class Attributes implements Persistent, Cloneable {
72

    
73
    public static final String PERSISTENCE_DEFINITION_NAME = "Attributes";
74
    public static final String DPI_FIELD = "DPI";
75
    public static final String DPISCREEN_FIELD = "DPISCREEN";
76
    public static final String TYPEUNIT_FIELD = "typeUnit";
77
    public static final String NUMX_FIELD = "numX";
78
    public static final String NUMY_FIELD = "numY";
79
    public static final String UNITX_FIELD = "unitX";
80
    public static final String UNITY_FIELD = "unitY";
81
    public static final String HASMARGIN_FIELD = "hasMargin";
82
    public static final String RESOLUTIONSEL_FIELD = "resolutionSel";
83
    public static final String TYPESEL_FIELD = "typeSel";
84
    public static final String SELTYPEUNIT_FIELD = "selTypeUnit";
85
    public static final String SIZEPAPER_FIELD = "sizePaper";
86
    public static final String AREA_FIELD = "area";
87
    public static final String SIZEINUNITS_FIELD = "sizeInUnits";
88
    public static final String ISLANDSEL_FIELD = "isLandSel";
89
    public static final String DEGREES = "Grados";
90
    public static final int HIGH = 0;
91
    public static final int NORMAL = 1;
92
    public static final int DRAFT = 2;
93

    
94
    // Para impresi?n
95
    public final static double PULGADA = 2.54;
96

    
97
    public final static Size STANDARD_LETTER_PAPER_SIZE = new Size(8.5, 11.0);
98
    public final static Size STANDARD_FOLIO_PAPER_SIZE = new Size(8.5, 13.0);
99
    public final static Size STANDARD_LEGAL_PAPER_SIZE = new Size(8.5, 14.0);
100
    public final static Size STANDARD_TABLOID_PAPER_SIZE = new Size(11.0, 17.0);
101
    public final static Size METRIC_A0_PAPER_SIZE = new Size(118.9, 84.1);
102
    public final static Size METRIC_A1_PAPER_SIZE = new Size(84.1, 59.4);
103
    public final static Size METRIC_A2_PAPER_SIZE = new Size(59.4, 42.0);
104
    public final static Size METRIC_A3_PAPER_SIZE = new Size(42.0, 29.7);
105
    public final static Size METRIC_A4_PAPER_SIZE = new Size(29.7, 21.0);
106
    public final static Size METRIC_A5_PAPER_SIZE = new Size(21.0, 14.8);
107
    public final static Size ANSI_ENG_A_PAPER_SIZE = new Size(11.0, 8.5);
108
    public final static Size ANSI_ENG_B_PAPER_SIZE = new Size(17.0, 11.0);
109
    public final static Size ANSI_ENG_C_PAPER_SIZE = new Size(22.0, 17.0);
110
    public final static Size ANSI_ENG_D_PAPER_SIZE = new Size(34.0, 22.0);
111
    public final static Size ANSI_ENG_E_PAPER_SIZE = new Size(44.0, 34.0);
112
    public final static Size ANSI_ARCH_A_PAPER_SIZE = new Size(12.0, 9.0);
113
    public final static Size ANSI_ARCH_B_PAPER_SIZE = new Size(18.0, 12.0);
114
    public final static Size ANSI_ARCH_C_PAPER_SIZE = new Size(24.0, 18.0);
115
    public final static Size ANSI_ARCH_D_PAPER_SIZE = new Size(36.0, 24.0);
116
    public final static Size ANSI_ARCH_E_PAPER_SIZE = new Size(42.0, 30.0);
117
    
118
    /**
119
     * Stores the last custom paper size. It will be equal to
120
     * {@link #getPaperSize()} when {@link #getType()} is
121
     * {@link #CUSTOM}.
122
     * CUSTOM_PAPER_SIZE knows about portrait/landscape
123
     * (same as other PAPER constants)
124
     */
125
    private Size last_custom_paper_size = new Size(100.0, 100.0);
126
    
127
    public final static int PRINT = 0;
128
    public final static int CUSTOM = 6;
129
    public final static int A0 = 5;
130
    public final static int A1 = 4;
131
    public final static int A2 = 3;
132
    public final static int A3 = 2;
133
    public final static int A4 = 1;
134
    public static int DPI = 300;
135
    public static int DPISCREEN = 72;
136
    private static Point2D defaultGridGap;
137

    
138
    private PrintRequestAttributeSet attributes =
139
        new HashPrintRequestAttributeSet();
140
    /**
141
     * Media size name, used when type is {@link #PRINT}.
142
     * @see #setType(int)
143
     */
144
    private MediaSizeName m_type = MediaSizeName.ISO_A4;
145
    private OrientationRequested m_Orientation;
146
    /**
147
     * Change factor from configured map unit (m_selTypeUnit) to centimeters
148
     */
149
    private double m_TypeUnit = getConversionFactorToCm(2); // [2] = Centimeters
150
    private String m_NameUnit;
151
    /**
152
     * Horizontal space between grid lines, measured in centimeters
153
     */
154
    private Double m_gridGapCmX = null;
155
    /**
156
     * Vertical space between grid lines, measured  in centimeters
157
     */
158
    private Double m_gridGapCmY = null;
159
    /**
160
     * Horizontal space between grid lines, measured in centimeters
161
     */
162
    private double m_gridGapPxX = 0;
163
    /**
164
     * Vertical space between grid lines, measured  in pixels
165
     */
166
    private double m_gridGapPxY = 0;
167
    private boolean hasmargin;
168
    private int m_resolutionSel = NORMAL;
169
    private int m_typeSel = PRINT;
170
    /**
171
     * The selected unit (km, cm, etc) for this layout. It defaults to centimeters
172
     * 
173
     * @see {@link MapContext#getDistanceNames()}
174
     * @see {@link MapContext#getDistanceTrans2Meter()}
175
     */
176
    private int m_selTypeUnit = 2;
177
    
178
    /**
179
     * Paper size in centimeters.
180
     * This field changes when it's landscape
181
     * (it "knows" about portrait/landscape)
182
     */
183
    private Size m_sizePaper = new Size(
184
        METRIC_A4_PAPER_SIZE.getHeight(),
185
        METRIC_A4_PAPER_SIZE.getWidth());
186

    
187
    /**
188
     * Print area (top, bottom, left, right).
189
     * This array changes when it's landscape
190
     * (it "knows" about portrait/landscape)
191
     */
192
    private double[] area_tblr = {
193
        0,
194
        0,
195
        0,
196
        0 };
197
    
198
    /**
199
     * Page size in user selected units
200
     * This field changes when it's landscape
201
     * (it "knows" about portrait/landscape)
202
     */
203
    private Size m_sizeinUnits = m_sizePaper;
204

    
205
    /**
206
     * Create a new object of Attributes.
207
     * 
208
     */
209
    public Attributes() {
210
        m_NameUnit = Messages.getText("Centimetros");
211
        m_Orientation = OrientationRequested.LANDSCAPE;
212
        m_type = MediaSizeName.ISO_A4;
213
        resetSizeinUnits();
214

    
215
        hasmargin = false;
216
        attributes.add(new Copies(1));
217
        attributes.add(MediaSizeName.ISO_A4);
218
        attributes.add(Sides.ONE_SIDED);
219
        attributes.add(Fidelity.FIDELITY_FALSE);
220
        attributes.add(PrintQuality.NORMAL);
221

    
222
        setType(m_typeSel);
223

    
224
        attributes.add(new MediaPrintableArea(0, 0,
225
                (float) ((getPaperSize().getWidth() * DPISCREEN) / PULGADA),
226
                (float) ((getPaperSize().getHeight() * DPISCREEN) / PULGADA),
227
                MediaPrintableArea.MM)); // FIXME: centimeters or milimeters
228
    }
229

    
230
    /**
231
     * @return
232
     */
233
    public boolean isLandscape() {
234
        return m_Orientation.getValue() ==
235
            OrientationRequested.LANDSCAPE.getValue();
236
    }
237
    
238
    /**
239
     * @return
240
     */
241
    public boolean isPortrait() {
242
        return m_Orientation.getValue() ==
243
            OrientationRequested.PORTRAIT.getValue();
244
    }
245

    
246
    /**
247
     * Inserts the Layout's properties to print.
248
     * 
249
     * @param typeSel
250
     *            Type of sheet.
251
     * @param units
252
     *            Units.
253
     * @param isLand
254
     *            True if the sheet is horizontal.
255
     * @param margin
256
     *            True if the sheet has margin.
257
     * @param resolution
258
     *            Type of quality of resolution.
259
     * @param area
260
     *            Printing area (top, bottom, left, right). 
261
     */
262
    public void setSelectedOptions(int typeSel, int units, boolean isLand,
263
        boolean margin, int resolution, double[] _area_tplr) {
264
        setType(typeSel);
265
        setUnit(units);
266
        setIsLandScape(isLand);
267
        hasmargin = margin;
268
        setResolution(resolution);
269
        area_tblr = _area_tplr;
270
    }
271
    
272
    /**
273
     * Paper size, measured in centimeters.
274
     * This field knows about orientation.
275
     * 
276
     * Note that this size is always measured in centimeters, regardless
277
     * the selected unit for the page. However, the user should be always
278
     * presented measures in the selected unit.
279
     * 
280
     * @see #getUnit()
281
     * @see #setUnit(int)
282
     * 
283
     * @return
284
     */
285
    public Size getPaperSize() {
286
        return m_sizePaper;
287
    }
288
    
289
    /**
290
     * Sets the paper size, measured in centimeters
291
     * @param paperSize
292
     */
293
    public void setPaperSize(Size paperSize) {
294
            this.m_sizePaper = getCorrectedSize(paperSize);
295
            if (getType()==CUSTOM) {
296
                    this.last_custom_paper_size = paperSize;
297
            }
298
            resetSizeinUnits();
299
    }
300
    
301
    /**
302
     * Gets the last selected custom paper size. It will be equal to
303
     * {@link #getPaperSize()} when {@link #getType()} is
304
     * {@link #CUSTOM}.
305
     * @return
306
     */
307
    public Size getCustomPaperSize() {
308
            return this.last_custom_paper_size;
309
    }
310

    
311
    /**
312
     * Returns a PageFormat with the properties of printing.
313
     * 
314
     * @return PageFormat
315
     */
316
    public PageFormat getPageFormat() {
317
        PageFormat pf1 = new PageFormat();
318
        Paper paper = pf1.getPaper();
319

    
320
        paper.setSize(
321
            (m_sizePaper.getWidth() * DPISCREEN) / PULGADA,
322
            (m_sizePaper.getHeight() * DPISCREEN) / PULGADA);
323

    
324
        if (isLandscape()) {
325
            pf1.setOrientation(0);
326
            /*
327
             * 'area_tblr' and 'm_sizeinUnits' depend on
328
             * whether it is portrait or landscape, but
329
             * 'Paper' and 'm_sizePaper' must not know if it's
330
             * portrait/landscape, so we change order of
331
             * parameters. 'PageFormat' will do the changes again
332
             * because it is aware of portrait/landscape
333
             */
334
            paper.setImageableArea(
335
                getInPixels(area_tblr[1]),
336
                getInPixels(area_tblr[2]),
337
                getInPixels(m_sizeinUnits.getHeight() - area_tblr[0] - area_tblr[1]),
338
                getInPixels(m_sizeinUnits.getWidth()  - area_tblr[2] - area_tblr[3]));
339
        } else {
340
            // portrait
341
            pf1.setOrientation(1);
342
            paper.setImageableArea(
343
                getInPixels(area_tblr[2]),
344
                getInPixels(area_tblr[0]),
345
                getInPixels(m_sizeinUnits.getWidth() - area_tblr[2] - area_tblr[3]),
346
                getInPixels(m_sizeinUnits.getHeight()  - area_tblr[0] - area_tblr[1]));
347
        }
348

    
349
        pf1.setPaper(paper);
350
        return pf1;
351
    }
352

    
353
    /**
354
     * Changes centimeters to pixels.
355
     * 
356
     * @param d
357
     *            Centimeters.
358
     * 
359
     * @return Pixels.
360
     */
361
    private double getInPixels(double d) {
362
        return d * (DPISCREEN / PULGADA);
363
    }
364

    
365
    /**
366
     * Changes pixels to centimeters.
367
     * 
368
     * @param d
369
     *            Pixel.
370
     * 
371
     * @return Centimeters.
372
     */
373
    private double getInCM(double d) {
374
        return d / (DPISCREEN / PULGADA);
375
    }
376

    
377
    /**
378
     * Returns the printing area.
379
     * The value returned changes if orientation changes
380
     * (it "knows" about portrait/landscape)
381
     * 
382
     * @return Rectangle Area.
383
     */
384
    public Rectangle2D getArea() {
385
        Rectangle2D.Double rect = new Rectangle2D.Double();
386
        rect.setRect(
387
                area_tblr[2],
388
                area_tblr[0],
389
                m_sizePaper.getWidth() - area_tblr[2] - area_tblr[3],
390
                m_sizePaper.getHeight()  - area_tblr[0] - area_tblr[1]);
391
        return rect;
392
    }
393
    
394
    public double[] getAreaInsets() {
395
        return area_tblr;
396
    }
397

    
398
    /**
399
     * Inserts the attributes with a PageFormat.
400
     * 
401
     * @param pf
402
     *            PageFormat.
403
     */
404
    public void setPageFormat(PageFormat pf) {
405
        
406
        /*
407
         * Paper does not know about orientation
408
         */
409
        Size portrait_size = new Size(
410
            pf.getPaper().getHeight(),
411
            pf.getPaper().getWidth());
412
        setType(getTypePaper(portrait_size, true));
413

    
414
        if (pf.getOrientation() == 0) {
415
            setIsLandScape(true);
416
        } else {
417
            setIsLandScape(false);
418
        }
419
        
420

    
421
        attributes.add(m_Orientation);
422

    
423
        // tipo formato
424
        if (isLandscape()) {
425
            // landscape
426
            area_tblr[1] = getInCM(pf.getImageableX());
427
            area_tblr[2] = getInCM(pf.getImageableY());
428
            area_tblr[0] = 
429
                portrait_size.getWidth() - getInCM(pf.getImageableHeight())
430
                - area_tblr[1];
431
            area_tblr[3] =
432
                portrait_size.getHeight() - getInCM(pf.getImageableWidth())
433
                - area_tblr[2];
434
        } else {
435
            // portrait
436
            area_tblr[2] = getInCM(pf.getImageableX());
437
            area_tblr[0] = getInCM(pf.getImageableY());
438
            area_tblr[1] =
439
                portrait_size.getHeight() - getInCM(pf.getImageableHeight())
440
                - area_tblr[0];
441
            area_tblr[3] =
442
                portrait_size.getWidth() - getInCM(pf.getImageableWidth())
443
                - area_tblr[2];
444
        }
445
        
446
        /*
447
         * 'area_tblr' and 'pf' "know"
448
         * about orientation
449
         * 
450
         */
451
        attributes.add(new MediaPrintableArea(
452
            (float) (area_tblr[2] * 10),
453
            (float) (area_tblr[0] * 10),
454
            (float) (getInCM(pf.getImageableWidth()) * 10),
455
            (float) (getInCM(pf.getImageableHeight()) * 10),
456
            MediaPrintableArea.MM));
457
    }
458

    
459
    /**
460
     * Returns a PrintRequestAttributeSet with all properties to the
461
     * PrintDialog.
462
     * 
463
     * @return PrintRequestAttributesSet.
464
     */
465
    public PrintRequestAttributeSet toPrintRequestAttributeSet() {
466
        HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
467

    
468
        setType(m_typeSel);
469

    
470
        resul.add(m_type);
471

    
472
        // units, no hace falta a?adirlo a attributes
473
        resul.add(m_Orientation);
474
        // setArea(area_tblr);
475
        
476
        Rectangle2D rect = this.getArea();
477
        resul.add(new MediaPrintableArea(
478
            (float) (rect.getX() *10),
479
            (float) (rect.getY() *10),
480
            (float) (rect.getWidth() *10),
481
            (float) (rect.getHeight() *10),
482
            MediaPrintableArea.MM));
483

    
484
        resul.add(toPrintQuality(m_resolutionSel));
485
        return resul;
486
    }
487
    
488
    public DocAttributeSet toDocAttributes() {
489
        HashDocAttributeSet resul = new HashDocAttributeSet();
490

    
491
        setType(m_typeSel);
492

    
493
        resul.add(m_type);
494

    
495
        // units, no hace falta a?adirlo a attributes
496
        resul.add(m_Orientation);
497
        // setArea(area_tblr);
498
        
499
        Rectangle2D rect = this.getArea();
500
        resul.add(new MediaPrintableArea(
501
            (float) (rect.getX() *10),
502
            (float) (rect.getY() *10),
503
            (float) (rect.getWidth() *10),
504
            (float) (rect.getHeight() *10),
505
            MediaPrintableArea.MM));
506

    
507
        resul.add(toPrintQuality(m_resolutionSel));
508
        return resul;
509
    }
510

    
511
    public PrintAttributes toPrintAttributes() {
512
        PrintAttributes resul =
513
            CompatLocator.getGraphicsUtils().createPrintAttributes();
514

    
515
        if (m_resolutionSel == DRAFT) {
516
            resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_DRAFT);
517

    
518
        } else
519
            if (m_resolutionSel == NORMAL) {
520
                resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_NORMAL);
521

    
522
            } else
523
                if (m_resolutionSel == HIGH) {
524
                    resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_HIGH);
525
                } else {
526
                    throw new UnsupportedOperationException(
527
                        "Unsupported Print Quality: " + m_resolutionSel);
528
                }
529
        setType(m_typeSel);
530

    
531
        return resul;
532
    }
533

    
534
    /**
535
     * Returns a PrintRequestAttributeSet with all properties to the
536
     * PrintDialog.
537
     * 
538
     * @return PrintRequestAttributesSet.
539
     */
540
    public PrintRequestAttributeSet toPrintAttributeSet() {
541
        HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
542

    
543
        setType(m_typeSel);
544

    
545
        resul.add(m_type);
546

    
547
        resul.add(m_Orientation);
548
        
549
        Rectangle2D rect = this.getArea();
550
        resul.add(new MediaPrintableArea(
551
            (float) (rect.getX() * DPISCREEN / PULGADA),
552
            (float) (rect.getY() * DPISCREEN / PULGADA),
553
            (float) (rect.getWidth() * DPISCREEN / PULGADA),
554
            (float) (rect.getHeight() * DPISCREEN / PULGADA),
555
            MediaPrintableArea.MM));
556

    
557
        resul.add(toPrintQuality(m_resolutionSel));
558
        return resul;
559
    }
560
    /**
561
     * Returns true if the margin should be used.
562
     * 
563
     * @return True if margin should be used.
564
     */
565
    public boolean isMargin() {
566
        return hasmargin;
567
    }
568

    
569
    /**
570
     * Returns the resolution of properties to print.
571
     * 
572
     * @return Resolution.
573
     */
574
    public int getResolution() {
575
        return m_resolutionSel;
576
    }
577

    
578
    /**
579
     * Inserts the resolution to apply at printing.
580
     * 
581
     * @param i
582
     *            Type of resolution.
583
     * 
584
     *            The type of resolution can be:
585
     * 
586
     *            DRAFT: 27 dpi.
587
     *            NORMAL: 300 dpi.
588
     *            HIGH: 600 dpi.
589
     */
590
    public void setResolution(int i) {
591
        m_resolutionSel = i;
592
        switch (i) {
593
        case (HIGH):
594
            DPI = 600;
595
            break;
596

    
597
        case (NORMAL):
598
            DPI = 300;
599
            break;
600

    
601
        case (DRAFT):
602
            DPI = 72;
603
            break;
604
        }
605
        attributes.add(toPrintQuality(i));
606
    }
607
    
608
    /**
609
     * Converts print quality from Attributes format
610
     * ({@link #HIGH}, {@link #NORMAL}, {@link #DRAFT})
611
     * to {@link PrintQuality} format.
612
     * 
613
     * @param quality
614
     * @return
615
     */
616
    public static PrintQuality toPrintQuality(int quality) {
617
        switch (quality) {
618
        case (HIGH):
619
                return PrintQuality.HIGH;
620
        case (NORMAL):
621
            return PrintQuality.NORMAL;
622
        case (DRAFT):
623
                return PrintQuality.DRAFT;
624
        default:
625
                return PrintQuality.NORMAL;
626
        }
627
    }
628

    
629
    /**
630
     * Returns attributes of PrintDialog.
631
     * 
632
     * @return Attributes of printing.
633
     */
634
    public PrintRequestAttributeSet getAttributes() {
635
        return attributes;
636
    }
637

    
638
    /**
639
     * Returns True if the position of sheet is horizontal.
640
     * 
641
     * @return True if position is horizontal.
642
     */
643

    
644
    /**
645
     * Returns the format of sheet.
646
     * 
647
     * @return Format of sheet.
648
     */
649
    public int getType() {
650
        return m_typeSel;
651
    }
652

    
653
    /**
654
     * Inserts the number of units to define the coords X grid.
655
     * 
656
     * @param d
657
     *            number of units to define the grid.
658
     */
659
    public void setHGridGrapCm(double d) {
660
        m_gridGapCmX = new Double(d);
661
    }
662

    
663
    /**
664
     * Gets the grid gap for the x axis, in centimeters. The grip gap is the
665
     * distance from one point to the next point in the snapping grid.
666
     * 
667
     * @return Default grid gap for the x axis, measured in centimeters
668
     */
669
    public double getHGridGapCm() {
670
        if (m_gridGapCmX == null) {
671
            m_gridGapCmX = new Double(getDefaultGridGap().getX());
672
        }
673
        return m_gridGapCmX.doubleValue();
674
    }
675

    
676
    /**
677
     * Gets the grid gap for the y axis, in centimeters. The grip gap is the
678
     * distance from one point to the next point in the snapping grid.
679
     * 
680
     * @return Default grid gap for the y axis, measured in centimeters
681
     */
682
    public double getVGridGapCm() {
683
        if (m_gridGapCmY == null) {
684
            m_gridGapCmY = new Double(getDefaultGridGap().getY());
685
        }
686
        return m_gridGapCmY.doubleValue();
687
    }
688

    
689
    /**
690
     * Inserts the number of units to define the coords X grid.
691
     * 
692
     * @param d
693
     *            number of units to define the grid.
694
     */
695
    public void setVGridGrapCm(double d) {
696
        m_gridGapCmY = new Double(d);
697
    }
698

    
699
    /**
700
     * Inserts true if the sheet is horizontal or false if is vertical.
701
     * 
702
     * @param b
703
     *            True if sheet is horizontal.
704
     */
705
    public void setIsLandScape(boolean b) {
706
        if (b) {
707
            m_Orientation = OrientationRequested.LANDSCAPE;
708
        } else {
709
            m_Orientation = OrientationRequested.PORTRAIT;
710
        }
711
        setType(m_typeSel);
712
    }
713

    
714
    /**
715
     * Sets the type of sheet to show. See
716
     * {@link #PRINT}, {@link #CUSTOM}, {@link #A4}, {@link #A3}, {@link #A2},
717
     * {@link #A1}, {@link #A0}.
718
     * 
719
     * @param t
720
     *            Type of sheet.
721
     */
722
    public void setType(int t) {
723
        m_typeSel = t;
724
        switch (t) {
725
        case (PRINT):
726
            m_type = ((MediaSizeName) attributes.get(Media.class));
727
                // FIXME: Shouldn't we adapt the actual size to the printer???? 
728
                setPaperSize(METRIC_A4_PAPER_SIZE);
729
            break;
730

    
731
        case (A4):
732
            m_type = MediaSizeName.ISO_A4;
733
                setPaperSize(METRIC_A4_PAPER_SIZE);
734
            break;
735

    
736
        case (A3):
737
            m_type = MediaSizeName.ISO_A3;
738
                setPaperSize(METRIC_A3_PAPER_SIZE);
739
            break;
740

    
741
        case (A2):
742
            m_type = MediaSizeName.ISO_A2;
743
                setPaperSize(METRIC_A2_PAPER_SIZE);
744
            break;
745

    
746
        case (A1):
747
            m_type = MediaSizeName.ISO_A1;
748
                setPaperSize(METRIC_A1_PAPER_SIZE);
749
            break;
750

    
751
        case (A0):
752
            m_type = MediaSizeName.ISO_A0;
753
                setPaperSize(METRIC_A0_PAPER_SIZE);
754

    
755
            break;
756

    
757
        case (CUSTOM):
758
            m_type = MediaSizeName.PERSONAL_ENVELOPE;
759
                setPaperSize(last_custom_paper_size);
760
            break;
761
        }
762
        attributes.add(m_type);
763
    }
764

    
765
    /**
766
     * Sets the paper size, measured on the selected units
767
     * @see #getUnit()
768
     */
769
    public void setPaperSizeInUnits(Size sizeInUnits) {
770
            setPaperSize(new Size(sizeInUnits.getHeight() * m_TypeUnit,
771
                            sizeInUnits.getWidth() * m_TypeUnit));
772
    }
773
    
774
    /**
775
     * Returns the size of sheet.
776
     * 
777
     * @param isLand
778
     *            True if is horizontal and false if is vertical.
779
     * @param type
780
     *            Type of sheet.
781
     * 
782
     * @return Size of sheet with these properties.
783
     */
784
    public Size getPaperSizeinUnits(boolean isLand, int type) {
785
        Size size = null;
786

    
787
        switch (type) {
788
        case (PRINT):
789

    
790
            if (isLand) {
791
                size =
792
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
793
                        METRIC_A4_PAPER_SIZE.getHeight());
794
            } else {
795
                size = METRIC_A4_PAPER_SIZE;
796
            }
797

    
798
            break;
799

    
800
        case (A4):
801

    
802
            if (isLand) {
803
                size =
804
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
805
                        METRIC_A4_PAPER_SIZE.getHeight());
806
            } else {
807
                size = METRIC_A4_PAPER_SIZE;
808
            }
809

    
810
            break;
811

    
812
        case (A3):
813

    
814
            if (isLand) {
815
                size =
816
                    new Size(METRIC_A3_PAPER_SIZE.getWidth(),
817
                        METRIC_A3_PAPER_SIZE.getHeight());
818
            } else {
819
                size = METRIC_A3_PAPER_SIZE;
820
            }
821

    
822
            break;
823

    
824
        case (A2):
825

    
826
            if (isLand) {
827
                size =
828
                    new Size(METRIC_A2_PAPER_SIZE.getWidth(),
829
                        METRIC_A2_PAPER_SIZE.getHeight());
830
            } else {
831
                size = METRIC_A2_PAPER_SIZE;
832
            }
833

    
834
            break;
835

    
836
        case (A1):
837

    
838
            if (isLand) {
839
                size =
840
                    new Size(METRIC_A1_PAPER_SIZE.getWidth(),
841
                        METRIC_A1_PAPER_SIZE.getHeight());
842
            } else {
843
                size = METRIC_A1_PAPER_SIZE;
844
            }
845

    
846
            break;
847

    
848
        case (A0):
849

    
850
            if (isLand) {
851
                size =
852
                    new Size(METRIC_A0_PAPER_SIZE.getWidth(),
853
                        METRIC_A0_PAPER_SIZE.getHeight());
854
            } else {
855
                size = METRIC_A0_PAPER_SIZE;
856
            }
857

    
858
            break;
859

    
860
        case (CUSTOM):
861
            if (isLandscape()) {
862
                size =
863
                    new Size(
864
                        last_custom_paper_size.getWidth() * m_TypeUnit,
865
                        last_custom_paper_size.getHeight() * m_TypeUnit);
866
            } else {
867
                size =
868
                    new Size(
869
                        last_custom_paper_size.getHeight() * m_TypeUnit,
870
                        last_custom_paper_size.getWidth() * m_TypeUnit);
871
            }
872
            break;
873
        }
874

    
875
        Size resp = new Size(
876
            size.getHeight() / m_TypeUnit, 
877
            size.getWidth() / m_TypeUnit);
878

    
879
        return resp;
880
    }
881
    
882
    
883
    
884
    /**
885
     * Returns the type of unit in use (cm, mm, km, etc), codified as
886
     * a position in the array returned by {@link MapContext#getDistanceNames()}
887
     *
888
     * @see #setUnit(int)
889
     * @see MapContext#getDistanceNames() 
890
     * @see MapContext#getDistanceTrans2Meter()
891
     * @see MapContext#getDistanceAbbr()
892
     * @see MapContext#getDistancePosition(String)
893
     * @see MapContext#getDistancePosition(String)
894

895
     * 
896
     * @return Type of units.
897
     */
898
    public int getUnit() {
899
        return m_selTypeUnit;
900
    }
901

    
902
    /**
903
     * @deprecated Use {@link #getUnit()} instead
904
     */
905
    @Deprecated
906
    public int getSelTypeUnit() {
907
            return getUnit();
908
    }
909
    /**
910
     * Returns the name of the selected units.
911
     * @see #getUnit()
912
     * 
913
     * @return String Name of units.
914
     */
915
    public String getNameUnit() {
916
        return m_NameUnit;
917
    }
918

    
919
    /**
920
     * Sets the type of unit that is used in the Layout, defined as an
921
     * integer indicating a position in the array
922
     * {@link MapContext#getDistanceNames()}. By default is
923
     * centimeters.
924
     * 
925
     * @param sel
926
     *            Type of unit.
927
     * @see #getUnit()
928
     */
929
    public void setUnit(int sel) {
930
        Logger logger = LoggerFactory
931
                .getLogger(LayoutExtension.class);
932
        logger.debug("setUnit: "+sel);
933
        m_selTypeUnit = sel;
934
        m_TypeUnit = getConversionFactorToCm(sel);
935
        resetSizeinUnits();
936
        m_NameUnit =
937
            PluginServices.getText(this, MapContext.getDistanceNames()[sel]);
938
    }
939

    
940
    /**
941
     * Actualize to date m_sizeinUnits with the height and width of the sheet
942
     * in the units of media selected.
943
     * 
944
     * @param b
945
     *            True if is horizontal.
946
     */
947
    private void resetSizeinUnits() {
948
            Size correctedSize = getCorrectedSize(m_sizePaper);
949
            m_sizeinUnits =
950
                new Size(correctedSize.getHeight() / m_TypeUnit,
951
                                correctedSize.getWidth() / m_TypeUnit);
952
    }
953

    
954
    /**
955
     * Returns a double in the unit of measure selected from the double in
956
     * centimeters.
957
     * 
958
     * @param d
959
     *            Distance in centimeters.
960
     * 
961
     * @return Distance in the unit measure selected.
962
     */
963
    public double toUnits(double d) {
964
        NumberFormat nf = NumberFormat.getInstance();
965
        nf.setMaximumFractionDigits(2);
966

    
967
        String s = String.valueOf(nf.format(d));
968
        s = s.replace(',', '.');
969

    
970
        return (Double.valueOf(s).doubleValue() / m_TypeUnit);
971
    }
972

    
973
    /**
974
     * Returns a double in centimeters from the the unit of measure selected.
975
     * 
976
     * @param d
977
     *            Distance in the unit measure selected.
978
     * 
979
     * @return Distance in centimeters.
980
     */
981
    public double fromUnits(double d) {
982
        return (d * m_TypeUnit);
983
    }
984

    
985
    /**
986
     * Returns one centimeter in pixels on screen in this moment.
987
     * 
988
     * @param rect
989
     *            Rectangle of sheet.
990
     * 
991
     * @return Pixels from centimeter.
992
     */
993
    public double getPixXCm(Rectangle2D rect) {
994
        double value = m_sizePaper.getWidth();
995
        double cm = getConversionFactorToCm(2);
996
        double unidades = 0;
997
        unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / cm)));
998
        return unidades;
999
    }
1000

    
1001
    /**
1002
     * Calculates the grid gap for the x axis, in pixels,
1003
     * based on the paper width (in cm) and the width
1004
     * of the rectangle of sheet in screen, measured in
1005
     * pixels.
1006
     *  
1007
     * The grip gap is the distance from one
1008
     * point to the next point in the snapping grid.
1009
     * 
1010
     * @param rect
1011
     *            Rectangle of sheet, measured in pixels
1012
     */
1013
    public void calculateGridGapX(Rectangle2D rect) {
1014
        double width = m_sizePaper.getWidth();
1015
        double numberOfGaps = width / getHGridGapCm();
1016
        double unidades =
1017
            (rect.getMaxX() - rect.getMinX()) / numberOfGaps;
1018
        m_gridGapPxX = unidades;
1019
    }
1020

    
1021
    /**
1022
     * Calculates the grid gap for the y axis, in pixels,
1023
     * based on the paper height (in cm) and the height
1024
     * of the rectangle of sheet in screen, measured in
1025
     * pixels.
1026
     *  
1027
     * The grip gap is the distance from one
1028
     * point to the next point in the snapping grid.
1029
     * 
1030
     * @param rect
1031
     *            Rectangle of sheet, measured in pixels
1032
     */
1033
    public void calculateGridGapY(Rectangle2D rect) {
1034
        double height = m_sizePaper.getHeight();
1035
        double numberOfGaps = height / getVGridGapCm();
1036
        double unidades =
1037
            (rect.getMaxY() - rect.getMinY()) / numberOfGaps;
1038
        m_gridGapPxY = unidades;
1039
    }
1040

    
1041
    
1042
    private double getPaperSizeRatio() {
1043
        double resp = 1;
1044
        if (isLandscape()) {
1045
            resp = m_sizePaper.getWidth() / m_sizePaper.getHeight();
1046
        } else {
1047
            resp = m_sizePaper.getHeight() / m_sizePaper.getWidth();
1048
        }
1049
        return resp;
1050
    }
1051
    /**
1052
     * Obtains the rectangle that represents the sheet with the characteristics
1053
     * that contains attributes
1054
     * 
1055
     * @param rect
1056
     *            rectangle.
1057
     * 
1058
     */
1059
    public void obtainRect(boolean isPrint, Rectangle2D rect, int w, int h) {
1060
        double value1 = 0;
1061
        double value2 = 0;
1062

    
1063
        if (!isPrint) {
1064
            if (isLandscape()) {
1065
                rect = getRectangleLandscape(rect, w, h);
1066
            } else {
1067
                rect = getRectanglePortrait(rect, w, h);
1068
            }
1069
        } else {
1070
            value1 = m_sizePaper.getWidth();
1071
            value2 = m_sizePaper.getHeight();
1072
            rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
1073
                ((value2 / PULGADA) * DPI));
1074
        }
1075

    
1076
        calculateGridGapX(rect);
1077
        calculateGridGapY(rect);
1078
    }
1079

    
1080
    /**
1081
     * It obtains the rect that is adjusted to the size of the window,
1082
     * to see the full extent of horizontal layout.
1083
     * 
1084
     * @param rect
1085
     *            Rectangle sheet.
1086
     * @param w
1087
     *            Width of Layout.
1088
     * @param h
1089
     *            Height of Layout.
1090
     * 
1091
     * @return Rectangle modified.
1092
     */
1093
    public Rectangle2D.Double getRectangleLandscape(Rectangle2D rect, int w,
1094
        int h) {
1095
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
1096
        int x0 = (int) rect.getMinX();
1097
        int y0 = (int) rect.getMinY();
1098

    
1099
        // Don't use the whole component space to draw the sheet,
1100
        // add some margin to make the sheet evident.
1101
        // 2 * x0: margin left and margin right
1102
        // 2 * y0: margin top and margin bottom
1103
        int newHeight = (h - (2 * y0));
1104
        int newWidth = (int) (newHeight * getPaperSizeRatio());
1105
        if (((int) (((h) - (2 * y0)) * getPaperSizeRatio())) > ((w) - (2 * x0))) {
1106
                newWidth = ((w) - (2 * x0));
1107
                newHeight = (int) (newWidth / getPaperSizeRatio());
1108
        }
1109
        rectaux.setRect(x0, y0, newWidth, newHeight);
1110
        return rectaux;
1111
    }
1112

    
1113
    /**
1114
     * Returns the size of sheet in the units of measure selected.
1115
     * 
1116
     * @return Size of sheet.
1117
     */
1118
    public Size getPaperSizeInUnits() {
1119
        return m_sizeinUnits;
1120
    }
1121
    
1122
    /**
1123
     * @deprecated Use {@link #getPaperSizeInUnits()} instead
1124
     * 
1125
     */
1126
    @Deprecated
1127
    public Size getSizeInUnits() {
1128
            return getPaperSizeInUnits();
1129
    }
1130

    
1131
    /**
1132
     * It obtains the rect that is adjusted to the size of the window,
1133
     * to see the full extent of vertical layout.
1134
     * 
1135
     * @param rect
1136
     *            Rectangle sheet.
1137
     * @param w
1138
     *            Width of Layout.
1139
     * @param h
1140
     *            Height of Layout.
1141
     * 
1142
     * @return Rectangle modified.
1143
     */
1144
    public Rectangle2D.Double getRectanglePortrait(Rectangle2D rect, int w,
1145
        int h) {
1146
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
1147
        int x0 = (int) rect.getMinX();
1148
        int y0 = (int) rect.getMinY();
1149
                
1150
        // Don't use the whole component space to draw the sheet,
1151
        // add some margin to make the sheet evident.
1152
        // 2 * x0: margin left and margin right
1153
        // 2 * y0: margin top and margin bottom
1154
        int newWidth = (w - (2 * x0));
1155
        int newHeight = (int) (newWidth * getPaperSizeRatio());
1156
        if (((int) (((w) - (2 * x0)) * getPaperSizeRatio())) > ((h) - (2 * y0))) {
1157
                newHeight = (h - (2 * y0));
1158
                newWidth = (int) (newHeight / getPaperSizeRatio());
1159
        }
1160
        rectaux.setRect(x0, y0, newWidth, newHeight);
1161
        return rectaux;
1162
    }
1163
    
1164
    /**
1165
     * Adapts the provided size to the configured orientation
1166
     * @param size
1167
     * @return
1168
     */
1169
    private Size getCorrectedSize(Size size) {
1170
            Size result;
1171
            if ( (isLandscape() && (size.getWidth()<size.getHeight()))
1172
                            || (isPortrait() && (size.getHeight()<size.getWidth()))) {
1173
                    // swap sizes to ensure they match the configured orientation
1174
                    result = new Size(size.getWidth(), size.getHeight());                            
1175
            }
1176
            else {
1177
                    result = size;                
1178
            }
1179
            return result;
1180
    }
1181
    
1182
    /**
1183
     * Adapts the provided size to the configured orientation
1184
     * @param size
1185
     * @return
1186
     */
1187
    private Size getCorrectedSize(double height, double width) {
1188
            Size result;
1189
            if ( (isLandscape() && (width<height))
1190
                            || (isPortrait() && (height<width))) {
1191
                    // swap sizes to ensure they match the configured orientation
1192
                    result = new Size(width, height);                            
1193
            }
1194
            else {
1195
                    result = new Size(height, width);                
1196
            }
1197
            return result;
1198
    }
1199

    
1200
    /**
1201
     * It obtains the type of format from the size.
1202
     * 
1203
     * @param size
1204
     *            Size of sheet.
1205
     * 
1206
     * @return Type of sheet.
1207
     */
1208
    private int getTypePaper(Size size, boolean is_portrait) {
1209

    
1210
        double tol = 1d;
1211
        double width, height;
1212
        // the METRIC constants are defined as portrait sizes
1213
        // so we must compare with the right paper size
1214
        
1215
        if (size.getHeight()<size.getWidth()) {
1216
                height = size.getWidth();
1217
                width = size.getHeight();
1218
        }
1219
        else {
1220
                width = size.getWidth();
1221
                height = size.getHeight();
1222
        }
1223
        double conversionFactor = PULGADA / DPISCREEN;
1224
        
1225
        if (((width * conversionFactor)  <
1226
                        (tol + METRIC_A4_PAPER_SIZE.getWidth()))
1227
                        && ((height * conversionFactor) <
1228
                                        (tol + METRIC_A4_PAPER_SIZE.getHeight()))) {
1229
                return A4;
1230
        }
1231
        else if (((width * conversionFactor) <
1232
                        (tol + METRIC_A3_PAPER_SIZE.getWidth()))
1233
                        && ((height * conversionFactor) <
1234
                                        (tol + METRIC_A3_PAPER_SIZE.getHeight()))) {
1235
                return A3;
1236
        }
1237
        else if (((width * conversionFactor) <
1238
                        (tol + METRIC_A2_PAPER_SIZE.getWidth()))
1239
                        && ((height * conversionFactor) <
1240
                                        (tol + METRIC_A2_PAPER_SIZE.getHeight()))) {
1241
                return A2;
1242
        }
1243
        else if (((width * conversionFactor) < 
1244
                        (tol + METRIC_A1_PAPER_SIZE.getWidth()))
1245
                        && ((height * conversionFactor) < 
1246
                                        (tol + METRIC_A1_PAPER_SIZE.getHeight()))) {
1247
                return A1;
1248
        } else if (((width * conversionFactor) <
1249
                                        (tol + METRIC_A0_PAPER_SIZE.getWidth()))
1250
                                && ((height * conversionFactor) < 
1251
                                                (tol + METRIC_A0_PAPER_SIZE.getHeight()))) {
1252
                        return A0;
1253
                }
1254

    
1255
        return A4;
1256
    }
1257

    
1258
    /**
1259
     * Sets the default grid gap. The grip gap is the distance from one
1260
     * point to the next point in the snapping grid.
1261
     * 
1262
     * @param hGap
1263
     *            Horizontal distance.
1264
     * @param vGap
1265
     *            Vertical distance.
1266
     * @param hUnitAbbr The abbreviation of the measure in which hGap has
1267
     * been provided, codified as one of the abbreviations specified in
1268
     * {@link MapContext#getDistanceAbbr()}
1269
     * 
1270
     * @param vUnitAbbr The abbreviation of the measure in which vGap has
1271
     * been provided, codified as one of the abbreviations specified in
1272
     * {@link MapContext#getDistanceAbbr()}
1273
     */
1274
    public static void setDefaultGridGap(double hGap, double vGap, String hUnitAbbr, String vUnitAbbr) {
1275
            int hUnit = getDistanceAbbrPosition(hUnitAbbr);
1276
        int vUnit = getDistanceAbbrPosition(vUnitAbbr);
1277
        double hGapCm = hGap*getConversionFactorToCm(hUnit);
1278
        double vGapCm = vGap*getConversionFactorToCm(vUnit);
1279
        defaultGridGap = new Point2D.Double(hGapCm, vGapCm);
1280
    }
1281

    
1282
    /**
1283
     * Gets the default grid gap, in centimeters. The grip gap is the distance
1284
     * from one point to the next point in the snapping grid.
1285
     * 
1286
     * @return Default grid gap, measured in centimeters
1287
     */
1288
    public static Point2D getDefaultGridGap() {
1289
        if (defaultGridGap == null) {
1290
                PluginServices pluginServices = PluginsLocator.getManager().getPlugin(LayoutExtension.class);
1291
            XMLEntity xml = null;
1292
            if (pluginServices != null) {
1293
                xml = pluginServices.getPersistentXML();
1294
            }
1295
            double hGap =
1296
                ((xml != null) && (xml
1297
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_KEY_NAME))) ? xml
1298
                    .getDoubleProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_KEY_NAME)
1299
                    : PreferenceKeys.FACTORY_DEFAULT_HORIZONTAL_GAP;
1300
            double vGap =
1301
                ((xml != null) && (xml
1302
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_KEY_NAME))) ? xml
1303
                    .getDoubleProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_KEY_NAME)
1304
                    : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP;
1305
            String hUnitName = ((xml != null) && (xml
1306
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_UNITS_KEY_NAME))) ? xml
1307
                    .getStringProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_UNITS_KEY_NAME)
1308
                    : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP_UNIT; // cm by default
1309
            String vUnitName = ((xml != null) && (xml
1310
                            .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_UNITS_KEY_NAME))) ? xml
1311
                                            .getStringProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_UNITS_KEY_NAME)
1312
                                            : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP_UNIT; // cm by default
1313
            int hUnit = getDistanceAbbrPosition(hUnitName);
1314
            int vUnit = getDistanceAbbrPosition(vUnitName);
1315
            double hGapCm = hGap*getConversionFactorToCm(hUnit);
1316
            double vGapCm = vGap*getConversionFactorToCm(vUnit);
1317
            defaultGridGap = new Point2D.Double(hGapCm, vGapCm);
1318
        }
1319
        return defaultGridGap;
1320
    }
1321
    
1322
    public static int getDistanceAbbrPosition(String unitAbbr) {
1323
            ArrayList list = MapContext.DISTANCEABBR;
1324
                for (int i = 0; i < list.size(); i++) {
1325
                        if (list.get(i).equals(unitAbbr)){
1326
                                return i;
1327
                        }
1328
                }
1329
                return 0;
1330
            
1331
    }
1332

    
1333
    /**
1334
     * Gets the grid gap for the y axis, in pixels.
1335
     * The grip gap is the distance from one
1336
     * point to the next point in the snapping grid.
1337
     * 
1338
     * @return Number of pixels of each grid gap for the y axis
1339
     */
1340
    public double getVGridGapPx() {
1341
        return m_gridGapPxY;
1342
    }
1343

    
1344
    /**
1345
     * Gets the grid gap for the x axis, in pixels.
1346
     * The grip gap is the distance from one
1347
     * point to the next point in the snapping grid.
1348
     * 
1349
     * @return Number of pixels of each grid gap for the x axis
1350
     */
1351
    public double getHGridGapPx() {
1352
        return m_gridGapPxX;
1353
    }
1354

    
1355
    public static void registerPersistent() {
1356
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1357
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
1358
            DynStruct definition =
1359
                manager.addDefinition(Attributes.class,
1360
                    PERSISTENCE_DEFINITION_NAME,
1361
                    "Attributes persistence definition", null, null);
1362

    
1363
            definition.addDynFieldInt(DPI_FIELD).setMandatory(true);
1364
            definition.addDynFieldInt(DPISCREEN_FIELD).setMandatory(true);
1365
            definition.addDynFieldDouble(TYPEUNIT_FIELD).setMandatory(true);
1366
            definition.addDynFieldDouble(NUMX_FIELD).setMandatory(false);
1367
            definition.addDynFieldDouble(NUMY_FIELD).setMandatory(false);
1368
            definition.addDynFieldDouble(UNITX_FIELD).setMandatory(true);
1369
            definition.addDynFieldDouble(UNITY_FIELD).setMandatory(true);
1370
            definition.addDynFieldBoolean(HASMARGIN_FIELD).setMandatory(true);
1371
            definition.addDynFieldInt(RESOLUTIONSEL_FIELD).setMandatory(true);
1372
            definition.addDynFieldInt(TYPESEL_FIELD).setMandatory(true);
1373
            definition.addDynFieldInt(SELTYPEUNIT_FIELD).setMandatory(true);
1374
            definition.addDynFieldObject(SIZEPAPER_FIELD).setMandatory(true)
1375
                .setClassOfValue(Size.class);
1376
            definition.addDynFieldArray(AREA_FIELD)
1377
                .setClassOfItems(String.class).setMandatory(true);
1378
            definition.addDynFieldObject(SIZEINUNITS_FIELD).setMandatory(true)
1379
                .setClassOfValue(Size.class);
1380
            definition.addDynFieldBoolean(ISLANDSEL_FIELD).setMandatory(true);
1381
        }
1382

    
1383
        Size.registerPersistent();
1384
    }
1385

    
1386
    public void loadFromState(PersistentState state)
1387
        throws PersistenceException {
1388
        DPI = state.getInt(DPI_FIELD);
1389
        DPISCREEN = state.getInt(DPISCREEN_FIELD);
1390
        m_TypeUnit = state.getDouble(TYPEUNIT_FIELD);
1391
        m_gridGapCmX = (Double) state.get(NUMX_FIELD);
1392
        m_gridGapCmY = (Double) state.get(NUMY_FIELD);
1393
        m_gridGapPxX = (Double) state.get(UNITX_FIELD);
1394
        m_gridGapPxY = (Double) state.get(UNITY_FIELD);
1395
        hasmargin = state.getBoolean(HASMARGIN_FIELD);
1396
        m_resolutionSel = state.getInt(RESOLUTIONSEL_FIELD);
1397
        m_typeSel = state.getInt(TYPESEL_FIELD);
1398
        m_selTypeUnit = state.getInt(SELTYPEUNIT_FIELD);
1399
        m_sizePaper = (Size) state.get(SIZEPAPER_FIELD);
1400
        if(m_typeSel == CUSTOM){
1401
            last_custom_paper_size=m_sizePaper;
1402
        }
1403
        area_tblr = state.getDoubleArray(AREA_FIELD);
1404
        m_sizeinUnits = (Size) state.get(SIZEINUNITS_FIELD);
1405
        boolean lands = state.getBoolean(ISLANDSEL_FIELD);
1406
        this.setIsLandScape(lands);
1407
    }
1408

    
1409
    public void saveToState(PersistentState state) throws PersistenceException {
1410
        state.set(DPI_FIELD, DPI);
1411
        state.set(DPISCREEN_FIELD, DPISCREEN);
1412
        state.set(TYPEUNIT_FIELD, m_TypeUnit);
1413
        state.set(NUMX_FIELD, m_gridGapCmX);
1414
        state.set(NUMY_FIELD, m_gridGapCmY);
1415
        state.set(UNITX_FIELD, m_gridGapPxX);
1416
        state.set(UNITY_FIELD, m_gridGapPxY);
1417
        state.set(HASMARGIN_FIELD, hasmargin);
1418
        state.set(RESOLUTIONSEL_FIELD, m_resolutionSel);
1419
        state.set(TYPESEL_FIELD, m_typeSel);
1420
        state.set(SELTYPEUNIT_FIELD, m_selTypeUnit);
1421
        state.set(SIZEPAPER_FIELD, m_sizePaper);
1422
        state.set(AREA_FIELD, area_tblr);
1423
        state.set(SIZEINUNITS_FIELD, m_sizeinUnits);
1424
        state.set(ISLANDSEL_FIELD, this.isLandscape());
1425
    }
1426
    
1427
    /**
1428
     * Gets the conversion factor from the specified unit to centimeters.
1429
     * If you have some distance measured in the specified unit, you can
1430
     * multiply it by this factor in order to convert the distance to cm. 
1431
     * 
1432
     * @param unit The source unit, codified as a position in the array
1433
     *             {@link MapContext#getDistanceNames()}
1434
     * @return A conversion factor from the specified unit to cm
1435
     */
1436
    public static double getConversionFactorToCm(int unit) {
1437
            return 100.0d*MapContext.getDistanceTrans2Meter()[unit];
1438
    }
1439
    
1440
    @Override
1441
    public Object clone() {
1442
            Attributes cloned = null;
1443
            try {
1444
                    cloned = (Attributes) super.clone();
1445

    
1446
                    cloned.setSelectedOptions(this.getType(), this.getUnit(), this.isLandscape(), this.isMargin(), this.getResolution(), this.getAreaInsets());
1447
                    cloned.setPaperSize(this.getPaperSize());
1448
                    cloned.setVGridGrapCm(this.getVGridGapCm());
1449
                    cloned.setHGridGrapCm(this.getHGridGapCm());
1450
                    
1451
            } catch (CloneNotSupportedException e) {}
1452
            return cloned;
1453
    }
1454

    
1455
}