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

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

https://github.com/semantalytics/RightField
Java | 255 lines | 194 code | 41 blank | 20 comment | 29 complexity | 8006ccadf35cd12a6eac3ddf43745fb4 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.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import org.apache.poi.hssf.usermodel.HSSFCell;
  13. import org.apache.poi.hssf.usermodel.HSSFDataValidation;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import org.apache.poi.ss.usermodel.DataValidationConstraint;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.apache.poi.ss.util.CellRangeAddressList;
  21. import uk.ac.manchester.cs.owl.semspreadsheets.model.Cell;
  22. import uk.ac.manchester.cs.owl.semspreadsheets.model.PropertyValidationForumlaDefinition;
  23. import uk.ac.manchester.cs.owl.semspreadsheets.model.Range;
  24. import uk.ac.manchester.cs.owl.semspreadsheets.model.Sheet;
  25. import uk.ac.manchester.cs.owl.semspreadsheets.model.Validation;
  26. import uk.ac.manchester.cs.owl.semspreadsheets.model.Workbook;
  27. import uk.ac.manchester.cs.owl.semspreadsheets.model.impl.ValidationImpl;
  28. /**
  29. * @author Stuart Owen
  30. * @author Matthew Horridge
  31. */
  32. public class SheetHSSFImpl implements Sheet {
  33. private WorkbookHSSFImpl workbook;
  34. private HSSFWorkbook hssfWorkbook;
  35. private HSSFSheet sheet;
  36. private static final short MAX_ROWS = Short.MAX_VALUE;
  37. private static final int MAX_COLUMNS = 256;
  38. public SheetHSSFImpl(WorkbookHSSFImpl workbook, HSSFSheet hssfSheet) {
  39. this.workbook = workbook;
  40. this.hssfWorkbook = workbook.getHSSFWorkbook();
  41. this.sheet = hssfSheet;
  42. }
  43. public Workbook getWorkbook() {
  44. return workbook;
  45. }
  46. public List<Cell> getCellsWithContent() {
  47. List<Cell> cells = new ArrayList<Cell>();
  48. int firstRow = sheet.getFirstRowNum();
  49. int lastRow = sheet.getLastRowNum();
  50. for (int rowIndex = firstRow ; rowIndex <= lastRow; rowIndex++) {
  51. HSSFRow row = sheet.getRow(rowIndex);
  52. if (row!=null) {
  53. int firstCell = row.getFirstCellNum();
  54. int lastCell = row.getLastCellNum();
  55. for (int cellIndex = firstCell ; cellIndex <= lastCell;cellIndex++) {
  56. HSSFCell cell = row.getCell(cellIndex);
  57. if (cell!=null && !cell.getStringCellValue().isEmpty()) {
  58. cells.add(new CellHSSFImpl(hssfWorkbook, cell));
  59. }
  60. }
  61. }
  62. }
  63. return cells;
  64. }
  65. public int getIndex() {
  66. for (int index = 0 ; index < getWorkbook().getSheets().size(); index++) {
  67. if (getWorkbook().getSheet(index).equals(this)) {
  68. return index;
  69. }
  70. }
  71. return -1;
  72. }
  73. public void setName(String name) {
  74. String oldName = sheet.getSheetName();
  75. hssfWorkbook.setSheetName(hssfWorkbook.getSheetIndex(sheet), name);
  76. workbook.fireSheetRenamed(oldName, name);
  77. }
  78. public boolean isHidden() {
  79. return hssfWorkbook.isSheetHidden(hssfWorkbook.getSheetIndex(sheet));
  80. }
  81. public void setHidden(boolean b) {
  82. hssfWorkbook.setSheetHidden(hssfWorkbook.getSheetIndex(sheet), b);
  83. }
  84. public void setVeryHidden(boolean b) {
  85. if (b) {
  86. hssfWorkbook.setSheetHidden(hssfWorkbook.getSheetIndex(sheet), 2);
  87. }
  88. else {
  89. hssfWorkbook.setSheetHidden(hssfWorkbook.getSheetIndex(sheet), false);
  90. }
  91. }
  92. @Override
  93. public boolean isVeryHidden() {
  94. return hssfWorkbook.isSheetVeryHidden(hssfWorkbook.getSheetIndex(sheet));
  95. }
  96. public boolean equals(Object obj) {
  97. if (!(obj instanceof SheetHSSFImpl)) {
  98. return false;
  99. }
  100. SheetHSSFImpl other = (SheetHSSFImpl) obj;
  101. return sheet == other.sheet;
  102. }
  103. public HSSFSheet getHSSFSheet() {
  104. return sheet;
  105. }
  106. public int getColumnWidth(int col) {
  107. int width = (sheet.getColumnWidth(col) / 256) * 6;
  108. return width;
  109. }
  110. public String getName() {
  111. return hssfWorkbook.getSheetName(hssfWorkbook.getSheetIndex(sheet));
  112. }
  113. public int getMaxRows() {
  114. return MAX_ROWS;
  115. }
  116. public int getMaxColumns() {
  117. return MAX_COLUMNS;
  118. }
  119. public void clearAllCells() {
  120. for(Iterator<Row> it = sheet.rowIterator(); it.hasNext(); ) {
  121. Row row = it.next();
  122. sheet.removeRow(row);
  123. }
  124. }
  125. public Cell getCellAt(int col, int row) {
  126. HSSFRow hssfRow = sheet.getRow(row);
  127. if (hssfRow == null) {
  128. return null;
  129. }
  130. HSSFCell hssfCell = hssfRow.getCell(col);
  131. if (hssfCell == null) {
  132. return null;
  133. }
  134. else {
  135. return new CellHSSFImpl(hssfWorkbook, hssfCell);
  136. }
  137. }
  138. public Cell addCellAt(int col, int row) {
  139. HSSFRow hssfRow = sheet.getRow(row);
  140. if (hssfRow == null) {
  141. hssfRow = sheet.createRow(row);
  142. }
  143. HSSFCell cell = hssfRow.getCell(col);
  144. if (cell == null) {
  145. cell = hssfRow.createCell(col);
  146. }
  147. return new CellHSSFImpl(hssfWorkbook, cell);
  148. }
  149. public void clearCellAt(int col, int row) {
  150. HSSFRow theRow = sheet.getRow(row);
  151. if(theRow != null) {
  152. HSSFCell theCell = theRow.getCell(col);
  153. theCell.setCellValue("");
  154. }
  155. }
  156. public Collection<Validation> getIntersectingValidations(Range range) {
  157. ArrayList<Validation> intersectingValidations = new ArrayList<Validation>();
  158. for (Validation validation : range.getSheet().getValidations()) {
  159. if (validation.getRange().intersectsRange(range)) {
  160. intersectingValidations.add(validation);
  161. }
  162. }
  163. return intersectingValidations;
  164. }
  165. public Collection<Validation> getContainingValidations(Range range) {
  166. ArrayList<Validation> containingValidations = new ArrayList<Validation>();
  167. for (Validation validation : range.getSheet().getValidations()) {
  168. if (validation.getRange().containsRange(range)) {
  169. containingValidations.add(validation);
  170. }
  171. }
  172. return containingValidations;
  173. }
  174. /**
  175. * Creates a Formula List constraint validation, with creates a dropdown list based on values defined by namedRange(the name of the hidden sheet).
  176. */
  177. public void addValidation(String namedRange, int firstCol, int firstRow, int lastCol, int lastRow) {
  178. DataValidationConstraint constraint=sheet.getDataValidationHelper().createFormulaListConstraint(namedRange);
  179. addConstraint(constraint, firstCol, firstRow, lastCol, lastRow);
  180. }
  181. /**
  182. * Creates a custom validation that embeds the hidden sheet name (that contains the ontology details) .
  183. * e.g
  184. * =AND(A1<>"propliteral^wksowlv0")
  185. * this embeds the information, without restricting the use of the field (except the highly unlikely case of wanting to type the encoded string).
  186. */
  187. public void addLiteralValidation(String hiddenSheetName, int firstCol, int firstRow, int lastCol, int lastRow) {
  188. String encoded = PropertyValidationForumlaDefinition.encode(hiddenSheetName);
  189. //the cell title A1 is irrelevant, when the sheet is saved it gets turned into the current cell.
  190. String formula="AND(A1<>\""+encoded+"\")";
  191. DataValidationConstraint constraint = sheet.getDataValidationHelper().createCustomConstraint(formula);
  192. addConstraint(constraint, firstCol, firstRow, lastCol, lastRow);
  193. }
  194. protected void addConstraint(DataValidationConstraint constraint, int firstCol, int firstRow, int lastCol, int lastRow) {
  195. CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
  196. HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, constraint);
  197. sheet.addValidationData(dataValidation);
  198. }
  199. public Collection<Validation> getValidations() {
  200. List<Validation> validationList = new ArrayList<Validation>();
  201. for (HSSFDataValidation validation : getValidationData()) {
  202. for (CellRangeAddress address : validation.getRegions().getCellRangeAddresses()) {
  203. validationList.add(new ValidationImpl(validation.getConstraint().getFormula1(), this, address.getFirstColumn(), address.getLastColumn(), address.getFirstRow(), address.getLastRow()));
  204. }
  205. }
  206. return validationList;
  207. }
  208. public List<HSSFDataValidation> getValidationData() {
  209. return PatchedPoi.getInstance().getValidationData(sheet, hssfWorkbook);
  210. }
  211. public void clearValidationData() {
  212. PatchedPoi.getInstance().clearValidationData(sheet);
  213. }
  214. }