001package com.hfg.xml.msofficexml.xlsx.spreadsheetml; 002 003import java.awt.Color; 004import java.awt.Font; 005 006import com.hfg.graphics.ColorUtil; 007import com.hfg.xml.XMLTag; 008import com.hfg.xml.msofficexml.xlsx.ExcelIndexedColor; 009import com.hfg.xml.msofficexml.xlsx.Xlsx; 010import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlTextVerticalAlignRunType; 011import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlUnderlineType; 012 013//------------------------------------------------------------------------------ 014/** 015 Represents an Office Open XML rich text run properties (<ssml:rPr>) tag. 016 This is the root tag of a table part. 017 018 @author J. Alex Taylor, hairyfatguy.com 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class SsmlTextRunProperties extends SsmlXMLTag 042{ 043 private XMLTag mBoldTag; 044 private XMLTag mColorTag; 045 private XMLTag mFontTag; 046 private XMLTag mFontSizeTag; 047 private XMLTag mItalicTag; 048 private XMLTag mShadowTag; 049 private XMLTag mStrikeThroughTag; 050 private XMLTag mCondenseTag; 051 private XMLTag mExtendTag; 052 private XMLTag mUnderlineTag; 053 private XMLTag mVertAlignTag; 054 055 //########################################################################## 056 // CONSTRUCTORS 057 //########################################################################## 058 059 //--------------------------------------------------------------------------- 060 public SsmlTextRunProperties(Xlsx inXlsx) 061 { 062 super(SsmlXML.TEXT_RUN_PROPS, inXlsx); 063 064 setDefaults(); 065 } 066 067 //--------------------------------------------------------------------------- 068 private void setDefaults() 069 { 070 } 071 072 //########################################################################## 073 // PUBLIC METHODS 074 //########################################################################## 075 076 //--------------------------------------------------------------------------- 077 public SsmlTextRunProperties setFont(Font inValue) 078 { 079 if (null == mFontTag) 080 { 081 // Check it it has been added via addSubtag()... 082 mFontTag = getOptionalSubtagByName(SsmlXML.TEXT_RUN_FONT); 083 } 084 085 if (inValue != null) 086 { 087 if (null == mFontTag) 088 { 089 mFontTag = new XMLTag(SsmlXML.TEXT_RUN_FONT); 090 addSubtag(mFontTag); 091 } 092 093 mFontTag.setAttribute(SsmlXML.VALUE_ATT, inValue.getName()); 094 095 setFontSize(inValue.getSize()); 096 } 097 else if (mFontTag != null) 098 { 099 removeSubtag(mFontTag); 100 mFontTag = null; 101 } 102 103 return this; 104 } 105 106 //--------------------------------------------------------------------------- 107 public SsmlTextRunProperties setFontSize(Integer inValue) 108 { 109 if (null == mFontSizeTag) 110 { 111 // Check it it has been added via addSubtag()... 112 mFontSizeTag = getOptionalSubtagByName(SsmlXML.SIZE); 113 } 114 115 if (inValue != null) 116 { 117 if (null == mFontSizeTag) 118 { 119 mFontSizeTag = new XMLTag(SsmlXML.SIZE); 120 addSubtag(mFontSizeTag); 121 } 122 123 mFontSizeTag.setAttribute(SsmlXML.VALUE_ATT, inValue); 124 } 125 else if (mFontSizeTag != null) 126 { 127 removeSubtag(mFontSizeTag); 128 mFontSizeTag = null; 129 } 130 131 return this; 132 } 133 134 //--------------------------------------------------------------------------- 135 public SsmlTextRunProperties setBold(boolean inValue) 136 { 137 if (null == mBoldTag) 138 { 139 // Check it it has been added via addSubtag()... 140 mBoldTag = getOptionalSubtagByName(SsmlXML.BOLD); 141 } 142 143 if (inValue) 144 { 145 if (null == mBoldTag) 146 { 147 mBoldTag = new XMLTag(SsmlXML.BOLD); 148 addSubtag(mBoldTag); 149 } 150 } 151 else if (mBoldTag != null) 152 { 153 removeSubtag(mBoldTag); 154 mBoldTag = null; 155 } 156 157 return this; 158 } 159 160 //--------------------------------------------------------------------------- 161 public SsmlTextRunProperties setItalic(boolean inValue) 162 { 163 if (null == mItalicTag) 164 { 165 // Check it it has been added via addSubtag()... 166 mItalicTag = getOptionalSubtagByName(SsmlXML.ITALIC); 167 } 168 169 if (inValue) 170 { 171 if (null == mItalicTag) 172 { 173 mItalicTag = new XMLTag(SsmlXML.ITALIC); 174 addSubtag(mItalicTag); 175 } 176 } 177 else if (mItalicTag != null) 178 { 179 removeSubtag(mItalicTag); 180 mItalicTag = null; 181 } 182 183 return this; 184 } 185 186 //--------------------------------------------------------------------------- 187 public SsmlTextRunProperties setShadow(boolean inValue) 188 { 189 if (null == mShadowTag) 190 { 191 // Check it it has been added via addSubtag()... 192 mShadowTag = getOptionalSubtagByName(SsmlXML.SHADOW); 193 } 194 195 if (inValue) 196 { 197 if (null == mShadowTag) 198 { 199 mShadowTag = new XMLTag(SsmlXML.SHADOW); 200 addSubtag(mShadowTag); 201 } 202 } 203 else if (mShadowTag != null) 204 { 205 removeSubtag(mShadowTag); 206 mShadowTag = null; 207 } 208 209 return this; 210 } 211 212 //--------------------------------------------------------------------------- 213 public SsmlTextRunProperties setStrikeThrough(boolean inValue) 214 { 215 if (null == mStrikeThroughTag) 216 { 217 // Check it it has been added via addSubtag()... 218 mStrikeThroughTag = getOptionalSubtagByName(SsmlXML.STRIKE); 219 } 220 221 if (inValue) 222 { 223 if (null == mStrikeThroughTag) 224 { 225 mStrikeThroughTag = new XMLTag(SsmlXML.STRIKE); 226 addSubtag(mStrikeThroughTag); 227 } 228 } 229 else if (mStrikeThroughTag != null) 230 { 231 removeSubtag(mStrikeThroughTag); 232 mStrikeThroughTag = null; 233 } 234 235 return this; 236 } 237 238 //--------------------------------------------------------------------------- 239 public SsmlTextRunProperties setCondense(boolean inValue) 240 { 241 if (null == mCondenseTag) 242 { 243 // Check it it has been added via addSubtag()... 244 mCondenseTag = getOptionalSubtagByName(SsmlXML.CONDENSE); 245 } 246 247 if (inValue) 248 { 249 if (null == mCondenseTag) 250 { 251 mCondenseTag = new XMLTag(SsmlXML.CONDENSE); 252 addSubtag(mCondenseTag); 253 } 254 } 255 else if (mCondenseTag != null) 256 { 257 removeSubtag(mCondenseTag); 258 mCondenseTag = null; 259 } 260 261 return this; 262 } 263 264 //--------------------------------------------------------------------------- 265 public SsmlTextRunProperties setExtend(boolean inValue) 266 { 267 if (null == mExtendTag) 268 { 269 // Check it it has been added via addSubtag()... 270 mExtendTag = getOptionalSubtagByName(SsmlXML.EXTEND); 271 } 272 273 if (inValue) 274 { 275 if (null == mExtendTag) 276 { 277 mExtendTag = new XMLTag(SsmlXML.EXTEND); 278 addSubtag(mExtendTag); 279 } 280 } 281 else if (mExtendTag != null) 282 { 283 removeSubtag(mExtendTag); 284 mExtendTag = null; 285 } 286 287 return this; 288 } 289 290 //--------------------------------------------------------------------------- 291 public SsmlTextRunProperties setColor(Color inValue) 292 { 293 if (null == mColorTag) 294 { 295 // Check it it has been added via addSubtag()... 296 mColorTag = getOptionalSubtagByName(SsmlXML.COLOR); 297 } 298 299 if (inValue != null) 300 { 301 if (null == mColorTag) 302 { 303 mColorTag = new XMLTag(SsmlXML.COLOR); 304 addSubtag(mColorTag); 305 } 306 307 if (inValue instanceof ExcelIndexedColor) 308 { 309 mColorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor)inValue).getIndex()); 310 } 311 else 312 { 313 mColorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inValue)); 314 } 315 } 316 else if (mColorTag != null) 317 { 318 removeSubtag(mColorTag); 319 mColorTag = null; 320 } 321 322 return this; 323 } 324 325 //--------------------------------------------------------------------------- 326 public SsmlTextRunProperties setVertAlign(SsmlTextVerticalAlignRunType inValue) 327 { 328 if (null == mVertAlignTag) 329 { 330 // Check it it has been added via addSubtag()... 331 mVertAlignTag = getOptionalSubtagByName(SsmlXML.VERTICAL_ALIGN); 332 } 333 334 if (inValue != null) 335 { 336 if (null == mVertAlignTag) 337 { 338 mVertAlignTag = new XMLTag(SsmlXML.VERTICAL_ALIGN); 339 addSubtag(mVertAlignTag); 340 } 341 342 mVertAlignTag.setAttribute(SsmlXML.VALUE_ATT, inValue.name()); 343 } 344 else if (mVertAlignTag != null) 345 { 346 removeSubtag(mVertAlignTag); 347 mVertAlignTag = null; 348 } 349 350 return this; 351 } 352 353 //--------------------------------------------------------------------------- 354 public SsmlTextRunProperties setUnderline(SsmlUnderlineType inValue) 355 { 356 if (null == mUnderlineTag) 357 { 358 // Check it it has been added via addSubtag()... 359 mUnderlineTag = getOptionalSubtagByName(SsmlXML.UNDERLINE); 360 } 361 362 if (inValue != null) 363 { 364 if (null == mUnderlineTag) 365 { 366 mUnderlineTag = new XMLTag(SsmlXML.UNDERLINE); 367 addSubtag(mUnderlineTag); 368 } 369 370 mUnderlineTag.setAttribute(SsmlXML.VALUE_ATT, inValue.name()); 371 } 372 else if (mUnderlineTag != null) 373 { 374 removeSubtag(mUnderlineTag); 375 mUnderlineTag = null; 376 } 377 378 return this; 379 } 380 381}