PageRenderTime 81ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/manticore/report/CellRenderer.java

https://bitbucket.org/manticoretrader/etltools
Java | 316 lines | 272 code | 28 blank | 16 comment | 52 complexity | b476b10c4827335f24ae6b4baf18acfc MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.manticore.report;
  6. import java.awt.*;
  7. import java.awt.font.TextAttribute;
  8. import java.util.Map;
  9. import javax.swing.*;
  10. import javax.swing.border.CompoundBorder;
  11. import javax.swing.border.MatteBorder;
  12. import javax.swing.table.DefaultTableCellRenderer;
  13. import org.apache.poi.hssf.usermodel.HSSFPalette;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.hssf.util.HSSFColor;
  16. import org.apache.poi.ss.usermodel.*;
  17. import org.apache.poi.xssf.usermodel.XSSFCell;
  18. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  19. import org.apache.poi.xssf.usermodel.XSSFColor;
  20. import org.apache.poi.xssf.usermodel.XSSFFont;
  21. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  22. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  23. /**
  24. *
  25. * @author are
  26. */
  27. class CellRenderer extends DefaultTableCellRenderer {
  28. private final FixFormatReportDesigner designer;
  29. CellRenderer(final FixFormatReportDesigner outer) {
  30. this.designer = outer;
  31. }
  32. private int getThickness(BorderStyle borderStyle) {
  33. int retval = 1;
  34. switch (borderStyle) {
  35. case THIN:
  36. retval = 2;
  37. break;
  38. case MEDIUM:
  39. retval = 3;
  40. break;
  41. case THICK:
  42. retval = 4;
  43. break;
  44. case DASHED:
  45. retval = 1;
  46. break;
  47. case DASH_DOT_DOT:
  48. retval = 1;
  49. break;
  50. case MEDIUM_DASH_DOT_DOT:
  51. retval = 3;
  52. break;
  53. case HAIR:
  54. retval = 1;
  55. break;
  56. default:
  57. retval = 1;
  58. }
  59. return retval;
  60. }
  61. /**
  62. * This method retrieves the AWT Color representation from the colour hash table
  63. *
  64. * @param index Description of the Parameter
  65. * @param deflt Description of the Parameter
  66. * @return The aWTColor value
  67. */
  68. public final static java.awt.Color awtColor(org.apache.poi.ss.usermodel.Color color) {
  69. java.awt.Color awtColor = null;
  70. if (color.equals(HSSFColor.AUTOMATIC.getInstance())) {
  71. awtColor = java.awt.Color.BLACK;
  72. } else if (color instanceof HSSFColor) {
  73. HSSFColor hssfColor = (HSSFColor) color;
  74. short[] rgb = hssfColor.getTriplet();
  75. int r = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];
  76. int g = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];
  77. int b = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];
  78. awtColor = new java.awt.Color(r, g, b);
  79. } else if (color instanceof XSSFColor) {
  80. XSSFColor xssfColor = (XSSFColor) color;
  81. byte[] rgb = xssfColor.getRGB();
  82. int r = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];
  83. int g = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];
  84. int b = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];
  85. awtColor = new java.awt.Color(r, g, b);
  86. }
  87. return awtColor;
  88. }
  89. public final static java.awt.Color awtColor(org.apache.poi.ss.usermodel.Color color, java.awt.Color defaultColor) {
  90. java.awt.Color awtColor = null;
  91. if (color == null || color.equals(HSSFColor.AUTOMATIC.getInstance())) {
  92. awtColor = defaultColor;
  93. } else if (color instanceof HSSFColor) {
  94. HSSFColor hssfColor = (HSSFColor) color;
  95. short[] rgb = hssfColor.getTriplet();
  96. int r = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];
  97. int g = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];
  98. int b = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];
  99. awtColor = new java.awt.Color(r, g, b);
  100. } else if (color instanceof XSSFColor) {
  101. XSSFColor xssfColor = (XSSFColor) color;
  102. byte[] rgb = xssfColor.getARGB();
  103. int a = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];
  104. int r = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];
  105. int g = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];
  106. int b = (rgb[3] < 0) ? (rgb[3] + 256) : rgb[3];
  107. awtColor = new java.awt.Color(r, g, b, a);
  108. }
  109. return awtColor;
  110. }
  111. public final java.awt.Color awtColor(Short c, java.awt.Color defaultColor) {
  112. java.awt.Color awtColor = defaultColor;
  113. if (designer.workbook instanceof HSSFWorkbook) {
  114. HSSFPalette customPalette = ((HSSFWorkbook) designer.workbook).getCustomPalette();
  115. HSSFColor hssfColor = customPalette.getColor(c);
  116. if (hssfColor == null || hssfColor.equals(HSSFColor.AUTOMATIC.getInstance())) {
  117. awtColor = defaultColor;
  118. } else {
  119. short[] rgb = hssfColor.getTriplet();
  120. int r = (rgb[0] < 0)
  121. ? (rgb[0] + 256)
  122. : rgb[0];
  123. int g = (rgb[1] < 0)
  124. ? (rgb[1] + 256)
  125. : rgb[1];
  126. int b = (rgb[2] < 0)
  127. ? (rgb[2] + 256)
  128. : rgb[2];
  129. awtColor = new java.awt.Color(r, g, b);
  130. }
  131. }
  132. return awtColor;
  133. }
  134. @Override
  135. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
  136. int row, int column) {
  137. JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  138. String sheetname = designer.tabbedPane.getTitleAt(designer.tabbedPane.getSelectedIndex());
  139. String reference = designer.getExcelColumnName(column) + (row + 1);
  140. label.setBackground(java.awt.Color.WHITE);
  141. if (value instanceof Cell) {
  142. Cell cell = (Cell) value;
  143. Comment comment = cell.getCellComment();
  144. if (comment != null) {
  145. label.setToolTipText(cell.getCellComment().getString().getString());
  146. } else {
  147. label.setToolTipText(null);
  148. }
  149. CellStyle cellStyle1 = cell.getCellStyle();
  150. label.setText(new DataFormatter().formatCellValue(cell));
  151. org.apache.poi.ss.usermodel.Font f;
  152. if (cellStyle1 instanceof XSSFCellStyle) {
  153. XSSFCell xssfCell = (XSSFCell) cell;
  154. XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle();
  155. XSSFColor fillForegroundColorColor = xssfCellStyle.getFillForegroundColorColor();
  156. if (fillForegroundColorColor == null) {
  157. short fillId = (short) xssfCellStyle.getCoreXf().getFillId();
  158. XSSFCellFill fill = ((XSSFWorkbook) designer.workbook).getStylesSource().getFillAt(fillId);
  159. fillForegroundColorColor = fill.getFillForegroundColor();
  160. }
  161. label.setBackground(awtColor(fillForegroundColorColor, java.awt.Color.WHITE));
  162. f = designer.workbook.getFontAt(xssfCellStyle.getFontIndex());
  163. } else {
  164. label.setBackground(awtColor(cellStyle1.getFillBackgroundColorColor(), java.awt.Color.WHITE));
  165. label.setForeground(awtColor(cellStyle1.getFillForegroundColorColor(), java.awt.Color.BLACK));
  166. f = designer.workbook.getFontAt(cellStyle1.getFontIndex());
  167. }
  168. String fontName = f.getFontName();
  169. short fontHeightInPoints = f.getFontHeightInPoints();
  170. boolean italic = f.getItalic();
  171. short boldweight = f.getBoldweight();
  172. byte underline = f.getUnderline();
  173. java.awt.Font font = new java.awt.Font(fontName, java.awt.Font.PLAIN, fontHeightInPoints);
  174. final Map attributes = font.getAttributes();
  175. if (underline > 0) {
  176. attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
  177. }
  178. if (boldweight > 0) {
  179. attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
  180. }
  181. if (italic) {
  182. attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
  183. } else {
  184. attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
  185. }
  186. font = font.deriveFont(attributes);
  187. label.setFont(font);
  188. if (f instanceof XSSFFont) {
  189. XSSFFont xssfFont = (XSSFFont) f;
  190. label.setForeground(awtColor(xssfFont.getXSSFColor(), java.awt.Color.BLACK));
  191. } else {
  192. label.setForeground(awtColor(f.getColor(), java.awt.Color.BLACK));
  193. }
  194. MatteBorder topBorder = BorderFactory.createMatteBorder(0, 0, 0, 0, java.awt.Color.WHITE);
  195. MatteBorder bottomBorder = BorderFactory.createMatteBorder(0, 0, 0, 0, java.awt.Color.WHITE);
  196. MatteBorder leftBorder = BorderFactory.createMatteBorder(0, 0, 0, 0, java.awt.Color.WHITE);
  197. MatteBorder rightBorder = BorderFactory.createMatteBorder(0, 0, 0, 0, java.awt.Color.WHITE);
  198. if (cellStyle1 instanceof XSSFCellStyle) {
  199. XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle1;
  200. if (xssfCellStyle.getBorderTop()!=BorderStyle.NONE) {
  201. topBorder = BorderFactory.createMatteBorder(getThickness(cellStyle1.getBorderTop()), 0, 0, 0, awtColor(xssfCellStyle.getTopBorderXSSFColor(), java.awt.Color.BLACK));
  202. }
  203. if (cellStyle1.getBorderBottom()!=BorderStyle.NONE) {
  204. bottomBorder = BorderFactory.createMatteBorder(0, 0, getThickness(cellStyle1.getBorderBottom()), 0, awtColor(xssfCellStyle.getBottomBorderXSSFColor(), java.awt.Color.BLACK));
  205. }
  206. CompoundBorder compoundBorder1 = new CompoundBorder(topBorder, bottomBorder);
  207. if (cellStyle1.getBorderLeft()!=BorderStyle.NONE) {
  208. leftBorder = BorderFactory.createMatteBorder(0, getThickness(cellStyle1.getBorderLeft()), 0, 0, awtColor(xssfCellStyle.getLeftBorderXSSFColor(), java.awt.Color.BLACK));
  209. }
  210. if (cellStyle1.getBorderRight()!=BorderStyle.NONE) {
  211. rightBorder = BorderFactory.createMatteBorder(0, 0, 0, getThickness(cellStyle1.getBorderRight()), awtColor(xssfCellStyle.getRightBorderXSSFColor(), java.awt.Color.BLACK));
  212. }
  213. CompoundBorder compoundBorder2 = new CompoundBorder(leftBorder, rightBorder);
  214. if (!isSelected) {
  215. label.setBorder(new CompoundBorder(compoundBorder1, compoundBorder2));
  216. }
  217. } else {
  218. if (cellStyle1.getBorderTop()!=BorderStyle.NONE) {
  219. topBorder = BorderFactory.createMatteBorder(getThickness(cellStyle1.getBorderTop()), 0, 0, 0, awtColor(cellStyle1.getTopBorderColor(), java.awt.Color.BLACK));
  220. }
  221. if (cellStyle1.getBorderBottom()!=BorderStyle.NONE) {
  222. bottomBorder = BorderFactory.createMatteBorder(0, 0, getThickness(cellStyle1.getBorderBottom()), 0, awtColor(cellStyle1.getBottomBorderColor(), java.awt.Color.BLACK));
  223. }
  224. CompoundBorder compoundBorder1 = new CompoundBorder(topBorder, bottomBorder);
  225. if (cellStyle1.getBorderLeft()!=BorderStyle.NONE) {
  226. leftBorder = BorderFactory.createMatteBorder(0, getThickness(cellStyle1.getBorderLeft()), 0, 0, awtColor(cellStyle1.getLeftBorderColor(), java.awt.Color.BLACK));
  227. }
  228. if (cellStyle1.getBorderRight()!=BorderStyle.NONE) {
  229. rightBorder = BorderFactory.createMatteBorder(0, 0, 0, getThickness(cellStyle1.getBorderRight()), awtColor(cellStyle1.getRightBorderColor(), java.awt.Color.BLACK));
  230. }
  231. CompoundBorder compoundBorder2 = new CompoundBorder(leftBorder, rightBorder);
  232. if (!isSelected) {
  233. label.setBorder(new CompoundBorder(compoundBorder1, compoundBorder2));
  234. }
  235. }
  236. switch (cellStyle1.getAlignment()) {
  237. case 1:
  238. label.setHorizontalAlignment(JLabel.LEFT);
  239. break;
  240. case 2:
  241. label.setHorizontalAlignment(JLabel.CENTER);
  242. break;
  243. case 3:
  244. label.setHorizontalAlignment(JLabel.RIGHT);
  245. break;
  246. default:
  247. label.setHorizontalAlignment(JLabel.LEFT);
  248. }
  249. switch (cellStyle1.getVerticalAlignment()) {
  250. case 1:
  251. label.setVerticalAlignment(JLabel.TOP);
  252. break;
  253. case 2:
  254. label.setVerticalAlignment(JLabel.CENTER);
  255. break;
  256. case 3:
  257. label.setVerticalAlignment(JLabel.BOTTOM);
  258. break;
  259. default:
  260. label.setVerticalAlignment(JLabel.TOP);
  261. }
  262. }
  263. for (RecordSet r : designer.fixFormatReport.recordSets.values()) {
  264. for (FieldMapping m : r.fieldMappings) {
  265. if (m.sheetName.equalsIgnoreCase(sheetname)
  266. && m.reference.equalsIgnoreCase(reference)) {
  267. StringBuilder builder = new StringBuilder();
  268. if (!designer.previewMode) {
  269. builder.append("<html>");
  270. builder.append("<font size='0' color='blue'>").append(m.fieldName).append("</font>");
  271. builder.append("</html>");
  272. label.setText(builder.toString());
  273. } else {
  274. //label.setText(cell.);
  275. }
  276. builder.setLength(0);
  277. builder.append("<html>");
  278. builder.append("<font size='0' color='blue'>").append(m.recordSetId).append("</font>!");
  279. builder.append("<font size='0' color='blue'>").append(m.fieldName).append("</font>");
  280. builder.append("</html>");
  281. label.setToolTipText(builder.toString());
  282. break;
  283. }
  284. }
  285. }
  286. return label;
  287. }
  288. }