001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003import com.hfg.exception.ProgrammingException;
004import com.hfg.xml.XMLNamespace;
005import com.hfg.xml.XMLTag;
006import com.hfg.xml.msofficexml.xlsx.CellRange;
007
008
009//------------------------------------------------------------------------------
010/**
011 Represents an Office Open XML sheet data (<ssml:conditionalFormatting>) 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 SsmlConditionalFormatting extends SsmlXMLTag
037{
038   private SsmlWorksheet mParentWorksheet;
039   private CellRange mRange;
040
041   //###########################################################################
042   // CONSTRUCTORS
043   //###########################################################################
044
045   //---------------------------------------------------------------------------
046   public SsmlConditionalFormatting(SsmlWorksheet inParentWorksheet, CellRange inRange)
047   {
048      this(inParentWorksheet, inRange, SsmlXML.SPREADSHEETML_NAMESPACE);
049   }
050
051   //---------------------------------------------------------------------------
052   public SsmlConditionalFormatting(SsmlWorksheet inParentWorksheet, CellRange inRange, XMLNamespace inNamespace)
053   {
054      super(inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE) ? SsmlXML.X14_CONDITIONAL_FORMATTING : SsmlXML.CONDITIONAL_FORMATTING, inParentWorksheet.getParentDoc());
055      mParentWorksheet = inParentWorksheet;
056      mRange = inRange;
057
058      if (inNamespace.equals(SsmlXML.SPREADSHEETML_NAMESPACE))
059      {
060         // The cell reference is an attribute in this schema
061         setAttribute(SsmlXML.SEQ_OF_REFS_ATT, inRange.toString());
062      }
063      else if (inNamespace.equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
064      {
065         // The cell reference is a sub tag in this schema
066         addXMLNamespaceDeclaration(SsmlXML.EXCEL_2006_NAMESPACE);
067         XMLTag sqrefTag = addSubtag(SsmlXML.XM_SQREF);
068         sqrefTag.setContent(inRange.toString());
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 SsmlConditionalFormatting setPivot(boolean inValue)
082   {
083      setAttribute(SsmlXML.PIVOT_ATT, inValue);
084
085      return this;
086   }
087
088   //---------------------------------------------------------------------------
089   public SsmlCfRule addRule(SsmlCfRuleType inType)
090   {
091      SsmlCfRule rule;
092      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
093      {
094         rule = new SsmlCfRule(mParentWorksheet, inType);
095         addSubtag(rule);
096
097         // Create an extension for the rule
098         SsmlCfRule ruleExtension = mParentWorksheet.getOrAddExtension(SsmlXML.SPREADSHEETML_2009_NAMESPACE, SsmlExtension.URI_Type.conditionalFormattings)
099               .addConditionalFormatting(getRange())
100               .addRule(inType);
101
102         rule.setExtension(ruleExtension);
103      }
104      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
105      {
106         rule = new SsmlCfRule(mParentWorksheet, inType, SsmlXML.SPREADSHEETML_2009_NAMESPACE);
107         addSubtag(rule);
108      }
109      else
110      {
111         throw new ProgrammingException("Unexpected namespace: " + getNamespace());
112      }
113
114
115      return rule;
116   }
117
118   //---------------------------------------------------------------------------
119   public SsmlCfColorScaleRule addColorScaleRule()
120   {
121      SsmlCfColorScaleRule rule;
122      if (getNamespace().equals(SsmlXML.SPREADSHEETML_NAMESPACE))
123      {
124         rule = new SsmlCfColorScaleRule(mParentWorksheet);
125         addSubtag(rule);
126
127         // Create an extension for the rule
128         SsmlCfRule ruleExtension = mParentWorksheet.getOrAddExtension(SsmlXML.SPREADSHEETML_2009_NAMESPACE, SsmlExtension.URI_Type.conditionalFormattings)
129               .addConditionalFormatting(getRange())
130               .addRule(SsmlCfRuleType.colorScale);
131
132         rule.setExtension(ruleExtension);
133      }
134      else if (getNamespace().equals(SsmlXML.SPREADSHEETML_2009_NAMESPACE))
135      {
136         rule = new SsmlCfColorScaleRule(mParentWorksheet, SsmlXML.SPREADSHEETML_2009_NAMESPACE);
137         addSubtag(rule);
138      }
139      else
140      {
141         throw new ProgrammingException("Unexpected namespace: " + getNamespace());
142      }
143
144      return rule;
145   }
146
147   //---------------------------------------------------------------------------
148   public CellRange getRange()
149   {
150      return mRange;
151   }
152
153}