You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2017/11/13 11:46:04 UTC

svn commit: r1815086 - in /poi/trunk/src: java/org/apache/poi/hssf/util/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/usermodel/

Author: centic
Date: Mon Nov 13 11:46:04 2017
New Revision: 1815086

URL: http://svn.apache.org/viewvc?rev=1815086&view=rev
Log:
Fix setting a font-color if no previous color is defined for the font
Add unit-test to verify this

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/util/HSSFColor.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/util/HSSFColor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/util/HSSFColor.java?rev=1815086&r1=1815085&r2=1815086&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/util/HSSFColor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/util/HSSFColor.java Mon Nov 13 11:46:04 2017
@@ -366,7 +366,7 @@ public class HSSFColor implements Color
         // Currently the only benefit of this method is to throw an IllegalArgumentException
         // instead of a ClassCastException.
         if (color != null && !(color instanceof HSSFColor)) {
-            throw new IllegalArgumentException("Only HSSFColor objects are supported");
+            throw new IllegalArgumentException("Only HSSFColor objects are supported, but had " + color.getClass());
         }
         return (HSSFColor)color;
     }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java?rev=1815086&r1=1815085&r2=1815086&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java Mon Nov 13 11:46:04 2017
@@ -358,7 +358,7 @@ public class XSSFColor extends ExtendedC
         // Currently the only benefit of this method is to throw an IllegalArgumentException
         // instead of a ClassCastException.
         if (color != null && !(color instanceof XSSFColor)) {
-            throw new IllegalArgumentException("Only XSSFColor objects are supported");
+            throw new IllegalArgumentException("Only XSSFColor objects are supported, but had " + color.getClass());
         }
         return (XSSFColor)color;
     }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java?rev=1815086&r1=1815085&r2=1815086&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java Mon Nov 13 11:46:04 2017
@@ -121,6 +121,8 @@ public class XSSFFontFormatting implemen
         XSSFColor xcolor = XSSFColor.toXSSFColor(color);
         if (xcolor == null) {
             _font.getColorList().clear();
+        } else if(_font.sizeOfColorArray() == 0) {
+            _font.addNewColor().setRgb(xcolor.getRGB());
         } else {
             _font.setColorArray(0, xcolor.getCTColor());
         }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java?rev=1815086&r1=1815085&r2=1815086&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFConditionalFormatting.java Mon Nov 13 11:46:04 2017
@@ -25,6 +25,15 @@ import java.io.IOException;
 
 import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting;
 import org.apache.poi.ss.usermodel.Color;
+import org.apache.poi.ss.usermodel.ConditionalFormatting;
+import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
+import org.apache.poi.ss.usermodel.ExtendedColor;
+import org.apache.poi.ss.usermodel.FontFormatting;
+import org.apache.poi.ss.usermodel.PatternFormatting;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.junit.Test;
 
@@ -56,4 +65,69 @@ public class TestXSSFConditionalFormatti
     public void testReadOffice2007() throws IOException {
         testReadOffice2007("NewStyleConditionalFormattings.xlsx");
     }
+
+    private final static java.awt.Color PEAK_ORANGE = new java.awt.Color(255, 239, 221);
+
+    @Test
+    public void testFontFormattingColor() {
+        Workbook wb = XSSFITestDataProvider.instance.createWorkbook();
+        final Sheet sheet = wb.createSheet();
+
+        final SheetConditionalFormatting formatting = sheet.getSheetConditionalFormatting();
+
+        // the conditional formatting is not automatically added when it is created...
+        assertEquals(0, formatting.getNumConditionalFormattings());
+        ConditionalFormattingRule formattingRule = formatting.createConditionalFormattingRule("A1");
+        assertEquals(0, formatting.getNumConditionalFormattings());
+
+        // adding the formatting makes it available
+        int idx = formatting.addConditionalFormatting(new CellRangeAddress[] {}, formattingRule);
+
+        // verify that it can be accessed now
+        assertEquals(0, idx);
+        assertEquals(1, formatting.getNumConditionalFormattings());
+        assertEquals(1, formatting.getConditionalFormattingAt(idx).getNumberOfRules());
+
+        // this is confusing: the rule is not connected to the sheet, changes are not applied
+        // so we need to use setRule() explicitly!
+        FontFormatting fontFmt = formattingRule.createFontFormatting();
+        assertNotNull(formattingRule.getFontFormatting());
+        assertEquals(1, formatting.getConditionalFormattingAt(idx).getNumberOfRules());
+        formatting.getConditionalFormattingAt(idx).setRule(0, formattingRule);
+        assertNotNull(formatting.getConditionalFormattingAt(idx).getRule(0).getFontFormatting());
+
+        fontFmt.setFontStyle(true, false);
+
+        assertEquals(-1, fontFmt.getFontColorIndex());
+
+        //fontFmt.setFontColorIndex((short)11);
+        final ExtendedColor extendedColor = new XSSFColor(PEAK_ORANGE);
+        fontFmt.setFontColor(extendedColor);
+
+        PatternFormatting patternFmt = formattingRule.createPatternFormatting();
+        assertNotNull(patternFmt);
+        patternFmt.setFillBackgroundColor(extendedColor);
+
+        assertEquals(1, formatting.getConditionalFormattingAt(0).getNumberOfRules());
+        assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getFontFormatting());
+        assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getFontFormatting().getFontColor());
+        assertNotNull(formatting.getConditionalFormattingAt(0).getRule(0).getPatternFormatting().getFillBackgroundColorColor());
+
+        checkFontFormattingColorWriteOutAndReadBack(wb, extendedColor);
+    }
+
+    private void checkFontFormattingColorWriteOutAndReadBack(Workbook wb, ExtendedColor extendedColor) {
+        Workbook wbBack = XSSFITestDataProvider.instance.writeOutAndReadBack(wb);
+        assertNotNull(wbBack);
+
+        assertEquals(1, wbBack.getSheetAt(0).getSheetConditionalFormatting().getNumConditionalFormattings());
+        final ConditionalFormatting formattingBack = wbBack.getSheetAt(0).getSheetConditionalFormatting().getConditionalFormattingAt(0);
+        assertEquals(1, wbBack.getSheetAt(0).getSheetConditionalFormatting().getConditionalFormattingAt(0).getNumberOfRules());
+        final ConditionalFormattingRule ruleBack = formattingBack.getRule(0);
+        final FontFormatting fontFormattingBack = ruleBack.getFontFormatting();
+        assertNotNull(formattingBack);
+        assertNotNull(fontFormattingBack.getFontColor());
+        assertEquals(extendedColor, fontFormattingBack.getFontColor());
+        assertEquals(extendedColor, ruleBack.getPatternFormatting().getFillBackgroundColorColor());
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java?rev=1815086&r1=1815085&r2=1815086&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java Mon Nov 13 11:46:04 2017
@@ -80,6 +80,7 @@ public final class TestXSSFFont extends
 		assertEquals(true, ctFont.getBArray(0).getVal());
 	}
 
+	@SuppressWarnings("deprecation")
 	@Test
 	public void testCharSet() throws IOException {
 		CTFont ctFont=CTFont.Factory.newInstance();
@@ -108,7 +109,9 @@ public final class TestXSSFFont extends
         try {
            xssfFont.setCharSet(9999);
            fail("Shouldn't be able to set an invalid charset");
-        } catch(POIXMLException e) {}
+        } catch(POIXMLException e) {
+        	// expected here
+		}
       
 		
 		// Now try with a few sample files
@@ -120,7 +123,7 @@ public final class TestXSSFFont extends
         );
         wb1.close();
 		
-		// GB2312 charact set
+		// GB2312 charset
         XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx");
         assertEquals(134, 
               wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org