PageRenderTime 92ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/formula/AreaPtg.java

http://github.com/openmicroscopy/bioformats
Java | 326 lines | 175 code | 42 blank | 109 comment | 0 complexity | b386874db658ca08b6fcbb5474030ea0 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.formula;
  38. import loci.poi.util.LittleEndian;
  39. import loci.poi.util.BitField;
  40. import loci.poi.util.BitFieldFactory;
  41. import loci.poi.hssf.util.AreaReference;
  42. import loci.poi.hssf.util.CellReference;
  43. import loci.poi.hssf.model.Workbook;
  44. import loci.poi.hssf.record.RecordInputStream;
  45. /**
  46. * Specifies a rectangular area of cells A1:A4 for instance.
  47. * @author andy
  48. * @author Jason Height (jheight at chariot dot net dot au)
  49. */
  50. public class AreaPtg
  51. extends Ptg
  52. {
  53. public final static short sid = 0x25;
  54. private final static int SIZE = 9;
  55. private short field_1_first_row;
  56. private short field_2_last_row;
  57. private short field_3_first_column;
  58. private short field_4_last_column;
  59. private BitField rowRelative = BitFieldFactory.getInstance(0x8000);
  60. private BitField colRelative = BitFieldFactory.getInstance(0x4000);
  61. private BitField column = BitFieldFactory.getInstance(0x3FFF);
  62. protected AreaPtg() {
  63. //Required for clone methods
  64. }
  65. public AreaPtg(String arearef) {
  66. AreaReference ar = new AreaReference(arearef);
  67. setFirstRow((short)ar.getCells()[0].getRow());
  68. setFirstColumn((short)ar.getCells()[0].getCol());
  69. setLastRow((short)ar.getCells()[1].getRow());
  70. setLastColumn((short)ar.getCells()[1].getCol());
  71. setFirstColRelative(!ar.getCells()[0].isColAbsolute());
  72. setLastColRelative(!ar.getCells()[1].isColAbsolute());
  73. setFirstRowRelative(!ar.getCells()[0].isRowAbsolute());
  74. setLastRowRelative(!ar.getCells()[1].isRowAbsolute());
  75. }
  76. public AreaPtg(short firstRow, short lastRow, short firstColumn, short lastColumn, boolean firstRowRelative, boolean lastRowRelative, boolean firstColRelative, boolean lastColRelative) {
  77. setFirstRow(firstRow);
  78. setLastRow(lastRow);
  79. setFirstColumn(firstColumn);
  80. setLastColumn(lastColumn);
  81. setFirstRowRelative(firstRowRelative);
  82. setLastRowRelative(lastRowRelative);
  83. setFirstColRelative(firstColRelative);
  84. setLastColRelative(lastColRelative);
  85. }
  86. public AreaPtg(RecordInputStream in)
  87. {
  88. field_1_first_row = in.readShort();
  89. field_2_last_row = in.readShort();
  90. field_3_first_column = in.readShort();
  91. field_4_last_column = in.readShort();
  92. //System.out.println(toString());
  93. }
  94. public String getAreaPtgName() {
  95. return "AreaPtg";
  96. }
  97. public String toString()
  98. {
  99. StringBuffer buffer = new StringBuffer();
  100. buffer.append(getAreaPtgName());
  101. buffer.append("\n");
  102. buffer.append("firstRow = " + getFirstRow()).append("\n");
  103. buffer.append("lastRow = " + getLastRow()).append("\n");
  104. buffer.append("firstCol = " + getFirstColumn()).append("\n");
  105. buffer.append("lastCol = " + getLastColumn()).append("\n");
  106. buffer.append("firstColRowRel= "
  107. + isFirstRowRelative()).append("\n");
  108. buffer.append("lastColRowRel = "
  109. + isLastRowRelative()).append("\n");
  110. buffer.append("firstColRel = " + isFirstColRelative()).append("\n");
  111. buffer.append("lastColRel = " + isLastColRelative()).append("\n");
  112. return buffer.toString();
  113. }
  114. public void writeBytes(byte [] array, int offset) {
  115. array[offset] = (byte) (sid + ptgClass);
  116. LittleEndian.putShort(array,offset+1,field_1_first_row);
  117. LittleEndian.putShort(array,offset+3,field_2_last_row);
  118. LittleEndian.putShort(array,offset+5,field_3_first_column);
  119. LittleEndian.putShort(array,offset+7,field_4_last_column);
  120. }
  121. public int getSize()
  122. {
  123. return SIZE;
  124. }
  125. /**
  126. * @return the first row in the area
  127. */
  128. public short getFirstRow()
  129. {
  130. return field_1_first_row;
  131. }
  132. /**
  133. * sets the first row
  134. * @param row number (0-based)
  135. */
  136. public void setFirstRow(short row)
  137. {
  138. field_1_first_row = row;
  139. }
  140. /**
  141. * @return last row in the range (x2 in x1,y1-x2,y2)
  142. */
  143. public short getLastRow()
  144. {
  145. return field_2_last_row;
  146. }
  147. /**
  148. * @param row last row number in the area
  149. */
  150. public void setLastRow(short row)
  151. {
  152. field_2_last_row = row;
  153. }
  154. /**
  155. * @return the first column number in the area.
  156. */
  157. public short getFirstColumn()
  158. {
  159. return column.getShortValue(field_3_first_column);
  160. }
  161. /**
  162. * @return the first column number + the options bit settings unstripped
  163. */
  164. public short getFirstColumnRaw()
  165. {
  166. return field_3_first_column;
  167. }
  168. /**
  169. * @return whether or not the first row is a relative reference or not.
  170. */
  171. public boolean isFirstRowRelative()
  172. {
  173. return rowRelative.isSet(field_3_first_column);
  174. }
  175. /**
  176. * sets the first row to relative or not
  177. * @param rel is relative or not.
  178. */
  179. public void setFirstRowRelative(boolean rel) {
  180. field_3_first_column=rowRelative.setShortBoolean(field_3_first_column,rel);
  181. }
  182. /**
  183. * @return isrelative first column to relative or not
  184. */
  185. public boolean isFirstColRelative()
  186. {
  187. return colRelative.isSet(field_3_first_column);
  188. }
  189. /**
  190. * set whether the first column is relative
  191. */
  192. public void setFirstColRelative(boolean rel) {
  193. field_3_first_column=colRelative.setShortBoolean(field_3_first_column,rel);
  194. }
  195. /**
  196. * set the first column in the area
  197. */
  198. public void setFirstColumn(short column)
  199. {
  200. field_3_first_column = column; // fixme
  201. }
  202. /**
  203. * set the first column irespective of the bitmasks
  204. */
  205. public void setFirstColumnRaw(short column)
  206. {
  207. field_3_first_column = column;
  208. }
  209. /**
  210. * @return lastcolumn in the area
  211. */
  212. public short getLastColumn()
  213. {
  214. return column.getShortValue(field_4_last_column);
  215. }
  216. /**
  217. * @return last column and bitmask (the raw field)
  218. */
  219. public short getLastColumnRaw()
  220. {
  221. return field_4_last_column;
  222. }
  223. /**
  224. * @return last row relative or not
  225. */
  226. public boolean isLastRowRelative()
  227. {
  228. return rowRelative.isSet(field_4_last_column);
  229. }
  230. /**
  231. * set whether the last row is relative or not
  232. * @param rel <code>true</code> if the last row relative, else
  233. * <code>false</code>
  234. */
  235. public void setLastRowRelative(boolean rel) {
  236. field_4_last_column=rowRelative.setShortBoolean(field_4_last_column,rel);
  237. }
  238. /**
  239. * @return lastcol relative or not
  240. */
  241. public boolean isLastColRelative()
  242. {
  243. return colRelative.isSet(field_4_last_column);
  244. }
  245. /**
  246. * set whether the last column should be relative or not
  247. */
  248. public void setLastColRelative(boolean rel) {
  249. field_4_last_column=colRelative.setShortBoolean(field_4_last_column,rel);
  250. }
  251. /**
  252. * set the last column in the area
  253. */
  254. public void setLastColumn(short column)
  255. {
  256. field_4_last_column = column; // fixme
  257. }
  258. /**
  259. * set the last column irrespective of the bitmasks
  260. */
  261. public void setLastColumnRaw(short column)
  262. {
  263. field_4_last_column = column;
  264. }
  265. public String toFormulaString(Workbook book)
  266. {
  267. return (new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative())).toString() + ":" +
  268. (new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative())).toString();
  269. }
  270. public byte getDefaultOperandClass() {
  271. return Ptg.CLASS_REF;
  272. }
  273. public Object clone() {
  274. AreaPtg ptg = new AreaPtg();
  275. ptg.field_1_first_row = field_1_first_row;
  276. ptg.field_2_last_row = field_2_last_row;
  277. ptg.field_3_first_column = field_3_first_column;
  278. ptg.field_4_last_column = field_4_last_column;
  279. ptg.setClass(ptgClass);
  280. return ptg;
  281. }
  282. }