PageRenderTime 84ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/MergeCellsRecord.java

http://github.com/openmicroscopy/bioformats
Java | 302 lines | 142 code | 48 blank | 112 comment | 10 complexity | 566dd36cbee9d45551bc7589cba76b65 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. package loci.poi.hssf.record;
  38. import java.util.ArrayList;
  39. import java.util.Iterator;
  40. import loci.poi.util.LittleEndian;
  41. /**
  42. * Title: Merged Cells Record
  43. * <br>
  44. * Description: Optional record defining a square area of cells to "merged" into
  45. * one cell. <br>
  46. * REFERENCE: NONE (UNDOCUMENTED PRESENTLY) <br>
  47. * @author Andrew C. Oliver (acoliver at apache dot org)
  48. * @version 2.0-pre
  49. */
  50. public class MergeCellsRecord
  51. extends Record
  52. {
  53. public final static short sid = 0xe5;
  54. private ArrayList field_2_regions;
  55. public MergeCellsRecord()
  56. {
  57. }
  58. /**
  59. * Constructs a MergedCellsRecord and sets its fields appropriately
  60. * @param in the RecordInputstream to read the record from
  61. */
  62. public MergeCellsRecord(RecordInputStream in)
  63. {
  64. super(in);
  65. }
  66. protected void fillFields(RecordInputStream in)
  67. {
  68. short numAreas = in.readShort();
  69. field_2_regions = new ArrayList(numAreas + 10);
  70. for (int k = 0; k < numAreas; k++)
  71. {
  72. MergedRegion region =
  73. new MergedRegion(in.readShort(), in.readShort(),
  74. in.readShort(), in.readShort());
  75. field_2_regions.add(region);
  76. }
  77. }
  78. /**
  79. * get the number of merged areas. If this drops down to 0 you should just go
  80. * ahead and delete the record.
  81. * @return number of areas
  82. */
  83. public short getNumAreas()
  84. {
  85. //if the array size is larger than a short (65536), the record can't hold that many merges anyway
  86. if (field_2_regions == null) return 0;
  87. return (short)field_2_regions.size();
  88. }
  89. /**
  90. * set the number of merged areas. You do not need to call this if you use addArea,
  91. * it will be incremented automatically or decremented when an area is removed. If
  92. * you are setting this to 0 then you are a terrible person. Just remove the record.
  93. * (just kidding about you being a terrible person..hehe)
  94. * @deprecated We now link the size to the actual array of merged regions
  95. * @see #getNumAreas()
  96. * @param numareas number of areas
  97. */
  98. public void setNumAreas(short numareas)
  99. {
  100. }
  101. /**
  102. * Add an area to consider a merged cell. The index returned is only gauranteed to
  103. * be correct provided you do not add ahead of or remove ahead of it (in which case
  104. * you should increment or decrement appropriately....in other words its an arrayList)
  105. *
  106. * @param rowfrom - the upper left hand corner's row
  107. * @param colfrom - the upper left hand corner's col
  108. * @param rowto - the lower right hand corner's row
  109. * @param colto - the lower right hand corner's col
  110. * @return new index of said area (don't depend on it if you add/remove)
  111. */
  112. //public int addArea(short rowfrom, short colfrom, short rowto, short colto)
  113. public int addArea(int rowfrom, short colfrom, int rowto, short colto)
  114. {
  115. if (field_2_regions == null)
  116. {
  117. field_2_regions = new ArrayList(10);
  118. }
  119. MergedRegion region = new MergedRegion(rowfrom, rowto, colfrom,
  120. colto);
  121. field_2_regions.add(region);
  122. return field_2_regions.size() - 1;
  123. }
  124. /**
  125. * essentially unmerge the cells in the "area" stored at the passed in index
  126. * @param area index
  127. */
  128. public void removeAreaAt(int area)
  129. {
  130. field_2_regions.remove(area);
  131. }
  132. /**
  133. * return the MergedRegion at the given index.
  134. *
  135. * @return MergedRegion representing the area that is Merged (r1,c1 - r2,c2)
  136. */
  137. public MergedRegion getAreaAt(int index)
  138. {
  139. return ( MergedRegion ) field_2_regions.get(index);
  140. }
  141. public int getRecordSize()
  142. {
  143. int retValue;
  144. retValue = 6 + (8 * field_2_regions.size());
  145. return retValue;
  146. }
  147. public short getSid()
  148. {
  149. return sid;
  150. }
  151. public int serialize(int offset, byte [] data)
  152. {
  153. int recordsize = getRecordSize();
  154. int pos = 6;
  155. LittleEndian.putShort(data, offset + 0, sid);
  156. LittleEndian.putShort(data, offset + 2, ( short ) (recordsize - 4));
  157. LittleEndian.putShort(data, offset + 4, getNumAreas());
  158. for (int k = 0; k < getNumAreas(); k++)
  159. {
  160. MergedRegion region = getAreaAt(k);
  161. //LittleEndian.putShort(data, offset + pos, region.row_from);
  162. LittleEndian.putShort(data, offset + pos, ( short ) region.row_from);
  163. pos += 2;
  164. //LittleEndian.putShort(data, offset + pos, region.row_to);
  165. LittleEndian.putShort(data, offset + pos, ( short ) region.row_to);
  166. pos += 2;
  167. LittleEndian.putShort(data, offset + pos, region.col_from);
  168. pos += 2;
  169. LittleEndian.putShort(data, offset + pos, region.col_to);
  170. pos += 2;
  171. }
  172. return recordsize;
  173. }
  174. public String toString()
  175. {
  176. StringBuffer retval = new StringBuffer();
  177. retval.append("[MERGEDCELLS]").append("\n");
  178. retval.append(" .sid =").append(sid).append("\n");
  179. retval.append(" .numregions =").append(getNumAreas())
  180. .append("\n");
  181. for (int k = 0; k < getNumAreas(); k++)
  182. {
  183. MergedRegion region = ( MergedRegion ) field_2_regions.get(k);
  184. retval.append(" .rowfrom =").append(region.row_from)
  185. .append("\n");
  186. retval.append(" .colfrom =").append(region.col_from)
  187. .append("\n");
  188. retval.append(" .rowto =").append(region.row_to)
  189. .append("\n");
  190. retval.append(" .colto =").append(region.col_to)
  191. .append("\n");
  192. }
  193. retval.append("[MERGEDCELLS]").append("\n");
  194. return retval.toString();
  195. }
  196. protected void validateSid(short id)
  197. {
  198. if (id != sid)
  199. {
  200. throw new RecordFormatException("NOT A MERGEDCELLS RECORD!! "
  201. + id);
  202. }
  203. }
  204. /**
  205. * this is a low level representation of a MergedRegion of cells. It is an
  206. * inner class because we do not want it used without reference to this class.
  207. *
  208. */
  209. public class MergedRegion
  210. {
  211. /**
  212. * create a merged region all in one stroke.
  213. */
  214. //public MergedRegion(short row_from, short row_to, short col_from,
  215. public MergedRegion(int row_from, int row_to, short col_from,
  216. short col_to)
  217. {
  218. this.row_from = row_from;
  219. this.row_to = row_to;
  220. this.col_from = col_from;
  221. this.col_to = col_to;
  222. }
  223. /**
  224. * upper lefthand corner row
  225. */
  226. //public short row_from;
  227. public int row_from;
  228. /**
  229. * lower right hand corner row
  230. */
  231. //public short row_to;
  232. public int row_to;
  233. /**
  234. * upper right hand corner col
  235. */
  236. public short col_from;
  237. /**
  238. * lower right hand corner col
  239. */
  240. public short col_to;
  241. }
  242. public Object clone() {
  243. MergeCellsRecord rec = new MergeCellsRecord();
  244. rec.field_2_regions = new ArrayList();
  245. Iterator iterator = field_2_regions.iterator();
  246. while (iterator.hasNext()) {
  247. MergedRegion oldRegion = (MergedRegion)iterator.next();
  248. rec.addArea(oldRegion.row_from, oldRegion.col_from, oldRegion.row_to, oldRegion.col_to);
  249. }
  250. return rec;
  251. }
  252. }