PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/uk/ac/manchester/cs/owl/semspreadsheets/model/xssf/impl/CellXSSFImpl.java

https://github.com/semantalytics/RightField
Java | 348 lines | 291 code | 53 blank | 4 comment | 87 complexity | 999bd42f43674782a2cdf4ddecdfd4a9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package uk.ac.manchester.cs.owl.semspreadsheets.model.xssf.impl;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.swing.SwingConstants;
  7. import org.apache.log4j.Logger;
  8. import org.apache.poi.xssf.usermodel.XSSFCell;
  9. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  10. import org.apache.poi.xssf.usermodel.XSSFColor;
  11. import org.apache.poi.xssf.usermodel.XSSFComment;
  12. import org.apache.poi.xssf.usermodel.XSSFFont;
  13. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. import uk.ac.manchester.cs.owl.semspreadsheets.model.Cell;
  17. /**
  18. * @author Stuart Owen
  19. */
  20. public class CellXSSFImpl implements Cell {
  21. private static Logger logger = Logger.getLogger(CellXSSFImpl.class);
  22. public static final Font DEFAULT_FONT = new Font("verdana", Font.PLAIN, 10);
  23. private static Map<XSSFWorkbook,Map<XSSFFont, Font>> fontCache = new HashMap<XSSFWorkbook,Map<XSSFFont, Font>>();
  24. private static Map<XSSFWorkbook,Map<Color,XSSFCellStyle>> colourStylesForWorkbook = new HashMap<XSSFWorkbook, Map<Color,XSSFCellStyle>>();
  25. private XSSFCell theCell;
  26. private XSSFWorkbook workbook;
  27. private Color foreground;
  28. public CellXSSFImpl(XSSFWorkbook workbook, XSSFCell theCell) {
  29. this.workbook = workbook;
  30. this.theCell = theCell;
  31. }
  32. public Font getDefaultFont() {
  33. XSSFFont font = workbook.getFontAt((short) 0);
  34. if (font == null) {
  35. return DEFAULT_FONT;
  36. }
  37. return getFont(font);
  38. }
  39. public XSSFWorkbook getWorkbook() {
  40. return workbook;
  41. }
  42. public int getRow() {
  43. return theCell.getRowIndex();
  44. }
  45. public int getColumn() {
  46. return theCell.getColumnIndex();
  47. }
  48. public String getComment() {
  49. XSSFComment xssfComment = theCell.getCellComment();
  50. if (xssfComment == null) {
  51. return null;
  52. }
  53. else {
  54. return xssfComment.toString();
  55. }
  56. }
  57. public boolean isStrikeThrough() {
  58. XSSFFont xssfFont = theCell.getCellStyle().getFont();
  59. return xssfFont.getStrikeout();
  60. }
  61. public boolean isUnderline() {
  62. XSSFFont xssfFont = theCell.getCellStyle().getFont();
  63. return xssfFont.getUnderline() != 0;
  64. }
  65. public boolean isItalic() {
  66. XSSFFont xssfFont = theCell.getCellStyle().getFont();
  67. return xssfFont.getItalic();
  68. }
  69. public String getValue() {
  70. if (theCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
  71. return "";
  72. }
  73. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
  74. return Boolean.toString(theCell.getBooleanCellValue());
  75. }
  76. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
  77. return "<ERROR?>";
  78. }
  79. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
  80. return theCell.getCellFormula();
  81. }
  82. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
  83. return Double.toString(theCell.getNumericCellValue());
  84. }
  85. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
  86. return theCell.getRichStringCellValue().getString();
  87. }
  88. return "";
  89. }
  90. public void setValue(String value) {
  91. if (theCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
  92. theCell.setCellValue(new XSSFRichTextString(value));
  93. }
  94. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
  95. theCell.setCellValue(Boolean.parseBoolean(value));
  96. }
  97. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
  98. }
  99. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
  100. theCell.setCellFormula(value);
  101. }
  102. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
  103. theCell.setCellValue(Double.parseDouble(value));
  104. }
  105. else if (theCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
  106. theCell.setCellValue(new XSSFRichTextString(value));
  107. }
  108. }
  109. public boolean isBold() {
  110. return getFont().isBold();
  111. }
  112. public void setBold(boolean b) {
  113. XSSFCellStyle cellStyle = theCell.getCellStyle();
  114. if (cellStyle == null) {
  115. cellStyle = workbook.createCellStyle();
  116. theCell.setCellStyle(cellStyle);
  117. }
  118. XSSFFont font = cellStyle.getFont();
  119. if (font == null) {
  120. font = workbook.createFont();
  121. cellStyle.setFont(font);
  122. }
  123. if (b) {
  124. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
  125. }
  126. else {
  127. font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
  128. }
  129. fontCache.clear();
  130. }
  131. public Font getFont() {
  132. XSSFCellStyle cellStyle = theCell.getCellStyle();
  133. if (cellStyle == null) {
  134. return getDefaultFont();
  135. }
  136. XSSFFont xssfFont = cellStyle.getFont();
  137. return getFont(xssfFont);
  138. }
  139. private Font getFont(XSSFFont xssfFont) {
  140. Font font = getFontFromCache(xssfFont);
  141. if (font == null) {
  142. String name = xssfFont.getFontName();
  143. int size = xssfFont.getFontHeightInPoints();
  144. int style = Font.PLAIN;
  145. if (xssfFont.getBoldweight() == XSSFFont.BOLDWEIGHT_BOLD) {
  146. style = Font.BOLD;
  147. if (xssfFont.getItalic()) {
  148. style = style | Font.ITALIC;
  149. }
  150. }
  151. else if (xssfFont.getItalic()) {
  152. style = Font.ITALIC;
  153. }
  154. font = new Font(name, style, size);
  155. putFontInCache(xssfFont, font);
  156. }
  157. return font;
  158. }
  159. private Font getFontFromCache(XSSFFont xssfFont) {
  160. Map<XSSFFont,Font> cache = fontCache.get(getWorkbook());
  161. if (cache==null) {
  162. cache = new HashMap<XSSFFont,Font>();
  163. fontCache.put(getWorkbook(), cache);
  164. }
  165. return cache.get(xssfFont);
  166. }
  167. private void putFontInCache(XSSFFont xssfFont,Font font) {
  168. Map<XSSFFont,Font> cache = fontCache.get(getWorkbook());
  169. if (cache==null) {
  170. cache = new HashMap<XSSFFont,Font>();
  171. fontCache.put(getWorkbook(), cache);
  172. }
  173. cache.put(xssfFont, font);
  174. }
  175. @Override
  176. public Color getBackgroundFill() {
  177. Color colour = null;
  178. XSSFCellStyle cellStyle = theCell.getCellStyle();
  179. if (cellStyle == null) {
  180. colour = Color.WHITE;
  181. }
  182. else {
  183. XSSFColor xssfColour = cellStyle.getFillForegroundXSSFColor();
  184. if (xssfColour == null) {
  185. colour = Color.WHITE;
  186. }
  187. else {
  188. colour = translateRGB(xssfColour.getRgb());
  189. }
  190. }
  191. logger.debug("Background fill colour read as: "+colour);
  192. return colour;
  193. //return Color.WHITE;
  194. }
  195. private Color translateRGB(byte[] rgb) {
  196. if (rgb == null) {
  197. return Color.WHITE;
  198. }
  199. if (rgb.length>3) {
  200. return new Color(rgb[1] & 0xFF,rgb[2] & 0xFF, rgb[3] & 0xFF,rgb[0] & 0xFF);
  201. }
  202. else {
  203. return new Color(rgb[0] & 0xFF,rgb[1] & 0xFF, rgb[2] & 0xFF);
  204. }
  205. }
  206. @Override
  207. public void setBackgroundFill(Color colour) {
  208. XSSFCellStyle style = getFillStyleForColour(colour);
  209. try {
  210. theCell.setCellStyle(style);
  211. }
  212. catch(Exception e) {
  213. logger.error("Error setting cell style",e);
  214. }
  215. }
  216. private XSSFCellStyle getFillStyleForColour(Color colour) {
  217. Map<Color,XSSFCellStyle> styles = colourStylesForWorkbook.get(getWorkbook());
  218. if (styles == null) {
  219. styles = new HashMap<Color,XSSFCellStyle>();
  220. colourStylesForWorkbook.put(getWorkbook(), styles);
  221. }
  222. XSSFCellStyle style = styles.get(colour);
  223. if (style==null) {
  224. style = getWorkbook().createCellStyle();
  225. XSSFColor col = new XSSFColor(colour);
  226. style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
  227. style.setFillForegroundColor(col);
  228. styles.put(colour,style);
  229. }
  230. return style;
  231. }
  232. public Color getForeground() {
  233. if (foreground == null) {
  234. XSSFColor colour = theCell.getCellStyle().getFont().getXSSFColor();
  235. if (colour!=null) {
  236. foreground = translateRGB(colour.getRgb());
  237. }
  238. else {
  239. foreground = Color.BLACK;
  240. }
  241. }
  242. return foreground;
  243. }
  244. public int getAlignment() {
  245. XSSFCellStyle cellStyle = theCell.getCellStyle();
  246. if (cellStyle == null) {
  247. return SwingConstants.LEFT;
  248. }
  249. short xssfAlignment = cellStyle.getAlignment();
  250. if (xssfAlignment == XSSFCellStyle.ALIGN_LEFT) {
  251. return SwingConstants.LEFT;
  252. }
  253. else if (xssfAlignment == XSSFCellStyle.ALIGN_CENTER) {
  254. return SwingConstants.CENTER;
  255. }
  256. else if (xssfAlignment == XSSFCellStyle.ALIGN_RIGHT) {
  257. return SwingConstants.RIGHT;
  258. }
  259. else {
  260. return SwingConstants.LEFT;
  261. }
  262. }
  263. public String getValidationListName() {
  264. return null;
  265. }
  266. public boolean isEmpty() {
  267. return false;
  268. }
  269. @Override
  270. public int hashCode() {
  271. return theCell.hashCode();
  272. }
  273. @Override
  274. public boolean equals(Object obj) {
  275. if (obj instanceof CellXSSFImpl) {
  276. CellXSSFImpl cell = (CellXSSFImpl)obj;
  277. return cell.theCell.equals(this.theCell);
  278. }
  279. else {
  280. return false;
  281. }
  282. }
  283. public XSSFCell getInnards() {
  284. return theCell;
  285. }
  286. @Override
  287. public String getSheetName() {
  288. return workbook.getSheetName(getSheetIndex());
  289. }
  290. @Override
  291. public int getSheetIndex() {
  292. XSSFSheet sheet = theCell.getSheet();
  293. return workbook.getSheetIndex(sheet);
  294. }
  295. }