All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlCfRule Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.xlsx.spreadsheetml;


import com.hfg.exception.ProgrammingException;
import com.hfg.xml.XMLNamespace;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlDifferentialFormat;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML conditional formatting rule (<ssml:cfRule>) tag.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class SsmlCfRule extends SsmlXMLTag
{
   private final SsmlWorksheet mParentWorksheet;
   private XMLTag        mExtLst;
   private SsmlCfRule    mRuleExtension;

   private static int sPrioritySrc = 1;


   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SsmlCfRule(SsmlWorksheet inParentWorksheet, SsmlCfRuleType inType)
   {
      this(inParentWorksheet, inType, SsmlXML.SPREADSHEETML_NAMESPACE);
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule(SsmlWorksheet inParentWorksheet, SsmlCfRuleType inType, XMLNamespace inNamespace)
   {
      super(inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE) ? SsmlXML.X14_CONDITIONAL_FORMATTING_RULE : SsmlXML.CONDITIONAL_FORMATTING_RULE, inParentWorksheet.getParentDoc());
      mParentWorksheet = inParentWorksheet;
      setAttribute(SsmlXML.TYPE_ATT, inType.name());

      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
      {
         setPriority(sPrioritySrc++);
      }
      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
      {
         setAttribute(SsmlXML.ID, generateUID());
      }
      else
      {
         throw new ProgrammingException("Unrecognized namespace option for conditionalFormatting tag: " + inNamespace);
      }
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SsmlCfRuleType getType()
   {
      return SsmlCfRuleType.valueOf(getAttributeValue(SsmlXML.TYPE_ATT));
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setPriority(int inValue)
   {
      setAttribute(SsmlXML.PRIORITY_ATT, inValue);

      if (inValue > sPrioritySrc)
      {
         sPrioritySrc = inValue + 1;
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public void setExtension(SsmlCfRule inValue)
   {
      mRuleExtension = inValue;

      // Create the local reference to the extension
      SsmlExtension extension = new SsmlExtension(mParentWorksheet, SsmlXML.SPREADSHEETML_2009_NAMESPACE, SsmlExtension.URI_Type.id);
      extension.addSubtag(new XMLTag(SsmlXML.X14_ID).setContent(mRuleExtension.getAttributeValue(SsmlXML.ID)));

      // Extensions are collected under an extLst tag
      if (null == mExtLst)
      {
         // Check if it has been added via addSubtag()...
         mExtLst = getOptionalSubtagByName(SsmlXML.EXTENSION_LIST);
         if (null == mExtLst)
         {
            mExtLst = new XMLTag(SsmlXML.EXTENSION_LIST);
            addSubtag(mExtLst);
         }
      }

      mExtLst.addSubtag(extension);
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setFormat(SsmlDifferentialFormat inValue)
   {
      setAttribute(SsmlXML.DIFFERENTIAL_FORMAT_ID_ATT, inValue.getIndex());
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setStopIfTrue(boolean inValue)
   {
      setAttribute(SsmlXML.STOP_IF_TRUE_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setAboveAverage(boolean inValue)
   {
      setAttribute(SsmlXML.ABOVE_AVG_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setTop10Percent(boolean inValue)
   {
      setAttribute(SsmlXML.PERCENT_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setBottom(boolean inValue)
   {
      setAttribute(SsmlXML.BOTTOM_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setEqualAverage(boolean inValue)
   {
      setAttribute(SsmlXML.EQUAL_AVG_ATT, inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setOperator(SsmlCfOperator inValue)
   {
      if (inValue !=  null)
      {
         setAttribute(SsmlXML.OPERATOR_ATT, inValue.name());
      }
      else
      {
         removeAttribute(SsmlXML.OPERATOR_ATT);
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setText(String inValue)
   {
      if (inValue !=  null)
      {
         setAttribute(SsmlXML.TEXT_ATT, inValue);
      }
      else
      {
         removeAttribute(SsmlXML.TEXT_ATT);
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setTimePeriod(SsmlCfTimePeriod inValue)
   {
      if (inValue !=  null)
      {
         setAttribute(SsmlXML.TIME_PERIOD_ATT, inValue.name());
      }
      else
      {
         removeAttribute(SsmlXML.TIME_PERIOD_ATT);
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setRank(Integer inValue)
   {
      if (inValue !=  null)
      {
         setAttribute(SsmlXML.RANK_ATT, inValue);
      }
      else
      {
         removeAttribute(SsmlXML.RANK_ATT);
      }

      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule setStdDev(Integer inValue)
   {
      if (inValue !=  null)
      {
         setAttribute(SsmlXML.STD_DEV_ATT, inValue);
      }
      else
      {
         removeAttribute(SsmlXML.STD_DEV_ATT);
      }

      return this;
   }


   //---------------------------------------------------------------------------
   public SsmlDataBar getOrAddDataBar()
   {
      return getOrAddDataBar(getNamespace());
   }

   //---------------------------------------------------------------------------
   private SsmlDataBar getOrAddDataBar(XMLNamespace inNamespace)
   {
      SsmlDataBar dataBarTag;
      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
      {
         // Check if it has been added via addSubtag()...
         dataBarTag = getOptionalSubtagByName(SsmlXML.DATA_BAR);
         if (null == dataBarTag)
         {
            dataBarTag = new SsmlDataBar(mParentWorksheet);
            addSubtag(dataBarTag);

            // Create the extension for the data bar
            SsmlDataBar dataBarExtension = mRuleExtension.getOrAddDataBar(SsmlXML.SPREADSHEETML_2009_NAMESPACE);

            dataBarTag.setExtension(dataBarExtension);
         }
      }
      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
      {
         // Check if it has been added via addSubtag()...
         dataBarTag = getOptionalSubtagByName(SsmlXML.X14_DATA_BAR);
         if (null == dataBarTag)
         {
            dataBarTag = new SsmlDataBar(mParentWorksheet, SsmlXML.SPREADSHEETML_2009_NAMESPACE);
            addSubtag(dataBarTag);
         }
      }
      else
      {
         throw new ProgrammingException("Unrecognized namespace option for cfRule tag: " + inNamespace);
      }

      return dataBarTag;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule addFormula(String inValue)
   {
      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue));
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule addFormula(int inValue)
   {
      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue));
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlCfRule addFormula(float inValue)
   {
      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue + ""));
      return this;
   }
}
/*

  
    ROW()=COLUMN()
  
  
    90
    100
  
  
    80
    90
  
  
    70
    80
  
  
    60
    70
  
  
    0
    60
  

 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy