PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

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