PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/yaytay/spudsoft-birt-excel-emitters
Java | 241 lines | 181 code | 25 blank | 35 comment | 70 complexity | 37e957a3ed6fb87cd516e5e2edbf25dd MD5 | raw file
Possible License(s): EPL-1.0
  1. /*************************************************************************************
  2. * Copyright (c) 2011, 2012, 2013 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * James Talbut - Initial implementation.
  12. ************************************************************************************/
  13. package uk.co.spudsoft.birt.emitters.excel;
  14. import org.apache.poi.ss.usermodel.BorderStyle;
  15. import org.apache.poi.ss.usermodel.CellStyle;
  16. import org.apache.poi.ss.usermodel.FillPatternType;
  17. import org.apache.poi.ss.usermodel.Font;
  18. import org.apache.poi.ss.usermodel.RichTextString;
  19. import org.apache.poi.ss.usermodel.Workbook;
  20. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  21. import org.apache.poi.xssf.usermodel.XSSFColor;
  22. import org.apache.poi.xssf.usermodel.XSSFFont;
  23. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  24. import org.apache.poi.xssf.usermodel.XSSFShape;
  25. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  26. import org.eclipse.birt.report.engine.content.IStyle;
  27. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  28. import org.eclipse.birt.report.engine.css.engine.value.css.CSSConstants;
  29. import org.eclipse.birt.report.engine.ir.DimensionType;
  30. import org.eclipse.birt.report.model.api.util.ColorUtil;
  31. import org.w3c.dom.css.CSSValue;
  32. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  33. /**
  34. * StyleManagerXUtils is an extension of the StyleManagerUtils to provide XSSFWorkbook specific functionality.
  35. * @author Jim Talbut
  36. *
  37. */
  38. public class StyleManagerXUtils extends StyleManagerUtils {
  39. private static Factory factory = new StyleManagerUtils.Factory() {
  40. public StyleManagerUtils create(Logger log) {
  41. return new StyleManagerXUtils(log);
  42. }
  43. };
  44. public static Factory getFactory() {
  45. return factory;
  46. }
  47. /**
  48. * @param log
  49. * Logger used by StyleManagerXUtils to record anything of interest.
  50. */
  51. public StyleManagerXUtils(Logger log) {
  52. super(log);
  53. }
  54. @Override
  55. public RichTextString createRichTextString(String value) {
  56. XSSFRichTextString result = new XSSFRichTextString(value);
  57. return result;
  58. }
  59. /**
  60. * Converts a BIRT border style into a POI BorderStyle.
  61. * @param birtBorder
  62. * The BIRT border style.
  63. * @param width
  64. * The width of the border as understood by BIRT.
  65. * @return
  66. * A POI BorderStyle object.
  67. */
  68. private BorderStyle poiBorderStyleFromBirt( String birtBorder, String width ) {
  69. if( "none".equals(birtBorder) ) {
  70. return BorderStyle.NONE;
  71. }
  72. double pxWidth = 3.0;
  73. if( CSSConstants.CSS_THIN_VALUE.equals( width ) ) {
  74. pxWidth = 1.0;
  75. } else if( CSSConstants.CSS_MEDIUM_VALUE.equals( width ) ) {
  76. pxWidth = 3.0;
  77. } else if( CSSConstants.CSS_THICK_VALUE.equals( width ) ) {
  78. pxWidth = 4.0;
  79. } else {
  80. DimensionType dim = DimensionType.parserUnit( width );
  81. if( dim != null ) {
  82. if( "px".equals(dim.getUnits()) ) {
  83. pxWidth = dim.getMeasure();
  84. }
  85. }
  86. }
  87. if( "solid".equals(birtBorder) ) {
  88. if( pxWidth < 2.9 ) {
  89. return BorderStyle.THIN;
  90. } else if( pxWidth < 3.1 ) {
  91. return BorderStyle.MEDIUM;
  92. } else {
  93. return BorderStyle.THICK;
  94. }
  95. } else if( "dashed".equals(birtBorder) ) {
  96. if( pxWidth < 2.9 ) {
  97. return BorderStyle.DASHED;
  98. } else {
  99. return BorderStyle.MEDIUM_DASHED;
  100. }
  101. } else if( "dotted".equals(birtBorder) ) {
  102. return BorderStyle.DOTTED;
  103. } else if( "double".equals(birtBorder) ) {
  104. return BorderStyle.DOUBLE;
  105. }
  106. log.debug( "Border style \"", birtBorder, "\" is not recognised." );
  107. return BorderStyle.NONE;
  108. }
  109. @Override
  110. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  111. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  112. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  113. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  114. String widthString = width == null ? "medium" : width.getCssText();
  115. if( style instanceof XSSFCellStyle ) {
  116. XSSFCellStyle xStyle = (XSSFCellStyle)style;
  117. BorderStyle xBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  118. XSSFColor xBorderColour = getXColour(colourString);
  119. if(xBorderStyle != BorderStyle.NONE) {
  120. switch( side ) {
  121. case TOP:
  122. xStyle.setBorderTop(xBorderStyle);
  123. xStyle.setTopBorderColor(xBorderColour);
  124. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  125. break;
  126. case LEFT:
  127. xStyle.setBorderLeft(xBorderStyle);
  128. xStyle.setLeftBorderColor(xBorderColour);
  129. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  130. break;
  131. case RIGHT:
  132. xStyle.setBorderRight(xBorderStyle);
  133. xStyle.setRightBorderColor(xBorderColour);
  134. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  135. break;
  136. case BOTTOM:
  137. xStyle.setBorderBottom(xBorderStyle);
  138. xStyle.setBottomBorderColor(xBorderColour);
  139. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. private XSSFColor getXColour(String colour) {
  147. int[] rgbInt = ColorUtil.getRGBs(colour);
  148. if( rgbInt == null ) {
  149. return null;
  150. }
  151. byte[] rgbByte = { (byte)-1, (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  152. // System.out.println( "The X colour for " + colour + " is [ " + rgbByte[0] + "," + rgbByte[1] + "," + rgbByte[2] + "," + rgbByte[3] + "]" );
  153. XSSFColor result = new XSSFColor( rgbByte );
  154. return result;
  155. }
  156. @Override
  157. public void addColourToFont(Workbook workbook, Font font, String colour) {
  158. if(colour == null) {
  159. return ;
  160. }
  161. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  162. return ;
  163. }
  164. if(font instanceof XSSFFont) {
  165. XSSFFont xFont = (XSSFFont)font;
  166. XSSFColor xColour = getXColour(colour);
  167. if(xColour != null) {
  168. xFont.setColor(xColour);
  169. }
  170. }
  171. }
  172. @Override
  173. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  174. if(colour == null) {
  175. return ;
  176. }
  177. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  178. return ;
  179. }
  180. if(style instanceof XSSFCellStyle) {
  181. XSSFCellStyle cellStyle = (XSSFCellStyle)style;
  182. XSSFColor xColour = getXColour(colour);
  183. if(xColour != null) {
  184. cellStyle.setFillForegroundColor(xColour);
  185. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  186. }
  187. }
  188. }
  189. @Override
  190. public Font correctFontColorIfBackground( FontManager fm, Workbook wb, BirtStyle birtStyle, Font font ) {
  191. CSSValue bgColour = birtStyle.getProperty( StylePropertyIndexes.STYLE_BACKGROUND_COLOR );
  192. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  193. XSSFColor colour = ((XSSFFont)font).getXSSFColor();
  194. int fgRgb[] = rgbOnly( colour.getARgb() );
  195. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  196. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  197. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  198. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  199. }
  200. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  201. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  202. addedStyle.setColor( contrastColour( bgRgb ) );
  203. return fm.getFontWithExtraStyle( font, addedStyle );
  204. } else {
  205. return font;
  206. }
  207. }
  208. @Override
  209. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  210. return (int)(widthMM * 36000);
  211. }
  212. @Override
  213. public int anchorDyFromPoints( float height, float rowHeight ) {
  214. return (int)( height * XSSFShape.EMU_PER_POINT );
  215. }
  216. }