001package com.hfg.xml.msofficexml.xlsx.spreadsheetml.style;
002
003
004
005import java.util.ArrayList;
006import java.util.Collections;
007import java.util.List;
008
009import com.hfg.util.StringUtil;
010import com.hfg.xml.XMLTag;
011import com.hfg.xml.msofficexml.xlsx.Xlsx;
012import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
013import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
014
015
016//------------------------------------------------------------------------------
017/**
018 Represents an Office Open XML spreadsheetml (<ssml:numFmt>) tag.
019
020 @author J. Alex Taylor, hairyfatguy.com
021 */
022//------------------------------------------------------------------------------
023// com.hfg XML/HTML Coding Library
024//
025// This library is free software; you can redistribute it and/or
026// modify it under the terms of the GNU Lesser General Public
027// License as published by the Free Software Foundation; either
028// version 2.1 of the License, or (at your option) any later version.
029//
030// This library is distributed in the hope that it will be useful,
031// but WITHOUT ANY WARRANTY; without even the implied warranty of
032// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
033// Lesser General Public License for more details.
034//
035// You should have received a copy of the GNU Lesser General Public
036// License along with this library; if not, write to the Free Software
037// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
038//
039// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
040// jataylor@hairyfatguy.com
041//------------------------------------------------------------------------------
042
043public class SsmlNumberFormat extends SsmlXMLTag
044{
045   private static List<SsmlNumberFormat> sDefaultValues = new ArrayList<>(25);
046
047   // Default number formats      TODO: currently incomplete
048   public static final SsmlNumberFormat GENERAL              = new SsmlNumberFormat(0, "General");
049   public static final SsmlNumberFormat INTEGER              = new SsmlNumberFormat(1, "0");
050   public static final SsmlNumberFormat TWO_DECIMALS         = new SsmlNumberFormat(2, "0.00");
051   public static final SsmlNumberFormat INT_WITH_COMMA       = new SsmlNumberFormat(3, "#,##0");
052   public static final SsmlNumberFormat TWO_DECIMALS_WITH_COMMA = new SsmlNumberFormat(4, "#,##0.00");
053   public static final SsmlNumberFormat PERCENT_WHOLE_NUM    = new SsmlNumberFormat(9, "0%");
054   public static final SsmlNumberFormat PERCENT_TWO_DECIMALS = new SsmlNumberFormat(10, "0.00%");
055   public static final SsmlNumberFormat SCIENTIFIC           = new SsmlNumberFormat(11, "0.00E+00");
056   public static final SsmlNumberFormat DATE_MM_DD_YY        = new SsmlNumberFormat(14, "mm-dd-yy");
057   public static final SsmlNumberFormat DATE_D_MMM_YY        = new SsmlNumberFormat(15, "d-mmm-yy");
058   public static final SsmlNumberFormat DATE_D_MMM           = new SsmlNumberFormat(16, "d-mmm");
059   public static final SsmlNumberFormat DATE_MMM_YY          = new SsmlNumberFormat(17, "mmm-yy");
060   public static final SsmlNumberFormat TIME_H_MM_AM         = new SsmlNumberFormat(18, "h:mm AM/PM");
061   public static final SsmlNumberFormat TIME_H_MM_SS_AM      = new SsmlNumberFormat(19, "h:mm:ss AM/PM");
062   public static final SsmlNumberFormat TIME_H_MM            = new SsmlNumberFormat(20, "h:mm");
063   public static final SsmlNumberFormat TIME_H_MM_SS         = new SsmlNumberFormat(21, "h:mm:ss");
064   public static final SsmlNumberFormat DATE_TIME_M_D_YY_H_MM =  new SsmlNumberFormat(22, "m/d/yy h:mm");
065
066   private Integer mIndex;
067
068   //###########################################################################
069   // CONSTRUCTORS
070   //###########################################################################
071
072   //---------------------------------------------------------------------------
073   public SsmlNumberFormat(Xlsx inXlsx)
074   {
075      super(SsmlXML.NUM_FORMAT, inXlsx);
076      // Register with the styles part
077      mIndex = getParentDoc().getStylesPart().defineNumberFormat(this) + 164; // Custom styles should start at 164
078
079      setAttribute(SsmlXML.NUM_FORMAT_ID_ATT, mIndex);
080   }
081
082   //---------------------------------------------------------------------------
083   public SsmlNumberFormat(Xlsx inXlsx, XMLTag inXMLTag)
084   {
085      super(SsmlXML.NUM_FORMAT, inXlsx);
086
087      if (inXMLTag.hasAttribute(SsmlXML.NUM_FORMAT_ID_ATT))
088      {
089         mIndex = Integer.parseInt(inXMLTag.getAttributeValue(SsmlXML.NUM_FORMAT_ID_ATT));
090         setAttribute(SsmlXML.NUM_FORMAT_ID_ATT, mIndex);
091      }
092
093      if (inXMLTag.hasAttribute(SsmlXML.FORMAT_CODE_ATT))
094      {
095         setFormatCode(inXMLTag.getAttributeValue(SsmlXML.FORMAT_CODE_ATT));
096      }
097   }
098
099   //---------------------------------------------------------------------------
100   /**
101    Private constructor for defining default number formats
102    @param inId the default number format id
103    */
104   private SsmlNumberFormat(int inId, String inFormatCode)
105   {
106      super(SsmlXML.NUM_FORMAT, null);
107      mIndex = inId;
108      setFormatCode(inFormatCode);
109
110      sDefaultValues.add(this);
111   }
112
113   //###########################################################################
114   // PUBLIC METHODS
115   //###########################################################################
116
117   //---------------------------------------------------------------------------
118   @Override
119   public SsmlNumberFormat clone()
120   {
121      SsmlNumberFormat clone;
122      if (getFormatCode() != null)
123      {
124         // Don't clone a standard value!
125         clone = this;
126      }
127      else
128      {
129         clone = (SsmlNumberFormat) super.clone();
130
131         // Register with the styles part
132         clone.mIndex = getParentDoc().getStylesPart().defineNumberFormat(this) + 164; // Custom styles should start at 164
133      }
134      return clone;
135   }
136
137   //---------------------------------------------------------------------------
138   public static List<SsmlNumberFormat> getDefaultValues()
139   {
140      return Collections.unmodifiableList(sDefaultValues);
141   }
142
143
144   //---------------------------------------------------------------------------
145   public Integer getIndex()
146   {
147      return mIndex;
148   }
149
150   //---------------------------------------------------------------------------
151   public SsmlNumberFormat setFormatCode(String inValue)
152   {
153      String attrValue = inValue;
154      if (! StringUtil.isSet(inValue))
155      {
156         attrValue = "string";
157      }
158
159      setAttribute(SsmlXML.FORMAT_CODE_ATT, attrValue);
160
161      return this;
162   }
163
164
165   //---------------------------------------------------------------------------
166   public String getFormatCode()
167   {
168      return getAttributeValue(SsmlXML.FORMAT_CODE_ATT);
169   }
170}