001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003
004import java.awt.Color;
005
006import com.hfg.exception.ProgrammingException;
007import com.hfg.html.attribute.HTMLColor;
008import com.hfg.xml.XMLNamespace;
009import com.hfg.xml.XMLTag;
010
011//------------------------------------------------------------------------------
012/**
013 Represents an Office Open XML conditional formatting data bar (<ssml:dataBar>) tag.
014
015 @author J. Alex Taylor, hairyfatguy.com
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037
038public class SsmlDataBar extends SsmlXMLTag
039{
040   private SsmlWorksheet mParentWorksheet;
041   private SsmlDataBar   mDataBarExtension;
042
043
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //---------------------------------------------------------------------------
049   public SsmlDataBar(SsmlWorksheet inParentWorksheet)
050   {
051      this(inParentWorksheet, SsmlXML.SPREADSHEETML_NAMESPACE);
052   }
053
054   //---------------------------------------------------------------------------
055   public SsmlDataBar(SsmlWorksheet inParentWorksheet, XMLNamespace inNamespace)
056   {
057      super(inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE) ? SsmlXML.X14_DATA_BAR : SsmlXML.DATA_BAR, inParentWorksheet.getParentDoc());
058      mParentWorksheet = inParentWorksheet;
059
060      // Set some defaults
061      setMinLength(0);
062      setMaxLength(90);
063
064      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
065      {
066         addSubtag(new XMLTag(SsmlXML.CONDITIONAL_FORMATTING_VALUE_OBJ).setAttribute(SsmlXML.TYPE_ATT, "min"));
067         addSubtag(new XMLTag(SsmlXML.CONDITIONAL_FORMATTING_VALUE_OBJ).setAttribute(SsmlXML.TYPE_ATT, "max"));
068      }
069      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
070      {
071         // Extension properties
072         addSubtag(new XMLTag(SsmlXML.X14_CONDITIONAL_FORMATTING_VALUE_OBJ).setAttribute(SsmlXML.TYPE_ATT, "autoMin"));
073         addSubtag(new XMLTag(SsmlXML.X14_CONDITIONAL_FORMATTING_VALUE_OBJ).setAttribute(SsmlXML.TYPE_ATT, "autoMax"));
074         addSubtag(new XMLTag(SsmlXML.X14_NEGATIVE_FILL_COLOR).setAttribute(SsmlXML.RGB_ATT, colorToCT_Color(HTMLColor.RED)));
075         addSubtag(new XMLTag(SsmlXML.X14_AXIS_COLOR).setAttribute(SsmlXML.RGB_ATT, colorToCT_Color(HTMLColor.BLACK)));
076      }
077      else
078      {
079         throw new ProgrammingException("Unrecognized namespace option for conditionalFormatting tag: " + inNamespace);
080      }
081   }
082
083
084   //###########################################################################
085   // PUBLIC METHODS
086   //###########################################################################
087
088   //---------------------------------------------------------------------------
089   public void setExtension(SsmlDataBar inValue)
090   {
091      mDataBarExtension = inValue;
092   }
093
094   //---------------------------------------------------------------------------
095   /**
096    Specifies the minimum length of the data bar, as a percentage of the cell's width.
097    Default is 0.
098    * @param inValue the minimum percent of the cell width for data bar values
099    * @return this SsmlDataBar object to enable method chaining.
100    */
101   public SsmlDataBar setMinLength(Integer inValue)
102   {
103      if (inValue != null)
104      {
105         setAttribute(SsmlXML.MIN_LENGTH_ATT, inValue);
106      }
107      else
108      {
109         removeAttribute(SsmlXML.MIN_LENGTH_ATT);
110      }
111
112      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE)
113          && mDataBarExtension != null)
114      {
115         mDataBarExtension.setMinLength(inValue);
116      }
117
118      return this;
119   }
120
121   //---------------------------------------------------------------------------
122   /**
123    Specifies the maximum length of the data bar, as a percentage of the cell's width.
124    Default is 90.
125    * @param inValue the maximum percent of the cell width for data bar values
126    * @return this SsmlDataBar object to enable method chaining.
127    */
128   public SsmlDataBar setMaxLength(Integer inValue)
129   {
130      if (inValue != null)
131      {
132         setAttribute(SsmlXML.MAX_LENGTH_ATT, inValue);
133      }
134      else
135      {
136         removeAttribute(SsmlXML.MAX_LENGTH_ATT);
137      }
138
139      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE)
140          && mDataBarExtension != null)
141      {
142         mDataBarExtension.setMaxLength(inValue);
143      }
144
145      return this;
146   }
147
148   //---------------------------------------------------------------------------
149   /**
150    Specifies whether or not to display the numeric value to the right of the data bar.
151    Default is "true".
152    * @param inValue whether or not to display the numeric value
153    * @return this SsmlDataBar object to enable method chaining.
154    */
155   public SsmlDataBar setShowValue(Boolean inValue)
156   {
157      if (inValue != null)
158      {
159         setAttribute(SsmlXML.SHOW_VALUE_ATT, inValue ? "true" : "false");
160      }
161      else
162      {
163         removeAttribute(SsmlXML.SHOW_VALUE_ATT);
164      }
165
166      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE)
167          && mDataBarExtension != null)
168      {
169         mDataBarExtension.setShowValue(inValue);
170      }
171
172      return this;
173   }
174
175   //---------------------------------------------------------------------------
176   public SsmlDataBar setColor(Color inValue)
177   {
178      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
179      {
180         // Check if it has been added via addSubtag()...
181         XMLTag colorTag = getOptionalSubtagByName(SsmlXML.COLOR);
182         if (null == colorTag)
183         {
184            colorTag = new XMLTag(SsmlXML.COLOR);
185            addSubtag(colorTag);
186         }
187
188         colorTag.setAttribute(SsmlXML.RGB_ATT, colorToCT_Color(inValue));
189
190         if (mDataBarExtension != null)
191         {
192            mDataBarExtension.setColor(inValue);
193         }
194      }
195      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
196      {
197/*         // Check if it has been added via addSubtag()...
198         XMLTag colorTag = getOptionalSubtagByName(SsmlXML.X14_FILL_COLOR);
199         if (null == colorTag)
200         {
201            colorTag = new XMLTag(SsmlXML.X14_FILL_COLOR);
202            addSubtag(colorTag);
203         }
204
205         colorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue));
206*/
207      }
208      else
209      {
210         throw new ProgrammingException("Unrecognized namespace option for dataBar tag: " + getNamespace());
211      }
212
213      return this;
214   }
215
216   //---------------------------------------------------------------------------
217   public SsmlDataBar setBorderColor(Color inValue)
218   {
219      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
220      {
221         if (mDataBarExtension != null)
222         {
223            mDataBarExtension.setBorderColor(inValue);
224         }
225      }
226      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
227      {
228         // Check if it has been added via addSubtag()...
229         XMLTag borderColorTag = getOptionalSubtagByName(SsmlXML.X14_BORDER_COLOR);
230         if (null == borderColorTag)
231         {
232            borderColorTag = new XMLTag(SsmlXML.X14_BORDER_COLOR);
233            addSubtag(borderColorTag);
234         }
235
236         if (inValue != null)
237         {
238            borderColorTag.setAttribute(SsmlXML.RGB_ATT, colorToCT_Color(inValue));
239            setBorder(true);
240         }
241         else
242         {
243            removeSubtag(borderColorTag);
244         }
245      }
246      else
247      {
248         throw new ProgrammingException("Unrecognized namespace option for dataBar tag: " + getNamespace());
249      }
250
251      return this;
252   }
253
254   //---------------------------------------------------------------------------
255   public SsmlDataBar setGradient(Boolean inValue)
256   {
257      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
258      {
259         if (mDataBarExtension != null)
260         {
261            mDataBarExtension.setGradient(inValue);
262         }
263      }
264      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
265      {
266         if (inValue != null)
267         {
268            setAttribute(SsmlXML.X14_GRADIENT_ATT, inValue ? "1" : "0");
269         }
270         else
271         {
272            removeAttribute(SsmlXML.X14_GRADIENT_ATT);
273         }
274      }
275      else
276      {
277         throw new ProgrammingException("Unrecognized namespace option for dataBar tag: " + getNamespace());
278      }
279
280      return this;
281   }
282
283   //---------------------------------------------------------------------------
284   public SsmlDataBar setBorder(Boolean inValue)
285   {
286      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
287      {
288         if (mDataBarExtension != null)
289         {
290            mDataBarExtension.setBorder(inValue);
291         }
292      }
293      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
294      {
295         if (inValue != null)
296         {
297            setAttribute(SsmlXML.X14_BORDER_ATT, inValue ? "1" : "0");
298         }
299         else
300         {
301            removeAttribute(SsmlXML.X14_BORDER_ATT);
302         }
303      }
304      else
305      {
306         throw new ProgrammingException("Unrecognized namespace option for dataBar tag: " + getNamespace());
307      }
308
309      return this;
310   }
311}