PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/usermodel/HSSFRow.java

http://github.com/openmicroscopy/bioformats
Java | 535 lines | 264 code | 81 blank | 190 comment | 44 complexity | 9bdfaebc73779ded20ea18e833663dcc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. /*
  38. * HSSFRow.java
  39. *
  40. * Created on September 30, 2001, 3:44 PM
  41. */
  42. package loci.poi.hssf.usermodel;
  43. import loci.poi.hssf.model.Sheet;
  44. import loci.poi.hssf.model.Workbook;
  45. import loci.poi.hssf.record.CellValueRecordInterface;
  46. import loci.poi.hssf.record.RowRecord;
  47. import java.util.HashMap;
  48. import java.util.Iterator;
  49. import java.util.NoSuchElementException;
  50. /**
  51. * High level representation of a row of a spreadsheet.
  52. *
  53. * Only rows that have cells should be added to a Sheet.
  54. * @version 1.0-pre
  55. * @author Andrew C. Oliver (acoliver at apache dot org)
  56. * @author Glen Stampoultzis (glens at apache.org)
  57. */
  58. public class HSSFRow
  59. implements Comparable
  60. {
  61. // used for collections
  62. public final static int INITIAL_CAPACITY = 5;
  63. //private short rowNum;
  64. private int rowNum;
  65. private HSSFCell[] cells=new HSSFCell[INITIAL_CAPACITY];
  66. // private short firstcell = -1;
  67. // private short lastcell = -1;
  68. /**
  69. * reference to low level representation
  70. */
  71. private RowRecord row;
  72. /**
  73. * reference to containing low level Workbook
  74. */
  75. private Workbook book;
  76. /**
  77. * reference to containing Sheet
  78. */
  79. private Sheet sheet;
  80. protected HSSFRow()
  81. {
  82. }
  83. /**
  84. * Creates new HSSFRow from scratch. Only HSSFSheet should do this.
  85. *
  86. * @param book low-level Workbook object containing the sheet that contains this row
  87. * @param sheet low-level Sheet object that contains this Row
  88. * @param rowNum the row number of this row (0 based)
  89. * @see loci.poi.hssf.usermodel.HSSFSheet#createRow(int)
  90. */
  91. //protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
  92. protected HSSFRow(Workbook book, Sheet sheet, int rowNum)
  93. {
  94. this.rowNum = rowNum;
  95. this.book = book;
  96. this.sheet = sheet;
  97. row = new RowRecord();
  98. row.setOptionFlags( (short)0x100 ); // seems necessary for outlining to work.
  99. row.setHeight((short) 0xff);
  100. row.setLastCol((short) -1);
  101. row.setFirstCol((short) -1);
  102. setRowNum(rowNum);
  103. }
  104. /**
  105. * Creates an HSSFRow from a low level RowRecord object. Only HSSFSheet should do
  106. * this. HSSFSheet uses this when an existing file is read in.
  107. *
  108. * @param book low-level Workbook object containing the sheet that contains this row
  109. * @param sheet low-level Sheet object that contains this Row
  110. * @param record the low level api object this row should represent
  111. * @see loci.poi.hssf.usermodel.HSSFSheet#createRow(int)
  112. */
  113. protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)
  114. {
  115. this.book = book;
  116. this.sheet = sheet;
  117. row = record;
  118. setRowNum(record.getRowNumber());
  119. }
  120. /**
  121. * Use this to create new cells within the row and return it.
  122. * <p>
  123. * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
  124. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
  125. *
  126. * @param column - the column number this cell represents
  127. *
  128. * @return HSSFCell a high level representation of the created cell.
  129. */
  130. public HSSFCell createCell(short column)
  131. {
  132. return this.createCell(column,HSSFCell.CELL_TYPE_BLANK);
  133. }
  134. /**
  135. * Use this to create new cells within the row and return it.
  136. * <p>
  137. * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
  138. * either through calling setCellValue or setCellType.
  139. *
  140. * @param column - the column number this cell represents
  141. *
  142. * @return HSSFCell a high level representation of the created cell.
  143. */
  144. public HSSFCell createCell(short column, int type)
  145. {
  146. HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column, type);
  147. addCell(cell);
  148. sheet.addValueRecord(getRowNum(), cell.getCellValueRecord());
  149. return cell;
  150. }
  151. /**
  152. * remove the HSSFCell from this row.
  153. * @param cell to remove
  154. */
  155. public void removeCell(HSSFCell cell)
  156. {
  157. CellValueRecordInterface cval = cell.getCellValueRecord();
  158. sheet.removeValueRecord(getRowNum(), cval);
  159. short column=cell.getCellNum();
  160. if(cell!=null && column<cells.length)
  161. {
  162. cells[column]=null;
  163. }
  164. if (cell.getCellNum() == row.getLastCol())
  165. {
  166. row.setLastCol(findLastCell(row.getLastCol()));
  167. }
  168. if (cell.getCellNum() == row.getFirstCol())
  169. {
  170. row.setFirstCol(findFirstCell(row.getFirstCol()));
  171. }
  172. }
  173. /**
  174. * create a high level HSSFCell object from an existing low level record. Should
  175. * only be called from HSSFSheet or HSSFRow itself.
  176. * @param cell low level cell to create the high level representation from
  177. * @return HSSFCell representing the low level record passed in
  178. */
  179. protected HSSFCell createCellFromRecord(CellValueRecordInterface cell)
  180. {
  181. HSSFCell hcell = new HSSFCell(book, sheet, getRowNum(), cell);
  182. addCell(hcell);
  183. // sheet.addValueRecord(getRowNum(),cell.getCellValueRecord());
  184. return hcell;
  185. }
  186. /**
  187. * set the row number of this row.
  188. * @param rowNum the row number (0-based)
  189. * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
  190. */
  191. //public void setRowNum(short rowNum)
  192. public void setRowNum(int rowNum)
  193. {
  194. if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER))
  195. throw new IndexOutOfBoundsException("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">");
  196. this.rowNum = rowNum;
  197. if (row != null)
  198. {
  199. row.setRowNumber(rowNum); // used only for KEY comparison (HSSFRow)
  200. }
  201. }
  202. /**
  203. * get row number this row represents
  204. * @return the row number (0 based)
  205. */
  206. //public short getRowNum()
  207. public int getRowNum()
  208. {
  209. return rowNum;
  210. }
  211. /**
  212. * used internally to add a cell.
  213. */
  214. private void addCell(HSSFCell cell)
  215. {
  216. short column=cell.getCellNum();
  217. if (row.getFirstCol() == -1)
  218. {
  219. row.setFirstCol(column);
  220. }
  221. if (row.getLastCol() == -1)
  222. {
  223. row.setLastCol(column);
  224. }
  225. if(column>=cells.length)
  226. {
  227. HSSFCell[] oldCells=cells;
  228. int newSize=oldCells.length*2;
  229. if(newSize<column+1) newSize=column+1;
  230. cells=new HSSFCell[newSize];
  231. System.arraycopy(oldCells,0,cells,0,oldCells.length);
  232. }
  233. cells[column]=cell;
  234. if (column < row.getFirstCol())
  235. {
  236. row.setFirstCol(column);
  237. }
  238. if (column > row.getLastCol())
  239. {
  240. row.setLastCol(column);
  241. }
  242. }
  243. /**
  244. * get the hssfcell representing a given column (logical cell) 0-based. If you
  245. * ask for a cell that is not defined....you get a null.
  246. *
  247. * @param cellnum 0 based column number
  248. * @return HSSFCell representing that column or null if undefined.
  249. */
  250. public HSSFCell getCell(short cellnum)
  251. {
  252. if(cellnum<0||cellnum>=cells.length) return null;
  253. return cells[cellnum];
  254. }
  255. /**
  256. * get the number of the first cell contained in this row.
  257. * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
  258. */
  259. public short getFirstCellNum()
  260. {
  261. if (getPhysicalNumberOfCells() == 0)
  262. return -1;
  263. else
  264. return row.getFirstCol();
  265. }
  266. /**
  267. * gets the number of the last cell contained in this row <b>PLUS ONE</b>.
  268. * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the row does not contain any cells.
  269. */
  270. public short getLastCellNum()
  271. {
  272. if (getPhysicalNumberOfCells() == 0)
  273. return -1;
  274. else
  275. return row.getLastCol();
  276. }
  277. /**
  278. * gets the number of defined cells (NOT number of cells in the actual row!).
  279. * That is to say if only columns 0,4,5 have values then there would be 3.
  280. * @return int representing the number of defined cells in the row.
  281. */
  282. public int getPhysicalNumberOfCells()
  283. {
  284. int count=0;
  285. for(int i=0;i<cells.length;i++)
  286. {
  287. if(cells[i]!=null) count++;
  288. }
  289. return count;
  290. }
  291. /**
  292. * set the row's height or set to ff (-1) for undefined/default-height. Set the height in "twips" or
  293. * 1/20th of a point.
  294. * @param height rowheight or 0xff for undefined (use sheet default)
  295. */
  296. public void setHeight(short height)
  297. {
  298. // row.setOptionFlags(
  299. row.setBadFontHeight(true);
  300. row.setHeight(height);
  301. }
  302. /**
  303. * set whether or not to display this row with 0 height
  304. * @param zHeight height is zero or not.
  305. */
  306. public void setZeroHeight(boolean zHeight) {
  307. row.setZeroHeight(zHeight);
  308. }
  309. /**
  310. * get whether or not to display this row with 0 height
  311. * @return - zHeight height is zero or not.
  312. */
  313. public boolean getZeroHeight() {
  314. return row.getZeroHeight();
  315. }
  316. /**
  317. * set the row's height in points.
  318. * @param height row height in points
  319. */
  320. public void setHeightInPoints(float height)
  321. {
  322. // row.setOptionFlags(
  323. row.setBadFontHeight(true);
  324. row.setHeight((short) (height * 20));
  325. }
  326. /**
  327. * get the row's height or ff (-1) for undefined/default-height in twips (1/20th of a point)
  328. * @return rowheight or 0xff for undefined (use sheet default)
  329. */
  330. public short getHeight()
  331. {
  332. return row.getHeight();
  333. }
  334. /**
  335. * get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())
  336. * @return rowheight or 0xff for undefined (use sheet default)
  337. */
  338. public float getHeightInPoints()
  339. {
  340. return (row.getHeight() / 20);
  341. }
  342. /**
  343. * get the lowlevel RowRecord represented by this object - should only be called
  344. * by other parts of the high level API
  345. *
  346. * @return RowRecord this row represents
  347. */
  348. protected RowRecord getRowRecord()
  349. {
  350. return row;
  351. }
  352. /**
  353. * used internally to refresh the "last cell" when the last cell is removed.
  354. */
  355. private short findLastCell(short lastcell)
  356. {
  357. short cellnum = (short) (lastcell - 1);
  358. HSSFCell r = getCell(cellnum);
  359. while (r == null && cellnum >= 0)
  360. {
  361. r = getCell(--cellnum);
  362. }
  363. return cellnum;
  364. }
  365. /**
  366. * used internally to refresh the "first cell" when the first cell is removed.
  367. */
  368. private short findFirstCell(short firstcell)
  369. {
  370. short cellnum = (short) (firstcell + 1);
  371. HSSFCell r = getCell(cellnum);
  372. while (r == null && cellnum <= getLastCellNum())
  373. {
  374. r = getCell(++cellnum);
  375. }
  376. if (cellnum > getLastCellNum())
  377. return -1;
  378. return cellnum;
  379. }
  380. /**
  381. * @return cell iterator of the physically defined cells. Note element 4 may
  382. * actually be row cell depending on how many are defined!
  383. */
  384. public Iterator cellIterator()
  385. {
  386. return new CellIterator();
  387. }
  388. private class CellIterator implements Iterator
  389. {
  390. int thisId=-1;
  391. int nextId=-1;
  392. public CellIterator()
  393. {
  394. findNext();
  395. }
  396. public boolean hasNext() {
  397. return nextId<cells.length;
  398. }
  399. public Object next() {
  400. if (!hasNext())
  401. throw new NoSuchElementException("At last element");
  402. HSSFCell cell=cells[nextId];
  403. thisId=nextId;
  404. findNext();
  405. return cell;
  406. }
  407. public void remove() {
  408. if (thisId == -1)
  409. throw new IllegalStateException("remove() called before next()");
  410. cells[thisId]=null;
  411. }
  412. private void findNext()
  413. {
  414. int i=nextId+1;
  415. for(;i<cells.length;i++)
  416. {
  417. if(cells[i]!=null) break;
  418. }
  419. nextId=i;
  420. }
  421. }
  422. public int compareTo(Object obj)
  423. {
  424. HSSFRow loc = (HSSFRow) obj;
  425. if (this.getRowNum() == loc.getRowNum())
  426. {
  427. return 0;
  428. }
  429. if (this.getRowNum() < loc.getRowNum())
  430. {
  431. return -1;
  432. }
  433. if (this.getRowNum() > loc.getRowNum())
  434. {
  435. return 1;
  436. }
  437. return -1;
  438. }
  439. public boolean equals(Object obj)
  440. {
  441. if (!(obj instanceof HSSFRow))
  442. {
  443. return false;
  444. }
  445. HSSFRow loc = (HSSFRow) obj;
  446. if (this.getRowNum() == loc.getRowNum())
  447. {
  448. return true;
  449. }
  450. return false;
  451. }
  452. }