001package com.hfg.xml.msofficexml.xlsx.spreadsheetml.style; 002 003 004 005import com.hfg.graphics.ColorUtil; 006import com.hfg.util.CompareUtil; 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.SsmlXML; 011import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXMLTag; 012 013import java.awt.Color; 014import java.util.ArrayList; 015import java.util.List; 016 017//------------------------------------------------------------------------------ 018/** 019 Represents an Office Open XML spreadsheetml (<ssml:border>) tag. 020 021 @author J. Alex Taylor, hairyfatguy.com 022 */ 023//------------------------------------------------------------------------------ 024// com.hfg XML/HTML Coding Library 025// 026// This library is free software; you can redistribute it and/or 027// modify it under the terms of the GNU Lesser General Public 028// License as published by the Free Software Foundation; either 029// version 2.1 of the License, or (at your option) any later version. 030// 031// This library is distributed in the hope that it will be useful, 032// but WITHOUT ANY WARRANTY; without even the implied warranty of 033// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 034// Lesser General Public License for more details. 035// 036// You should have received a copy of the GNU Lesser General Public 037// License along with this library; if not, write to the Free Software 038// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 039// 040// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 041// jataylor@hairyfatguy.com 042//------------------------------------------------------------------------------ 043 044public class SsmlBorder extends SsmlXMLTag implements Comparable 045{ 046 private Integer mIndex; 047 048 //########################################################################## 049 // CONSTRUCTORS 050 //########################################################################## 051 052 //--------------------------------------------------------------------------- 053 public SsmlBorder(Xlsx inXlsx) 054 { 055 this(inXlsx.getStylesPart()); 056 } 057 058 //--------------------------------------------------------------------------- 059 public SsmlBorder(SsmlXMLTag inSsmlXMLTag) 060 { 061 this(inSsmlXMLTag.getParentDoc().getStylesPart()); 062 } 063 064 //--------------------------------------------------------------------------- 065 public SsmlBorder(StylesPart inStylesPart) 066 { 067 super(SsmlXML.BORDER, inStylesPart.getParentDoc()); 068 // Register with the styles part 069 mIndex = inStylesPart.defineBorder(this); 070 071 // For some reason, 'start' and 'end' were defined in the Xsd instead of 'left' and 'right'. 072 // I edited the sml.xsd to correct this. 073// addSubtag(new XMLTag(SsmlXML.START)); 074// addSubtag(new XMLTag(SsmlXML.END)); 075 addSubtag(new XMLTag(SsmlXML.LEFT)); 076 addSubtag(new XMLTag(SsmlXML.RIGHT)); 077 addSubtag(new XMLTag(SsmlXML.TOP)); 078 addSubtag(new XMLTag(SsmlXML.BOTTOM)); 079 addSubtag(new XMLTag(SsmlXML.DIAGONAL)); 080 } 081 082 083 //########################################################################## 084 // PUBLIC METHODS 085 //########################################################################## 086 087 //--------------------------------------------------------------------------- 088 @Override 089 public SsmlBorder clone() 090 { 091 SsmlBorder clone = (SsmlBorder) super.clone(); 092 093 // Register with the styles part 094 clone.mIndex = getParentDoc().getStylesPart().defineBorder(clone); 095 096 return clone; 097 } 098 099 //-------------------------------------------------------------------------- 100 @Override 101 public boolean equals(Object inObj2) 102 { 103 return (0 == compareTo(inObj2)); 104 } 105 106 //-------------------------------------------------------------------------- 107 @Override 108 public int compareTo(Object inObj2) 109 { 110 int result = -1; 111 112 if (inObj2 instanceof SsmlBorder) 113 { 114 SsmlBorder border2 = (SsmlBorder) inObj2; 115 116 for (SsmlBorderType borderType : SsmlBorderType.values()) 117 { 118 XMLTag type1 = getOptionalSubtagByName(borderType.getXMLName()); 119 XMLTag type2 = border2.getOptionalSubtagByName(borderType.getXMLName()); 120 121 result = CompareUtil.compare(type1, type2); 122 } 123 } 124 125 return result; 126 } 127 128 //--------------------------------------------------------------------------- 129 /** 130 Only for use by the StylesPart when setting a new value for the default font. 131 * @param inValue the new index value 132 * @return this object to enable chaining 133 */ 134 protected SsmlBorder setIndex(Integer inValue) 135 { 136 mIndex = inValue; 137 return this; 138 } 139 140 //--------------------------------------------------------------------------- 141 public Integer getIndex() 142 { 143 return mIndex; 144 } 145 146 //--------------------------------------------------------------------------- 147 public SsmlBorder set(SsmlBorderStyle inBorderStyle) 148 { 149 return set(null, inBorderStyle, null); 150 } 151 152 //--------------------------------------------------------------------------- 153 public SsmlBorder set(SsmlBorderType inBorderType, SsmlBorderStyle inBorderStyle) 154 { 155 return set(inBorderType, inBorderStyle, null); 156 } 157 158 //--------------------------------------------------------------------------- 159 public SsmlBorder set(SsmlBorderType inBorderType, SsmlBorderStyle inBorderStyle, Color inColor) 160 { 161 List<SsmlBorderType> borderTypes = new ArrayList<>(4); 162 if (inBorderType != null) 163 { 164 borderTypes.add(inBorderType); 165 } 166 else 167 { 168// borderTypes.add(SsmlBorderType.start); 169// borderTypes.add(SsmlBorderType.end); 170 borderTypes.add(SsmlBorderType.left); 171 borderTypes.add(SsmlBorderType.right); 172 borderTypes.add(SsmlBorderType.top); 173 borderTypes.add(SsmlBorderType.bottom); 174 } 175 176 for (SsmlBorderType borderType : borderTypes) 177 { 178 XMLTag subtag = getOptionalSubtagByName(borderType.getXMLName()); 179 if (null == subtag) 180 { 181 subtag = new XMLTag(borderType.getXMLName()); 182 addSubtag(subtag); 183 } 184 185 subtag.setAttribute(SsmlXML.STYLE_ATT, inBorderStyle.name()); 186 187 XMLTag colorTag = subtag.getOptionalSubtagByName(SsmlXML.COLOR); 188 if (null == colorTag) 189 { 190 colorTag = new XMLTag(SsmlXML.COLOR); 191 subtag.addSubtag(colorTag); 192 } 193 194 if (inColor != null) 195 { 196 if (inColor instanceof ExcelIndexedColor) 197 { 198 colorTag.setAttribute(SsmlXML.INDEXED_ATT, ((ExcelIndexedColor) inColor).getIndex()); 199 } 200 else 201 { 202 colorTag.setAttribute(SsmlXML.RGB_ATT, ColorUtil.colorToHex(inColor)); 203 } 204 } 205 else 206 { 207 colorTag.setAttribute(SsmlXML.AUTO_ATT, "1"); 208 } 209 } 210 211 return this; 212 } 213}