ASF Bugzilla – Attachment 30653 Details for
Bug 55330
[PATCH] add PageMargin enum
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Page Margins Enumeration
Bug-55330-Converted-page-margin-set-and-get.patch (text/plain), 29.36 KB, created by
Ryan O'Meara
on 2013-08-01 05:41:51 UTC
(
hide
)
Description:
Page Margins Enumeration
Filename:
MIME Type:
Creator:
Ryan O'Meara
Created:
2013-08-01 05:41:51 UTC
Size:
29.36 KB
patch
obsolete
>From 37fd1af77e1aa90f2873ae9b7daf646a81922a51 Mon Sep 17 00:00:00 2001 >From: Ryan OMeara <romeara@blackducksoftware.com> >Date: Thu, 1 Aug 2013 01:29:43 -0400 >Subject: [PATCH] Bug 55330 Converted page margin set and get operations to use > enumeration instead of numeric constants. Included unit tests for new calls > >--- > .../hssf/record/aggregates/PageSettingsBlock.java | 141 +++++++++++++-------- > .../org/apache/poi/hssf/usermodel/HSSFSheet.java | 84 +++++++++--- > .../org/apache/poi/ss/usermodel/PageMargin.java | 91 +++++++++++++ > src/java/org/apache/poi/ss/usermodel/Sheet.java | 71 ++++++++++- > .../org/apache/poi/xssf/streaming/SXSSFSheet.java | 28 +++- > .../org/apache/poi/xssf/usermodel/XSSFSheet.java | 138 ++++++++++++++------ > .../org/apache/poi/hssf/model/TestSheet.java | 22 ++++ > .../org/apache/poi/ss/usermodel/BaseTestSheet.java | 39 ++++++ > 8 files changed, 493 insertions(+), 121 deletions(-) > create mode 100644 src/java/org/apache/poi/ss/usermodel/PageMargin.java > >diff --git a/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java b/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java >index c183b50..a049c83 100644 >--- a/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java >+++ b/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java >@@ -26,6 +26,7 @@ import java.util.Map; > import org.apache.poi.hssf.model.RecordStream; > import org.apache.poi.hssf.model.InternalSheet; > import org.apache.poi.hssf.record.*; >+import org.apache.poi.ss.usermodel.PageMargin; > import org.apache.poi.util.HexDump; > > /** >@@ -403,67 +404,105 @@ public final class PageSettingsBlock extends RecordAggregate { > _printSetup = newPrintSetup; > } > >- >- private Margin getMarginRec(int marginIndex) { >- switch (marginIndex) { >- case InternalSheet.LeftMargin: return _leftMargin; >- case InternalSheet.RightMargin: return _rightMargin; >- case InternalSheet.TopMargin: return _topMargin; >- case InternalSheet.BottomMargin: return _bottomMargin; >+ /** >+ * Returns one of the standard page margins >+ * >+ * @param margin >+ * One of the four standard margins: {@link PageMargin.LEFT}, >+ * {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or >+ * {@link PageMargin.BOTTOM}. Header and Footer margins are not >+ * available via this function >+ * @return The requested margin >+ */ >+ private Margin getMarginRec(PageMargin margin) { >+ switch (margin) { >+ case LEFT: >+ return _leftMargin; >+ case RIGHT: >+ return _rightMargin; >+ case TOP: >+ return _topMargin; >+ case BOTTOM: >+ return _bottomMargin; >+ default: >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); > } >- throw new IllegalArgumentException( "Unknown margin constant: " + marginIndex ); > } > > > /** >- * Gets the size of the margin in inches. >- * @param margin which margin to get >- * @return the size of the margin >+ * Retrieves the margin for one of the four standard page margins >+ * >+ * @param margin >+ * One of the four standard margins: {@link PageMargin.LEFT}, >+ * {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or >+ * {@link PageMargin.BOTTOM}. Header and Footer margins are not >+ * available via this function >+ * @return The page margin requested, in inches > */ >- public double getMargin(short margin) { >- Margin m = getMarginRec(margin); >- if (m != null) { >- return m.getMargin(); >+ public double getMargin(PageMargin margin) { >+ Margin marginData = getMarginRec(margin); >+ >+ if (marginData != null) { >+ return marginData.getMargin(); > } >+ >+ // If no margin could be retrieved, return the default value for that >+ // margin > switch (margin) { >- case InternalSheet.LeftMargin: return .75; >- case InternalSheet.RightMargin: return .75; >- case InternalSheet.TopMargin: return 1.0; >- case InternalSheet.BottomMargin: return 1.0; >+ case LEFT: >+ return 0.75; >+ case RIGHT: >+ return 0.75; >+ case TOP: >+ return 1.0; >+ case BOTTOM: >+ return 1.0; >+ default: >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); > } >- throw new IllegalArgumentException( "Unknown margin constant: " + margin ); >- } >- >- /** >- * Sets the size of the margin in inches. >- * @param margin which margin to get >- * @param size the size of the margin >- */ >- public void setMargin(short margin, double size) { >- Margin m = getMarginRec(margin); >- if (m == null) { >- switch (margin) { >- case InternalSheet.LeftMargin: >- _leftMargin = new LeftMarginRecord(); >- m = _leftMargin; >- break; >- case InternalSheet.RightMargin: >- _rightMargin = new RightMarginRecord(); >- m = _rightMargin; >- break; >- case InternalSheet.TopMargin: >- _topMargin = new TopMarginRecord(); >- m = _topMargin; >- break; >- case InternalSheet.BottomMargin: >- _bottomMargin = new BottomMarginRecord(); >- m = _bottomMargin; >- break; >- default : >- throw new IllegalArgumentException( "Unknown margin constant: " + margin ); >- } >+ } >+ >+ /** >+ * Sets the size of one of the four standard page margins in inches >+ * >+ * @param margin >+ * One of the four standard margins: {@link PageMargin.LEFT}, >+ * {@link PageMargin.RIGHT}, {@link PageMargin.TOP}, or >+ * {@link PageMargin.BOTTOM}. Header and Footer margins are not >+ * available via this function >+ * @param size >+ * The size to set the margin to, in inches >+ */ >+ public void setMargin(PageMargin margin, double size) { >+ Margin marginData = getMarginRec(margin); >+ >+ // If no data exists, create it >+ switch (margin) { >+ case LEFT: >+ _leftMargin = new LeftMarginRecord(); >+ marginData = _leftMargin; >+ break; >+ case RIGHT: >+ _rightMargin = new RightMarginRecord(); >+ marginData = _rightMargin; >+ break; >+ case TOP: >+ _topMargin = new TopMarginRecord(); >+ marginData = _topMargin; >+ break; >+ case BOTTOM: >+ _bottomMargin = new BottomMarginRecord(); >+ marginData = _bottomMargin; >+ break; >+ default: >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); > } >- m.setMargin( size ); >+ >+ marginData.setMargin(size); > } > > /** >diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java >index 76008dd..564aa91 100644 >--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java >+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java >@@ -45,6 +45,7 @@ import org.apache.poi.ss.usermodel.CellRange; > import org.apache.poi.ss.usermodel.CellStyle; > import org.apache.poi.ss.usermodel.DataValidation; > import org.apache.poi.ss.usermodel.DataValidationHelper; >+import org.apache.poi.ss.usermodel.PageMargin; > import org.apache.poi.ss.usermodel.Row; > import org.apache.poi.ss.util.CellRangeAddress; > import org.apache.poi.ss.util.CellReference; >@@ -1122,36 +1123,81 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { > /** > * Gets the size of the margin in inches. > * >- * @param margin which margin to get >+ * @param margin >+ * which margin to get > * @return the size of the margin >+ * @deprecated (type safety) Use {@link HSSFSheet#getMargin(PageMargin)} >+ * instead > */ >+ @Override >+ @Deprecated > public double getMargin(short margin) { >+ // Convert to type safe enumeration >+ PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin); >+ >+ // We check if the page margin value is null (and therefore was >+ // unrecognized) here instead of letting it happen in the call to the >+ // new method to preserve the information about what unrecognized value >+ // was passed to the function >+ if (pageMargin == null) { >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); >+ } >+ >+ return getMargin(pageMargin); >+ } >+ >+ @Override >+ public double getMargin(PageMargin margin) { > switch (margin) { >- case FooterMargin: >- return _sheet.getPageSettings().getPrintSetup().getFooterMargin(); >- case HeaderMargin: >- return _sheet.getPageSettings().getPrintSetup().getHeaderMargin(); >- default: >- return _sheet.getPageSettings().getMargin(margin); >+ case FOOTER: >+ return _sheet.getPageSettings().getPrintSetup().getFooterMargin(); >+ case HEADER: >+ return _sheet.getPageSettings().getPrintSetup().getHeaderMargin(); >+ default: >+ return _sheet.getPageSettings().getMargin(margin); > } > } > > /** > * Sets the size of the margin in inches. >- * >- * @param margin which margin to get >- * @param size the size of the margin >- */ >+ * >+ * @param margin >+ * which margin to get >+ * @param size >+ * the size of the margin >+ * @deprecated (type safety) Use >+ * {@link HSSFSheet#setMargin(PageMargin, double)} instead >+ */ >+ @Deprecated >+ @Override > public void setMargin(short margin, double size) { >+ // Convert to type safe enumeration >+ PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin); >+ >+ // We check if the page margin value is null (and therefore was >+ // unrecognized) here instead of letting it happen in the call to the >+ // new method to preserve the information about what unrecognized value >+ // was passed to the function >+ if (pageMargin == null) { >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); >+ } >+ >+ setMargin(pageMargin, size); >+ } >+ >+ @Override >+ public void setMargin(PageMargin margin, double size) { > switch (margin) { >- case FooterMargin: >- _sheet.getPageSettings().getPrintSetup().setFooterMargin(size); >- break; >- case HeaderMargin: >- _sheet.getPageSettings().getPrintSetup().setHeaderMargin(size); >- break; >- default: >- _sheet.getPageSettings().setMargin(margin, size); >+ case FOOTER: >+ _sheet.getPageSettings().getPrintSetup().setFooterMargin(size); >+ break; >+ case HEADER: >+ _sheet.getPageSettings().getPrintSetup().setHeaderMargin(size); >+ break; >+ default: >+ _sheet.getPageSettings().setMargin(margin, size); > } > } > >diff --git a/src/java/org/apache/poi/ss/usermodel/PageMargin.java b/src/java/org/apache/poi/ss/usermodel/PageMargin.java >new file mode 100644 >index 0000000..7f28c78 >--- /dev/null >+++ b/src/java/org/apache/poi/ss/usermodel/PageMargin.java >@@ -0,0 +1,91 @@ >+package org.apache.poi.ss.usermodel; >+ >+import java.util.HashMap; >+import java.util.Map; >+ >+/** >+ * Enumeration which represents the various margins which are present within an >+ * Excel worksheet >+ * >+ * <p> >+ * Page margins are relevant when printing worksheets, and define the amount of >+ * empty space on the edges of each printed page >+ * </p> >+ */ >+public enum PageMargin { >+ >+ /** >+ * Left margin, the empty space on the left of displayed worksheet data when >+ * printing >+ */ >+ LEFT((short) 0), >+ >+ /** >+ * Right margin, the empty space on the right of displayed worksheet data >+ * when printing >+ */ >+ RIGHT((short) 1), >+ >+ /** >+ * Top margin, the empty space on the top of displayed worksheet data when >+ * printing >+ */ >+ TOP((short) 2), >+ >+ /** >+ * Bottom margin, the empty space on the bottom of displayed worksheet data >+ * when printing >+ */ >+ BOTTOM((short) 3), >+ >+ /** >+ * Header margin, the empty space between the header and the top of the page >+ * when printing >+ */ >+ HEADER((short) 4), >+ >+ /** >+ * Footer margin, the empty space between the footer and the bottom of the >+ * page when printing >+ */ >+ FOOTER((short) 5); >+ >+ /** >+ * Map relating the old API constant values to their corresponding >+ * enumeration value >+ */ >+ private static final Map<Short, PageMargin> PAGE_MARGIN_BY_LEGACY_API_VALUE; >+ >+ /** The old API constant value which corresponded to this page margin */ >+ private final short legacyApiValue; >+ >+ static { >+ PAGE_MARGIN_BY_LEGACY_API_VALUE = new HashMap<Short, PageMargin>(); >+ >+ for (PageMargin margin : values()) { >+ PAGE_MARGIN_BY_LEGACY_API_VALUE.put(margin.legacyApiValue, margin); >+ } >+ } >+ >+ /** >+ * @param legacyApiValue >+ * The old API constant value which corresponded to this page >+ * margin >+ */ >+ private PageMargin(short legacyApiValue) { >+ this.legacyApiValue = legacyApiValue; >+ } >+ >+ /** >+ * Retrieves the enumeration value which corresponds to a legacy API >+ * constant value >+ * >+ * @param legacyApiValue >+ * An old API margin constant value >+ * @return The PageMargin enumeration which corresponds to the given value, >+ * or null if no corresponding value exists >+ */ >+ public static PageMargin getByLegacyApiValue(short legacyApiValue) { >+ return PAGE_MARGIN_BY_LEGACY_API_VALUE.get(legacyApiValue); >+ } >+} >diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java >index 91caad0..08fce36 100644 >--- a/src/java/org/apache/poi/ss/usermodel/Sheet.java >+++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java >@@ -34,16 +34,46 @@ import org.apache.poi.ss.util.CellRangeAddress; > public interface Sheet extends Iterable<Row> { > > /* Constants for margins */ >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#LEFT} >+ * instead >+ */ >+ @Deprecated > public static final short LeftMargin = 0; > >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#RIGHT} >+ * instead >+ */ >+ @Deprecated > public static final short RightMargin = 1; > >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#TOP} >+ * instead >+ */ >+ @Deprecated > public static final short TopMargin = 2; > >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#BOTTOM} >+ * instead >+ */ >+ @Deprecated > public static final short BottomMargin = 3; > >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#HEADER} >+ * instead >+ */ >+ @Deprecated > public static final short HeaderMargin = 4; > >+ /** >+ * @deprecated Use {@link org.apache.poi.ss.usermodel.PageMargin#FOOTER} >+ * instead >+ */ >+ @Deprecated > public static final short FooterMargin = 5; > > public static final byte PANE_LOWER_RIGHT = (byte) 0; >@@ -515,21 +545,50 @@ public interface Sheet extends Iterable<Row> { > > /** > * Gets the size of the margin in inches. >- * >- * @param margin which margin to get >+ * >+ * @param margin >+ * which margin to get > * @return the size of the margin >+ * @deprecated (type safety) Use {@link Sheet#getMargin(PageMargin)} instead > */ >+ @Deprecated > double getMargin(short margin); > > /** > * Sets the size of the margin in inches. >- * >- * @param margin which margin to get >- * @param size the size of the margin >- */ >+ * >+ * @param margin >+ * which margin to get >+ * @param size >+ * the size of the margin >+ * @deprecated (type safety) Use {@link Sheet#setMargin(PageMargin, double)} >+ * instead >+ */ >+ @Deprecated > void setMargin(short margin, double size); > > /** >+ * Gets the size of the page margin in inches >+ * >+ * @param margin >+ * The page margin to get >+ * @return The size of the margin in inches >+ * @see org.apache.poi.ss.usermodel.PageMargin >+ */ >+ double getMargin(PageMargin margin); >+ >+ /** >+ * Sets the size of the page margin in inches >+ * >+ * @param margin >+ * The page margin to set >+ * @param size >+ * The size to set the page margin to, in inches >+ * @see org.apache.poi.ss.usermodel.PageMargin >+ */ >+ void setMargin(PageMargin margin, double size); >+ >+ /** > * Answer whether protection is enabled or disabled > * > * @return true => protection enabled; false => protection disabled >diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java >index b92addd..cdf6fb1 100644 >--- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java >+++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java >@@ -35,6 +35,7 @@ import org.apache.poi.ss.usermodel.DataValidationHelper; > import org.apache.poi.ss.usermodel.Drawing; > import org.apache.poi.ss.usermodel.Footer; > import org.apache.poi.ss.usermodel.Header; >+import org.apache.poi.ss.usermodel.PageMargin; > import org.apache.poi.ss.usermodel.PrintSetup; > import org.apache.poi.ss.usermodel.Row; > import org.apache.poi.ss.usermodel.Sheet; >@@ -688,25 +689,46 @@ public class SXSSFSheet implements Sheet, Cloneable > /** > * Gets the size of the margin in inches. > * >- * @param margin which margin to get >+ * @param margin >+ * which margin to get > * @return the size of the margin >+ * @deprecated (type safety) Use {@link SXSSFSheet#getMargin(PageMargin)} >+ * instead > */ >+ @Override >+ @Deprecated > public double getMargin(short margin) > { > return _sh.getMargin(margin); > } > >+ @Override >+ public double getMargin(PageMargin margin) { >+ return _sh.getMargin(margin); >+ } >+ > /** > * Sets the size of the margin in inches. > * >- * @param margin which margin to get >- * @param size the size of the margin >+ * @param margin >+ * which margin to get >+ * @param size >+ * the size of the margin >+ * @deprecated (type safety) Use >+ * {@link SXSSFSheet#setMargin(PageMargin, double)} instead > */ >+ @Override >+ @Deprecated > public void setMargin(short margin, double size) > { > _sh.setMargin(margin,size); > } > >+ @Override >+ public void setMargin(PageMargin margin, double size) { >+ _sh.setMargin(margin, size); >+ } >+ > /** > * Answer whether protection is enabled or disabled > * >diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java >index 439c269..de17ced 100644 >--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java >+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java >@@ -875,8 +875,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { > > /** > * Gets the size of the margin in inches. >- * >- * @param margin which margin to get >+ * >+ * @param margin >+ * which margin to get > * @return the size of the margin > * @see Sheet#LeftMargin > * @see Sheet#RightMargin >@@ -884,65 +885,118 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { > * @see Sheet#BottomMargin > * @see Sheet#HeaderMargin > * @see Sheet#FooterMargin >+ * @deprecated (type safety) Use {@link XSSFSheet#getMargin(PageMargin)} >+ * instead > */ >+ @Deprecated >+ @Override > public double getMargin(short margin) { >- if (!worksheet.isSetPageMargins()) return 0; >+ if (!worksheet.isSetPageMargins()) { >+ return 0; >+ } >+ >+ // Convert to type safe enumeration >+ PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin); >+ >+ // We check if the page margin value is null (and therefore was >+ // unrecognized) here instead of letting it happen in the call to the >+ // new method to preserve the information about what unrecognized value >+ // was passed to the function >+ if (pageMargin == null) { >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); >+ } >+ >+ return getMargin(pageMargin); >+ } >+ >+ @Override >+ public double getMargin(PageMargin margin) { >+ if (!worksheet.isSetPageMargins()) { >+ return 0; >+ } > > CTPageMargins pageMargins = worksheet.getPageMargins(); > switch (margin) { >- case LeftMargin: >- return pageMargins.getLeft(); >- case RightMargin: >- return pageMargins.getRight(); >- case TopMargin: >- return pageMargins.getTop(); >- case BottomMargin: >- return pageMargins.getBottom(); >- case HeaderMargin: >- return pageMargins.getHeader(); >- case FooterMargin: >- return pageMargins.getFooter(); >- default : >- throw new IllegalArgumentException("Unknown margin constant: " + margin); >+ case LEFT: >+ return pageMargins.getLeft(); >+ case RIGHT: >+ return pageMargins.getRight(); >+ case TOP: >+ return pageMargins.getTop(); >+ case BOTTOM: >+ return pageMargins.getBottom(); >+ case HEADER: >+ return pageMargins.getHeader(); >+ case FOOTER: >+ return pageMargins.getFooter(); >+ default: >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); > } > } > > /** > * Sets the size of the margin in inches. >- * >- * @param margin which margin to get >- * @param size the size of the margin >+ * >+ * @param margin >+ * which margin to get >+ * @param size >+ * the size of the margin > * @see Sheet#LeftMargin > * @see Sheet#RightMargin > * @see Sheet#TopMargin > * @see Sheet#BottomMargin > * @see Sheet#HeaderMargin > * @see Sheet#FooterMargin >+ * @deprecated (type safety) Use >+ * {@link XSSFSheet#setMargin(PageMargin, double)} instead > */ >+ @Deprecated >+ @Override > public void setMargin(short margin, double size) { >- CTPageMargins pageMargins = worksheet.isSetPageMargins() ? >- worksheet.getPageMargins() : worksheet.addNewPageMargins(); >+ // Convert to type safe enumeration >+ PageMargin pageMargin = PageMargin.getByLegacyApiValue(margin); >+ >+ // We check if the page margin value is null (and therefore was >+ // unrecognized) here instead of letting it happen in the call to the >+ // new method to preserve the information about what unrecognized value >+ // was passed to the function >+ if (pageMargin == null) { >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); >+ } >+ >+ setMargin(pageMargin, size); >+ } >+ >+ @Override >+ public void setMargin(PageMargin margin, double size) { >+ CTPageMargins pageMargins = worksheet.isSetPageMargins() ? worksheet >+ .getPageMargins() : worksheet.addNewPageMargins(); >+ > switch (margin) { >- case LeftMargin: >- pageMargins.setLeft(size); >- break; >- case RightMargin: >- pageMargins.setRight(size); >- break; >- case TopMargin: >- pageMargins.setTop(size); >- break; >- case BottomMargin: >- pageMargins.setBottom(size); >- break; >- case HeaderMargin: >- pageMargins.setHeader(size); >- break; >- case FooterMargin: >- pageMargins.setFooter(size); >- break; >- default : >- throw new IllegalArgumentException( "Unknown margin constant: " + margin ); >+ case LEFT: >+ pageMargins.setLeft(size); >+ break; >+ case RIGHT: >+ pageMargins.setRight(size); >+ break; >+ case TOP: >+ pageMargins.setTop(size); >+ break; >+ case BOTTOM: >+ pageMargins.setBottom(size); >+ break; >+ case HEADER: >+ pageMargins.setHeader(size); >+ break; >+ case FOOTER: >+ pageMargins.setFooter(size); >+ break; >+ default: >+ throw new IllegalArgumentException("Unknown margin constant: " >+ + margin); > } > } > >diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheet.java b/src/testcases/org/apache/poi/hssf/model/TestSheet.java >index 57ccf1f..1209030 100644 >--- a/src/testcases/org/apache/poi/hssf/model/TestSheet.java >+++ b/src/testcases/org/apache/poi/hssf/model/TestSheet.java >@@ -32,6 +32,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet; > import org.apache.poi.hssf.usermodel.HSSFWorkbook; > import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector; > import org.apache.poi.ss.formula.FormulaShifter; >+import org.apache.poi.ss.usermodel.PageMargin; > import org.apache.poi.ss.util.CellRangeAddress; > import org.apache.poi.util.HexRead; > >@@ -578,6 +579,27 @@ public final class TestSheet extends TestCase { > } > } > >+ /** >+ * In 3.1, setting margins between creating first row and first cell caused >+ * an exception. >+ */ >+ public void testSetMargins_bug45717_typesafemargin() { >+ HSSFWorkbook workbook = new HSSFWorkbook(); >+ HSSFSheet sheet = workbook.createSheet("Vorschauliste"); >+ HSSFRow row = sheet.createRow(0); >+ >+ sheet.setMargin(PageMargin.LEFT, 0.3); >+ try { >+ row.createCell(0); >+ } catch (IllegalStateException e) { >+ if (e.getMessage().equals( >+ "Cannot create value records before row records exist")) { >+ throw new AssertionFailedError("Identified bug 45717"); >+ } >+ throw e; >+ } >+ } >+ > /** > * Some apps seem to write files with missing DIMENSION records. > * Excel(2007) tolerates this, so POI should too. >diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java >index d879e1b..30b32e0 100644 >--- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java >+++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java >@@ -571,6 +571,45 @@ public abstract class BaseTestSheet extends TestCase { > } > } > >+ public void baseTestGetSetMarginTypeSafeMarginEnumeration( >+ double[] defaultMargins) { >+ double marginLeft = defaultMargins[0]; >+ double marginRight = defaultMargins[1]; >+ double marginTop = defaultMargins[2]; >+ double marginBottom = defaultMargins[3]; >+ double marginHeader = defaultMargins[4]; >+ double marginFooter = defaultMargins[5]; >+ >+ Workbook workbook = _testDataProvider.createWorkbook(); >+ Sheet sheet = workbook.createSheet("Sheet 1"); >+ assertEquals(marginLeft, sheet.getMargin(PageMargin.LEFT), 0.0); >+ sheet.setMargin(PageMargin.LEFT, 10.0); >+ // left margin is custom, all others are default >+ assertEquals(10.0, sheet.getMargin(PageMargin.LEFT), 0.0); >+ assertEquals(marginRight, sheet.getMargin(PageMargin.RIGHT), 0.0); >+ assertEquals(marginTop, sheet.getMargin(PageMargin.TOP), 0.0); >+ assertEquals(marginBottom, sheet.getMargin(PageMargin.BOTTOM), 0.0); >+ sheet.setMargin(PageMargin.RIGHT, 11.0); >+ assertEquals(11.0, sheet.getMargin(PageMargin.RIGHT), 0.0); >+ sheet.setMargin(PageMargin.TOP, 12.0); >+ assertEquals(12.0, sheet.getMargin(PageMargin.TOP), 0.0); >+ sheet.setMargin(PageMargin.BOTTOM, 13.0); >+ assertEquals(13.0, sheet.getMargin(PageMargin.BOTTOM), 0.0); >+ >+ sheet.setMargin(PageMargin.FOOTER, 5.6); >+ assertEquals(5.6, sheet.getMargin(PageMargin.FOOTER), 0.0); >+ sheet.setMargin(PageMargin.HEADER, 11.5); >+ assertEquals(11.5, sheet.getMargin(PageMargin.HEADER), 0.0); >+ >+ // incorrect margin constant >+ try { >+ sheet.setMargin(null, 15); >+ fail("Expected exception"); >+ } catch (IllegalArgumentException e) { >+ assertEquals("Unknown margin constant: 65", e.getMessage()); >+ } >+ } >+ > public void testRowBreaks() { > Workbook workbook = _testDataProvider.createWorkbook(); > Sheet sheet = workbook.createSheet(); >-- >1.8.1.msysgit.1 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 55330
: 30653