001package com.hfg.xml.msofficexml.xlsx.spreadsheetml.style;
002
003import com.hfg.xml.msofficexml.xlsx.Xlsx;
004import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
005import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
006
007//------------------------------------------------------------------------------
008/**
009 Represents an Office Open XML spreadsheetml style format (<ssml:xf>) tag.
010
011 @author J. Alex Taylor, hairyfatguy.com
012 */
013//------------------------------------------------------------------------------
014// com.hfg XML/HTML Coding Library
015//
016// This library is free software; you can redistribute it and/or
017// modify it under the terms of the GNU Lesser General Public
018// License as published by the Free Software Foundation; either
019// version 2.1 of the License, or (at your option) any later version.
020//
021// This library is distributed in the hope that it will be useful,
022// but WITHOUT ANY WARRANTY; without even the implied warranty of
023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024// Lesser General Public License for more details.
025//
026// You should have received a copy of the GNU Lesser General Public
027// License along with this library; if not, write to the Free Software
028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029//
030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
031// jataylor@hairyfatguy.com
032//------------------------------------------------------------------------------
033
034public class SsmlStyleFormat extends SsmlXMLTag
035{
036   private SsmlFont   mFont;
037   private SsmlFill   mFill;
038   private SsmlBorder mBorder;
039   private Integer    mIndex;
040   private SsmlAlignment    mAlignmentTag;
041
042   //---------------------------------------------------------------------------
043   public SsmlStyleFormat(Xlsx inXlsx)
044   {
045      this(inXlsx.getStylesPart());
046   }
047
048   //---------------------------------------------------------------------------
049   public SsmlStyleFormat(StylesPart inStylesPart)
050   {
051      super(SsmlXML.CELL_FORMAT, inStylesPart.getParentDoc());
052      // Register with the styles part
053      mIndex = inStylesPart.defineStyleFormat(this);
054
055      // Initialize to the default style values
056      setBorder(inStylesPart.getDefaultBorder());
057      setFill(inStylesPart.getDefaultFill());
058      setFont(inStylesPart.getDefaultFont());
059      setNumberFormat(SsmlNumberFormat.GENERAL);
060   }
061
062   //##########################################################################
063   // PUBLIC METHODS
064   //##########################################################################
065
066   //---------------------------------------------------------------------------
067   @Override
068   public SsmlStyleFormat clone()
069   {
070      SsmlStyleFormat clone = (SsmlStyleFormat) super.clone();
071
072      // Register with the styles part
073      clone.mIndex = getParentDoc().getStylesPart().defineStyleFormat(clone);
074
075      return clone;
076   }
077
078
079   //---------------------------------------------------------------------------
080   public Integer getIndex()
081   {
082      return mIndex;
083   }
084
085   //---------------------------------------------------------------------------
086   public SsmlStyleFormat setFont(SsmlFont inValue)
087   {
088      mFont = inValue;
089      if (mFont != null)
090      {
091         setAttribute(SsmlXML.FONT_ID_ATT, mFont.getIndex());
092      }
093      else
094      {
095         removeAttribute(SsmlXML.FONT_ID_ATT);
096      }
097
098      return this;
099   }
100
101   //---------------------------------------------------------------------------
102   public SsmlFont getFont()
103   {
104      if (null == mFont)
105      {
106         // Check if it has been added via addSubtag()...
107         mFont = getOptionalSubtagByName(SsmlXML.FONT);
108      }
109
110      return mFont;
111   }
112
113
114   //---------------------------------------------------------------------------
115   public SsmlStyleFormat setFill(SsmlFill inValue)
116   {
117      mFill = inValue;
118      if (mFill != null)
119      {
120         setAttribute(SsmlXML.FILL_ID_ATT, mFill.getIndex());
121      }
122      else
123      {
124         removeAttribute(SsmlXML.FILL_ID_ATT);
125      }
126
127      return this;
128   }
129
130
131   //---------------------------------------------------------------------------
132   public SsmlFill getFill()
133   {
134      if (null == mFill)
135      {
136         // Check if it has been added via addSubtag()...
137         mFill = getOptionalSubtagByName(SsmlXML.FILL);
138      }
139
140      return mFill;
141   }
142
143   //---------------------------------------------------------------------------
144   public SsmlStyleFormat setBorder(SsmlBorder inValue)
145   {
146      mBorder = inValue;
147      if (mBorder != null)
148      {
149         setAttribute(SsmlXML.BORDER_ID_ATT, mBorder.getIndex());
150      }
151      else
152      {
153         removeAttribute(SsmlXML.BORDER_ID_ATT);
154      }
155
156      return this;
157   }
158
159   //---------------------------------------------------------------------------
160   public SsmlBorder getBorder()
161   {
162      if (null == mBorder)
163      {
164         // Check if it has been added via addSubtag()...
165         mBorder = getOptionalSubtagByName(SsmlXML.BORDER);
166      }
167
168      return mBorder;
169   }
170
171   //---------------------------------------------------------------------------
172   public SsmlStyleFormat setNumberFormat(SsmlNumberFormat inValue)
173   {
174      if (inValue != null)
175      {
176         if (null == inValue.getParentDoc())
177         {
178            // This may be true if it was a standard format
179            inValue.setParentDoc(getParentDoc());
180         }
181
182         setAttribute(SsmlXML.NUM_FORMAT_ID_ATT, inValue.getIndex());
183      }
184      else
185      {
186         removeAttribute(SsmlXML.NUM_FORMAT_ID_ATT);
187      }
188
189      return this;
190   }
191
192   //---------------------------------------------------------------------------
193   public SsmlNumberFormat getNumberFormat()
194   {
195      SsmlNumberFormat numberFormat = null;
196
197      if (hasAttribute(SsmlXML.NUM_FORMAT_ID_ATT))
198      {
199         int numFormatId = Integer.parseInt(getAttributeValue(SsmlXML.NUM_FORMAT_ID_ATT));
200         if (numFormatId >= 164) // Custom num format?
201         {
202            for (SsmlNumberFormat customNumFormat : getParentDoc().getStylesPart().getNumberFormats())
203            {
204               if (customNumFormat.getIndex().equals(numFormatId))
205               {
206                  numberFormat = customNumFormat;
207                  break;
208               }
209            }
210         }
211         else
212         {
213            for (SsmlNumberFormat defaultFormat : SsmlNumberFormat.getDefaultValues())
214            {
215               if (defaultFormat.getIndex().equals(numFormatId))
216               {
217                  numberFormat = defaultFormat;
218                  break;
219               }
220            }
221         }
222      }
223
224      return numberFormat;
225   }
226
227   //---------------------------------------------------------------------------
228   public SsmlAlignment getAlignment()
229   {
230      if (null == mAlignmentTag)
231      {
232         // Check it it has been added via addSubtag()...
233         mAlignmentTag = getOptionalSubtagByName(SsmlXML.ALIGNMENT);
234      }
235
236      return mAlignmentTag;
237   }
238
239   //---------------------------------------------------------------------------
240   public SsmlStyleFormat setAlignment(SsmlAlignment inValue)
241   {
242      removeSubtagsByName(SsmlXML.ALIGNMENT);
243
244      mAlignmentTag = inValue;
245      if (mAlignmentTag != null)
246      {
247         addSubtag(mAlignmentTag);
248      }
249
250      return this;
251   }
252
253}