]> source.dussan.org Git - poi.git/commitdiff
Further HSSF Color Gradient support
authorNick Burch <nick@apache.org>
Sun, 19 Jul 2015 04:59:51 +0000 (04:59 +0000)
committerNick Burch <nick@apache.org>
Sun, 19 Jul 2015 04:59:51 +0000 (04:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691796 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/CFRule12Record.java
src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java [new file with mode: 0644]
src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java

index 3e72e5810eb6ba034f982d99e45e8126f52c795f..e7974a0922e1002d461c34d02b95daae8b1f47dc 100644 (file)
@@ -202,6 +202,21 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord {
         return multistate;
     }
 
+    public boolean containsColorGradientBlock() {
+        return (color_gradient != null);
+    }
+    public ColorGradientFormatting getColorGradientFormatting() {
+        return color_gradient;
+    }
+    public ColorGradientFormatting createColorGradientFormatting() {
+        if (color_gradient != null) return color_gradient;
+        
+        // Convert, setup and return
+        setConditionType(CONDITION_TYPE_COLOR_SCALE);
+        color_gradient = new ColorGradientFormatting();
+        return color_gradient;
+    }
+
     /**
      * get the stack of the scale expression as a list
      *
index 12b1f71d161ca6ac2f62ba78c195d4fd6f1f3f38..3b58af74e2c2e908b21bcc44aba0b8db72497fb9 100644 (file)
@@ -64,6 +64,16 @@ public final class ColorGradientFormatting implements Cloneable {
         in.readFully(colors);
     }
     
+    public int getNumControlPoints() {
+        return thresholds.length;
+    }
+    public void setNumControlPoints(int num) {
+        if (num != thresholds.length) {
+            thresholds = new Threshold[num];
+            // TODO Colors
+        }
+    }
+    
     public Threshold[] getThresholds() {
         return thresholds;
     }
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java
new file mode 100644 (file)
index 0000000..6e82072
--- /dev/null
@@ -0,0 +1,75 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.usermodel;
+
+import org.apache.poi.hssf.record.CFRule12Record;
+import org.apache.poi.hssf.record.cf.ColorGradientFormatting;
+import org.apache.poi.hssf.record.cf.Threshold;
+import org.apache.poi.ss.usermodel.Color;
+import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold;
+
+/**
+ * High level representation for Color Scale / Color Gradient 
+ *  Formatting component of Conditional Formatting settings
+ */
+public final class HSSFColorScaleFormatting implements org.apache.poi.ss.usermodel.ColorScaleFormatting {
+    private final HSSFSheet sheet;
+    private final CFRule12Record cfRule12Record;
+    private final ColorGradientFormatting colorFormatting;
+
+    protected HSSFColorScaleFormatting(CFRule12Record cfRule12Record, HSSFSheet sheet) {
+        this.sheet = sheet;
+        this.cfRule12Record = cfRule12Record;
+        this.colorFormatting = this.cfRule12Record.getColorGradientFormatting();
+    }
+
+    public int getNumControlPoints() {
+        return colorFormatting.getNumControlPoints();
+    }
+    public void setNumControlPoints(int num) {
+        colorFormatting.setNumControlPoints(num);
+    }
+
+    public Color[] getColors() {
+        return null; // TODO
+    }
+    public void setColors(Color[] colors) {
+        // TODO
+    }
+
+    public HSSFConditionalFormattingThreshold[] getThresholds() {
+        Threshold[] t = colorFormatting.getThresholds();
+        HSSFConditionalFormattingThreshold[] ht = new HSSFConditionalFormattingThreshold[t.length];
+        for (int i=0; i<t.length; i++) {
+            ht[i] = new HSSFConditionalFormattingThreshold(t[i], sheet);
+        }
+        return ht;
+    }
+
+    public void setThresholds(ConditionalFormattingThreshold[] thresholds) {
+        Threshold[] t = new Threshold[thresholds.length];
+        for (int i=0; i<t.length; i++) {
+            t[i] = ((HSSFConditionalFormattingThreshold)thresholds[i]).getThreshold();
+        }
+        colorFormatting.setThresholds(t);
+    }
+
+    public HSSFConditionalFormattingThreshold createThreshold() {
+        return new HSSFConditionalFormattingThreshold(new Threshold(), sheet);
+    }
+}
index a999488d38980471208c5e1dffd4a06f9887c35f..a6cdbcfd0340ac7322166464edbd965a22ab3d1f 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
 import org.apache.poi.hssf.record.CFRuleRecord;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
+import org.apache.poi.hssf.record.cf.ColorGradientFormatting;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
 import org.apache.poi.hssf.record.cf.PatternFormatting;
@@ -55,10 +56,18 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
         cfRuleRecord = pRuleRecord;
     }
 
-    CFRuleBase getCfRuleRecord()
-    {
+    CFRuleBase getCfRuleRecord() {
         return cfRuleRecord;
     }
+    private CFRule12Record getCFRule12Record(boolean create) {
+        if (cfRuleRecord instanceof CFRule12Record) {
+            // Good
+        } else {
+            if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
+            return null;
+        }
+        return (CFRule12Record)cfRuleRecord;
+    }
 
     private HSSFFontFormatting getFontFormatting(boolean create)
     {
@@ -171,13 +180,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
     }
     
     private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
-        if (cfRuleRecord instanceof CFRule12Record) {
-            // Good
-        } else {
-            if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
-            return null;
-        }
-        CFRule12Record cfRule12Record = (CFRule12Record)cfRuleRecord;
+        CFRule12Record cfRule12Record = getCFRule12Record(create);
         IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
         if (iconFormatting != null)
         {
@@ -193,14 +196,12 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
             return null;
         }
     }
-    
     /**
      * @return icon / multi-state formatting object if defined, <code>null</code> otherwise
      */
     public HSSFIconMultiStateFormatting getMultiStateFormatting() {
         return getMultiStateFormatting(false);
     }
-
     /**
      * create a new icon / multi-state formatting object if it does not exist,
      * otherwise just return the existing object.
@@ -209,6 +210,37 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
         return getMultiStateFormatting(true);
     }
     
+    private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) {
+        CFRule12Record cfRule12Record = getCFRule12Record(create);
+        ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting();
+        if (colorFormatting != null)
+        {
+            return new HSSFColorScaleFormatting(cfRule12Record, sheet);
+        }
+        else if( create )
+        {
+            colorFormatting = cfRule12Record.createColorGradientFormatting();
+            return new HSSFColorScaleFormatting(cfRule12Record, sheet);
+        }
+        else
+        {
+            return null;
+        }
+    }
+    /**
+     * @return color scale / gradient formatting object if defined, <code>null</code> otherwise
+     */
+    public HSSFColorScaleFormatting getColorScaleFormatting() {
+        return getColorScaleFormatting(false);
+    }
+    /**
+     * create a new color scale / gradient formatting object if it does not exist,
+     * otherwise just return the existing object.
+     */
+    public HSSFColorScaleFormatting createColorScaleFormatting() {
+        return getColorScaleFormatting(true);
+    }
+    
     /**
      * @return -  the conditiontype for the cfrule
      */