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 @ 357

History | View | Annotate | Download (45.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.app.project.documents.layout;
23

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

    
31
import javax.print.attribute.HashPrintRequestAttributeSet;
32
import javax.print.attribute.PrintRequestAttributeSet;
33
import javax.print.attribute.standard.Copies;
34
import javax.print.attribute.standard.Fidelity;
35
import javax.print.attribute.standard.Media;
36
import javax.print.attribute.standard.MediaPrintableArea;
37
import javax.print.attribute.standard.MediaSizeName;
38
import javax.print.attribute.standard.OrientationRequested;
39
import javax.print.attribute.standard.PrintQuality;
40
import javax.print.attribute.standard.Sides;
41

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

    
60
/**
61
 * Attributes of Layout.
62
 * 
63
 * @author Vicente Caballero Navarro
64
 * @author jldominguez
65
 */
66
public class Attributes implements Persistent {
67

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

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

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

    
130
    private PrintRequestAttributeSet attributes =
131
        new HashPrintRequestAttributeSet();
132
    private MediaSizeName m_type = MediaSizeName.ISO_A4;
133
    private OrientationRequested m_Orientation;
134
    /**
135
     * Change factor from configured map unit (m_selTypeUnit) to centimeters
136
     */
137
    private double m_TypeUnit = getConversionFactorToCm(2); // [2] = Centimeters
138
    private String m_NameUnit;
139
    /**
140
     * Horizontal space between grid lines, measured in centimeters
141
     */
142
    private Double m_gridGapCmX = null;
143
    /**
144
     * Vertical space between grid lines, measured  in centimeters
145
     */
146
    private Double m_gridGapCmY = null;
147
    /**
148
     * Horizontal space between grid lines, measured in centimeters
149
     */
150
    private double m_gridGapPxX = 0;
151
    /**
152
     * Vertical space between grid lines, measured  in pixels
153
     */
154
    private double m_gridGapPxY = 0;
155
    private boolean hasmargin;
156
    private int m_resolutionSel = NORMAL;
157
    private int m_typeSel = PRINT;
158
    /**
159
     * The selected unit (km, cm, etc) for this layout. It defaults to centimeters
160
     * 
161
     * @see {@link MapContext#getDistanceNames()}
162
     * @see {@link MapContext#getDistanceTrans2Meter()}
163
     */
164
    private int m_selTypeUnit = 2;
165
    
166
    /**
167
     * This field does not change when it's landscape
168
     * (it does not "know" about portrait/landscape)
169
     */
170
    public Size m_sizePaper = new Size(
171
        METRIC_A4_PAPER_SIZE.getHeight(),
172
        METRIC_A4_PAPER_SIZE.getWidth());
173

    
174
    /**
175
     * Print area (top, bottom, left, right).
176
     * This array changes when it's landscape
177
     * (it "knows" about portrait/landscape)
178
     */
179
    private double[] area_tblr = {
180
        0,
181
        0,
182
        0,
183
        0 };
184
    
185
    /**
186
     * This field changes when it's landscape
187
     * (it "knows" about portrait/landscape)
188
     */
189
    private Size m_sizeinUnits = m_sizePaper;
190
    private PrintQuality m_resolution = PrintQuality.NORMAL;
191

    
192
    /**
193
     * Create a new object of Attributes.
194
     * 
195
     */
196
    public Attributes() {
197
        m_NameUnit = Messages.getText("Centimetros");
198
        m_Orientation = OrientationRequested.LANDSCAPE;
199
        m_type = MediaSizeName.ISO_A4;
200
        resetSizeinUnits(isLandscape());
201

    
202
        hasmargin = false;
203
        attributes.add(new Copies(1));
204
        attributes.add(MediaSizeName.ISO_A4);
205
        attributes.add(Sides.ONE_SIDED);
206
        attributes.add(Fidelity.FIDELITY_FALSE);
207
        attributes.add(PrintQuality.NORMAL);
208

    
209
        setType(m_typeSel);
210

    
211
        if (isLandscape()) {
212
            attributes.add(new MediaPrintableArea(0, 0,
213
                (float) ((m_sizePaper.getHeight() * DPISCREEN) / PULGADA),
214
                (float) ((m_sizePaper.getWidth() * DPISCREEN) / PULGADA),
215
                MediaPrintableArea.MM));
216
        } else {
217
            attributes.add(new MediaPrintableArea(0, 0,
218
                (float) ((m_sizePaper.getWidth() * DPISCREEN) / PULGADA),
219
                (float) ((m_sizePaper.getHeight() * DPISCREEN) / PULGADA),
220
                MediaPrintableArea.MM));
221
        }
222
    }
223
    
224
        
225

    
226
    /**
227
     * @return
228
     */
229
    public boolean isLandscape() {
230
        return m_Orientation.getValue() ==
231
            OrientationRequested.LANDSCAPE.getValue();
232
    }
233

    
234
    /**
235
     * Inserts the Layout's properties to print.
236
     * 
237
     * @param typeSel
238
     *            Type of sheet.
239
     * @param units
240
     *            Units.
241
     * @param isLand
242
     *            True if the sheet is horizontal.
243
     * @param margin
244
     *            True if the sheet has margin.
245
     * @param resolution
246
     *            Type of quality of resolution.
247
     * @param area
248
     *            Printing area (top, bottom, left, right). 
249
     */
250
    public void setSelectedOptions(int typeSel, int units, boolean isLand,
251
        boolean margin, int resolution, double[] _area_tplr) {
252
        setType(typeSel);
253
        setUnit(units);
254
        setIsLandScape(isLand);
255
        hasmargin = margin;
256
        setResolution(resolution);
257
        area_tblr = _area_tplr;
258
    }
259
    
260
    /**
261
     * Paper size, measured in centimeters.
262
     * This field does not know about orientation,its value is the same
263
     * for portrait and landscape.
264
     * 
265
     * Note that this size is always measured in centimeters, regardless
266
     * the selected unit for the page. However, the user should be always
267
     * presented measures in the selected unit.
268
     * 
269
     * @see #getSelTypeUnit()
270
     * @see #setUnit(int)
271
     * 
272
     * @return
273
     */
274
    public Size getPaperSize() {
275
        return m_sizePaper;
276
    }
277

    
278
    /**
279
     * Returns a PageFormat with the properties of printing.
280
     * 
281
     * @return PageFormat
282
     */
283
    public PageFormat getPageFormat() {
284
        PageFormat pf1 = new PageFormat();
285
        Paper paper = pf1.getPaper();
286

    
287
        paper.setSize(
288
            (m_sizePaper.getWidth() * DPISCREEN) / PULGADA,
289
            (m_sizePaper.getHeight() * DPISCREEN) / PULGADA);
290

    
291
        if (isLandscape()) {
292
            pf1.setOrientation(0);
293
            /*
294
             * 'area_tblr' and 'm_sizeinUnits' depend on
295
             * whether it is portrait or landscape, but
296
             * 'Paper' and 'm_sizePaper' must not know if it's
297
             * portrait/landscape, so we change order of
298
             * parameters. 'PageFormat' will do the changes again
299
             * because it is aware of portrait/landscape
300
             */
301
            paper.setImageableArea(
302
                getInPixels(area_tblr[1]),
303
                getInPixels(area_tblr[2]),
304
                getInPixels(m_sizeinUnits.getHeight() - area_tblr[0] - area_tblr[1]),
305
                getInPixels(m_sizeinUnits.getWidth()  - area_tblr[2] - area_tblr[3]));
306
        } else {
307
            // portrait
308
            pf1.setOrientation(1);
309
            paper.setImageableArea(
310
                getInPixels(area_tblr[2]),
311
                getInPixels(area_tblr[0]),
312
                getInPixels(m_sizeinUnits.getWidth() - area_tblr[2] - area_tblr[3]),
313
                getInPixels(m_sizeinUnits.getHeight()  - area_tblr[0] - area_tblr[1]));
314
        }
315

    
316
        pf1.setPaper(paper);
317
        return pf1;
318
    }
319

    
320
    /**
321
     * Changes centimeters to pixels.
322
     * 
323
     * @param d
324
     *            Centimeters.
325
     * 
326
     * @return Pixels.
327
     */
328
    private double getInPixels(double d) {
329
        return d * (DPISCREEN / PULGADA);
330
    }
331

    
332
    /**
333
     * Changes pixels to centimeters.
334
     * 
335
     * @param d
336
     *            Pixel.
337
     * 
338
     * @return Centimeters.
339
     */
340
    private double getInCM(double d) {
341
        return d / (DPISCREEN / PULGADA);
342
    }
343

    
344
    /**
345
     * Returns the printing area.
346
     * The value returned changes if orientation changes
347
     * (it "knows" about portrait/landscape)
348
     * 
349
     * @return Rectangle Area.
350
     */
351
    public Rectangle2D getArea() {
352
        Rectangle2D.Double rect = new Rectangle2D.Double();
353
        if (isLandscape()) {
354
            rect.setRect(
355
                area_tblr[2],
356
                area_tblr[0],
357
                m_sizePaper.getHeight() - area_tblr[2] - area_tblr[3],
358
                m_sizePaper.getWidth()  - area_tblr[0] - area_tblr[1]);
359
        } else {
360
            rect.setRect(
361
                area_tblr[2],
362
                area_tblr[0],
363
                m_sizePaper.getWidth() - area_tblr[2] - area_tblr[3],
364
                m_sizePaper.getHeight()  - area_tblr[0] - area_tblr[1]);
365
        }
366
        return rect;
367
    }
368
    
369
    public double[] getAreaInsets() {
370
        return area_tblr;
371
    }
372

    
373
    /**
374
     * Inserts the attributes with a PageFormat.
375
     * 
376
     * @param pf
377
     *            PageFormat.
378
     */
379
    public void setPageFormat(PageFormat pf) {
380
        
381
        /*
382
         * Paper does not know about orientation
383
         */
384
        Size portrait_size = new Size(
385
            pf.getPaper().getHeight(),
386
            pf.getPaper().getWidth());
387
        setType(getTypePaper(portrait_size, true));
388

    
389
        if (pf.getOrientation() == 0) {
390
            setIsLandScape(true);
391
        } else {
392
            setIsLandScape(false);
393
        }
394
        
395

    
396
        attributes.add(m_Orientation);
397

    
398
        // tipo formato
399
        if (isLandscape()) {
400
            // landscape
401
            area_tblr[1] = getInCM(pf.getImageableX());
402
            area_tblr[2] = getInCM(pf.getImageableY());
403
            area_tblr[0] = 
404
                portrait_size.getWidth() - getInCM(pf.getImageableHeight())
405
                - area_tblr[1];
406
            area_tblr[3] =
407
                portrait_size.getHeight() - getInCM(pf.getImageableWidth())
408
                - area_tblr[2];
409
        } else {
410
            // portrait
411
            area_tblr[2] = getInCM(pf.getImageableX());
412
            area_tblr[0] = getInCM(pf.getImageableY());
413
            area_tblr[1] =
414
                portrait_size.getHeight() - getInCM(pf.getImageableHeight())
415
                - area_tblr[0];
416
            area_tblr[3] =
417
                portrait_size.getWidth() - getInCM(pf.getImageableWidth())
418
                - area_tblr[2];
419
        }
420
        
421
        /*
422
         * 'area_tblr' and 'pf' "know"
423
         * about orientation
424
         * 
425
         */
426
        attributes.add(new MediaPrintableArea(
427
            (float) (area_tblr[2] * 10),
428
            (float) (area_tblr[0] * 10),
429
            (float) (getInCM(pf.getImageableWidth()) * 10),
430
            (float) (getInCM(pf.getImageableHeight()) * 10),
431
            MediaPrintableArea.MM));
432
    }
433

    
434
    /**
435
     * Returns a PrintRequestAttributeSet with all properties to the
436
     * PrintDialog.
437
     * 
438
     * @return PrintRequestAttributesSet.
439
     */
440
    public PrintRequestAttributeSet toPrintRequestAttributeSet() {
441
        HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
442

    
443
        setType(m_typeSel);
444

    
445
        resul.add(m_type);
446

    
447
        // units, no hace falta a?adirlo a attributes
448
        resul.add(m_Orientation);
449
        // setArea(area_tblr);
450
        
451
        Rectangle2D rect = this.getArea();
452
        resul.add(new MediaPrintableArea(
453
            (float) (rect.getX() * DPISCREEN / PULGADA),
454
            (float) (rect.getY() * DPISCREEN / PULGADA),
455
            (float) (rect.getWidth() * DPISCREEN / PULGADA),
456
            (float) (rect.getHeight() * DPISCREEN / PULGADA),
457
            MediaPrintableArea.MM));
458

    
459
        resul.add(m_resolution);
460
        return resul;
461
    }
462

    
463
    public PrintAttributes toPrintAttributes() {
464
        PrintAttributes resul =
465
            CompatLocator.getGraphicsUtils().createPrintAttributes();
466

    
467
        if (m_resolution.getValue() == PrintQuality.DRAFT.getValue()) {
468
            resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_DRAFT);
469

    
470
        } else
471
            if (m_resolution.getValue() == PrintQuality.NORMAL.getValue()) {
472
                resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_NORMAL);
473

    
474
            } else
475
                if (m_resolution.getValue() == PrintQuality.HIGH.getValue()) {
476
                    resul.setPrintQuality(PrintAttributes.PRINT_QUALITY_HIGH);
477
                } else {
478
                    throw new UnsupportedOperationException(
479
                        "Unsupported Print Quality: " + m_resolution.getName()
480
                            + "(" + m_resolution.getValue() + ")");
481
                }
482
        setType(m_typeSel);
483

    
484
        return resul;
485
    }
486

    
487
    /**
488
     * Returns a PrintRequestAttributeSet with all properties to the
489
     * PrintDialog.
490
     * 
491
     * @return PrintRequestAttributesSet.
492
     */
493
    public PrintRequestAttributeSet toPrintAttributeSet() {
494
        HashPrintRequestAttributeSet resul = new HashPrintRequestAttributeSet();
495

    
496
        setType(m_typeSel);
497

    
498
        resul.add(m_type);
499

    
500
        resul.add(m_Orientation);
501
        
502
        Rectangle2D rect = this.getArea();
503
        resul.add(new MediaPrintableArea(
504
            (float) (rect.getX() * DPISCREEN / PULGADA),
505
            (float) (rect.getY() * DPISCREEN / PULGADA),
506
            (float) (rect.getWidth() * DPISCREEN / PULGADA),
507
            (float) (rect.getHeight() * DPISCREEN / PULGADA),
508
            MediaPrintableArea.MM));
509

    
510
        resul.add(m_resolution);
511
        return resul;
512
    }
513
    /**
514
     * Returns true if the margin should be used.
515
     * 
516
     * @return True if margin should be used.
517
     */
518
    public boolean isMargin() {
519
        return hasmargin;
520
    }
521

    
522
    /**
523
     * Returns the resolution of properties to print.
524
     * 
525
     * @return Resolution.
526
     */
527
    public int getResolution() {
528
        return m_resolutionSel;
529
    }
530

    
531
    /**
532
     * Inserts the resolution to apply at printing.
533
     * 
534
     * @param i
535
     *            Type of resolution.
536
     * 
537
     *            The type of resolution can be:
538
     * 
539
     *            DRAFT: 27 dpi.
540
     *            NORMAL: 300 dpi.
541
     *            HIGH: 600 dpi.
542
     */
543
    private void setResolution(int i) {
544
        m_resolutionSel = i;
545

    
546
        switch (i) {
547
        case (HIGH):
548

    
549
            m_resolution = PrintQuality.HIGH;
550
            DPI = 600;
551

    
552
            break;
553

    
554
        case (NORMAL):
555

    
556
            m_resolution = PrintQuality.NORMAL;
557
            DPI = 300;
558

    
559
            break;
560

    
561
        case (DRAFT):
562

    
563
            m_resolution = PrintQuality.DRAFT;
564
            DPI = 72;
565

    
566
            break;
567
        }
568
        attributes.add(m_resolution);
569
    }
570

    
571
    /**
572
     * Returns attributes of PrintDialog.
573
     * 
574
     * @return Attributes of printing.
575
     */
576
    public PrintRequestAttributeSet getAttributes() {
577
        return attributes;
578
    }
579

    
580
    /**
581
     * Returns True if the position of sheet is horizontal.
582
     * 
583
     * @return True if position is horizontal.
584
     */
585

    
586
    /**
587
     * Returns the format of sheet.
588
     * 
589
     * @return Format of sheet.
590
     */
591
    public int getType() {
592
        return m_typeSel;
593
    }
594

    
595
    /**
596
     * Inserts the number of units to define the coords X grid.
597
     * 
598
     * @param d
599
     *            number of units to define the grid.
600
     */
601
    public void setHGridGrapCm(double d) {
602
        m_gridGapCmX = new Double(d);
603
    }
604

    
605
    /**
606
     * Gets the grid gap for the x axis, in centimeters. The grip gap is the
607
     * distance from one point to the next point in the snapping grid.
608
     * 
609
     * @return Default grid gap for the x axis, measured in centimeters
610
     */
611
    public double getHGridGapCm() {
612
        if (m_gridGapCmX == null) {
613
            m_gridGapCmX = new Double(getDefaultGridGap().getX());
614
        }
615
        return m_gridGapCmX.doubleValue();
616
    }
617

    
618
    /**
619
     * Gets the grid gap for the y axis, in centimeters. The grip gap is the
620
     * distance from one point to the next point in the snapping grid.
621
     * 
622
     * @return Default grid gap for the y axis, measured in centimeters
623
     */
624
    public double getVGridGapCm() {
625
        if (m_gridGapCmY == null) {
626
            m_gridGapCmY = new Double(getDefaultGridGap().getY());
627
        }
628
        return m_gridGapCmY.doubleValue();
629
    }
630

    
631
    /**
632
     * Inserts the number of units to define the coords X grid.
633
     * 
634
     * @param d
635
     *            number of units to define the grid.
636
     */
637
    public void setVGridGrapCm(double d) {
638
        m_gridGapCmY = new Double(d);
639
    }
640

    
641
    /**
642
     * Inserts true if the sheet is horizontal or false if is vertical.
643
     * 
644
     * @param b
645
     *            True if sheet is horizontal.
646
     */
647
    public void setIsLandScape(boolean b) {
648
        if (b) {
649
            m_Orientation = OrientationRequested.LANDSCAPE;
650
        } else {
651
            m_Orientation = OrientationRequested.PORTRAIT;
652
        }
653
        setType(m_typeSel);
654
    }
655

    
656
    /**
657
     * Inserts the type of sheet to show.
658
     * 
659
     * @param t
660
     *            Type of sheet.
661
     */
662
    public void setType(int t) {
663
        m_typeSel = t;
664

    
665
        // m_sizePaper=getSizePaper(t);
666
        switch (t) {
667
        case (PRINT):
668
            m_type = ((MediaSizeName) attributes.get(Media.class));
669

    
670
            if (isLandscape()) {
671
                m_sizePaper =
672
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
673
                        METRIC_A4_PAPER_SIZE.getHeight());
674
            } else {
675
                m_sizePaper = METRIC_A4_PAPER_SIZE;
676
            }
677

    
678
            break;
679

    
680
        case (A4):
681
            m_type = MediaSizeName.ISO_A4;
682

    
683
            if (isLandscape()) {
684
                m_sizePaper =
685
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
686
                        METRIC_A4_PAPER_SIZE.getHeight());
687
            } else {
688
                m_sizePaper = METRIC_A4_PAPER_SIZE;
689
            }
690

    
691
            break;
692

    
693
        case (A3):
694
            m_type = MediaSizeName.ISO_A3;
695

    
696
            if (isLandscape()) {
697
                m_sizePaper =
698
                    new Size(METRIC_A3_PAPER_SIZE.getWidth(),
699
                        METRIC_A3_PAPER_SIZE.getHeight());
700
            } else {
701
                m_sizePaper = METRIC_A3_PAPER_SIZE;
702
            }
703

    
704
            break;
705

    
706
        case (A2):
707
            m_type = MediaSizeName.ISO_A2;
708

    
709
            if (isLandscape()) {
710
                m_sizePaper =
711
                    new Size(METRIC_A2_PAPER_SIZE.getWidth(),
712
                        METRIC_A2_PAPER_SIZE.getHeight());
713
            } else {
714
                m_sizePaper = METRIC_A2_PAPER_SIZE;
715
            }
716

    
717
            break;
718

    
719
        case (A1):
720
            m_type = MediaSizeName.ISO_A1;
721

    
722
            if (isLandscape()) {
723
                m_sizePaper =
724
                    new Size(METRIC_A1_PAPER_SIZE.getWidth(),
725
                        METRIC_A1_PAPER_SIZE.getHeight());
726
            } else {
727
                m_sizePaper = METRIC_A1_PAPER_SIZE;
728
            }
729

    
730
            break;
731

    
732
        case (A0):
733
            m_type = MediaSizeName.ISO_A0;
734

    
735
            if (isLandscape()) {
736
                m_sizePaper =
737
                    new Size(METRIC_A0_PAPER_SIZE.getWidth(),
738
                        METRIC_A0_PAPER_SIZE.getHeight());
739
            } else {
740
                m_sizePaper = METRIC_A0_PAPER_SIZE;
741
            }
742

    
743
            break;
744

    
745
        case (CUSTOM):
746
            m_type = MediaSizeName.PERSONAL_ENVELOPE;
747
            m_sizePaper = new Size(
748
                CUSTOM_PAPER_SIZE.getHeight()*m_TypeUnit,
749
                CUSTOM_PAPER_SIZE.getWidth()*m_TypeUnit);
750
            break;
751
        }
752
        attributes.add(m_type);
753
        resetSizeinUnits(isLandscape());
754
    }
755

    
756
    /**
757
     * Returns the size of sheet.
758
     * 
759
     * @param isLand
760
     *            True if is horizontal and false if is vertical.
761
     * @param type
762
     *            Type of sheet.
763
     * 
764
     * @return Size of sheet with these properties.
765
     */
766
    public Size getSizeinUnits(boolean isLand, int type) {
767
        Size size = null;
768

    
769
        switch (type) {
770
        case (PRINT):
771

    
772
            if (isLand) {
773
                size =
774
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
775
                        METRIC_A4_PAPER_SIZE.getHeight());
776
            } else {
777
                size = METRIC_A4_PAPER_SIZE;
778
            }
779

    
780
            break;
781

    
782
        case (A4):
783

    
784
            if (isLand) {
785
                size =
786
                    new Size(METRIC_A4_PAPER_SIZE.getWidth(),
787
                        METRIC_A4_PAPER_SIZE.getHeight());
788
            } else {
789
                size = METRIC_A4_PAPER_SIZE;
790
            }
791

    
792
            break;
793

    
794
        case (A3):
795

    
796
            if (isLand) {
797
                size =
798
                    new Size(METRIC_A3_PAPER_SIZE.getWidth(),
799
                        METRIC_A3_PAPER_SIZE.getHeight());
800
            } else {
801
                size = METRIC_A3_PAPER_SIZE;
802
            }
803

    
804
            break;
805

    
806
        case (A2):
807

    
808
            if (isLand) {
809
                size =
810
                    new Size(METRIC_A2_PAPER_SIZE.getWidth(),
811
                        METRIC_A2_PAPER_SIZE.getHeight());
812
            } else {
813
                size = METRIC_A2_PAPER_SIZE;
814
            }
815

    
816
            break;
817

    
818
        case (A1):
819

    
820
            if (isLand) {
821
                size =
822
                    new Size(METRIC_A1_PAPER_SIZE.getWidth(),
823
                        METRIC_A1_PAPER_SIZE.getHeight());
824
            } else {
825
                size = METRIC_A1_PAPER_SIZE;
826
            }
827

    
828
            break;
829

    
830
        case (A0):
831

    
832
            if (isLand) {
833
                size =
834
                    new Size(METRIC_A0_PAPER_SIZE.getWidth(),
835
                        METRIC_A0_PAPER_SIZE.getHeight());
836
            } else {
837
                size = METRIC_A0_PAPER_SIZE;
838
            }
839

    
840
            break;
841

    
842
        case (CUSTOM):
843
            if (isLandscape()) {
844
                size =
845
                    new Size(
846
                        CUSTOM_PAPER_SIZE.getWidth() * m_TypeUnit,
847
                        CUSTOM_PAPER_SIZE.getHeight() * m_TypeUnit);
848
            } else {
849
                size =
850
                    new Size(
851
                        CUSTOM_PAPER_SIZE.getHeight() * m_TypeUnit,
852
                        CUSTOM_PAPER_SIZE.getWidth() * m_TypeUnit);
853
            }
854
            break;
855
        }
856

    
857
        Size resp = new Size(
858
            size.getHeight() / m_TypeUnit, 
859
            size.getWidth() / m_TypeUnit);
860

    
861
        return resp;
862
    }
863

    
864
    
865
    public static Size sizeForPaperType(int type) {
866
        Size size = null;
867

    
868
        switch (type) {
869
        case (PRINT):
870
            size = METRIC_A4_PAPER_SIZE;
871
            break;
872

    
873
        case (A4):
874
            size = METRIC_A4_PAPER_SIZE;
875
            break;
876

    
877
        case (A3):
878
            size = METRIC_A3_PAPER_SIZE;
879
            break;
880

    
881
        case (A2):
882
            size = METRIC_A2_PAPER_SIZE;
883
            break;
884

    
885
        case (A1):
886
            size = METRIC_A1_PAPER_SIZE;
887
            break;
888

    
889
        case (A0):
890
            size = METRIC_A0_PAPER_SIZE;
891
            break;
892

    
893
        case (CUSTOM):
894
            size = new Size(
895
                CUSTOM_PAPER_SIZE.getHeight(),
896
                CUSTOM_PAPER_SIZE.getWidth());
897
            break;
898
        }
899

    
900
        return size;
901
    }
902

    
903
    
904
    
905
    
906
    /**
907
     * Returns the type of unit in use (cm, mm, km, etc), codified as
908
     * a position in the array returned by {@link MapContext#getDistanceNames()}
909
     *
910
     * @see #setUnit(int)
911
     * @see MapContext#getDistanceNames() 
912
     * @see MapContext#getDistanceTrans2Meter()
913
     * @see MapContext#getDistanceAbbr()
914
     * @see MapContext#getDistancePosition(String)
915
     * @see MapContext#getDistancePosition(String)
916

917
     * 
918
     * @return Type of units.
919
     */
920
    public int getSelTypeUnit() {
921
        return m_selTypeUnit;
922
    }
923

    
924
    /**
925
     * Returns the name of the selected units.
926
     * @see #getSelTypeUnit()
927
     * 
928
     * @return String Name of units.
929
     */
930
    public String getNameUnit() {
931
        return m_NameUnit;
932
    }
933

    
934
    /**
935
     * Sets the type of unit that is used in the Layout, defined as an
936
     * integer indicating a position in the array
937
     * {@link MapContext#getDistanceNames()}. By default is
938
     * centimeters.
939
     * 
940
     * @param sel
941
     *            Type of unit.
942
     * @see #getSelTypeUnit()
943
     */
944
    public void setUnit(int sel) {
945
        Logger logger = LoggerFactory
946
                .getLogger(LayoutExtension.class);
947
        logger.debug("setUnit: "+sel);
948
        m_selTypeUnit = sel;
949
        m_TypeUnit = getConversionFactorToCm(sel);
950
        m_NameUnit =
951
            PluginServices.getText(this, MapContext.getDistanceNames()[sel]);
952
    }
953

    
954
    /**
955
     * Actualize to date m_sizeinUnits with the height and width of the sheet
956
     * in the units of mediated selected.
957
     * 
958
     * @param b
959
     *            True if is horizontal.
960
     */
961
    public void resetSizeinUnits(boolean is_landscape) {
962
        if (is_landscape) {
963
            m_sizeinUnits =
964
                new Size(m_sizePaper.getHeight() / m_TypeUnit,
965
                    m_sizePaper.getWidth() / m_TypeUnit);
966
        } else {
967
            m_sizeinUnits =
968
                new Size(m_sizePaper.getWidth() / m_TypeUnit,
969
                    m_sizePaper.getHeight() / m_TypeUnit);
970
        }
971
    }
972

    
973
    /**
974
     * Returns a double in the unit of measure selected from the double in
975
     * centimeters.
976
     * 
977
     * @param d
978
     *            Distance in centimeters.
979
     * 
980
     * @return Distance in the unit measure selected.
981
     */
982
    public double toUnits(double d) {
983
        NumberFormat nf = NumberFormat.getInstance();
984
        nf.setMaximumFractionDigits(2);
985

    
986
        String s = String.valueOf(nf.format(d));
987
        s = s.replace(',', '.');
988

    
989
        return (Double.valueOf(s).doubleValue() / m_TypeUnit);
990
    }
991

    
992
    /**
993
     * Returns a double in centimeters from the the unit of measure selected.
994
     * 
995
     * @param d
996
     *            Distance in the unit measure selected.
997
     * 
998
     * @return Distance in centimeters.
999
     */
1000
    public double fromUnits(double d) {
1001
        return (d * m_TypeUnit);
1002
    }
1003

    
1004
    /**
1005
     * Returns one centimeter in pixels on screen in this moment.
1006
     * 
1007
     * @param rect
1008
     *            Rectangle of sheet.
1009
     * 
1010
     * @return Pixels from centimeter.
1011
     */
1012
    public double getPixXCm(Rectangle2D rect) {
1013
        double value = m_sizePaper.getWidth();
1014
        double cm = getConversionFactorToCm(2);
1015
        double unidades = 0;
1016
        unidades = ((rect.getMaxX() - rect.getMinX()) / ((value / cm)));
1017
        return unidades;
1018
    }
1019

    
1020
    /**
1021
     * Calculates the grid gap for the x axis, in pixels,
1022
     * based on the paper width (in cm) and the width
1023
     * of the rectangle of sheet in screen, measured in
1024
     * pixels.
1025
     *  
1026
     * The grip gap is the distance from one
1027
     * point to the next point in the snapping grid.
1028
     * 
1029
     * @param rect
1030
     *            Rectangle of sheet, measured in pixels
1031
     */
1032
    public void calculateGridGapX(Rectangle2D rect) {
1033
        double width = m_sizePaper.getWidth();
1034
        double numberOfGaps = width / getHGridGapCm();
1035
        double unidades =
1036
            (rect.getMaxX() - rect.getMinX()) / numberOfGaps;
1037
        m_gridGapPxX = unidades;
1038
    }
1039

    
1040
    /**
1041
     * Calculates the grid gap for the y axis, in pixels,
1042
     * based on the paper height (in cm) and the height
1043
     * of the rectangle of sheet in screen, measured in
1044
     * pixels.
1045
     *  
1046
     * The grip gap is the distance from one
1047
     * point to the next point in the snapping grid.
1048
     * 
1049
     * @param rect
1050
     *            Rectangle of sheet, measured in pixels
1051
     */
1052
    public void calculateGridGapY(Rectangle2D rect) {
1053
        double height = m_sizePaper.getHeight();
1054
        double numberOfGaps = height / getVGridGapCm();
1055
        double unidades =
1056
            (rect.getMaxY() - rect.getMinY()) / numberOfGaps;
1057
        m_gridGapPxY = unidades;
1058
    }
1059

    
1060
    
1061
    private double getPaperSizeRatio() {
1062
        double resp = 1;
1063
        if (isLandscape()) {
1064
            resp = m_sizePaper.getWidth() / m_sizePaper.getHeight();
1065
        } else {
1066
            resp = m_sizePaper.getHeight() / m_sizePaper.getWidth();
1067
        }
1068
        return resp;
1069
    }
1070
    /**
1071
     * Obtains the rectangle that represents the sheet with the characteristics
1072
     * that contains attributes
1073
     * 
1074
     * @param rect
1075
     *            rectangle.
1076
     * 
1077
     */
1078
    public void obtainRect(boolean isPrint, Rectangle2D rect, int w, int h) {
1079
        double value1 = 0;
1080
        double value2 = 0;
1081

    
1082
        if (!isPrint) {
1083
            if (isLandscape()) {
1084
                rect = getRectangleLandscape(rect, w, h);
1085
            } else {
1086
                rect = getRectanglePortrait(rect, w, h);
1087
            }
1088
        } else {
1089
            value1 = m_sizePaper.getWidth();
1090
            value2 = m_sizePaper.getHeight();
1091
            rect.setRect(0, 0, ((value1 / PULGADA) * DPI),
1092
                ((value2 / PULGADA) * DPI));
1093
        }
1094

    
1095
        calculateGridGapX(rect);
1096
        calculateGridGapY(rect);
1097
    }
1098

    
1099
    /**
1100
     * It obtains the rect that is adjusted to the size of the window,
1101
     * to see the full extent of horizontal layout.
1102
     * 
1103
     * @param rect
1104
     *            Rectangle sheet.
1105
     * @param w
1106
     *            Width of Layout.
1107
     * @param h
1108
     *            Height of Layout.
1109
     * 
1110
     * @return Rectangle modified.
1111
     */
1112
    public Rectangle2D.Double getRectangleLandscape(Rectangle2D rect, int w,
1113
        int h) {
1114
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
1115
        int x0 = (int) rect.getMinX();
1116
        int y0 = (int) rect.getMinY();
1117

    
1118
        // Don't use the whole component space to draw the sheet,
1119
        // add some margin to make the sheet evident.
1120
        // 2 * x0: margin left and margin right
1121
        // 2 * y0: margin top and margin bottom
1122
        int newHeight = (h - (2 * y0));
1123
        int newWidth = (int) (newHeight * getPaperSizeRatio());
1124
        if (((int) (((h) - (2 * y0)) * getPaperSizeRatio())) > ((w) - (2 * x0))) {
1125
                newWidth = ((w) - (2 * x0));
1126
                newHeight = (int) (newWidth / getPaperSizeRatio());
1127
        }
1128
        rectaux.setRect(x0, y0, newWidth, newHeight);
1129
        return rectaux;
1130
    }
1131

    
1132
    /**
1133
     * Returns the size of sheet in the units of measure selected.
1134
     * 
1135
     * @return Size of sheet.
1136
     */
1137
    public Size getSizeInUnits() {
1138
        return m_sizeinUnits;
1139
    }
1140

    
1141
    /**
1142
     * It obtains the rect that is adjusted to the size of the window,
1143
     * to see the full extent of vertical layout.
1144
     * 
1145
     * @param rect
1146
     *            Rectangle sheet.
1147
     * @param w
1148
     *            Width of Layout.
1149
     * @param h
1150
     *            Height of Layout.
1151
     * 
1152
     * @return Rectangle modified.
1153
     */
1154
    public Rectangle2D.Double getRectanglePortrait(Rectangle2D rect, int w,
1155
        int h) {
1156
        Rectangle2D.Double rectaux = new Rectangle2D.Double();
1157
        int x0 = (int) rect.getMinX();
1158
        int y0 = (int) rect.getMinY();
1159
                
1160
        // Don't use the whole component space to draw the sheet,
1161
        // add some margin to make the sheet evident.
1162
        // 2 * x0: margin left and margin right
1163
        // 2 * y0: margin top and margin bottom
1164
        int newWidth = (w - (2 * x0));
1165
        int newHeight = (int) (newWidth * getPaperSizeRatio());
1166
        if (((int) (((w) - (2 * x0)) * getPaperSizeRatio())) > ((h) - (2 * y0))) {
1167
                newHeight = (h - (2 * y0));
1168
                newWidth = (int) (newHeight / getPaperSizeRatio());
1169
        }
1170
        rectaux.setRect(x0, y0, newWidth, newHeight);
1171
        return rectaux;
1172
    }
1173

    
1174
    /**
1175
     * It obtains the type of format from the size.
1176
     * 
1177
     * @param size
1178
     *            Size of sheet.
1179
     * 
1180
     * @return Type of sheet.
1181
     */
1182
    private int getTypePaper(Size size, boolean is_portrait) {
1183

    
1184
        int tol = 1;
1185
        Size portrait_size = null;
1186

    
1187
        if (is_portrait) {
1188
            portrait_size = new Size(size.getHeight(), size.getWidth());
1189
        } else {
1190
            portrait_size = new Size(size.getWidth(), size.getHeight());
1191
        }
1192

    
1193
        if ((((portrait_size.getWidth() * PULGADA) / DPISCREEN) < (tol + METRIC_A4_PAPER_SIZE
1194
            .getWidth()))
1195
            && (((portrait_size.getHeight() * PULGADA) / DPISCREEN) < (tol + METRIC_A4_PAPER_SIZE
1196
                .getHeight()))) {
1197
            return A4;
1198
        } else
1199
            if ((((portrait_size.getWidth() * PULGADA) / DPISCREEN) < (tol + METRIC_A3_PAPER_SIZE
1200
                .getWidth()))
1201
                && (((portrait_size.getHeight() * PULGADA) / DPISCREEN) < (tol + METRIC_A3_PAPER_SIZE
1202
                    .getHeight()))) {
1203
                return A3;
1204
            } else
1205
                if ((((portrait_size.getWidth() * PULGADA) / DPISCREEN) < (tol + METRIC_A2_PAPER_SIZE
1206
                    .getWidth()))
1207
                    && (((portrait_size.getHeight() * PULGADA) / DPISCREEN) < (tol + METRIC_A2_PAPER_SIZE
1208
                        .getHeight()))) {
1209
                    return A2;
1210
                } else
1211
                    if ((((portrait_size.getWidth() * PULGADA) / DPISCREEN) < (tol + METRIC_A1_PAPER_SIZE
1212
                        .getWidth()))
1213
                        && (((portrait_size.getHeight() * PULGADA) / DPISCREEN) < (tol + METRIC_A1_PAPER_SIZE
1214
                            .getHeight()))) {
1215
                        return A1;
1216
                    } else
1217
                        if ((((portrait_size.getWidth() * PULGADA) / DPISCREEN) < (tol + METRIC_A0_PAPER_SIZE
1218
                            .getWidth()))
1219
                            && (((portrait_size.getHeight() * PULGADA) / DPISCREEN) < (tol + METRIC_A0_PAPER_SIZE
1220
                                .getHeight()))) {
1221
                            return A0;
1222
                        }
1223

    
1224
        return A4;
1225
    }
1226

    
1227
    /**
1228
     * Sets the default grid gap. The grip gap is the distance from one
1229
     * point to the next point in the snapping grid.
1230
     * 
1231
     * @param hGap
1232
     *            Horizontal distance.
1233
     * @param vGap
1234
     *            Vertical distance.
1235
     * @param hUnitAbbr The abbreviation of the measure in which hGap has
1236
     * been provided, codified as one of the abbreviations specified in
1237
     * {@link MapContext#getDistanceAbbr()}
1238
     * 
1239
     * @param vUnitAbbr The abbreviation of the measure in which vGap has
1240
     * been provided, codified as one of the abbreviations specified in
1241
     * {@link MapContext#getDistanceAbbr()}
1242
     */
1243
    public static void setDefaultGridGap(double hGap, double vGap, String hUnitAbbr, String vUnitAbbr) {
1244
            int hUnit = getDistanceAbbrPosition(hUnitAbbr);
1245
        int vUnit = getDistanceAbbrPosition(vUnitAbbr);
1246
        double hGapCm = hGap*getConversionFactorToCm(hUnit);
1247
        double vGapCm = vGap*getConversionFactorToCm(vUnit);
1248
        defaultGridGap = new Point2D.Double(hGapCm, vGapCm);
1249
    }
1250

    
1251
    /**
1252
     * Gets the default grid gap, in centimeters. The grip gap is the distance
1253
     * from one point to the next point in the snapping grid.
1254
     * 
1255
     * @return Default grid gap, measured in centimeters
1256
     */
1257
    public static Point2D getDefaultGridGap() {
1258
        if (defaultGridGap == null) {
1259
                PluginServices pluginServices = PluginsLocator.getManager().getPlugin(LayoutExtension.class);
1260
            XMLEntity xml = null;
1261
            if (pluginServices != null) {
1262
                xml = pluginServices.getPersistentXML();
1263
            }
1264
            double hGap =
1265
                ((xml != null) && (xml
1266
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_KEY_NAME))) ? xml
1267
                    .getDoubleProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_KEY_NAME)
1268
                    : PreferenceKeys.FACTORY_DEFAULT_HORIZONTAL_GAP;
1269
            double vGap =
1270
                ((xml != null) && (xml
1271
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_KEY_NAME))) ? xml
1272
                    .getDoubleProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_KEY_NAME)
1273
                    : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP;
1274
            String hUnitName = ((xml != null) && (xml
1275
                    .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_UNITS_KEY_NAME))) ? xml
1276
                    .getStringProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_HORIZONTAL_GAP_UNITS_KEY_NAME)
1277
                    : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP_UNIT; // cm by default
1278
            String vUnitName = ((xml != null) && (xml
1279
                            .contains(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_UNITS_KEY_NAME))) ? xml
1280
                                            .getStringProperty(PreferenceKeys.DEFAULT_LAYOUT_GRID_VERTICAL_GAP_UNITS_KEY_NAME)
1281
                                            : PreferenceKeys.FACTORY_DEFAULT_VERTICAL_GAP_UNIT; // cm by default
1282
            int hUnit = getDistanceAbbrPosition(hUnitName);
1283
            int vUnit = getDistanceAbbrPosition(vUnitName);
1284
            double hGapCm = hGap*getConversionFactorToCm(hUnit);
1285
            double vGapCm = vGap*getConversionFactorToCm(vUnit);
1286
            defaultGridGap = new Point2D.Double(hGapCm, vGapCm);
1287
        }
1288
        return defaultGridGap;
1289
    }
1290
    
1291
    public static int getDistanceAbbrPosition(String unitAbbr) {
1292
            ArrayList list = MapContext.DISTANCEABBR;
1293
                for (int i = 0; i < list.size(); i++) {
1294
                        if (list.get(i).equals(unitAbbr)){
1295
                                return i;
1296
                        }
1297
                }
1298
                return 0;
1299
            
1300
    }
1301

    
1302
    /**
1303
     * Gets the grid gap for the y axis, in pixels.
1304
     * The grip gap is the distance from one
1305
     * point to the next point in the snapping grid.
1306
     * 
1307
     * @return Number of pixels of each grid gap for the y axis
1308
     */
1309
    public double getVGridGapPx() {
1310
        return m_gridGapPxY;
1311
    }
1312

    
1313
    /**
1314
     * Gets the grid gap for the x axis, in pixels.
1315
     * The grip gap is the distance from one
1316
     * point to the next point in the snapping grid.
1317
     * 
1318
     * @return Number of pixels of each grid gap for the x axis
1319
     */
1320
    public double getHGridGapPx() {
1321
        return m_gridGapPxX;
1322
    }
1323

    
1324
    public static void registerPersistent() {
1325
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1326
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
1327
            DynStruct definition =
1328
                manager.addDefinition(Attributes.class,
1329
                    PERSISTENCE_DEFINITION_NAME,
1330
                    "Attributes persistence definition", null, null);
1331

    
1332
            definition.addDynFieldInt(DPI_FIELD).setMandatory(true);
1333
            definition.addDynFieldInt(DPISCREEN_FIELD).setMandatory(true);
1334
            definition.addDynFieldDouble(TYPEUNIT_FIELD).setMandatory(true);
1335
            definition.addDynFieldDouble(NUMX_FIELD).setMandatory(false);
1336
            definition.addDynFieldDouble(NUMY_FIELD).setMandatory(false);
1337
            definition.addDynFieldDouble(UNITX_FIELD).setMandatory(true);
1338
            definition.addDynFieldDouble(UNITY_FIELD).setMandatory(true);
1339
            definition.addDynFieldBoolean(HASMARGIN_FIELD).setMandatory(true);
1340
            definition.addDynFieldInt(RESOLUTIONSEL_FIELD).setMandatory(true);
1341
            definition.addDynFieldInt(TYPESEL_FIELD).setMandatory(true);
1342
            definition.addDynFieldInt(SELTYPEUNIT_FIELD).setMandatory(true);
1343
            definition.addDynFieldObject(SIZEPAPER_FIELD).setMandatory(true)
1344
                .setClassOfValue(Size.class);
1345
            definition.addDynFieldArray(AREA_FIELD)
1346
                .setClassOfItems(String.class).setMandatory(true);
1347
            definition.addDynFieldObject(SIZEINUNITS_FIELD).setMandatory(true)
1348
                .setClassOfValue(Size.class);
1349
            definition.addDynFieldBoolean(ISLANDSEL_FIELD).setMandatory(true);
1350
        }
1351

    
1352
        Size.registerPersistent();
1353
    }
1354

    
1355
    public void loadFromState(PersistentState state)
1356
        throws PersistenceException {
1357
        DPI = state.getInt(DPI_FIELD);
1358
        DPISCREEN = state.getInt(DPISCREEN_FIELD);
1359
        m_TypeUnit = state.getDouble(TYPEUNIT_FIELD);
1360
        m_gridGapCmX = (Double) state.get(NUMX_FIELD);
1361
        m_gridGapCmY = (Double) state.get(NUMY_FIELD);
1362
        m_gridGapPxX = (Double) state.get(UNITX_FIELD);
1363
        m_gridGapPxY = (Double) state.get(UNITY_FIELD);
1364
        hasmargin = state.getBoolean(HASMARGIN_FIELD);
1365
        m_resolutionSel = state.getInt(RESOLUTIONSEL_FIELD);
1366
        m_typeSel = state.getInt(TYPESEL_FIELD);
1367
        m_selTypeUnit = state.getInt(SELTYPEUNIT_FIELD);
1368
        m_sizePaper = (Size) state.get(SIZEPAPER_FIELD);
1369
        area_tblr = state.getDoubleArray(AREA_FIELD);
1370
        m_sizeinUnits = (Size) state.get(SIZEINUNITS_FIELD);
1371
        boolean lands = state.getBoolean(ISLANDSEL_FIELD);
1372
        this.setIsLandScape(lands);
1373
    }
1374

    
1375
    public void saveToState(PersistentState state) throws PersistenceException {
1376
        state.set(DPI_FIELD, DPI);
1377
        state.set(DPISCREEN_FIELD, DPISCREEN);
1378
        state.set(TYPEUNIT_FIELD, m_TypeUnit);
1379
        state.set(NUMX_FIELD, m_gridGapCmX);
1380
        state.set(NUMY_FIELD, m_gridGapCmY);
1381
        state.set(UNITX_FIELD, m_gridGapPxX);
1382
        state.set(UNITY_FIELD, m_gridGapPxY);
1383
        state.set(HASMARGIN_FIELD, hasmargin);
1384
        state.set(RESOLUTIONSEL_FIELD, m_resolutionSel);
1385
        state.set(TYPESEL_FIELD, m_typeSel);
1386
        state.set(SELTYPEUNIT_FIELD, m_selTypeUnit);
1387
        state.set(SIZEPAPER_FIELD, m_sizePaper);
1388
        state.set(AREA_FIELD, area_tblr);
1389
        state.set(SIZEINUNITS_FIELD, m_sizeinUnits);
1390
        state.set(ISLANDSEL_FIELD, this.isLandscape());
1391
    }
1392
    
1393
    /**
1394
     * Gets the conversion factor from the specified unit to centimeters.
1395
     * If you have some distance measured in the specified unit, you can
1396
     * multiply it by this factor in order to convert the distance to cm. 
1397
     * 
1398
     * @param unit The source unit, codified as a position in the array
1399
     *             {@link MapContext#getDistanceNames()}
1400
     * @return A conversion factor from the specified unit to cm
1401
     */
1402
    public static double getConversionFactorToCm(int unit) {
1403
            return 100.0d*MapContext.getDistanceTrans2Meter()[unit];
1404
    }
1405

    
1406
}