PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/SpudSoft BIRT Excel Emitters/src/uk/co/spudsoft/birt/emitters/excel/StyleManagerHUtils.java

https://bitbucket.org/ravisanthu0/spudsoft-birt-excel-emitters1
Java | 287 lines | 208 code | 29 blank | 50 comment | 89 complexity | f52bc965a2ba649aac4b70c046bcb23d MD5 | raw file
Possible License(s): GPL-3.0
  1. /********************************************************************************
  2. * (C) Copyright 2011, by James Talbut.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  18. * in the United States and other countries.]
  19. ********************************************************************************/
  20. package uk.co.spudsoft.birt.emitters.excel;
  21. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  22. import org.apache.poi.hssf.usermodel.HSSFFont;
  23. import org.apache.poi.hssf.usermodel.HSSFPalette;
  24. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.hssf.util.HSSFColor;
  27. import org.apache.poi.ss.usermodel.CellStyle;
  28. import org.apache.poi.ss.usermodel.Font;
  29. import org.apache.poi.ss.usermodel.RichTextString;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  33. import org.eclipse.birt.report.engine.content.IPageContent;
  34. import org.eclipse.birt.report.engine.content.IStyle;
  35. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  36. import org.eclipse.birt.report.engine.css.engine.StyleConstants;
  37. import org.eclipse.birt.report.engine.ir.DimensionType;
  38. import org.eclipse.birt.report.model.api.util.ColorUtil;
  39. import org.w3c.dom.css.CSSValue;
  40. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  41. /**
  42. * StyleManagerHUtils is an extension of the StyleManagerUtils to provide HSSFWorkbook specific functionality.
  43. * @author Jim Talbut
  44. *
  45. */
  46. public class StyleManagerHUtils extends StyleManagerUtils {
  47. private static Factory factory = new StyleManagerUtils.Factory() {
  48. @Override
  49. public StyleManagerUtils create(Logger log) {
  50. return new StyleManagerHUtils(log);
  51. }
  52. };
  53. public static Factory getFactory() {
  54. return factory;
  55. }
  56. /**
  57. * @param log
  58. * Logger used by StyleManagerHUtils to record anything of interest.
  59. */
  60. public StyleManagerHUtils(Logger log) {
  61. super(log);
  62. }
  63. @Override
  64. public RichTextString createRichTextString(String value) {
  65. return new HSSFRichTextString(value);
  66. }
  67. /**
  68. * Converts a BIRT border style into a POI border style (short constant defined in CellStyle).
  69. * @param birtBorder
  70. * The BIRT border style.
  71. * @param width
  72. * The width of the border as understood by BIRT.
  73. * @return
  74. * One of the CellStyle BORDER constants.
  75. */
  76. private short poiBorderStyleFromBirt( String birtBorder, String width ) {
  77. if( "none".equals(birtBorder) ) {
  78. return CellStyle.BORDER_NONE;
  79. }
  80. DimensionType dim = DimensionType.parserUnit( width );
  81. double pxWidth = 3.0;
  82. if( ( dim != null ) && ( "px".equals(dim.getUnits()) ) ){
  83. pxWidth = dim.getMeasure();
  84. }
  85. if( "solid".equals(birtBorder) ) {
  86. if( pxWidth < 2.9 ) {
  87. return CellStyle.BORDER_THIN;
  88. } else if( pxWidth < 3.1 ) {
  89. return CellStyle.BORDER_MEDIUM;
  90. } else {
  91. return CellStyle.BORDER_THICK;
  92. }
  93. } else if( "dashed".equals(birtBorder) ) {
  94. if( pxWidth < 2.9 ) {
  95. return CellStyle.BORDER_DASHED;
  96. } else {
  97. return CellStyle.BORDER_MEDIUM_DASHED;
  98. }
  99. } else if( "dotted".equals(birtBorder) ) {
  100. return CellStyle.BORDER_DOTTED;
  101. } else if( "double".equals(birtBorder) ) {
  102. return CellStyle.BORDER_DOUBLE;
  103. } else if( "none".equals(birtBorder) ) {
  104. return CellStyle.BORDER_NONE;
  105. }
  106. log.debug( "Border style \"", birtBorder, "\" is not recognised" );
  107. return CellStyle.BORDER_NONE;
  108. }
  109. /**
  110. * Get an HSSFPalette index for a workbook that closely approximates the passed in colour.
  111. * @param workbook
  112. * The workbook for which the colour is being sought.
  113. * @param colour
  114. * The colour, in the form "rgb(<i>r</i>, <i>g</i>, <i>b</i>)".
  115. * @return
  116. * The index into the HSSFPallete for the workbook for a colour that approximates the passed in colour.
  117. */
  118. private short getHColour( HSSFWorkbook workbook, String colour ) {
  119. int[] rgbInt = ColorUtil.getRGBs(colour);
  120. if( rgbInt == null ) {
  121. return 0;
  122. }
  123. byte[] rgbByte = new byte[] { (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  124. HSSFPalette palette = workbook.getCustomPalette();
  125. HSSFColor result = palette.findColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  126. if( result == null) {
  127. result = palette.findSimilarColor(rgbByte[0], rgbByte[1], rgbByte[2]);
  128. }
  129. return result.getIndex();
  130. }
  131. @Override
  132. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  133. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  134. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  135. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  136. String widthString = width == null ? "medium" : width.getCssText();
  137. if( style instanceof HSSFCellStyle ) {
  138. HSSFCellStyle hStyle = (HSSFCellStyle)style;
  139. short hBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  140. short colourIndex = getHColour((HSSFWorkbook)workbook, colourString);
  141. if( colourIndex > 0 ) {
  142. if(hBorderStyle != CellStyle.BORDER_NONE) {
  143. switch( side ) {
  144. case TOP:
  145. hStyle.setBorderTop(hBorderStyle);
  146. hStyle.setTopBorderColor(colourIndex);
  147. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  148. break;
  149. case LEFT:
  150. hStyle.setBorderLeft(hBorderStyle);
  151. hStyle.setLeftBorderColor(colourIndex);
  152. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  153. break;
  154. case RIGHT:
  155. hStyle.setBorderRight(hBorderStyle);
  156. hStyle.setRightBorderColor(colourIndex);
  157. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  158. break;
  159. case BOTTOM:
  160. hStyle.setBorderBottom(hBorderStyle);
  161. hStyle.setBottomBorderColor(colourIndex);
  162. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. @Override
  171. public void addColourToFont(Workbook workbook, Font font, String colour) {
  172. if(colour == null) {
  173. return ;
  174. }
  175. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  176. return ;
  177. }
  178. if(font instanceof HSSFFont) {
  179. HSSFFont hFont = (HSSFFont)font;
  180. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  181. if( colourIndex > 0 ) {
  182. hFont.setColor(colourIndex);
  183. }
  184. }
  185. }
  186. @Override
  187. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  188. if(colour == null) {
  189. return ;
  190. }
  191. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  192. return ;
  193. }
  194. if(style instanceof HSSFCellStyle) {
  195. HSSFCellStyle cellStyle = (HSSFCellStyle)style;
  196. short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
  197. if( colourIndex > 0 ) {
  198. cellStyle.setFillForegroundColor(colourIndex);
  199. cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  200. }
  201. }
  202. }
  203. @Override
  204. public Font correctFontColorIfBackground(FontManager fm, Workbook wb, BirtStyle birtStyle, Font font) {
  205. HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette();
  206. CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
  207. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  208. short fgRgb[] = HSSFColor.BLACK.triplet;
  209. if( ( font != null ) && ( font.getColor() != Short.MAX_VALUE ) ) {
  210. fgRgb = palette.getColor(font.getColor()).getTriplet();
  211. }
  212. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  213. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  214. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  215. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  216. }
  217. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  218. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  219. addedStyle.setColor( contrastColour( bgRgb ) );
  220. return fm.getFontWithExtraStyle( font, addedStyle );
  221. } else {
  222. return font;
  223. }
  224. }
  225. @Override
  226. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  227. return (int)( 1023.0 * widthMM / colWidthMM );
  228. }
  229. @Override
  230. public int anchorDyFromPoints( float height, float rowHeight ) {
  231. return (int)( 255.0 * height / rowHeight );
  232. }
  233. @Override
  234. public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
  235. double headerHeight = 0.5;
  236. double footerHeight = 0.5;
  237. if( ( page.getHeaderHeight() != null ) && isAbsolute( page.getHeaderHeight() ) ) {
  238. headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
  239. sheet.getPrintSetup().setHeaderMargin(headerHeight);
  240. }
  241. if( ( page.getFooterHeight() != null ) && isAbsolute( page.getFooterHeight() ) ) {
  242. footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
  243. sheet.getPrintSetup().setFooterMargin(footerHeight);
  244. }
  245. if( ( page.getMarginBottom() != null ) && isAbsolute( page.getMarginBottom() ) ) {
  246. sheet.setMargin(Sheet.BottomMargin, footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
  247. }
  248. if( ( page.getMarginLeft() != null ) && isAbsolute( page.getMarginLeft() ) ) {
  249. sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
  250. }
  251. if( ( page.getMarginRight() != null ) && isAbsolute( page.getMarginRight() ) ) {
  252. sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
  253. }
  254. if( ( page.getMarginTop() != null ) && isAbsolute( page.getMarginTop() ) ) {
  255. sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
  256. }
  257. }
  258. }