PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ravisanthu0/spudsoft-birt-excel-emitters1
Java | 278 lines | 210 code | 26 blank | 42 comment | 88 complexity | afd1e481fb2efa8d3030d90a89833b16 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.ss.usermodel.BorderStyle;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.usermodel.FillPatternType;
  24. import org.apache.poi.ss.usermodel.Font;
  25. import org.apache.poi.ss.usermodel.RichTextString;
  26. import org.apache.poi.ss.usermodel.Sheet;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  29. import org.apache.poi.xssf.usermodel.XSSFColor;
  30. import org.apache.poi.xssf.usermodel.XSSFFont;
  31. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  32. import org.apache.poi.xssf.usermodel.XSSFShape;
  33. import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
  34. import org.eclipse.birt.report.engine.content.IPageContent;
  35. import org.eclipse.birt.report.engine.content.IStyle;
  36. import org.eclipse.birt.report.engine.css.dom.AreaStyle;
  37. import org.eclipse.birt.report.engine.css.engine.StyleConstants;
  38. import org.eclipse.birt.report.engine.css.engine.value.css.CSSConstants;
  39. import org.eclipse.birt.report.engine.ir.DimensionType;
  40. import org.eclipse.birt.report.model.api.util.ColorUtil;
  41. import org.w3c.dom.css.CSSValue;
  42. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  43. /**
  44. * StyleManagerXUtils is an extension of the StyleManagerUtils to provide XSSFWorkbook specific functionality.
  45. * @author Jim Talbut
  46. *
  47. */
  48. public class StyleManagerXUtils extends StyleManagerUtils {
  49. private static Factory factory = new StyleManagerUtils.Factory() {
  50. @Override
  51. public StyleManagerUtils create(Logger log) {
  52. return new StyleManagerXUtils(log);
  53. }
  54. };
  55. public static Factory getFactory() {
  56. return factory;
  57. }
  58. /**
  59. * @param log
  60. * Logger used by StyleManagerXUtils to record anything of interest.
  61. */
  62. public StyleManagerXUtils(Logger log) {
  63. super(log);
  64. }
  65. @Override
  66. public RichTextString createRichTextString(String value) {
  67. XSSFRichTextString result = new XSSFRichTextString(value);
  68. return result;
  69. }
  70. /**
  71. * Converts a BIRT border style into a POI BorderStyle.
  72. * @param birtBorder
  73. * The BIRT border style.
  74. * @param width
  75. * The width of the border as understood by BIRT.
  76. * @return
  77. * A POI BorderStyle object.
  78. */
  79. private BorderStyle poiBorderStyleFromBirt( String birtBorder, String width ) {
  80. if( "none".equals(birtBorder) ) {
  81. return BorderStyle.NONE;
  82. }
  83. double pxWidth = 3.0;
  84. if( CSSConstants.CSS_THIN_VALUE.equals( width ) ) {
  85. pxWidth = 1.0;
  86. } else if( CSSConstants.CSS_MEDIUM_VALUE.equals( width ) ) {
  87. pxWidth = 3.0;
  88. } else if( CSSConstants.CSS_THICK_VALUE.equals( width ) ) {
  89. pxWidth = 4.0;
  90. } else {
  91. DimensionType dim = DimensionType.parserUnit( width );
  92. if( dim != null ) {
  93. if( "px".equals(dim.getUnits()) ) {
  94. pxWidth = dim.getMeasure();
  95. }
  96. }
  97. }
  98. if( "solid".equals(birtBorder) ) {
  99. if( pxWidth < 2.9 ) {
  100. return BorderStyle.THIN;
  101. } else if( pxWidth < 3.1 ) {
  102. return BorderStyle.MEDIUM;
  103. } else {
  104. return BorderStyle.THICK;
  105. }
  106. } else if( "dashed".equals(birtBorder) ) {
  107. if( pxWidth < 2.9 ) {
  108. return BorderStyle.DASHED;
  109. } else {
  110. return BorderStyle.MEDIUM_DASHED;
  111. }
  112. } else if( "dotted".equals(birtBorder) ) {
  113. return BorderStyle.DOTTED;
  114. } else if( "double".equals(birtBorder) ) {
  115. return BorderStyle.DOUBLE;
  116. }
  117. log.debug( "Border style \"", birtBorder, "\" is not recognised." );
  118. return BorderStyle.NONE;
  119. }
  120. @Override
  121. public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
  122. if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
  123. String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
  124. String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
  125. String widthString = width == null ? "medium" : width.getCssText();
  126. if( style instanceof XSSFCellStyle ) {
  127. XSSFCellStyle xStyle = (XSSFCellStyle)style;
  128. BorderStyle xBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
  129. XSSFColor xBorderColour = getXColour(colourString);
  130. if(xBorderStyle != BorderStyle.NONE) {
  131. switch( side ) {
  132. case TOP:
  133. xStyle.setBorderTop(xBorderStyle);
  134. xStyle.setTopBorderColor(xBorderColour);
  135. // log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
  136. break;
  137. case LEFT:
  138. xStyle.setBorderLeft(xBorderStyle);
  139. xStyle.setLeftBorderColor(xBorderColour);
  140. // log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
  141. break;
  142. case RIGHT:
  143. xStyle.setBorderRight(xBorderStyle);
  144. xStyle.setRightBorderColor(xBorderColour);
  145. // log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
  146. break;
  147. case BOTTOM:
  148. xStyle.setBorderBottom(xBorderStyle);
  149. xStyle.setBottomBorderColor(xBorderColour);
  150. // log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. private XSSFColor getXColour(String colour) {
  158. int[] rgbInt = ColorUtil.getRGBs(colour);
  159. if( rgbInt == null ) {
  160. return null;
  161. }
  162. byte[] rgbByte = { (byte)-1, (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
  163. // System.out.println( "The X colour for " + colour + " is [ " + rgbByte[0] + "," + rgbByte[1] + "," + rgbByte[2] + "," + rgbByte[3] + "]" );
  164. XSSFColor result = new XSSFColor( rgbByte );
  165. return result;
  166. }
  167. @Override
  168. public void addColourToFont(Workbook workbook, Font font, String colour) {
  169. if(colour == null) {
  170. return ;
  171. }
  172. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  173. return ;
  174. }
  175. if(font instanceof XSSFFont) {
  176. XSSFFont xFont = (XSSFFont)font;
  177. XSSFColor xColour = getXColour(colour);
  178. if(xColour != null) {
  179. xFont.setColor(xColour);
  180. }
  181. }
  182. }
  183. @Override
  184. public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
  185. if(colour == null) {
  186. return ;
  187. }
  188. if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
  189. return ;
  190. }
  191. if(style instanceof XSSFCellStyle) {
  192. XSSFCellStyle cellStyle = (XSSFCellStyle)style;
  193. XSSFColor xColour = getXColour(colour);
  194. if(xColour != null) {
  195. cellStyle.setFillForegroundColor(xColour);
  196. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  197. }
  198. }
  199. }
  200. @Override
  201. public Font correctFontColorIfBackground( FontManager fm, Workbook wb, BirtStyle birtStyle, Font font ) {
  202. CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
  203. int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" );
  204. XSSFColor colour = ((XSSFFont)font).getXSSFColor();
  205. int fgRgb[] = rgbOnly( colour.getARgb() );
  206. if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) {
  207. fgRgb[0]=fgRgb[1]=fgRgb[2]=0;
  208. } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) {
  209. fgRgb[0]=fgRgb[1]=fgRgb[2]=255;
  210. }
  211. if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) {
  212. IStyle addedStyle = new AreaStyle( fm.getCssEngine() );
  213. addedStyle.setColor( contrastColour( bgRgb ) );
  214. return fm.getFontWithExtraStyle( font, addedStyle );
  215. } else {
  216. return font;
  217. }
  218. }
  219. @Override
  220. public int anchorDxFromMM( double widthMM, double colWidthMM ) {
  221. return (int)(widthMM * 36000);
  222. }
  223. @Override
  224. public int anchorDyFromPoints( float height, float rowHeight ) {
  225. return (int)( height * XSSFShape.EMU_PER_POINT );
  226. }
  227. @Override
  228. public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
  229. double headerHeight = 0.5;
  230. double footerHeight = 0.5;
  231. if( ( page.getHeaderHeight() != null ) && isAbsolute( page.getHeaderHeight() ) ) {
  232. headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
  233. sheet.setMargin(Sheet.HeaderMargin, headerHeight);
  234. }
  235. if( ( page.getFooterHeight() != null ) && isAbsolute( page.getFooterHeight() ) ) {
  236. footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
  237. sheet.setMargin(Sheet.FooterMargin, footerHeight);
  238. }
  239. if( ( page.getMarginBottom() != null ) && isAbsolute( page.getMarginBottom() ) ) {
  240. sheet.setMargin(Sheet.BottomMargin, footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
  241. }
  242. if( ( page.getMarginLeft() != null ) && isAbsolute( page.getMarginLeft() ) ) {
  243. sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
  244. }
  245. if( ( page.getMarginRight() != null ) && isAbsolute( page.getMarginRight() ) ) {
  246. sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
  247. }
  248. if( ( page.getMarginTop() != null ) && isAbsolute( page.getMarginTop() ) ) {
  249. sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
  250. }
  251. }
  252. }