001package com.hfg.xml.msofficexml.xlsx.spreadsheetml; 002 003//------------------------------------------------------------------------------ 004 005import com.hfg.exception.InvalidValueException; 006import com.hfg.graphics.TextUtil; 007import com.hfg.graphics.units.GfxSize; 008import com.hfg.graphics.units.GfxUnits; 009import com.hfg.util.StringUtil; 010import com.hfg.xml.msofficexml.xlsx.spreadsheetml.style.SsmlCellFormat; 011 012/** 013 Represents an Office Open XML worksheet column (<ssml:col>) tag. 014 015 @author J. Alex Taylor, hairyfatguy.com 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// [email protected] 036//------------------------------------------------------------------------------ 037 038public class SsmlCol extends SsmlXMLTag 039{ 040 private SsmlWorksheet mParentWorksheet; 041 042 //--------------------------------------------------------------------------- 043 public SsmlCol(SsmlWorksheet inParentWorksheet) 044 { 045 super(SsmlXML.COLUMN, inParentWorksheet.getParentDoc()); 046 mParentWorksheet = inParentWorksheet; 047 } 048 049 //--------------------------------------------------------------------------- 050 /** 051 Sets the style to apply to this column's cells. 052 @param inValue cell format to apply 053 @return this column instance 054 */ 055 public SsmlCol setStyle(SsmlCellFormat inValue) 056 { 057 setAttribute(SsmlXML.STYLE_ATT, inValue.getIndex()); 058 059 return this; 060 } 061 062 //--------------------------------------------------------------------------- 063 /** 064 Sets the 1-based index of the first column that this spec applies to. 065 @param inValue 1-based index of the first column that this spec applies to 066 @return this column instance 067 */ 068 public SsmlCol setMin(int inValue) 069 { 070 if (inValue <= 0) 071 { 072 throw new InvalidValueException(StringUtil.singleQuote(inValue) + " is not a valid value! The Excel column min cannot be <= 0!"); 073 } 074 075 setAttribute(SsmlXML.MIN_ATT, inValue); 076 077 return this; 078 } 079 080 //--------------------------------------------------------------------------- 081 /** 082 Sets the 1-based index of the last column that this spec applies to. 083 @param inValue 1-based index of the last column that this spec applies to 084 @return this column instance 085 */ 086 public SsmlCol setMax(int inValue) 087 { 088 if (inValue <= 0) 089 { 090 throw new InvalidValueException(StringUtil.singleQuote(inValue) + " is not a valid value! The Excel column max cannot be <= 0!"); 091 } 092 093 setAttribute(SsmlXML.MAX_ATT, inValue); 094 095 return this; 096 } 097 098 //--------------------------------------------------------------------------- 099 public SsmlCol setHidden(boolean inValue) 100 { 101 setAttribute(SsmlXML.HIDDEN_ATT, inValue); 102 103 return this; 104 } 105 106 //--------------------------------------------------------------------------- 107 /** 108 Column width measured as the number of characters of the maximum digit width 109 of the numbers 0, 1, 2, …, 9 as rendered in the normal style's font. There 110 are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines. 111 @param inValue width to use for the column display 112 @return this column instance 113 */ 114 // width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}] / {Maximum Digit Width} * 256) / 256 115 public SsmlCol setWidth(float inValue) 116 { 117 setAttribute(SsmlXML.WIDTH_ATT, inValue); 118 setAttribute(SsmlXML.CUSTOM_WIDTH_ATT, "1"); 119 120 return this; 121 } 122 123 //--------------------------------------------------------------------------- 124 /** 125 Column width measured as the number of characters of the maximum digit width 126 of the numbers 0, 1, 2, …, 9 as rendered in the normal style's font. There 127 are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines. 128 @param inValue width to use for the column display 129 @return this column instance 130 */ 131 // width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel padding}] / {Maximum Digit Width} * 256) / 256 132 public SsmlCol setWidth(GfxSize inValue) 133 { 134 float digitWidth = (float) TextUtil.getStringRect("9", getParentDoc().getStylesPart().getCellFormats().get(0).getFont().toFont()).getWidth(); 135 136 float pixels = inValue.to(GfxUnits.pixels) - 5; 137 138 return setWidth(pixels / digitWidth); 139 } 140 141}