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

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

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


import java.util.List;

import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXmlWarning;
import com.hfg.xml.msofficexml.xlsx.CellRange;
import com.hfg.xml.msofficexml.xlsx.Xlsx;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML data validation (<ssml:dataValidation>) 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 SsmlDataValidation extends SsmlXMLTag
{
   //###########################################################################
   // PRIVATE FIELDS
   //###########################################################################

   private XMLTag mFormulaTag;

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

   //---------------------------------------------------------------------------
   public SsmlDataValidation(Xlsx inXlsx, CellRange inCellRange)
   {
      super(SsmlXML.DATA_VALIDATION, inXlsx);

      // Set some default attributes
      setType(SsmlDataValidationType.list);
      setAllowBlank(true);

      setCellRange(inCellRange);
   }

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

   //---------------------------------------------------------------------------
   public SsmlDataValidation setType(SsmlDataValidationType inValue)
   {
      setAttribute(SsmlXML.TYPE_ATT, inValue.name());
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlDataValidation setAllowBlank(boolean inValue)
   {
      setAttribute(SsmlXML.ALLOW_BLANK_ATT, inValue ? "1" : "0");
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlDataValidation setCellRange(CellRange inValue)
   {
      setAttribute(SsmlXML.SQUARE_REF_ATT, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public SsmlDataValidation setValues(List inValues)
   {
      if (null == mFormulaTag)
      {
         // Check if it has been added via addSubtag()...
         mFormulaTag = getOptionalSubtagByName(SsmlXML.FORMULA1);
         if (null == mFormulaTag)
         {
            mFormulaTag = new XMLTag(SsmlXML.FORMULA1);
            addSubtag(mFormulaTag);
         }
      }

      String formulaContent = StringUtil.join(inValues, ",");
      mFormulaTag.setContent(StringUtil.quote(formulaContent));

      if (formulaContent.length() > 255)
      {
         throw new OfficeOpenXmlWarning("The formula content for the data validation cannot be longer than 255 characters or Excel will throw an error when opening the document!");
      }

      return this;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy