PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/uk/ac/manchester/cs/owl/semspreadsheets/model/hssf/impl/CellHSSFImpl.java

https://github.com/semantalytics/RightField
Java | 344 lines | 276 code | 43 blank | 25 comment | 83 complexity | 69c13b066ab7080e5b5737d464e674f4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright (c) 2009-2012, University of Manchester
  3. *
  4. * Licensed under the New BSD License.
  5. * Please see LICENSE file that is distributed with the source code
  6. ******************************************************************************/
  7. package uk.ac.manchester.cs.owl.semspreadsheets.model.hssf.impl;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import javax.swing.SwingConstants;
  13. import org.apache.log4j.Logger;
  14. import org.apache.poi.hssf.usermodel.HSSFCell;
  15. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  16. import org.apache.poi.hssf.usermodel.HSSFComment;
  17. import org.apache.poi.hssf.usermodel.HSSFFont;
  18. import org.apache.poi.hssf.usermodel.HSSFPalette;
  19. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  20. import org.apache.poi.hssf.usermodel.HSSFSheet;
  21. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  22. import org.apache.poi.hssf.util.HSSFColor;
  23. import uk.ac.manchester.cs.owl.semspreadsheets.model.Cell;
  24. /**
  25. * @author Stuart Owen
  26. * @author Matthew Horridge
  27. */
  28. public class CellHSSFImpl implements Cell {
  29. private static Logger logger = Logger.getLogger(CellHSSFImpl.class);
  30. public static final Font DEFAULT_FONT = new Font("verdana", Font.PLAIN, 10);
  31. private static Map<HSSFFont, Font> fontCache = new HashMap<HSSFFont, Font>();
  32. private static Map<HSSFWorkbook,Map<Color,HSSFCellStyle>> colourStylesForWorkbook = new HashMap<HSSFWorkbook, Map<Color,HSSFCellStyle>>();
  33. private HSSFCell theCell;
  34. private HSSFWorkbook workbook;
  35. private Color foreground;
  36. public CellHSSFImpl(HSSFWorkbook workbook, HSSFCell theCell) {
  37. this.workbook = workbook;
  38. this.theCell = theCell;
  39. }
  40. public Font getDefaultFont() {
  41. HSSFFont font = getWorkbook().getFontAt((short) 0);
  42. if (font == null) {
  43. return DEFAULT_FONT;
  44. }
  45. return getFont(font);
  46. }
  47. public int getRow() {
  48. return theCell.getRowIndex();
  49. }
  50. public int getColumn() {
  51. return theCell.getColumnIndex();
  52. }
  53. public String getComment() {
  54. HSSFComment hssfComment = theCell.getCellComment();
  55. if (hssfComment == null) {
  56. return null;
  57. }
  58. else {
  59. return hssfComment.toString();
  60. }
  61. }
  62. public boolean isStrikeThrough() {
  63. HSSFFont hssfFont = theCell.getCellStyle().getFont(getWorkbook());
  64. return hssfFont.getStrikeout();
  65. }
  66. public boolean isUnderline() {
  67. HSSFFont hssfFont = theCell.getCellStyle().getFont(getWorkbook());
  68. return hssfFont.getUnderline() != 0;
  69. }
  70. public boolean isItalic() {
  71. HSSFFont hssfFont = theCell.getCellStyle().getFont(getWorkbook());
  72. return hssfFont.getItalic();
  73. }
  74. public String getValue() {
  75. if (theCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
  76. return "";
  77. }
  78. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
  79. return Boolean.toString(theCell.getBooleanCellValue());
  80. }
  81. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
  82. return "<ERROR?>";
  83. }
  84. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
  85. return theCell.getCellFormula();
  86. }
  87. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
  88. return Double.toString(theCell.getNumericCellValue());
  89. }
  90. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
  91. return theCell.getRichStringCellValue().getString();
  92. }
  93. return "";
  94. }
  95. public void setValue(String value) {
  96. if (theCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
  97. theCell.setCellValue(new HSSFRichTextString(value));
  98. }
  99. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
  100. theCell.setCellValue(Boolean.parseBoolean(value));
  101. }
  102. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
  103. }
  104. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
  105. theCell.setCellFormula(value);
  106. }
  107. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
  108. theCell.setCellValue(Double.parseDouble(value));
  109. }
  110. else if (theCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
  111. theCell.setCellValue(new HSSFRichTextString(value));
  112. }
  113. }
  114. public boolean isBold() {
  115. return getFont().isBold();
  116. }
  117. public void setBold(boolean b) {
  118. HSSFCellStyle cellStyle = theCell.getCellStyle();
  119. if (cellStyle == null) {
  120. cellStyle = getWorkbook().createCellStyle();
  121. theCell.setCellStyle(cellStyle);
  122. }
  123. HSSFFont font = cellStyle.getFont(getWorkbook());
  124. if (font == null) {
  125. font = getWorkbook().createFont();
  126. cellStyle.setFont(font);
  127. }
  128. if (b) {
  129. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  130. }
  131. else {
  132. font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
  133. }
  134. fontCache.clear();
  135. }
  136. public Font getFont() {
  137. HSSFCellStyle cellStyle = theCell.getCellStyle();
  138. if (cellStyle == null) {
  139. return getDefaultFont();
  140. }
  141. HSSFFont hssfFont = cellStyle.getFont(getWorkbook());
  142. return getFont(hssfFont);
  143. }
  144. private Font getFont(HSSFFont hssfFont) {
  145. Font font = fontCache.get(hssfFont);
  146. if (font == null) {
  147. String name = hssfFont.getFontName();
  148. int size = hssfFont.getFontHeightInPoints();
  149. int style = Font.PLAIN;
  150. if (hssfFont.getBoldweight() == HSSFFont.BOLDWEIGHT_BOLD) {
  151. style = Font.BOLD;
  152. if (hssfFont.getItalic()) {
  153. style = style | Font.ITALIC;
  154. }
  155. }
  156. else if (hssfFont.getItalic()) {
  157. style = Font.ITALIC;
  158. }
  159. font = new Font(name, style, size);
  160. fontCache.put(hssfFont, font);
  161. }
  162. return font;
  163. }
  164. @Override
  165. public Color getBackgroundFill() {
  166. HSSFCellStyle cellStyle = theCell.getCellStyle();
  167. if (cellStyle == null) {
  168. logger.debug("Cell style not found, so using background colour of WHITE");
  169. return Color.WHITE;
  170. }
  171. short colorIndex=cellStyle.getFillForegroundColor();
  172. logger.debug("Background fill colour index found as "+colorIndex);
  173. return translateColour(colorIndex);
  174. }
  175. @Override
  176. public void setBackgroundFill(Color colour) {
  177. HSSFColor col = translateColour(colour);
  178. if (col==null) {
  179. logger.warn("Unable to find similar colour in palette for "+colour.toString());
  180. }
  181. else {
  182. theCell.setCellStyle(getFillStyleForColour(colour));
  183. if (logger.isDebugEnabled()) {
  184. logger.debug("Cell colour changed to "+col.getHexString()+"with index: "+col.getIndex());
  185. }
  186. }
  187. }
  188. private HSSFCellStyle getFillStyleForColour(Color colour) {
  189. Map<Color,HSSFCellStyle> styles = colourStylesForWorkbook.get(getWorkbook());
  190. if (styles == null) {
  191. styles = new HashMap<Color,HSSFCellStyle>();
  192. colourStylesForWorkbook.put(getWorkbook(), styles);
  193. }
  194. HSSFCellStyle style = styles.get(colour);
  195. if (style == null) {
  196. HSSFColor col = translateColour(colour);
  197. style = getWorkbook().createCellStyle();
  198. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
  199. style.setFillForegroundColor(col.getIndex());
  200. styles.put(colour, style);
  201. }
  202. return style;
  203. }
  204. public Color getForeground() {
  205. if (foreground == null) {
  206. HSSFCellStyle cellStyle = theCell.getCellStyle();
  207. if (cellStyle == null) {
  208. return Color.BLACK;
  209. }
  210. HSSFFont hssfFont = cellStyle.getFont(getWorkbook());
  211. short colorIndex = hssfFont.getColor();
  212. Color theColor = translateColour(colorIndex);
  213. foreground = theColor;
  214. }
  215. return foreground;
  216. }
  217. /**
  218. * Translates a java Color to the colour index in the workbook palette
  219. * @param colour
  220. * @return
  221. */
  222. private HSSFColor translateColour(Color colour) {
  223. HSSFPalette palette = getWorkbook().getCustomPalette();
  224. HSSFColor col = palette.findSimilarColor((byte)colour.getRed(), (byte)colour.getGreen(), (byte)colour.getBlue());
  225. return col;
  226. }
  227. /**
  228. * Translates the colorIndex from the workbook palette to a <br>
  229. * java Color.
  230. * @param colorIndex
  231. * @return java Color
  232. */
  233. private Color translateColour(short colorIndex) {
  234. HSSFPalette palette = getWorkbook().getCustomPalette();
  235. HSSFColor color = palette.getColor(colorIndex);
  236. Color theColor = Color.BLACK;
  237. if (color != null) {
  238. short[] triplet = color.getTriplet();
  239. theColor = new Color(triplet[0], triplet[1], triplet[2]);
  240. }
  241. return theColor;
  242. }
  243. public int getAlignment() {
  244. HSSFCellStyle cellStyle = theCell.getCellStyle();
  245. if (cellStyle == null) {
  246. return SwingConstants.LEFT;
  247. }
  248. short hssfAlignment = cellStyle.getAlignment();
  249. if (hssfAlignment == HSSFCellStyle.ALIGN_LEFT) {
  250. return SwingConstants.LEFT;
  251. }
  252. else if (hssfAlignment == HSSFCellStyle.ALIGN_CENTER) {
  253. return SwingConstants.CENTER;
  254. }
  255. else if (hssfAlignment == HSSFCellStyle.ALIGN_RIGHT) {
  256. return SwingConstants.RIGHT;
  257. }
  258. else {
  259. return SwingConstants.LEFT;
  260. }
  261. }
  262. public String getValidationListName() {
  263. return null;
  264. }
  265. public boolean isEmpty() {
  266. return false;
  267. }
  268. @Override
  269. public int hashCode() {
  270. return theCell.hashCode();
  271. }
  272. @Override
  273. public boolean equals(Object obj) {
  274. if (obj instanceof CellHSSFImpl) {
  275. CellHSSFImpl cell = (CellHSSFImpl)obj;
  276. return cell.theCell.equals(this.theCell);
  277. }
  278. else {
  279. return false;
  280. }
  281. }
  282. /**
  283. * Gets access to the POI internals for this cell - for debugging,testing and subclassing purposes only
  284. * @return
  285. */
  286. public HSSFCell getInnards() {
  287. return theCell;
  288. }
  289. public HSSFWorkbook getWorkbook() {
  290. return workbook;
  291. }
  292. @Override
  293. public String getSheetName() {
  294. return workbook.getSheetName(getSheetIndex());
  295. }
  296. @Override
  297. public int getSheetIndex() {
  298. HSSFSheet sheet = theCell.getSheet();
  299. return workbook.getSheetIndex(sheet);
  300. }
  301. }