You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFClientAnchor.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.poi.ddf.EscherClientAnchorRecord;
  17. import org.apache.poi.ddf.EscherRecord;
  18. import org.apache.poi.ss.usermodel.ClientAnchor;
  19. /**
  20. * A client anchor is attached to an excel worksheet. It anchors against a
  21. * top-left and buttom-right cell.
  22. */
  23. public final class HSSFClientAnchor extends HSSFAnchor implements ClientAnchor {
  24. private EscherClientAnchorRecord _escherClientAnchor;
  25. public HSSFClientAnchor(EscherClientAnchorRecord escherClientAnchorRecord) {
  26. this._escherClientAnchor = escherClientAnchorRecord;
  27. }
  28. /**
  29. * Creates a new client anchor and defaults all the anchor positions to 0.
  30. */
  31. public HSSFClientAnchor() {
  32. }
  33. /**
  34. * Creates a new client anchor and sets the top-left and bottom-right
  35. * coordinates of the anchor.
  36. *
  37. * Note: Microsoft Excel seems to sometimes disallow
  38. * higher y1 than y2 or higher x1 than x2, you might need to
  39. * reverse them and draw shapes vertically or horizontally flipped!
  40. *
  41. * @param dx1 the x coordinate within the first cell.
  42. * @param dy1 the y coordinate within the first cell.
  43. * @param dx2 the x coordinate within the second cell.
  44. * @param dy2 the y coordinate within the second cell.
  45. * @param col1 the column (0 based) of the first cell.
  46. * @param row1 the row (0 based) of the first cell.
  47. * @param col2 the column (0 based) of the second cell.
  48. * @param row2 the row (0 based) of the second cell.
  49. */
  50. public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) {
  51. super(dx1, dy1, dx2, dy2);
  52. checkRange(dx1, 0, 1023, "dx1");
  53. checkRange(dx2, 0, 1023, "dx2");
  54. checkRange(dy1, 0, 255, "dy1");
  55. checkRange(dy2, 0, 255, "dy2");
  56. checkRange(col1, 0, 255, "col1");
  57. checkRange(col2, 0, 255, "col2");
  58. checkRange(row1, 0, 255 * 256, "row1");
  59. checkRange(row2, 0, 255 * 256, "row2");
  60. setCol1((short) Math.min(col1, col2));
  61. setCol2((short) Math.max(col1, col2));
  62. setRow1((short) Math.min(row1, row2));
  63. setRow2((short) Math.max(row1, row2));
  64. if (col1 > col2){
  65. _isHorizontallyFlipped = true;
  66. }
  67. if (row1 > row2){
  68. _isVerticallyFlipped = true;
  69. }
  70. }
  71. /**
  72. * Calculates the height of a client anchor in points.
  73. *
  74. * @param sheet the sheet the anchor will be attached to
  75. * @return the shape height.
  76. */
  77. public float getAnchorHeightInPoints(HSSFSheet sheet) {
  78. int y1 = getDy1();
  79. int y2 = getDy2();
  80. int row1 = Math.min(getRow1(), getRow2());
  81. int row2 = Math.max(getRow1(), getRow2());
  82. float points = 0;
  83. if (row1 == row2) {
  84. points = ((y2 - y1) / 256.0f) * getRowHeightInPoints(sheet, row2);
  85. } else {
  86. points += ((256.0f - y1) / 256.0f) * getRowHeightInPoints(sheet, row1);
  87. for (int i = row1 + 1; i < row2; i++) {
  88. points += getRowHeightInPoints(sheet, i);
  89. }
  90. points += (y2 / 256.0f) * getRowHeightInPoints(sheet, row2);
  91. }
  92. return points;
  93. }
  94. private float getRowHeightInPoints(HSSFSheet sheet, int rowNum) {
  95. HSSFRow row = sheet.getRow(rowNum);
  96. if (row == null) {
  97. return sheet.getDefaultRowHeightInPoints();
  98. }
  99. return row.getHeightInPoints();
  100. }
  101. /**
  102. * @return the column(0 based) of the first cell.
  103. */
  104. public short getCol1() {
  105. return _escherClientAnchor.getCol1();
  106. }
  107. /**
  108. * @param col1 the column(0 based) of the first cell.
  109. */
  110. public void setCol1(short col1) {
  111. checkRange(col1, 0, 255, "col1");
  112. _escherClientAnchor.setCol1(col1);
  113. }
  114. /**
  115. * @param col1 0-based column of the first cell.
  116. */
  117. public void setCol1(int col1) {
  118. setCol1((short) col1);
  119. }
  120. /**
  121. * @return the column(0 based) of the first cell.
  122. */
  123. public short getCol2() {
  124. return _escherClientAnchor.getCol2();
  125. }
  126. /**
  127. * @param col2 the column(0 based) of the second cell.
  128. */
  129. public void setCol2(short col2) {
  130. checkRange(col2, 0, 255, "col2");
  131. _escherClientAnchor.setCol2(col2);
  132. }
  133. /**
  134. * @param col2 the column(0 based) of the second cell.
  135. */
  136. public void setCol2(int col2) {
  137. setCol2((short) col2);
  138. }
  139. /**
  140. * @return the row(0 based) of the first cell.
  141. */
  142. public int getRow1() {
  143. return _escherClientAnchor.getRow1();
  144. }
  145. /**
  146. * @param row1 0-based row of the first cell.
  147. */
  148. public void setRow1(int row1) {
  149. checkRange(row1, 0, 256 * 256, "row1");
  150. _escherClientAnchor.setRow1(Integer.valueOf(row1).shortValue());
  151. }
  152. /**
  153. * @return the row(0 based) of the second cell.
  154. */
  155. public int getRow2() {
  156. return _escherClientAnchor.getRow2();
  157. }
  158. /**
  159. * @param row2 the row(0 based) of the second cell.
  160. */
  161. public void setRow2(int row2) {
  162. checkRange(row2, 0, 256 * 256, "row2");
  163. _escherClientAnchor.setRow2(Integer.valueOf(row2).shortValue());
  164. }
  165. /**
  166. * Sets the top-left and bottom-right coordinates of
  167. * the anchor.
  168. *
  169. * Note: Microsoft Excel seems to sometimes disallow
  170. * higher y1 than y2 or higher x1 than x2, you might need to
  171. * reverse them and draw shapes vertically or horizontally flipped!
  172. *
  173. * @param x1 the x coordinate within the first cell.
  174. * @param y1 the y coordinate within the first cell.
  175. * @param x2 the x coordinate within the second cell.
  176. * @param y2 the y coordinate within the second cell.
  177. * @param col1 the column (0 based) of the first cell.
  178. * @param row1 the row (0 based) of the first cell.
  179. * @param col2 the column (0 based) of the second cell.
  180. * @param row2 the row (0 based) of the second cell.
  181. */
  182. public void setAnchor(short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2) {
  183. checkRange(getDx1(), 0, 1023, "dx1");
  184. checkRange(getDx2(), 0, 1023, "dx2");
  185. checkRange(getDy1(), 0, 255, "dy1");
  186. checkRange(getDy2(), 0, 255, "dy2");
  187. checkRange(getCol1(), 0, 255, "col1");
  188. checkRange(getCol2(), 0, 255, "col2");
  189. checkRange(getRow1(), 0, 255 * 256, "row1");
  190. checkRange(getRow2(), 0, 255 * 256, "row2");
  191. setCol1(col1);
  192. setRow1(row1);
  193. setDx1(x1);
  194. setDy1(y1);
  195. setCol2(col2);
  196. setRow2(row2);
  197. setDx2(x2);
  198. setDy2(y2);
  199. }
  200. public boolean isHorizontallyFlipped() {
  201. return _isHorizontallyFlipped;
  202. }
  203. public boolean isVerticallyFlipped() {
  204. return _isVerticallyFlipped;
  205. }
  206. @Override
  207. protected EscherRecord getEscherAnchor() {
  208. return _escherClientAnchor;
  209. }
  210. @Override
  211. protected void createEscherAnchor() {
  212. _escherClientAnchor = new EscherClientAnchorRecord();
  213. }
  214. /**
  215. * Gets the anchor type
  216. * <p/>
  217. * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
  218. */
  219. public int getAnchorType() {
  220. return _escherClientAnchor.getFlag();
  221. }
  222. /**
  223. * Sets the anchor type
  224. * <p/>
  225. * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
  226. */
  227. public void setAnchorType(int anchorType) {
  228. _escherClientAnchor.setFlag(Integer.valueOf(anchorType).shortValue());
  229. }
  230. private void checkRange(int value, int minRange, int maxRange, String varName) {
  231. if (value < minRange || value > maxRange)
  232. throw new IllegalArgumentException(varName + " must be between " + minRange + " and " + maxRange + ", but was: " + value);
  233. }
  234. @Override
  235. public boolean equals(Object obj) {
  236. if (obj == null)
  237. return false;
  238. if (obj == this)
  239. return true;
  240. if (obj.getClass() != getClass())
  241. return false;
  242. HSSFClientAnchor anchor = (HSSFClientAnchor) obj;
  243. return anchor.getCol1() == getCol1() && anchor.getCol2() == getCol2() && anchor.getDx1() == getDx1()
  244. && anchor.getDx2() == getDx2() && anchor.getDy1() == getDy1() && anchor.getDy2() == getDy2()
  245. && anchor.getRow1() == getRow1() && anchor.getRow2() == getRow2() && anchor.getAnchorType() == getAnchorType();
  246. }
  247. @Override
  248. public int hashCode() {
  249. assert false : "hashCode not designed";
  250. return 42; // any arbitrary constant will do
  251. }
  252. @Override
  253. public int getDx1() {
  254. return _escherClientAnchor.getDx1();
  255. }
  256. @Override
  257. public void setDx1(int dx1) {
  258. _escherClientAnchor.setDx1(Integer.valueOf(dx1).shortValue());
  259. }
  260. @Override
  261. public int getDy1() {
  262. return _escherClientAnchor.getDy1();
  263. }
  264. @Override
  265. public void setDy1(int dy1) {
  266. _escherClientAnchor.setDy1(Integer.valueOf(dy1).shortValue());
  267. }
  268. @Override
  269. public int getDy2() {
  270. return _escherClientAnchor.getDy2();
  271. }
  272. @Override
  273. public void setDy2(int dy2) {
  274. _escherClientAnchor.setDy2(Integer.valueOf(dy2).shortValue());
  275. }
  276. @Override
  277. public int getDx2() {
  278. return _escherClientAnchor.getDx2();
  279. }
  280. @Override
  281. public void setDx2(int dx2) {
  282. _escherClientAnchor.setDx2(Integer.valueOf(dx2).shortValue());
  283. }
  284. }