001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003
004import com.hfg.exception.ProgrammingException;
005import com.hfg.xml.XMLNamespace;
006import com.hfg.xml.XMLTag;
007import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlDifferentialFormat;
008
009//------------------------------------------------------------------------------
010/**
011 Represents an Office Open XML conditional formatting rule (<ssml:cfRule>) tag.
012
013 @author J. Alex Taylor, hairyfatguy.com
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class SsmlCfRule extends SsmlXMLTag
037{
038   private final SsmlWorksheet mParentWorksheet;
039   private XMLTag        mExtLst;
040   private SsmlCfRule    mRuleExtension;
041
042   private static int sPrioritySrc = 1;
043
044
045   //###########################################################################
046   // CONSTRUCTORS
047   //###########################################################################
048
049   //---------------------------------------------------------------------------
050   public SsmlCfRule(SsmlWorksheet inParentWorksheet, SsmlCfRuleType inType)
051   {
052      this(inParentWorksheet, inType, SsmlXML.SPREADSHEETML_NAMESPACE);
053   }
054
055   //---------------------------------------------------------------------------
056   public SsmlCfRule(SsmlWorksheet inParentWorksheet, SsmlCfRuleType inType, XMLNamespace inNamespace)
057   {
058      super(inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE) ? SsmlXML.X14_CONDITIONAL_FORMATTING_RULE : SsmlXML.CONDITIONAL_FORMATTING_RULE, inParentWorksheet.getParentDoc());
059      mParentWorksheet = inParentWorksheet;
060      setAttribute(SsmlXML.TYPE_ATT, inType.name());
061
062      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
063      {
064         setPriority(sPrioritySrc++);
065      }
066      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
067      {
068         setAttribute(SsmlXML.ID, generateUID());
069      }
070      else
071      {
072         throw new ProgrammingException("Unrecognized namespace option for conditionalFormatting tag: " + inNamespace);
073      }
074   }
075
076   //###########################################################################
077   // PUBLIC METHODS
078   //###########################################################################
079
080   //---------------------------------------------------------------------------
081   public SsmlCfRuleType getType()
082   {
083      return SsmlCfRuleType.valueOf(getAttributeValue(SsmlXML.TYPE_ATT));
084   }
085
086   //---------------------------------------------------------------------------
087   public SsmlCfRule setPriority(int inValue)
088   {
089      setAttribute(SsmlXML.PRIORITY_ATT, inValue);
090
091      if (inValue > sPrioritySrc)
092      {
093         sPrioritySrc = inValue + 1;
094      }
095
096      return this;
097   }
098
099   //---------------------------------------------------------------------------
100   public void setExtension(SsmlCfRule inValue)
101   {
102      mRuleExtension = inValue;
103
104      // Create the local reference to the extension
105      SsmlExtension extension = new SsmlExtension(mParentWorksheet, SsmlXML.SPREADSHEETML_2009_NAMESPACE, SsmlExtension.URI_Type.id);
106      extension.addSubtag(new XMLTag(SsmlXML.X14_ID).setContent(mRuleExtension.getAttributeValue(SsmlXML.ID)));
107
108      // Extensions are collected under an extLst tag
109      if (null == mExtLst)
110      {
111         // Check if it has been added via addSubtag()...
112         mExtLst = getOptionalSubtagByName(SsmlXML.EXTENSION_LIST);
113         if (null == mExtLst)
114         {
115            mExtLst = new XMLTag(SsmlXML.EXTENSION_LIST);
116            addSubtag(mExtLst);
117         }
118      }
119
120      mExtLst.addSubtag(extension);
121   }
122
123   //---------------------------------------------------------------------------
124   public SsmlCfRule setFormat(SsmlDifferentialFormat inValue)
125   {
126      setAttribute(SsmlXML.DIFFERENTIAL_FORMAT_ID_ATT, inValue.getIndex());
127      return this;
128   }
129
130   //---------------------------------------------------------------------------
131   public SsmlCfRule setStopIfTrue(boolean inValue)
132   {
133      setAttribute(SsmlXML.STOP_IF_TRUE_ATT, inValue);
134
135      return this;
136   }
137
138   //---------------------------------------------------------------------------
139   public SsmlCfRule setAboveAverage(boolean inValue)
140   {
141      setAttribute(SsmlXML.ABOVE_AVG_ATT, inValue);
142
143      return this;
144   }
145
146   //---------------------------------------------------------------------------
147   public SsmlCfRule setTop10Percent(boolean inValue)
148   {
149      setAttribute(SsmlXML.PERCENT_ATT, inValue);
150
151      return this;
152   }
153
154   //---------------------------------------------------------------------------
155   public SsmlCfRule setBottom(boolean inValue)
156   {
157      setAttribute(SsmlXML.BOTTOM_ATT, inValue);
158
159      return this;
160   }
161
162   //---------------------------------------------------------------------------
163   public SsmlCfRule setEqualAverage(boolean inValue)
164   {
165      setAttribute(SsmlXML.EQUAL_AVG_ATT, inValue);
166
167      return this;
168   }
169
170   //---------------------------------------------------------------------------
171   public SsmlCfRule setOperator(SsmlCfOperator inValue)
172   {
173      if (inValue !=  null)
174      {
175         setAttribute(SsmlXML.OPERATOR_ATT, inValue.name());
176      }
177      else
178      {
179         removeAttribute(SsmlXML.OPERATOR_ATT);
180      }
181
182      return this;
183   }
184
185   //---------------------------------------------------------------------------
186   public SsmlCfRule setText(String inValue)
187   {
188      if (inValue !=  null)
189      {
190         setAttribute(SsmlXML.TEXT_ATT, inValue);
191      }
192      else
193      {
194         removeAttribute(SsmlXML.TEXT_ATT);
195      }
196
197      return this;
198   }
199
200   //---------------------------------------------------------------------------
201   public SsmlCfRule setTimePeriod(SsmlCfTimePeriod inValue)
202   {
203      if (inValue !=  null)
204      {
205         setAttribute(SsmlXML.TIME_PERIOD_ATT, inValue.name());
206      }
207      else
208      {
209         removeAttribute(SsmlXML.TIME_PERIOD_ATT);
210      }
211
212      return this;
213   }
214
215   //---------------------------------------------------------------------------
216   public SsmlCfRule setRank(Integer inValue)
217   {
218      if (inValue !=  null)
219      {
220         setAttribute(SsmlXML.RANK_ATT, inValue);
221      }
222      else
223      {
224         removeAttribute(SsmlXML.RANK_ATT);
225      }
226
227      return this;
228   }
229
230   //---------------------------------------------------------------------------
231   public SsmlCfRule setStdDev(Integer inValue)
232   {
233      if (inValue !=  null)
234      {
235         setAttribute(SsmlXML.STD_DEV_ATT, inValue);
236      }
237      else
238      {
239         removeAttribute(SsmlXML.STD_DEV_ATT);
240      }
241
242      return this;
243   }
244
245
246   //---------------------------------------------------------------------------
247   public SsmlDataBar getOrAddDataBar()
248   {
249      return getOrAddDataBar(getNamespace());
250   }
251
252   //---------------------------------------------------------------------------
253   private SsmlDataBar getOrAddDataBar(XMLNamespace inNamespace)
254   {
255      SsmlDataBar dataBarTag;
256      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
257      {
258         // Check if it has been added via addSubtag()...
259         dataBarTag = getOptionalSubtagByName(SsmlXML.DATA_BAR);
260         if (null == dataBarTag)
261         {
262            dataBarTag = new SsmlDataBar(mParentWorksheet);
263            addSubtag(dataBarTag);
264
265            // Create the extension for the data bar
266            SsmlDataBar dataBarExtension = mRuleExtension.getOrAddDataBar(SsmlXML.SPREADSHEETML_2009_NAMESPACE);
267
268            dataBarTag.setExtension(dataBarExtension);
269         }
270      }
271      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
272      {
273         // Check if it has been added via addSubtag()...
274         dataBarTag = getOptionalSubtagByName(SsmlXML.X14_DATA_BAR);
275         if (null == dataBarTag)
276         {
277            dataBarTag = new SsmlDataBar(mParentWorksheet, SsmlXML.SPREADSHEETML_2009_NAMESPACE);
278            addSubtag(dataBarTag);
279         }
280      }
281      else
282      {
283         throw new ProgrammingException("Unrecognized namespace option for cfRule tag: " + inNamespace);
284      }
285
286      return dataBarTag;
287   }
288
289   //---------------------------------------------------------------------------
290   public SsmlCfRule addFormula(String inValue)
291   {
292      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue));
293      return this;
294   }
295
296   //---------------------------------------------------------------------------
297   public SsmlCfRule addFormula(int inValue)
298   {
299      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue));
300      return this;
301   }
302
303   //---------------------------------------------------------------------------
304   public SsmlCfRule addFormula(float inValue)
305   {
306      addSubtag(new XMLTag(SsmlXML.FORMULA).setContent(inValue + ""));
307      return this;
308   }
309}
310/*
311<conditionalFormatting sqref="B2:AJ36">
312  <cfRule type="expression" dxfId="10" priority="1" stopIfTrue="1">
313    <formula>ROW()=COLUMN()</formula>
314  </cfRule>
315  <cfRule type="cellIs" dxfId="11" priority="2" stopIfTrue="1" operator="between">
316    <formula>90</formula>
317    <formula>100</formula>
318  </cfRule>
319  <cfRule type="cellIs" dxfId="9" priority="3" stopIfTrue="1" operator="between">
320    <formula>80</formula>
321    <formula>90</formula>
322  </cfRule>
323  <cfRule type="cellIs" dxfId="8" priority="4" stopIfTrue="1" operator="between">
324    <formula>70</formula>
325    <formula>80</formula>
326  </cfRule>
327  <cfRule type="cellIs" dxfId="7" priority="5" stopIfTrue="1" operator="between">
328    <formula>60</formula>
329    <formula>70</formula>
330  </cfRule>
331  <cfRule type="cellIs" dxfId="6" priority="7" stopIfTrue="1" operator="between">
332    <formula>0</formula>
333    <formula>60</formula>
334  </cfRule>
335</conditionalFormatting>
336 */