001package com.hfg.xml.msofficexml.xlsx.spreadsheetml; 002 003 004import com.hfg.util.StringUtil; 005import com.hfg.xml.msofficexml.xlsx.Xlsx; 006 007//------------------------------------------------------------------------------ 008/** 009 Represents an Office Open XML worksheet's header and footer (<ssml:headerFooter>) 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// Ex: 034// <ssml:headerFooter> 035// <ssml:oddHeader>string</ssml:oddHeader> 036// <ssml:oddFooter>string</ssml:oddFooter> 037// <ssml:evenHeader>string</ssml:evenHeader> 038// <ssml:evenFooter>string</ssml:evenFooter> 039// <ssml:firstHeader>string</ssml:firstHeader> 040// <ssml:firstFooter>string</ssml:firstFooter> 041// </ssml:headerFooter> 042public class SsmlHeaderFooter extends SsmlXMLTag 043{ 044 private SsmlXMLTag mOddHeaderTag; 045 private SsmlXMLTag mOddFooterTag; 046 047 //--------------------------------------------------------------------------- 048 public SsmlHeaderFooter(Xlsx inXlsx) 049 { 050 super(SsmlXML.HEADER_FOOTER, inXlsx); 051 init(); 052 } 053 054 055 //--------------------------------------------------------------------------- 056 private void init() 057 { 058 059 } 060 061 //--------------------------------------------------------------------------- 062 // Defaults to false 063 public SsmlHeaderFooter setDifferentOddEven(Boolean inValue) 064 { 065 if (inValue != null) 066 { 067 setAttribute(SsmlXML.DIFFERENT_ODD_EVEN_ATT, inValue); 068 } 069 else 070 { 071 removeAttribute(SsmlXML.DIFFERENT_ODD_EVEN_ATT); 072 } 073 074 return this; 075 } 076 077 //--------------------------------------------------------------------------- 078 // Defaults to false 079 public SsmlHeaderFooter setDifferentFirst(Boolean inValue) 080 { 081 if (inValue != null) 082 { 083 setAttribute(SsmlXML.DIFFERENT_FIRST_ATT, inValue); 084 } 085 else 086 { 087 removeAttribute(SsmlXML.DIFFERENT_FIRST_ATT); 088 } 089 090 return this; 091 } 092 093 //--------------------------------------------------------------------------- 094 // Defaults to true 095 public SsmlHeaderFooter setScaleWithDoc(Boolean inValue) 096 { 097 if (inValue != null) 098 { 099 setAttribute(SsmlXML.SCALE_WITH_DOC_ATT, inValue); 100 } 101 else 102 { 103 removeAttribute(SsmlXML.SCALE_WITH_DOC_ATT); 104 } 105 106 return this; 107 } 108 109 //--------------------------------------------------------------------------- 110 // Defaults to true 111 public SsmlHeaderFooter setAlignWithMargins(Boolean inValue) 112 { 113 if (inValue != null) 114 { 115 setAttribute(SsmlXML.ALIGN_WITH_MARGINS_ATT, inValue); 116 } 117 else 118 { 119 removeAttribute(SsmlXML.ALIGN_WITH_MARGINS_ATT); 120 } 121 122 return this; 123 } 124 125 //--------------------------------------------------------------------------- 126 // '&L' precedes the left header, '&C' precedes the center header, and '&R' precedes the right header 127 public SsmlHeaderFooter setHeader(String inValue) 128 { 129 SsmlXMLTag oddHeaderTag = null; 130 if (StringUtil.isSet(inValue)) 131 { 132 oddHeaderTag = new SsmlXMLTag(SsmlXML.ODD_HEADER, getParentDoc()); 133 oddHeaderTag.setContent(inValue); 134 } 135 136 setOddHeaderTag(oddHeaderTag); 137 138 setDifferentOddEven(null); 139 140 return this; 141 } 142 143 //--------------------------------------------------------------------------- 144 public String getHeader() 145 { 146 SsmlXMLTag oddHeaderTag = getOddHeaderTag(); 147 return (oddHeaderTag != null ? oddHeaderTag.getUnescapedContent() : null); 148 } 149 150 //--------------------------------------------------------------------------- 151 // '&P' codes for the current page number 152 // '&N' codes for the number of pages 153 // '&L' precedes the left footer, '&C' precedes the center footer, and '&R' precedes the right footer 154 public SsmlHeaderFooter setFooter(String inValue) 155 { 156 SsmlXMLTag oddFooterTag = null; 157 if (StringUtil.isSet(inValue)) 158 { 159 oddFooterTag = new SsmlXMLTag(SsmlXML.ODD_FOOTER, getParentDoc()); 160 oddFooterTag.setContent(inValue); 161 } 162 163 setOddFooterTag(oddFooterTag); 164 165 setDifferentOddEven(null); 166 167 return this; 168 } 169 170 //--------------------------------------------------------------------------- 171 public String getFooter() 172 { 173 SsmlXMLTag oddFooterTag = getOddFooterTag(); 174 return (oddFooterTag != null ? oddFooterTag.getUnescapedContent() : null); 175 } 176 177 //########################################################################### 178 // PRIVATE METHODS 179 //########################################################################### 180 181 //--------------------------------------------------------------------------- 182 private void setOddHeaderTag(SsmlXMLTag inTag) 183 { 184 // Remove the previous tag 185 SsmlXMLTag oddHeaderTag = getOddHeaderTag(); 186 if (oddHeaderTag != null) 187 { 188 removeSubtag(oddHeaderTag); 189 } 190 191 if (inTag != null) 192 { 193 addSubtag(inTag); 194 } 195 196 mOddHeaderTag = inTag; 197 } 198 199 //--------------------------------------------------------------------------- 200 private SsmlXMLTag getOddHeaderTag() 201 { 202 if (null == mOddHeaderTag) 203 { 204 // Check it it has been added via addSubtag()... 205 mOddHeaderTag = getOptionalSubtagByName(SsmlXML.ODD_HEADER); 206 } 207 208 return mOddHeaderTag; 209 } 210 211 //--------------------------------------------------------------------------- 212 private void setOddFooterTag(SsmlXMLTag inTag) 213 { 214 // Remove the previous tag 215 SsmlXMLTag oddFooterTag = getOddFooterTag(); 216 if (oddFooterTag != null) 217 { 218 removeSubtag(oddFooterTag); 219 } 220 221 if (inTag != null) 222 { 223 addSubtag(inTag); 224 } 225 226 mOddFooterTag = inTag; 227 } 228 229 //--------------------------------------------------------------------------- 230 private SsmlXMLTag getOddFooterTag() 231 { 232 if (null == mOddFooterTag) 233 { 234 // Check it it has been added via addSubtag()... 235 mOddFooterTag = getOptionalSubtagByName(SsmlXML.ODD_FOOTER); 236 } 237 238 return mOddFooterTag; 239 } 240 241}