diff options
author | PJ Fanning <fanningpj@apache.org> | 2018-02-20 00:25:43 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2018-02-20 00:25:43 +0000 |
commit | 05b0de574cce4c56fc862737242846df4abc2e6e (patch) | |
tree | d63312db51c79e1cc39a0ece41bcc59669ab164e /src | |
parent | 61b0d86a2d0c1444b77fda00d2450cf289322300 (diff) | |
download | poi-05b0de574cce4c56fc862737242846df4abc2e6e.tar.gz poi-05b0de574cce4c56fc862737242846df4abc2e6e.zip |
[bug-62018] use ints to index fonts
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824826 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
20 files changed, 338 insertions, 238 deletions
diff --git a/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java b/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java index 41de3d77c4..f80b073a0e 100644 --- a/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java +++ b/src/examples/src/org/apache/poi/hssf/view/SVTableCellEditor.java @@ -40,173 +40,176 @@ import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** * Sheet Viewer Table Cell Editor -- not commented via javadoc as it * nearly completely consists of overridden methods. * - * @author Jason Height + * @author Jason Height */ public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { - private static final Color black = getAWTColor(HSSFColorPredefined.BLACK); - private static final Color white = getAWTColor(HSSFColorPredefined.WHITE); - - private HSSFWorkbook wb; - private JTextField editor; - - public SVTableCellEditor(HSSFWorkbook wb) { - this.wb = wb; - this.editor = new JTextField(); - } - - - /** - * Gets the cellEditable attribute of the SVTableCellEditor object - * - * @return The cellEditable value - */ - @Override - public boolean isCellEditable(java.util.EventObject e) { - if (e instanceof MouseEvent) { - return ((MouseEvent) e).getClickCount() >= 2; + private static final Color black = getAWTColor(HSSFColorPredefined.BLACK); + private static final Color white = getAWTColor(HSSFColorPredefined.WHITE); + private static final POILogger logger = POILogFactory.getLogger(SVTableCellEditor.class); + + private HSSFWorkbook wb; + private JTextField editor; + + public SVTableCellEditor(HSSFWorkbook wb) { + this.wb = wb; + this.editor = new JTextField(); + } + + + /** + * Gets the cellEditable attribute of the SVTableCellEditor object + * + * @return The cellEditable value + */ + @Override + public boolean isCellEditable(java.util.EventObject e) { + if (e instanceof MouseEvent) { + return ((MouseEvent) e).getClickCount() >= 2; + } + return false; + } + + + @Override + public boolean shouldSelectCell(EventObject anEvent) { + return true; + } + + + public boolean startCellEditing(EventObject anEvent) { + logger.log(POILogger.INFO, "Start Cell Editing"); + return true; + } + + + @Override + public boolean stopCellEditing() { + logger.log(POILogger.INFO, "Stop Cell Editing"); + fireEditingStopped(); + return true; + } + + + @Override + public void cancelCellEditing() { + logger.log(POILogger.INFO, "Cancel Cell Editing"); + fireEditingCanceled(); + } + + + @Override + public void actionPerformed(ActionEvent e) { + logger.log(POILogger.INFO, "Action performed"); + stopCellEditing(); + } + + + /** + * Gets the cellEditorValue attribute of the SVTableCellEditor object + * + * @return The cellEditorValue value + */ + @Override + public Object getCellEditorValue() { + logger.log(POILogger.INFO, "GetCellEditorValue"); + //JMH Look at when this method is called. Should it return a HSSFCell? + return editor.getText(); } - return false; - } - - - @Override - public boolean shouldSelectCell(EventObject anEvent) { - return true; - } - - - public boolean startCellEditing(EventObject anEvent) { - System.out.println("Start Cell Editing"); - return true; - } - - - @Override - public boolean stopCellEditing() { - System.out.println("Stop Cell Editing"); - fireEditingStopped(); - return true; - } - - - @Override - public void cancelCellEditing() { - System.out.println("Cancel Cell Editing"); - fireEditingCanceled(); - } - - - @Override - public void actionPerformed(ActionEvent e) { - System.out.println("Action performed"); - stopCellEditing(); - } - - - /** - * Gets the cellEditorValue attribute of the SVTableCellEditor object - * - * @return The cellEditorValue value - */ - @Override - public Object getCellEditorValue() { - System.out.println("GetCellEditorValue"); - //JMH Look at when this method is called. Should it return a HSSFCell? - return editor.getText(); - } - - - /** - * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object - * - * @return The tableCellEditorComponent value - */ - @Override - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, - int row, - int column) { - System.out.println("GetTableCellEditorComponent"); - HSSFCell cell = (HSSFCell) value; - if (cell != null) { - HSSFCellStyle style = cell.getCellStyle(); - HSSFFont f = wb.getFontAt(style.getFontIndex()); - boolean isbold = f.getBold(); - boolean isitalics = f.getItalic(); - - int fontstyle = Font.PLAIN; - - if (isbold) { - fontstyle = Font.BOLD; - } - if (isitalics) { - fontstyle = fontstyle | Font.ITALIC; - } - - int fontheight = f.getFontHeightInPoints(); - if (fontheight == 9) { - fontheight = 10; //fix for stupid ol Windows - } - - Font font = new Font(f.getFontName(),fontstyle,fontheight); - editor.setFont(font); - - if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) { - editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); - } else { - editor.setBackground(white); - } - - editor.setForeground(getAWTColor(f.getColor(), black)); - - - //Set the value that is rendered for the cell - switch (cell.getCellType()) { - case BLANK: - editor.setText(""); - break; - case BOOLEAN: - if (cell.getBooleanCellValue()) { - editor.setText("true"); - } else { - editor.setText("false"); - } - break; - case NUMERIC: - editor.setText(Double.toString(cell.getNumericCellValue())); - break; - case STRING: - editor.setText(cell.getRichStringCellValue().getString()); - break; - case FORMULA: - default: - editor.setText("?"); - } - switch (style.getAlignment()) { - case LEFT: - case JUSTIFY: - case FILL: - editor.setHorizontalAlignment(SwingConstants.LEFT); - break; - case CENTER: - case CENTER_SELECTION: - editor.setHorizontalAlignment(SwingConstants.CENTER); - break; - case GENERAL: - case RIGHT: - editor.setHorizontalAlignment(SwingConstants.RIGHT); - break; - default: - editor.setHorizontalAlignment(SwingConstants.LEFT); - break; - } + + /** + * Gets the tableCellEditorComponent attribute of the SVTableCellEditor object + * + * @return The tableCellEditorComponent value + */ + @Override + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, + int row, + int column) { + logger.log(POILogger.INFO, "GetTableCellEditorComponent"); + HSSFCell cell = (HSSFCell) value; + if (cell != null) { + HSSFCellStyle style = cell.getCellStyle(); + HSSFFont f = wb.getFontAt(style.getFontIntIndex()); + boolean isbold = f.getBold(); + boolean isitalics = f.getItalic(); + + int fontstyle = Font.PLAIN; + + if (isbold) { + fontstyle = Font.BOLD; + } + if (isitalics) { + fontstyle = fontstyle | Font.ITALIC; + } + + int fontheight = f.getFontHeightInPoints(); + if (fontheight == 9) { + fontheight = 10; //fix for stupid ol Windows + } + + Font font = new Font(f.getFontName(), fontstyle, fontheight); + editor.setFont(font); + + if (style.getFillPattern() == FillPatternType.SOLID_FOREGROUND) { + editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); + } else { + editor.setBackground(white); + } + + editor.setForeground(getAWTColor(f.getColor(), black)); + + + //Set the value that is rendered for the cell + switch (cell.getCellType()) { + case BLANK: + editor.setText(""); + break; + case BOOLEAN: + if (cell.getBooleanCellValue()) { + editor.setText("true"); + } else { + editor.setText("false"); + } + break; + case NUMERIC: + editor.setText(Double.toString(cell.getNumericCellValue())); + break; + case STRING: + editor.setText(cell.getRichStringCellValue().getString()); + break; + case FORMULA: + default: + editor.setText("?"); + } + switch (style.getAlignment()) { + case LEFT: + case JUSTIFY: + case FILL: + editor.setHorizontalAlignment(SwingConstants.LEFT); + break; + case CENTER: + case CENTER_SELECTION: + editor.setHorizontalAlignment(SwingConstants.CENTER); + break; + case GENERAL: + case RIGHT: + editor.setHorizontalAlignment(SwingConstants.RIGHT); + break; + default: + editor.setHorizontalAlignment(SwingConstants.LEFT); + break; + } + + } + return editor; } - return editor; - } } diff --git a/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java b/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java index d1f7d18b97..1b9a621458 100644 --- a/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java +++ b/src/examples/src/org/apache/poi/hssf/view/SVTableCellRenderer.java @@ -140,7 +140,7 @@ public class SVTableCellRenderer extends JLabel @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - boolean isBorderSet = false; + boolean isBorderSet = false; //If the JTables default cell renderer has been setup correctly the //value will be the HSSFCell that we are trying to render @@ -148,7 +148,7 @@ public class SVTableCellRenderer extends JLabel if (c != null) { HSSFCellStyle s = c.getCellStyle(); - HSSFFont f = wb.getFontAt(s.getFontIndex()); + HSSFFont f = wb.getFontAt(s.getFontIntIndex()); setFont(SVTableUtils.makeFont(f)); if (s.getFillPattern() == FillPatternType.SOLID_FOREGROUND) { diff --git a/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java b/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java index 9090b5ddf4..b2e7e0b94f 100644 --- a/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java +++ b/src/examples/src/org/apache/poi/ss/examples/CellStyleDetails.java @@ -63,7 +63,7 @@ public class CellStyleDetails { System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " "); System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " "); - Font font = wb.getFontAt(style.getFontIndex()); + Font font = wb.getFontAt(style.getFontIntIndex()); System.out.print("Font=" + font.getFontName() + " "); System.out.print("FontColor="); if (font instanceof HSSFFont) { diff --git a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java index 1787982dc7..8c97993f46 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java +++ b/src/examples/src/org/apache/poi/ss/examples/html/ToHtml.java @@ -300,7 +300,7 @@ public class ToHtml { } private void fontStyle(CellStyle style) { - Font font = wb.getFontAt(style.getFontIndex()); + Font font = wb.getFontAt(style.getFontIntIndex()); if (font.getBold()) { out.format(" font-weight: bold;%n"); diff --git a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java index 3d043681be..e187540300 100644 --- a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java +++ b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java @@ -1660,7 +1660,7 @@ public final class ExtendedFormatRecord @Override public String toString() { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append("[EXTENDEDFORMAT]\n"); if (getXFType() == XF_STYLE) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 64a0d12e98..a2169018cb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -169,7 +169,7 @@ public final class HSSFCellStyle implements CellStyle { * set the font for this style * @param font a font object created or retrieved from the HSSFWorkbook object * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) */ @Override public void setFont(Font font) { @@ -186,19 +186,31 @@ public final class HSSFCellStyle implements CellStyle { * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) */ @Override + @Deprecated public short getFontIndex() { return _format.getFontIndex(); } /** + * gets the index of the font for this style + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) + * @since 4.0.0 + */ + @Override + public int getFontIntIndex() + { + return _format.getFontIndex(); + } + + /** * gets the font for this style * @param parentWorkbook The HSSFWorkbook that this style belongs to - * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndex() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIntIndex() + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) */ public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) { - return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex()); + return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIntIndex()); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java index d07da152b1..722dc0b748 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java @@ -25,7 +25,7 @@ import org.apache.poi.ss.usermodel.Font; * Represents a Font used in a workbook. * * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont) */ public final class HSSFFont implements Font { @@ -46,12 +46,12 @@ public final class HSSFFont implements Font { public final static String FONT_ARIAL = "Arial"; - private FontRecord font; - private short index; + private FontRecord font; + private int index; /** Creates a new instance of HSSFFont */ - protected HSSFFont(short index, FontRecord rec) + protected HSSFFont(int index, FontRecord rec) { font = rec; this.index = index; @@ -85,7 +85,15 @@ public final class HSSFFont implements Font { * unless you're comparing which one is which) */ - public short getIndex() + public short getIndex() { return (short)index; } + + /** + * get the index within the HSSFWorkbook (sequence within the collection of Font objects) + * @return unique index number of the underlying record this Font represents (probably you don't care + * unless you're comparing which one is which) + */ + + public int getIndexAsInt() { return index; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 9d8ec90dab..cd38a136f4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -178,13 +178,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * this holds the HSSFFont objects attached to this workbook. * We only create these from the low level records as required. */ - private Map<Short,HSSFFont> fonts; + private Map<Integer,HSSFFont> fonts; /** * holds whether or not to preserve other nodes in the POIFS. Used * for macros and embedded objects. */ - private boolean preserveNodes; + private boolean preserveNodes; /** * Used to keep track of the data formatter so that all @@ -1171,13 +1171,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss public HSSFFont createFont() { /*FontRecord font =*/ workbook.createNewFont(); - short fontindex = (short) (getNumberOfFonts() - 1); + int fontindex = getNumberOfFontsAsInt() - 1; if (fontindex > 3) { fontindex++; // THERE IS NO FOUR!! } - if(fontindex == Short.MAX_VALUE){ + if(fontindex >= Short.MAX_VALUE){ throw new IllegalArgumentException("Maximum number of fonts was exceeded"); } @@ -1194,8 +1194,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { - short numberOfFonts = getNumberOfFonts(); - for (short i=0; i<=numberOfFonts; i++) { + int numberOfFonts = getNumberOfFontsAsInt(); + for (int i = 0; i <= numberOfFonts; i++) { // Remember - there is no 4! if(i == 4) { continue; @@ -1218,24 +1218,25 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return null; } - /** - * get the number of fonts in the font table - * @return number of fonts - */ + @Override + @Deprecated + public short getNumberOfFonts() { + return (short)getNumberOfFontsAsInt(); + } @Override - public short getNumberOfFonts() - { - return (short) workbook.getNumberOfFontRecords(); + public int getNumberOfFontsAsInt() { + return workbook.getNumberOfFontRecords(); } - /** - * Get the font at the given index number - * @param idx index number - * @return HSSFFont at the index - */ @Override + @Deprecated public HSSFFont getFontAt(short idx) { + return getFontAt((int)idx); + } + + @Override + public HSSFFont getFontAt(int idx) { if(fonts == null) { fonts = new HashMap<>(); } @@ -1243,7 +1244,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // So we don't confuse users, give them back // the same object every time, but create // them lazily - Short sIdx = Short.valueOf(idx); + Integer sIdx = Integer.valueOf(idx); if(fonts.containsKey(sIdx)) { return fonts.get(sIdx); } diff --git a/src/java/org/apache/poi/ss/usermodel/CellStyle.java b/src/java/org/apache/poi/ss/usermodel/CellStyle.java index 57b052b16d..3e9f374396 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/src/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -49,17 +49,26 @@ public interface CellStyle { * set the font for this style * @param font a font object created or retrieved from the Workbook object * @see Workbook#createFont() - * @see Workbook#getFontAt(short) + * @see Workbook#getFontAt(int) */ void setFont(Font font); /** * gets the index of the font for this style * @see Workbook#getFontAt(short) + * @deprecated use <code>getFontIntIndex()</code> instead */ + @Removal(version = "4.2") short getFontIndex(); /** + * gets the index of the font for this style + * @see Workbook#getFontAt(int) + * @since 4.0.0 + */ + int getFontIntIndex(); + + /** * set the cell's using this style to be hidden * @param hidden - whether the cell using this style should be hidden */ diff --git a/src/java/org/apache/poi/ss/usermodel/Font.java b/src/java/org/apache/poi/ss/usermodel/Font.java index 7c218531c4..66c54ba89c 100644 --- a/src/java/org/apache/poi/ss/usermodel/Font.java +++ b/src/java/org/apache/poi/ss/usermodel/Font.java @@ -18,6 +18,8 @@ package org.apache.poi.ss.usermodel; +import org.apache.poi.util.Removal; + public interface Font { /** @@ -267,9 +269,20 @@ public interface Font { * * @return unique index number of the underlying record this Font represents (probably you don't care * unless you're comparing which one is which) + * @deprecated use <code>getIndexAsInt()</code> instead */ + @Removal(version = "4.2") public short getIndex(); + /** + * get the index within the XSSFWorkbook (sequence within the collection of Font objects) + * + * @return unique index number of the underlying record this Font represents (probably you don't care + * unless you're comparing which one is which) + * @since 4.0.0 + */ + public int getIndexAsInt(); + public void setBold(boolean bold); public boolean getBold(); diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index f8f5b28ee1..bd34cb2b78 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -265,18 +265,39 @@ public interface Workbook extends Closeable, Iterable<Sheet> { * Get the number of fonts in the font table * * @return number of fonts + * @deprecated use <code>getNumberOfFontsAsInt()</code> instead */ + @Removal(version = "4.2") short getNumberOfFonts(); /** + * Get the number of fonts in the font table + * + * @return number of fonts + * @since 4.0.0 + */ + int getNumberOfFontsAsInt(); + + /** * Get the font at the given index number * * @param idx index number (0-based) * @return font at the index + * @deprecated use <code>getFontAt(int)</code> */ + @Removal(version = "4.2") Font getFontAt(short idx); /** + * Get the font at the given index number + * + * @param idx index number (0-based) + * @return font at the index + * @since 4.0.0 + */ + Font getFontAt(int idx); + + /** * Create a new Cell style and add it to the workbook's style table * * @return the new Cell Style object diff --git a/src/java/org/apache/poi/ss/util/CellUtil.java b/src/java/org/apache/poi/ss/util/CellUtil.java index e1b909917a..15036761a7 100644 --- a/src/java/org/apache/poi/ss/util/CellUtil.java +++ b/src/java/org/apache/poi/ss/util/CellUtil.java @@ -234,7 +234,7 @@ public final class CellUtil { public static void setFont(Cell cell, Font font) { // Check if font belongs to workbook Workbook wb = cell.getSheet().getWorkbook(); - final short fontIndex = font.getIndex(); + final int fontIndex = font.getIndexAsInt(); if (!wb.getFontAt(fontIndex).equals(font)) { throw new IllegalArgumentException("Font does not belong to this workbook"); } @@ -408,7 +408,7 @@ public final class CellUtil { style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); - style.setFont(workbook.getFontAt(getShort(properties, FONT))); + style.setFont(workbook.getFontAt(getInt(properties, FONT))); style.setHidden(getBoolean(properties, HIDDEN)); style.setIndention(getShort(properties, INDENTION)); style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR)); @@ -429,8 +429,24 @@ public final class CellUtil { */ private static short getShort(Map<String, Object> properties, String name) { Object value = properties.get(name); - if (value instanceof Short) { - return ((Short) value).shortValue(); + if (value instanceof Number) { + return ((Number) value).shortValue(); + } + return 0; + } + + /** + * Utility method that returns the named int value form the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return zero if the property does not exist, or is not a {@link Integer} + * otherwise the property value + */ + private static int getInt(Map<String, Object> properties, String name) { + Object value = properties.get(name); + if (value instanceof Number) { + return ((Number) value).intValue(); } return 0; } diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java index 8f1345230c..2293ebcb1c 100644 --- a/src/java/org/apache/poi/ss/util/SheetUtil.java +++ b/src/java/org/apache/poi/ss/util/SheetUtil.java @@ -144,7 +144,7 @@ public class SheetUtil { if (cellType == CellType.FORMULA) cellType = cell.getCachedFormulaResultType(); - Font font = wb.getFontAt(style.getFontIndex()); + Font font = wb.getFontAt(style.getFontIntIndex()); double width = -1; if (cellType == CellType.STRING) { @@ -266,7 +266,7 @@ public class SheetUtil { */ @Internal public static int getDefaultCharWidth(final Workbook wb) { - Font defaultFont = wb.getFontAt((short) 0); + Font defaultFont = wb.getFontAt( 0); AttributedString str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index b385628f76..acf9d0f303 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -811,28 +811,29 @@ public class SXSSFWorkbook implements Workbook { { return _wb.findFont(bold, color, fontHeight, name, italic, strikeout, typeOffset, underline); } - - /** - * Get the number of fonts in the font table - * - * @return number of fonts - */ @Override - public short getNumberOfFonts() + @Deprecated + public short getNumberOfFonts() { + return (short)getNumberOfFontsAsInt(); + } + + @Override + public int getNumberOfFontsAsInt() { - return _wb.getNumberOfFonts(); + return _wb.getNumberOfFontsAsInt(); } - /** - * Get the font at the given index number - * - * @param idx index number (0-based) - * @return font at the index - */ @Override + @Deprecated public Font getFontAt(short idx) { + return getFontAt((int)idx); + } + + @Override + public Font getFontAt(int idx) + { return _wb.getFontAt(idx); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index cd5a3bb766..df64296033 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -469,11 +469,24 @@ public class XSSFCellStyle implements CellStyle { * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(short) */ @Override + @Deprecated public short getFontIndex() { return (short) getFontId(); } /** + * Gets the index of the font for this style + * + * @return short - font index + * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(int) + * @since 4.0.0 + */ + @Override + public int getFontIntIndex() { + return getFontId(); + } + + /** * Get whether the cell's using this style are to be hidden * * @return boolean - whether the cell using this style is hidden diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index c24fb10008..50e76ba86c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -63,7 +63,7 @@ public class XSSFFont implements Font { private IndexedColorMap _indexedColorMap; private ThemesTable _themes; private CTFont _ctFont; - private short _index; + private int _index; /** * Create a new XSSFFont @@ -615,12 +615,14 @@ public class XSSFFont implements Font { setFamily(family.getValue()); } - /** - * get the index within the XSSFWorkbook (sequence within the collection of Font objects) - * @return unique index number of the underlying record this Font represents (probably you don't care - * unless you're comparing which one is which) - */ - public short getIndex() + @Override + @Deprecated + public short getIndex() { + return (short)getIndexAsInt(); + } + + @Override + public int getIndexAsInt() { return _index; } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 1bb256c62d..96176385c4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -925,6 +925,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { /** * Finds a font that matches the one with the supplied attributes + * + * @return the font with the matched attributes or <code>null</code> */ @Override public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { @@ -972,17 +974,16 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return stylesSource.getStyleAt(idx); } - /** - * Get the font at the given index number - * - * @param idx index number - * @return XSSFFont at the index - */ @Override public XSSFFont getFontAt(short idx) { return stylesSource.getFontAt(idx); } + @Override + public XSSFFont getFontAt(int idx) { + return stylesSource.getFontAt(idx); + } + /** * Get the first named range with the given name. * @@ -1075,13 +1076,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return stylesSource.getNumCellStyles(); } - /** - * Get the number of fonts in the this workbook - * - * @return number of fonts - */ @Override public short getNumberOfFonts() { + return (short)getNumberOfFontsAsInt(); + } + + @Override + public int getNumberOfFontsAsInt() { return (short)stylesSource.getFonts().size(); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java index 49309d93e6..2b40d37577 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -1013,19 +1013,19 @@ public abstract class BaseTestBugzillaIssues { Workbook wb = _testDataProvider.createWorkbook(); int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1; - assertEquals(startingFonts, wb.getNumberOfFonts()); + assertEquals(startingFonts, wb.getNumberOfFontsAsInt()); // Get a font, and slightly change it Font a = wb.createFont(); - assertEquals(startingFonts+1, wb.getNumberOfFonts()); + assertEquals(startingFonts+1, wb.getNumberOfFontsAsInt()); a.setFontHeightInPoints((short)23); - assertEquals(startingFonts+1, wb.getNumberOfFonts()); + assertEquals(startingFonts+1, wb.getNumberOfFontsAsInt()); // Get two more, unchanged /*Font b =*/ wb.createFont(); - assertEquals(startingFonts+2, wb.getNumberOfFonts()); + assertEquals(startingFonts+2, wb.getNumberOfFontsAsInt()); /*Font c =*/ wb.createFont(); - assertEquals(startingFonts+3, wb.getNumberOfFonts()); + assertEquals(startingFonts+3, wb.getNumberOfFontsAsInt()); wb.close(); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 59536005a1..acf0999922 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -276,7 +276,7 @@ public abstract class BaseTestCell { cs = c.getCellStyle(); assertNotNull("Formula Cell Style", cs); - assertEquals("Font Index Matches", f.getIndex(), cs.getFontIndex()); + assertEquals("Font Index Matches", f.getIndexAsInt(), cs.getFontIndex()); assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop()); assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft()); assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight()); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java index 5657babed8..30756ab609 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java @@ -58,7 +58,7 @@ public abstract class BaseTestFont { @Test public final void testGetNumberOfFonts() throws IOException { Workbook wb = _testDataProvider.createWorkbook(); - int num0 = wb.getNumberOfFonts(); + int num0 = wb.getNumberOfFontsAsInt(); Font f1=wb.createFont(); f1.setBold(true); |