001package com.hfg.xml.msofficexml.xlsx.spreadsheetml; 002 003import java.awt.Color; 004import java.util.List; 005 006import com.hfg.graphics.NonlinearColorScale; 007import com.hfg.xml.XMLNamespace; 008import com.hfg.xml.msofficexml.OfficeOpenXmlException; 009 010//------------------------------------------------------------------------------ 011/** 012 Represents an Office Open XML color scale conditional formatting rule (<ssml:cfRule>) tag. 013 014 @author J. Alex Taylor, hairyfatguy.com 015 */ 016//------------------------------------------------------------------------------ 017// com.hfg XML/HTML Coding Library 018// 019// This library is free software; you can redistribute it and/or 020// modify it under the terms of the GNU Lesser General Public 021// License as published by the Free Software Foundation; either 022// version 2.1 of the License, or (at your option) any later version. 023// 024// This library is distributed in the hope that it will be useful, 025// but WITHOUT ANY WARRANTY; without even the implied warranty of 026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 027// Lesser General Public License for more details. 028// 029// You should have received a copy of the GNU Lesser General Public 030// License along with this library; if not, write to the Free Software 031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 032// 033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 034// jataylor@hairyfatguy.com 035//------------------------------------------------------------------------------ 036 037public class SsmlCfColorScaleRule extends SsmlCfRule 038{ 039 private SsmlColorScale mColorScale; 040 041 //########################################################################### 042 // CONSTRUCTORS 043 //########################################################################### 044 045 //--------------------------------------------------------------------------- 046 public SsmlCfColorScaleRule(SsmlWorksheet inParentWorksheet) 047 { 048 this(inParentWorksheet, SsmlXML.SPREADSHEETML_NAMESPACE); 049 } 050 051 //--------------------------------------------------------------------------- 052 public SsmlCfColorScaleRule(SsmlWorksheet inParentWorksheet, XMLNamespace inNamespace) 053 { 054 super(inParentWorksheet, SsmlCfRuleType.colorScale, inNamespace); 055 } 056 057 //########################################################################### 058 // CONSTRUCTORS 059 //########################################################################### 060 061 //--------------------------------------------------------------------------- 062 public SsmlCfColorScaleRule setColorScale(SsmlColorScale inValue) 063 { 064 if (mColorScale != null) 065 { 066 removeSubtag(mColorScale); 067 } 068 069 mColorScale = inValue; 070 addSubtag(mColorScale); 071 072 return this; 073 } 074 075 //--------------------------------------------------------------------------- 076 public SsmlCfColorScaleRule setColorScale(NonlinearColorScale inValue) 077 { 078 if (inValue.size() < 2 079 || inValue.size() > 3) 080 { 081 throw new OfficeOpenXmlException("The color scale size can only be 2 or 3!"); 082 } 083 084 List<Float> thresholds = inValue.getThresholds(); 085 List<Color> colors = inValue.getColors(); 086 087 SsmlColorScale colorScale = new SsmlColorScale(getParentDoc()); 088 colorScale.setMinColor(colors.get(0)) 089 .setMinValue(thresholds.get(0)) 090 .setMaxColor(colors.get(colors.size() - 1)) 091 .setMaxValue(thresholds.get(thresholds.size() - 1)); 092 093 if (3 == inValue.size()) 094 { 095 colorScale.useNumberMidpoint(thresholds.get(1), colors.get(1)); 096 } 097 098 099 return setColorScale(colorScale); 100 } 101 102 //--------------------------------------------------------------------------- 103 public SsmlColorScale getColorScale() 104 { 105 if (null == mColorScale) 106 { 107 // Check if it has been added via addSubTag()... 108 mColorScale = getOptionalSubtagByName(SsmlXML.COLOR_SCALE); 109 if (null == mColorScale) 110 { // TODO: Should this method return null instead of creating an object? 111 mColorScale = new SsmlColorScale(getParentDoc()); 112 addSubtag(mColorScale); 113 } 114 } 115 116 return mColorScale; 117 } 118}