View | Details | Raw Unified | Return to bug 55330
Collapse All | Expand All

(-)a/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java (-51 / +90 lines)
Lines 26-31 import java.util.Map; Link Here
26
import org.apache.poi.hssf.model.RecordStream;
26
import org.apache.poi.hssf.model.RecordStream;
27
import org.apache.poi.hssf.model.InternalSheet;
27
import org.apache.poi.hssf.model.InternalSheet;
28
import org.apache.poi.hssf.record.*;
28
import org.apache.poi.hssf.record.*;
29
import org.apache.poi.ss.usermodel.PageMargin;
29
import org.apache.poi.util.HexDump;
30
import org.apache.poi.util.HexDump;
30
31
31
/**
32
/**
Lines 403-469 public final class PageSettingsBlock extends RecordAggregate { Link Here
403
        _printSetup = newPrintSetup;
404
        _printSetup = newPrintSetup;
404
    }
405
    }
405
406
406
407
    /**
407
    private Margin getMarginRec(int marginIndex) {
408
     * Returns one of the standard page margins
408
        switch (marginIndex) {
409
     *
409
            case InternalSheet.LeftMargin:   return _leftMargin;
410
     * @param margin
410
            case InternalSheet.RightMargin:  return _rightMargin;
411
     *            One of the four standard margins: {@link PageMargin.LEFT},
411
            case InternalSheet.TopMargin:    return _topMargin;
412
     *            {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or
412
            case InternalSheet.BottomMargin: return _bottomMargin;
413
     *            {@link PageMargin.BOTTOM}. Header and Footer margins are not
414
     *            available via this function
415
     * @return The requested margin
416
     */
417
    private Margin getMarginRec(PageMargin margin) {
418
        switch (margin) {
419
        case LEFT:
420
            return _leftMargin;
421
        case RIGHT:
422
            return _rightMargin;
423
        case TOP:
424
            return _topMargin;
425
        case BOTTOM:
426
            return _bottomMargin;
427
        default:
428
            throw new IllegalArgumentException("Unknown margin constant:  "
429
                    + margin);
413
        }
430
        }
414
        throw new IllegalArgumentException( "Unknown margin constant:  " + marginIndex );
415
    }
431
    }
416
432
417
433
418
    /**
434
    /**
419
     * Gets the size of the margin in inches.
435
     * Retrieves the margin for one of the four standard page margins
420
     * @param margin which margin to get
436
     *
421
     * @return the size of the margin
437
     * @param margin
438
     *            One of the four standard margins: {@link PageMargin.LEFT},
439
     *            {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or
440
     *            {@link PageMargin.BOTTOM}. Header and Footer margins are not
441
     *            available via this function
442
     * @return The page margin requested, in inches
422
     */
443
     */
423
    public double getMargin(short margin) {
444
    public double getMargin(PageMargin margin) {
424
        Margin m = getMarginRec(margin);
445
        Margin marginData = getMarginRec(margin);
425
        if (m != null) {
446
426
            return m.getMargin();
447
        if (marginData != null) {
448
            return marginData.getMargin();
427
        }
449
        }
450
451
        // If no margin could be retrieved, return the default value for that
452
        // margin
428
        switch (margin) {
453
        switch (margin) {
429
            case InternalSheet.LeftMargin:   return .75;
454
        case LEFT:
430
            case InternalSheet.RightMargin:  return .75;
455
            return 0.75;
431
            case InternalSheet.TopMargin:    return 1.0;
456
        case RIGHT:
432
            case InternalSheet.BottomMargin: return 1.0;
457
            return 0.75;
458
        case TOP:
459
            return 1.0;
460
        case BOTTOM:
461
            return 1.0;
462
        default:
463
            throw new IllegalArgumentException("Unknown margin constant:  "
464
                    + margin);
433
        }
465
        }
434
        throw new IllegalArgumentException( "Unknown margin constant:  " + margin );
466
    }
435
    }
467
436
468
    /**
437
    /**
469
     * Sets the size of one of the four standard page margins in inches
438
     * Sets the size of the margin in inches.
470
     * 
439
     * @param margin which margin to get
471
     * @param margin
440
     * @param size the size of the margin
472
     *            One of the four standard margins: {@link PageMargin.LEFT},
441
     */
473
     *            {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or
442
    public void setMargin(short margin, double size) {
474
     *            {@link PageMargin.BOTTOM}. Header and Footer margins are not
443
        Margin m = getMarginRec(margin);
475
     *            available via this function
444
        if (m  == null) {
476
     * @param size
445
            switch (margin) {
477
     *            The size to set the margin to, in inches
446
                case InternalSheet.LeftMargin:
478
     */
447
                    _leftMargin = new LeftMarginRecord();
479
    public void setMargin(PageMargin margin, double size) {
448
                    m = _leftMargin;
480
        Margin marginData = getMarginRec(margin);
449
                    break;
481
450
                case InternalSheet.RightMargin:
482
        // If no data exists, create it
451
                    _rightMargin = new RightMarginRecord();
483
        switch (margin) {
452
                    m = _rightMargin;
484
        case LEFT:
453
                    break;
485
            _leftMargin = new LeftMarginRecord();
454
                case InternalSheet.TopMargin:
486
            marginData = _leftMargin;
455
                    _topMargin = new TopMarginRecord();
487
            break;
456
                    m = _topMargin;
488
        case RIGHT:
457
                    break;
489
            _rightMargin = new RightMarginRecord();
458
                case InternalSheet.BottomMargin:
490
            marginData = _rightMargin;
459
                    _bottomMargin = new BottomMarginRecord();
491
            break;
460
                    m = _bottomMargin;
492
        case TOP:
461
                    break;
493
            _topMargin = new TopMarginRecord();
462
                default :
494
            marginData = _topMargin;
463
                    throw new IllegalArgumentException( "Unknown margin constant:  " + margin );
495
            break;
464
            }
496
        case BOTTOM:
497
            _bottomMargin = new BottomMarginRecord();
498
            marginData = _bottomMargin;
499
            break;
500
        default:
501
            throw new IllegalArgumentException("Unknown margin constant:  "
502
                    + margin);
465
        }
503
        }
466
        m.setMargin( size );
504
505
        marginData.setMargin(size);
467
    }
506
    }
468
507
469
    /**
508
    /**
(-)a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-19 / +65 lines)
Lines 45-50 import org.apache.poi.ss.usermodel.CellRange; Link Here
45
import org.apache.poi.ss.usermodel.CellStyle;
45
import org.apache.poi.ss.usermodel.CellStyle;
46
import org.apache.poi.ss.usermodel.DataValidation;
46
import org.apache.poi.ss.usermodel.DataValidation;
47
import org.apache.poi.ss.usermodel.DataValidationHelper;
47
import org.apache.poi.ss.usermodel.DataValidationHelper;
48
import org.apache.poi.ss.usermodel.PageMargin;
48
import org.apache.poi.ss.usermodel.Row;
49
import org.apache.poi.ss.usermodel.Row;
49
import org.apache.poi.ss.util.CellRangeAddress;
50
import org.apache.poi.ss.util.CellRangeAddress;
50
import org.apache.poi.ss.util.CellReference;
51
import org.apache.poi.ss.util.CellReference;
Lines 1122-1157 public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { Link Here
1122
    /**
1123
    /**
1123
     * Gets the size of the margin in inches.
1124
     * Gets the size of the margin in inches.
1124
     *
1125
     *
1125
     * @param margin which margin to get
1126
     * @param margin
1127
     *            which margin to get
1126
     * @return the size of the margin
1128
     * @return the size of the margin
1129
     * @deprecated (type safety) Use {@link HSSFSheet#getMargin(PageMargin)}
1130
     *             instead
1127
     */
1131
     */
1132
    @Override
1133
    @Deprecated
1128
    public double getMargin(short margin) {
1134
    public double getMargin(short margin) {
1135
        // Convert to type safe enumeration
1136
        PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin);
1137
1138
        // We check if the page margin value is null (and therefore was
1139
        // unrecognized) here instead of letting it happen in the call to the
1140
        // new method to preserve the information about what unrecognized value
1141
        // was passed to the function
1142
        if (pageMargin == null) {
1143
            throw new IllegalArgumentException("Unknown margin constant:  "
1144
                    + margin);
1145
        }
1146
1147
        return getMargin(pageMargin);
1148
    }
1149
1150
    @Override
1151
    public double getMargin(PageMargin margin) {
1129
        switch (margin) {
1152
        switch (margin) {
1130
            case FooterMargin:
1153
        case FOOTER:
1131
                return _sheet.getPageSettings().getPrintSetup().getFooterMargin();
1154
            return _sheet.getPageSettings().getPrintSetup().getFooterMargin();
1132
            case HeaderMargin:
1155
        case HEADER:
1133
                return _sheet.getPageSettings().getPrintSetup().getHeaderMargin();
1156
            return _sheet.getPageSettings().getPrintSetup().getHeaderMargin();
1134
            default:
1157
        default:
1135
                return _sheet.getPageSettings().getMargin(margin);
1158
            return _sheet.getPageSettings().getMargin(margin);
1136
        }
1159
        }
1137
    }
1160
    }
1138
1161
1139
    /**
1162
    /**
1140
     * Sets the size of the margin in inches.
1163
     * Sets the size of the margin in inches.
1141
     *
1164
     * 
1142
     * @param margin which margin to get
1165
     * @param margin
1143
     * @param size   the size of the margin
1166
     *            which margin to get
1144
     */
1167
     * @param size
1168
     *            the size of the margin
1169
     * @deprecated (type safety) Use
1170
     *             {@link HSSFSheet#setMargin(PageMargin, double)} instead
1171
     */
1172
    @Deprecated
1173
    @Override
1145
    public void setMargin(short margin, double size) {
1174
    public void setMargin(short margin, double size) {
1175
        // Convert to type safe enumeration
1176
        PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin);
1177
1178
        // We check if the page margin value is null (and therefore was
1179
        // unrecognized) here instead of letting it happen in the call to the
1180
        // new method to preserve the information about what unrecognized value
1181
        // was passed to the function
1182
        if (pageMargin == null) {
1183
            throw new IllegalArgumentException("Unknown margin constant:  "
1184
                    + margin);
1185
        }
1186
1187
        setMargin(pageMargin, size);
1188
    }
1189
1190
    @Override
1191
    public void setMargin(PageMargin margin, double size) {
1146
        switch (margin) {
1192
        switch (margin) {
1147
            case FooterMargin:
1193
        case FOOTER:
1148
                _sheet.getPageSettings().getPrintSetup().setFooterMargin(size);
1194
            _sheet.getPageSettings().getPrintSetup().setFooterMargin(size);
1149
                break;
1195
            break;
1150
            case HeaderMargin:
1196
        case HEADER:
1151
                _sheet.getPageSettings().getPrintSetup().setHeaderMargin(size);
1197
            _sheet.getPageSettings().getPrintSetup().setHeaderMargin(size);
1152
                break;
1198
            break;
1153
            default:
1199
        default:
1154
                _sheet.getPageSettings().setMargin(margin, size);
1200
            _sheet.getPageSettings().setMargin(margin, size);
1155
        }
1201
        }
1156
    }
1202
    }
1157
1203
(-)a/src/java/org/apache/poi/ss/usermodel/PageMargin.java (+91 lines)
Line 0 Link Here
1
package org.apache.poi.ss.usermodel;
2
3
import java.util.HashMap;
4
import java.util.Map;
5
6
/**
7
 * Enumeration which represents the various margins which are present within an
8
 * Excel worksheet
9
 * 
10
 * <p>
11
 * Page margins are relevant when printing worksheets, and define the amount of
12
 * empty space on the edges of each printed page
13
 * </p>
14
 */
15
public enum PageMargin {
16
17
    /**
18
     * Left margin, the empty space on the left of displayed worksheet data when
19
     * printing
20
     */
21
    LEFT((short) 0),
22
23
    /**
24
     * Right margin, the empty space on the right of displayed worksheet data
25
     * when printing
26
     */
27
    RIGHT((short) 1),
28
29
    /**
30
     * Top margin, the empty space on the top of displayed worksheet data when
31
     * printing
32
     */
33
    TOP((short) 2),
34
35
    /**
36
     * Bottom margin, the empty space on the bottom of displayed worksheet data
37
     * when printing
38
     */
39
    BOTTOM((short) 3),
40
41
    /**
42
     * Header margin, the empty space between the header and the top of the page
43
     * when printing
44
     */
45
    HEADER((short) 4),
46
47
    /**
48
     * Footer margin, the empty space between the footer and the bottom of the
49
     * page when printing
50
     */
51
    FOOTER((short) 5);
52
53
    /**
54
     * Map relating the old API constant values to their corresponding
55
     * enumeration value
56
     */
57
    private static final Map<Short, PageMargin> PAGE_MARGIN_BY_LEGACY_API_VALUE;
58
59
    /** The old API constant value which corresponded to this page margin */
60
    private final short legacyApiValue;
61
62
    static {
63
        PAGE_MARGIN_BY_LEGACY_API_VALUE = new HashMap<Short, PageMargin>();
64
65
        for (PageMargin margin : values()) {
66
            PAGE_MARGIN_BY_LEGACY_API_VALUE.put(margin.legacyApiValue, margin);
67
        }
68
    }
69
70
    /**
71
     * @param legacyApiValue
72
     *            The old API constant value which corresponded to this page
73
     *            margin
74
     */
75
    private PageMargin(short legacyApiValue) {
76
        this.legacyApiValue = legacyApiValue;
77
    }
78
79
    /**
80
     * Retrieves the enumeration value which corresponds to a legacy API
81
     * constant value
82
     * 
83
     * @param legacyApiValue
84
     *            An old API margin constant value
85
     * @return The PageMargin enumeration which corresponds to the given value,
86
     *         or null if no corresponding value exists
87
     */
88
    public static PageMargin getByLegacyApiValue(short legacyApiValue) {
89
        return PAGE_MARGIN_BY_LEGACY_API_VALUE.get(legacyApiValue);
90
    }
91
}
(-)a/src/java/org/apache/poi/ss/usermodel/Sheet.java (-6 / +65 lines)
Lines 34-49 import org.apache.poi.ss.util.CellRangeAddress; Link Here
34
public interface Sheet extends Iterable<Row> {
34
public interface Sheet extends Iterable<Row> {
35
35
36
    /* Constants for margins */
36
    /* Constants for margins */
37
    /**
38
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#LEFT}
39
     *             instead
40
     */
41
    @Deprecated
37
    public static final short LeftMargin = 0;
42
    public static final short LeftMargin = 0;
38
43
44
    /**
45
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#RIGHT}
46
     *             instead
47
     */
48
    @Deprecated
39
    public static final short RightMargin = 1;
49
    public static final short RightMargin = 1;
40
50
51
    /**
52
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#TOP}
53
     *             instead
54
     */
55
    @Deprecated
41
    public static final short TopMargin = 2;
56
    public static final short TopMargin = 2;
42
57
58
    /**
59
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#BOTTOM}
60
     *             instead
61
     */
62
    @Deprecated
43
    public static final short BottomMargin = 3;
63
    public static final short BottomMargin = 3;
44
64
65
    /**
66
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#HEADER}
67
     *             instead
68
     */
69
    @Deprecated
45
    public static final short HeaderMargin = 4;
70
    public static final short HeaderMargin = 4;
46
71
72
    /**
73
     * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#FOOTER}
74
     *             instead
75
     */
76
    @Deprecated
47
    public static final short FooterMargin = 5;
77
    public static final short FooterMargin = 5;
48
78
49
    public static final byte PANE_LOWER_RIGHT = (byte) 0;
79
    public static final byte PANE_LOWER_RIGHT = (byte) 0;
Lines 515-535 public interface Sheet extends Iterable<Row> { Link Here
515
545
516
    /**
546
    /**
517
     * Gets the size of the margin in inches.
547
     * Gets the size of the margin in inches.
518
     *
548
     * 
519
     * @param margin which margin to get
549
     * @param margin
550
     *            which margin to get
520
     * @return the size of the margin
551
     * @return the size of the margin
552
     * @deprecated (type safety) Use {@link Sheet#getMargin(PageMargin)} instead
521
     */
553
     */
554
    @Deprecated
522
    double getMargin(short margin);
555
    double getMargin(short margin);
523
556
524
    /**
557
    /**
525
     * Sets the size of the margin in inches.
558
     * Sets the size of the margin in inches.
526
     *
559
     * 
527
     * @param margin which margin to get
560
     * @param margin
528
     * @param size the size of the margin
561
     *            which margin to get
529
     */
562
     * @param size
563
     *            the size of the margin
564
     * @deprecated (type safety) Use {@link Sheet#setMargin(PageMargin, double)}
565
     *             instead
566
     */
567
    @Deprecated
530
    void setMargin(short margin, double size);
568
    void setMargin(short margin, double size);
531
569
532
    /**
570
    /**
571
     * Gets the size of the page margin in inches
572
     * 
573
     * @param margin
574
     *            The page margin to get
575
     * @return The size of the margin in inches
576
     * @see org.apache.poi.ss.usermodel.PageMargin
577
     */
578
    double getMargin(PageMargin margin);
579
580
    /**
581
     * Sets the size of the page margin in inches
582
     * 
583
     * @param margin
584
     *            The page margin to set
585
     * @param size
586
     *            The size to set the page margin to, in inches
587
     * @see org.apache.poi.ss.usermodel.PageMargin
588
     */
589
    void setMargin(PageMargin margin, double size);
590
591
    /**
533
     * Answer whether protection is enabled or disabled
592
     * Answer whether protection is enabled or disabled
534
     *
593
     *
535
     * @return true => protection enabled; false => protection disabled
594
     * @return true => protection enabled; false => protection disabled
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (-3 / +25 lines)
Lines 35-40 import org.apache.poi.ss.usermodel.DataValidationHelper; Link Here
35
import org.apache.poi.ss.usermodel.Drawing;
35
import org.apache.poi.ss.usermodel.Drawing;
36
import org.apache.poi.ss.usermodel.Footer;
36
import org.apache.poi.ss.usermodel.Footer;
37
import org.apache.poi.ss.usermodel.Header;
37
import org.apache.poi.ss.usermodel.Header;
38
import org.apache.poi.ss.usermodel.PageMargin;
38
import org.apache.poi.ss.usermodel.PrintSetup;
39
import org.apache.poi.ss.usermodel.PrintSetup;
39
import org.apache.poi.ss.usermodel.Row;
40
import org.apache.poi.ss.usermodel.Row;
40
import org.apache.poi.ss.usermodel.Sheet;
41
import org.apache.poi.ss.usermodel.Sheet;
Lines 688-712 public class SXSSFSheet implements Sheet, Cloneable Link Here
688
    /**
689
    /**
689
     * Gets the size of the margin in inches.
690
     * Gets the size of the margin in inches.
690
     *
691
     *
691
     * @param margin which margin to get
692
     * @param margin
693
     *            which margin to get
692
     * @return the size of the margin
694
     * @return the size of the margin
695
     * @deprecated (type safety) Use {@link SXSSFSheet#getMargin(PageMargin)}
696
     *             instead
693
     */
697
     */
698
    @Override
699
    @Deprecated
694
    public double getMargin(short margin)
700
    public double getMargin(short margin)
695
    {
701
    {
696
        return _sh.getMargin(margin);
702
        return _sh.getMargin(margin);
697
    }
703
    }
698
704
705
    @Override
706
    public double getMargin(PageMargin margin) {
707
        return _sh.getMargin(margin);
708
    }
709
699
    /**
710
    /**
700
     * Sets the size of the margin in inches.
711
     * Sets the size of the margin in inches.
701
     *
712
     *
702
     * @param margin which margin to get
713
     * @param margin
703
     * @param size the size of the margin
714
     *            which margin to get
715
     * @param size
716
     *            the size of the margin
717
     * @deprecated (type safety) Use
718
     *             {@link SXSSFSheet#setMargin(PageMargin, double)} instead
704
     */
719
     */
720
    @Override
721
    @Deprecated
705
    public void setMargin(short margin, double size)
722
    public void setMargin(short margin, double size)
706
    {
723
    {
707
        _sh.setMargin(margin,size);
724
        _sh.setMargin(margin,size);
708
    }
725
    }
709
726
727
    @Override
728
    public void setMargin(PageMargin margin, double size) {
729
        _sh.setMargin(margin, size);
730
    }
731
710
    /**
732
    /**
711
     * Answer whether protection is enabled or disabled
733
     * Answer whether protection is enabled or disabled
712
     *
734
     *
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (-42 / +96 lines)
Lines 875-882 public class XSSFSheet extends POIXMLDocumentPart implements Sheet { Link Here
875
875
876
    /**
876
    /**
877
     * Gets the size of the margin in inches.
877
     * Gets the size of the margin in inches.
878
     *
878
     * 
879
     * @param margin which margin to get
879
     * @param margin
880
     *            which margin to get
880
     * @return the size of the margin
881
     * @return the size of the margin
881
     * @see Sheet#LeftMargin
882
     * @see Sheet#LeftMargin
882
     * @see Sheet#RightMargin
883
     * @see Sheet#RightMargin
Lines 884-948 public class XSSFSheet extends POIXMLDocumentPart implements Sheet { Link Here
884
     * @see Sheet#BottomMargin
885
     * @see Sheet#BottomMargin
885
     * @see Sheet#HeaderMargin
886
     * @see Sheet#HeaderMargin
886
     * @see Sheet#FooterMargin
887
     * @see Sheet#FooterMargin
888
     * @deprecated (type safety) Use {@link XSSFSheet#getMargin(PageMargin)}
889
     *             instead
887
     */
890
     */
891
    @Deprecated
892
    @Override
888
    public double getMargin(short margin) {
893
    public double getMargin(short margin) {
889
        if (!worksheet.isSetPageMargins()) return 0;
894
        if (!worksheet.isSetPageMargins()) {
895
            return 0;
896
        }
897
898
        // Convert to type safe enumeration
899
        PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin);
900
901
        // We check if the page margin value is null (and therefore was
902
        // unrecognized) here instead of letting it happen in the call to the
903
        // new method to preserve the information about what unrecognized value
904
        // was passed to the function
905
        if (pageMargin == null) {
906
            throw new IllegalArgumentException("Unknown margin constant:  "
907
                    + margin);
908
        }
909
910
        return getMargin(pageMargin);
911
    }
912
913
    @Override
914
    public double getMargin(PageMargin margin) {
915
        if (!worksheet.isSetPageMargins()) {
916
            return 0;
917
        }
890
918
891
        CTPageMargins pageMargins = worksheet.getPageMargins();
919
        CTPageMargins pageMargins = worksheet.getPageMargins();
892
        switch (margin) {
920
        switch (margin) {
893
            case LeftMargin:
921
        case LEFT:
894
                return pageMargins.getLeft();
922
            return pageMargins.getLeft();
895
            case RightMargin:
923
        case RIGHT:
896
                return pageMargins.getRight();
924
            return pageMargins.getRight();
897
            case TopMargin:
925
        case TOP:
898
                return pageMargins.getTop();
926
            return pageMargins.getTop();
899
            case BottomMargin:
927
        case BOTTOM:
900
                return pageMargins.getBottom();
928
            return pageMargins.getBottom();
901
            case HeaderMargin:
929
        case HEADER:
902
                return pageMargins.getHeader();
930
            return pageMargins.getHeader();
903
            case FooterMargin:
931
        case FOOTER:
904
                return pageMargins.getFooter();
932
            return pageMargins.getFooter();
905
            default :
933
        default:
906
                throw new IllegalArgumentException("Unknown margin constant:  " + margin);
934
            throw new IllegalArgumentException("Unknown margin constant:  "
935
                    + margin);
907
        }
936
        }
908
    }
937
    }
909
938
910
    /**
939
    /**
911
     * Sets the size of the margin in inches.
940
     * Sets the size of the margin in inches.
912
     *
941
     * 
913
     * @param margin which margin to get
942
     * @param margin
914
     * @param size the size of the margin
943
     *            which margin to get
944
     * @param size
945
     *            the size of the margin
915
     * @see Sheet#LeftMargin
946
     * @see Sheet#LeftMargin
916
     * @see Sheet#RightMargin
947
     * @see Sheet#RightMargin
917
     * @see Sheet#TopMargin
948
     * @see Sheet#TopMargin
918
     * @see Sheet#BottomMargin
949
     * @see Sheet#BottomMargin
919
     * @see Sheet#HeaderMargin
950
     * @see Sheet#HeaderMargin
920
     * @see Sheet#FooterMargin
951
     * @see Sheet#FooterMargin
952
     * @deprecated (type safety) Use
953
     *             {@link XSSFSheet#setMargin(PageMargin, double)} instead
921
     */
954
     */
955
    @Deprecated
956
    @Override
922
    public void setMargin(short margin, double size) {
957
    public void setMargin(short margin, double size) {
923
        CTPageMargins pageMargins = worksheet.isSetPageMargins() ?
958
        // Convert to type safe enumeration
924
                worksheet.getPageMargins() : worksheet.addNewPageMargins();
959
        PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin);
960
961
        // We check if the page margin value is null (and therefore was
962
        // unrecognized) here instead of letting it happen in the call to the
963
        // new method to preserve the information about what unrecognized value
964
        // was passed to the function
965
        if (pageMargin == null) {
966
            throw new IllegalArgumentException("Unknown margin constant:  "
967
                    + margin);
968
        }
969
970
        setMargin(pageMargin, size);
971
    }
972
973
    @Override
974
    public void setMargin(PageMargin margin, double size) {
975
        CTPageMargins pageMargins = worksheet.isSetPageMargins() ? worksheet
976
                .getPageMargins() : worksheet.addNewPageMargins();
977
925
        switch (margin) {
978
        switch (margin) {
926
            case LeftMargin:
979
        case LEFT:
927
                pageMargins.setLeft(size);
980
            pageMargins.setLeft(size);
928
                break;
981
            break;
929
            case RightMargin:
982
        case RIGHT:
930
                pageMargins.setRight(size);
983
            pageMargins.setRight(size);
931
                break;
984
            break;
932
            case TopMargin:
985
        case TOP:
933
                pageMargins.setTop(size);
986
            pageMargins.setTop(size);
934
                break;
987
            break;
935
            case BottomMargin:
988
        case BOTTOM:
936
                pageMargins.setBottom(size);
989
            pageMargins.setBottom(size);
937
                break;
990
            break;
938
            case HeaderMargin:
991
        case HEADER:
939
                pageMargins.setHeader(size);
992
            pageMargins.setHeader(size);
940
                break;
993
            break;
941
            case FooterMargin:
994
        case FOOTER:
942
                pageMargins.setFooter(size);
995
            pageMargins.setFooter(size);
943
                break;
996
            break;
944
            default :
997
        default:
945
                throw new IllegalArgumentException( "Unknown margin constant:  " + margin );
998
            throw new IllegalArgumentException("Unknown margin constant:  "
999
                    + margin);
946
        }
1000
        }
947
    }
1001
    }
948
1002
(-)a/src/testcases/org/apache/poi/hssf/model/TestSheet.java (+22 lines)
Lines 32-37 import org.apache.poi.hssf.usermodel.HSSFSheet; Link Here
32
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
32
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
33
import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector;
33
import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector;
34
import org.apache.poi.ss.formula.FormulaShifter;
34
import org.apache.poi.ss.formula.FormulaShifter;
35
import org.apache.poi.ss.usermodel.PageMargin;
35
import org.apache.poi.ss.util.CellRangeAddress;
36
import org.apache.poi.ss.util.CellRangeAddress;
36
import org.apache.poi.util.HexRead;
37
import org.apache.poi.util.HexRead;
37
38
Lines 578-583 public final class TestSheet extends TestCase { Link Here
578
		}
579
		}
579
	}
580
	}
580
581
582
    /**
583
     * In 3.1, setting margins between creating first row and first cell caused
584
     * an exception.
585
     */
586
    public void testSetMargins_bug45717_typesafemargin() {
587
        HSSFWorkbook workbook = new HSSFWorkbook();
588
        HSSFSheet sheet = workbook.createSheet("Vorschauliste");
589
        HSSFRow row = sheet.createRow(0);
590
591
        sheet.setMargin(PageMargin.LEFT, 0.3);
592
        try {
593
            row.createCell(0);
594
        } catch (IllegalStateException e) {
595
            if (e.getMessage().equals(
596
                    "Cannot create value records before row records exist")) {
597
                throw new AssertionFailedError("Identified bug 45717");
598
            }
599
            throw e;
600
        }
601
    }
602
581
	/**
603
	/**
582
	 * Some apps seem to write files with missing DIMENSION records.
604
	 * Some apps seem to write files with missing DIMENSION records.
583
	 * Excel(2007) tolerates this, so POI should too.
605
	 * Excel(2007) tolerates this, so POI should too.
(-)a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java (-1 / +39 lines)
Lines 571-576 public abstract class BaseTestSheet extends TestCase { Link Here
571
        }
571
        }
572
    }
572
    }
573
573
574
    public void baseTestGetSetMarginTypeSafeMarginEnumeration(
575
            double[] defaultMargins) {
576
        double marginLeft = defaultMargins[0];
577
        double marginRight = defaultMargins[1];
578
        double marginTop = defaultMargins[2];
579
        double marginBottom = defaultMargins[3];
580
        double marginHeader = defaultMargins[4];
581
        double marginFooter = defaultMargins[5];
582
583
        Workbook workbook = _testDataProvider.createWorkbook();
584
        Sheet sheet = workbook.createSheet("Sheet 1");
585
        assertEquals(marginLeft, sheet.getMargin(PageMargin.LEFT), 0.0);
586
        sheet.setMargin(PageMargin.LEFT, 10.0);
587
        // left margin is custom, all others are default
588
        assertEquals(10.0, sheet.getMargin(PageMargin.LEFT), 0.0);
589
        assertEquals(marginRight, sheet.getMargin(PageMargin.RIGHT), 0.0);
590
        assertEquals(marginTop, sheet.getMargin(PageMargin.TOP), 0.0);
591
        assertEquals(marginBottom, sheet.getMargin(PageMargin.BOTTOM), 0.0);
592
        sheet.setMargin(PageMargin.RIGHT, 11.0);
593
        assertEquals(11.0, sheet.getMargin(PageMargin.RIGHT), 0.0);
594
        sheet.setMargin(PageMargin.TOP, 12.0);
595
        assertEquals(12.0, sheet.getMargin(PageMargin.TOP), 0.0);
596
        sheet.setMargin(PageMargin.BOTTOM, 13.0);
597
        assertEquals(13.0, sheet.getMargin(PageMargin.BOTTOM), 0.0);
598
599
        sheet.setMargin(PageMargin.FOOTER, 5.6);
600
        assertEquals(5.6, sheet.getMargin(PageMargin.FOOTER), 0.0);
601
        sheet.setMargin(PageMargin.HEADER, 11.5);
602
        assertEquals(11.5, sheet.getMargin(PageMargin.HEADER), 0.0);
603
604
        // incorrect margin constant
605
        try {
606
            sheet.setMargin(null, 15);
607
            fail("Expected exception");
608
        } catch (IllegalArgumentException e) {
609
            assertEquals("Unknown margin constant:  65", e.getMessage());
610
        }
611
    }
612
574
    public void testRowBreaks() {
613
    public void testRowBreaks() {
575
        Workbook workbook = _testDataProvider.createWorkbook();
614
        Workbook workbook = _testDataProvider.createWorkbook();
576
        Sheet sheet = workbook.createSheet();
615
        Sheet sheet = workbook.createSheet();
577
- 

Return to bug 55330