001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003import java.awt.Color;
004
005import com.hfg.graphics.ColorUtil;
006import com.hfg.xml.XMLTag;
007import com.hfg.xml.msofficexml.xlsx.ExcelIndexedColor;
008import com.hfg.xml.msofficexml.xlsx.Xlsx;
009
010//------------------------------------------------------------------------------
011/**
012 Represents an Office Open XML conditional formatting (<ssml:colorScale>) 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 SsmlColorScale extends SsmlXMLTag
038{
039   private XMLTag mMinTag;
040   private XMLTag mMidpointTag;
041   private XMLTag mMaxTag;
042
043   private XMLTag mMinColorTag;
044   private XMLTag mMidpointColorTag;
045   private XMLTag mMaxColorTag;
046
047   //###########################################################################
048   // CONSTRUCTORS
049   //###########################################################################
050
051   //---------------------------------------------------------------------------
052   public SsmlColorScale(Xlsx inParentDoc)
053   {
054      super(SsmlXML.COLOR_SCALE, inParentDoc);
055
056      mMinTag = new XMLTag(SsmlXML.CONDITIONAL_FORMATTING_VALUE_OBJ)
057            .setAttribute(SsmlXML.TYPE_ATT, "min");
058      addSubtag(mMinTag);
059
060      mMaxTag = new XMLTag(SsmlXML.CONDITIONAL_FORMATTING_VALUE_OBJ)
061            .setAttribute(SsmlXML.TYPE_ATT, "max");
062      addSubtag(mMaxTag);
063
064      mMinColorTag = new XMLTag(SsmlXML.COLOR);
065      addSubtag(mMinColorTag);
066      setMinColor(Color.RED);
067
068      mMaxColorTag = new XMLTag(SsmlXML.COLOR);
069      addSubtag(mMaxColorTag);
070      setMaxColor(Color.GREEN);
071   }
072
073   //###########################################################################
074   // PUBLIC METHODS
075   //###########################################################################
076
077   //---------------------------------------------------------------------------
078   public SsmlColorScale setMinValue(Float inValue)
079   {
080      if (inValue != null)
081      {
082         mMinTag.setAttribute(SsmlXML.TYPE_ATT, "num");
083         mMinTag.setAttribute(SsmlXML.VALUE_ATT, inValue);
084      }
085      else
086      {
087         mMinTag.setAttribute(SsmlXML.TYPE_ATT, "min");
088         mMinTag.removeAttribute(SsmlXML.VALUE_ATT);
089      }
090
091      return this;
092   }
093
094   //---------------------------------------------------------------------------
095   public SsmlColorScale setMaxValue(Float inValue)
096   {
097      if (inValue != null)
098      {
099         mMaxTag.setAttribute(SsmlXML.TYPE_ATT, "num");
100         mMaxTag.setAttribute(SsmlXML.VALUE_ATT, inValue);
101      }
102      else
103      {
104         mMaxTag.setAttribute(SsmlXML.TYPE_ATT, "max");
105         mMaxTag.removeAttribute(SsmlXML.VALUE_ATT);
106      }
107
108      return this;
109   }
110
111   //---------------------------------------------------------------------------
112   public SsmlColorScale useNumberMidpoint(Number inValue, Color inColor)
113   {
114      setMidpoint("num", inValue, inColor);
115      
116      return this;
117   }
118
119   //---------------------------------------------------------------------------
120   public SsmlColorScale usePercentileMidpoint(Integer inValue, Color inColor)
121   {
122      setMidpoint("percentile", inValue, inColor);
123
124      return this;
125   }
126
127
128   //---------------------------------------------------------------------------
129   public SsmlColorScale setMinColor(Color inValue)
130   {
131      setColor(mMinColorTag, inValue);
132
133      return this;
134   }
135
136   //---------------------------------------------------------------------------
137   public SsmlColorScale setMaxColor(Color inValue)
138   {
139      setColor(mMaxColorTag, inValue);
140
141      return this;
142   }
143
144   //###########################################################################
145   // PRIVATE METHODS
146   //###########################################################################
147
148   //---------------------------------------------------------------------------
149   private static void setColor(XMLTag inTag, Color inValue)
150   {
151      if (inValue != null)
152      {
153         if (inValue instanceof ExcelIndexedColor)
154         {
155            inTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
156         }
157         else
158         {
159            inTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue));
160         }
161      }
162      else
163      {
164         inTag.setAttribute(SsmlXML.AUTO_ATT, "1");
165      }
166   }
167
168   //---------------------------------------------------------------------------
169   private void setMidpoint(String inType, Number inValue, Color inColor)
170   {
171      if (null == mMidpointTag)
172      {
173         mMidpointTag = new XMLTag(SsmlXML.CONDITIONAL_FORMATTING_VALUE_OBJ);
174         addSubtag(1, mMidpointTag);
175      }
176
177      mMidpointTag.setAttribute(SsmlXML.TYPE_ATT, inType);
178      mMidpointTag.setAttribute(SsmlXML.VALUE_ATT, inValue);
179
180      if (null == mMidpointColorTag)
181      {
182         mMidpointColorTag = new XMLTag(SsmlXML.COLOR);
183         addSubtag(4, mMidpointColorTag);
184      }
185
186      setColor(mMidpointColorTag, inColor);
187   }
188
189}