001package com.hfg.xml.msofficexml.xlsx.spreadsheetml.style;
002
003
004
005import com.hfg.html.attribute.HTMLColor;
006import com.hfg.graphics.ColorUtil;
007import com.hfg.util.StringUtil;
008import com.hfg.xml.XMLTag;
009import com.hfg.xml.msofficexml.xlsx.ExcelIndexedColor;
010import com.hfg.xml.msofficexml.xlsx.Xlsx;
011import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
012import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag;
013
014import java.awt.Color;
015
016//------------------------------------------------------------------------------
017/**
018 Represents an Office Open XML spreadsheetml (<ssml:fill>) 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 SsmlFill extends SsmlXMLTag
044{
045   private Integer mIndex;
046   private XMLTag  mPatternFillTag;
047   private XMLTag  mFgColorTag;
048   private XMLTag  mBgColorTag;
049
050   //##########################################################################
051   // CONSTRUCTORS
052   //##########################################################################
053
054   //---------------------------------------------------------------------------
055   public SsmlFill(SsmlXMLTag inSsmlXMLTag)
056   {
057      this(inSsmlXMLTag.getParentDoc());
058   }
059
060   //---------------------------------------------------------------------------
061   public SsmlFill(Xlsx inXlsx)
062   {
063      this(inXlsx.getStylesPart());
064   }
065
066   //---------------------------------------------------------------------------
067   public SsmlFill(StylesPart inStylesPart)
068   {
069      super(SsmlXML.FILL, inStylesPart.getParentDoc());
070      // Register with the styles part
071      mIndex = inStylesPart.defineFill(this);
072
073      // Default to a solid pattern
074      setPatternType(SsmlPatternType.solid);
075   }
076
077   //##########################################################################
078   // PUBLIC METHODS
079   //##########################################################################
080
081   //---------------------------------------------------------------------------
082   @Override
083   public SsmlFill clone()
084   {
085      SsmlFill clone = (SsmlFill) super.clone();
086
087      // These cached values will be repopulated if needed
088      clone.mPatternFillTag = null;
089      SsmlPatternType patternType = getPatternType();
090      if (patternType != null)
091      {
092         clone.setPatternType(patternType);
093      }
094
095      if (mFgColorTag != null)
096      {
097         clone.mFgColorTag = null;
098         clone.setForegroundColor(getForegroundColor());
099      }
100
101      if (mBgColorTag != null)
102      {
103         clone.mBgColorTag = null;
104         clone.setBackgroundColor(getBackgroundColor());
105      }
106
107      // Register with the styles part
108      clone.mIndex = getParentDoc().getStylesPart().defineFill(clone);
109
110      return clone;
111   }
112
113
114   //---------------------------------------------------------------------------
115   public Integer getIndex()
116   {
117      return mIndex;
118   }
119
120   //---------------------------------------------------------------------------
121   public SsmlFill setPatternType(SsmlPatternType inValue)
122   {
123      XMLTag patternFillTag = getPatternFillTag();
124      if (inValue != null)
125      {
126         patternFillTag.setAttribute(SsmlXML.PATTERN_TYPE_ATT, inValue);
127      }
128      else
129      {
130         patternFillTag.removeAttribute(SsmlXML.PATTERN_TYPE_ATT.getLocalName());
131      }
132
133      return this;
134   }
135
136   //---------------------------------------------------------------------------
137   public SsmlPatternType getPatternType()
138   {
139      SsmlPatternType patternType = null;
140
141      if (mPatternFillTag != null)
142      {
143         String patternTypeString = mPatternFillTag.getAttributeValue(SsmlXML.PATTERN_TYPE_ATT);
144         if (StringUtil.isSet(patternTypeString))
145         {
146            patternType = SsmlPatternType.valueOf(patternTypeString);
147         }
148      }
149
150      return patternType;
151   }
152
153   //---------------------------------------------------------------------------
154   public SsmlFill setForegroundColor(Color inValue)
155   {
156      if (inValue != null)
157      {
158         if (null == mFgColorTag)
159         {
160            // Check if it has been added via addSubtag()...
161            XMLTag patternFillTag = getPatternFillTag();
162
163            mFgColorTag = patternFillTag.getOptionalSubtagByName(SsmlXML.FG_COLOR);
164            if (null == mFgColorTag)
165            {
166               mFgColorTag = new XMLTag(SsmlXML.FG_COLOR);
167               patternFillTag.addSubtag(mFgColorTag);
168            }
169         }
170
171         if (inValue instanceof ExcelIndexedColor)
172         {
173            mFgColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
174         }
175         else
176         {
177            mFgColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue).toUpperCase());
178         }
179      }
180      else
181      {
182         getPatternFillTag().removeSubtagsByName(SsmlXML.FG_COLOR);
183      }
184
185      return this;
186   }
187
188   //---------------------------------------------------------------------------
189   public Color getForegroundColor()
190   {
191      Color color = null;
192
193      if (mFgColorTag != null)
194      {
195         String rgbString = mFgColorTag.getAttributeValue(SsmlXML.RGB_ATT);
196         if (StringUtil.isSet(rgbString))
197         {
198            color = HTMLColor.valueOf(rgbString);
199         }
200         else
201         {
202            String index = mFgColorTag.getAttributeValue(SsmlXML.INDEXED_ATT);
203            if (StringUtil.isSet(index))
204            {
205               color = ExcelIndexedColor.valueOf(index);
206            }
207         }
208      }
209
210      return color;
211   }
212
213   //---------------------------------------------------------------------------
214   public SsmlFill setBackgroundColor(Color inValue)
215   {
216      if (inValue != null)
217      {
218         if (null == mBgColorTag)
219         {
220            // Check if it has been added via addSubtag()...
221            XMLTag patternFillTag = getPatternFillTag();
222
223            mBgColorTag = patternFillTag.getOptionalSubtagByName(SsmlXML.BG_COLOR);
224            if (null == mBgColorTag)
225            {
226               mBgColorTag = new XMLTag(SsmlXML.BG_COLOR);
227               patternFillTag.addSubtag(mBgColorTag);
228            }
229         }
230
231         if (inValue instanceof ExcelIndexedColor)
232         {
233            mBgColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex());
234         }
235         else
236         {
237            mBgColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue).toUpperCase());
238         }
239      }
240      else
241      {
242         getPatternFillTag().removeSubtagsByName(SsmlXML.BG_COLOR);
243      }
244
245      return this;
246   }
247
248   //---------------------------------------------------------------------------
249   public Color getBackgroundColor()
250   {
251      Color color = null;
252
253      if (mBgColorTag != null)
254      {
255         String rgbString = mBgColorTag.getAttributeValue(SsmlXML.RGB_ATT);
256         if (StringUtil.isSet(rgbString))
257         {
258            color = HTMLColor.valueOf(rgbString);
259         }
260         else
261         {
262            String index = mBgColorTag.getAttributeValue(SsmlXML.INDEXED_ATT);
263            if (StringUtil.isSet(index))
264            {
265               color = ExcelIndexedColor.valueOf(index);
266            }
267         }
268      }
269
270      return color;
271   }
272
273   //##########################################################################
274   // PRIVATE METHODS
275   //##########################################################################
276
277   //---------------------------------------------------------------------------
278   private XMLTag getPatternFillTag()
279   {
280      if (null == mPatternFillTag)
281      {
282         // Check it it has been added via addSubtag()...
283         mPatternFillTag = getOptionalSubtagByName(SsmlXML.PATTERN_FILL);
284         if (null == mPatternFillTag)
285         {
286            mPatternFillTag = new XMLTag(SsmlXML.PATTERN_FILL);
287            addSubtag(mPatternFillTag);
288         }
289      }
290
291      return mPatternFillTag;
292   }
293}