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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.ss.usermodel.ClientAnchor;
  18. /**
  19. * A client anchor is attached to an excel worksheet. It anchors against a
  20. * top-left and buttom-right cell.
  21. *
  22. * @author Glen Stampoultzis (glens at apache.org)
  23. */
  24. public final class HSSFClientAnchor extends HSSFAnchor implements ClientAnchor {
  25. short col1;
  26. int row1;
  27. short col2;
  28. int row2;
  29. int anchorType;
  30. private EscherClientAnchorRecord escherClientAnchorRecord;
  31. public HSSFClientAnchor(EscherClientAnchorRecord escherClientAnchorRecord) {
  32. this.escherClientAnchorRecord = escherClientAnchorRecord;
  33. //TODO set properties or read properties from EscherRecord ?
  34. }
  35. /**
  36. * Creates a new client anchor and defaults all the anchor positions to 0.
  37. */
  38. public HSSFClientAnchor()
  39. {
  40. }
  41. /**
  42. * Creates a new client anchor and sets the top-left and bottom-right
  43. * coordinates of the anchor.
  44. *
  45. * @param dx1 the x coordinate within the first cell.
  46. * @param dy1 the y coordinate within the first cell.
  47. * @param dx2 the x coordinate within the second cell.
  48. * @param dy2 the y coordinate within the second cell.
  49. * @param col1 the column (0 based) of the first cell.
  50. * @param row1 the row (0 based) of the first cell.
  51. * @param col2 the column (0 based) of the second cell.
  52. * @param row2 the row (0 based) of the second cell.
  53. */
  54. public HSSFClientAnchor( int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2 )
  55. {
  56. super( dx1, dy1, dx2, dy2 );
  57. checkRange(dx1, 0, 1023, "dx1");
  58. checkRange(dx2, 0, 1023, "dx2");
  59. checkRange(dy1, 0, 255, "dy1");
  60. checkRange(dy2, 0, 255, "dy2");
  61. checkRange(col1, 0, 255, "col1");
  62. checkRange(col2, 0, 255, "col2");
  63. checkRange(row1, 0, 255 * 256, "row1");
  64. checkRange(row2, 0, 255 * 256, "row2");
  65. this.col1 = col1;
  66. this.row1 = row1;
  67. this.col2 = col2;
  68. this.row2 = row2;
  69. }
  70. /**
  71. * Calculates the height of a client anchor in points.
  72. *
  73. * @param sheet the sheet the anchor will be attached to
  74. * @return the shape height.
  75. */
  76. public float getAnchorHeightInPoints(HSSFSheet sheet )
  77. {
  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. {
  85. points = ((y2 - y1) / 256.0f) * getRowHeightInPoints(sheet, row2);
  86. }
  87. else
  88. {
  89. points += ((256.0f - y1) / 256.0f) * getRowHeightInPoints(sheet, row1);
  90. for (int i = row1 + 1; i < row2; i++)
  91. {
  92. points += getRowHeightInPoints(sheet, i);
  93. }
  94. points += (y2 / 256.0f) * getRowHeightInPoints(sheet, row2);
  95. }
  96. return points;
  97. }
  98. private float getRowHeightInPoints(HSSFSheet sheet, int rowNum)
  99. {
  100. HSSFRow row = sheet.getRow(rowNum);
  101. if (row == null) {
  102. return sheet.getDefaultRowHeightInPoints();
  103. }
  104. return row.getHeightInPoints();
  105. }
  106. public short getCol1()
  107. {
  108. return col1;
  109. }
  110. public void setCol1( short col1 )
  111. {
  112. checkRange(col1, 0, 255, "col1");
  113. this.col1 = col1;
  114. }
  115. public void setCol1( int col1 ){
  116. setCol1((short)col1);
  117. }
  118. public short getCol2()
  119. {
  120. return col2;
  121. }
  122. public void setCol2( short col2 )
  123. {
  124. checkRange(col2, 0, 255, "col2");
  125. this.col2 = col2;
  126. }
  127. public void setCol2( int col2 ){
  128. setCol2((short)col2);
  129. }
  130. public int getRow1()
  131. {
  132. return row1;
  133. }
  134. public void setRow1( int row1 )
  135. {
  136. checkRange(row1, 0, 256 * 256, "row1");
  137. this.row1 = row1;
  138. }
  139. public int getRow2()
  140. {
  141. return row2;
  142. }
  143. public void setRow2( int row2 )
  144. {
  145. checkRange(row2, 0, 256 * 256, "row2");
  146. this.row2 = row2;
  147. }
  148. /**
  149. * Dets the top-left and bottom-right
  150. * coordinates of the anchor.
  151. *
  152. * @param x1 the x coordinate within the first cell.
  153. * @param y1 the y coordinate within the first cell.
  154. * @param x2 the x coordinate within the second cell.
  155. * @param y2 the y coordinate within the second cell.
  156. * @param col1 the column (0 based) of the first cell.
  157. * @param row1 the row (0 based) of the first cell.
  158. * @param col2 the column (0 based) of the second cell.
  159. * @param row2 the row (0 based) of the second cell.
  160. */
  161. public void setAnchor( short col1, int row1, int x1, int y1, short col2, int row2, int x2, int y2 )
  162. {
  163. checkRange(dx1, 0, 1023, "dx1");
  164. checkRange(dx2, 0, 1023, "dx2");
  165. checkRange(dy1, 0, 255, "dy1");
  166. checkRange(dy2, 0, 255, "dy2");
  167. checkRange(col1, 0, 255, "col1");
  168. checkRange(col2, 0, 255, "col2");
  169. checkRange(row1, 0, 255 * 256, "row1");
  170. checkRange(row2, 0, 255 * 256, "row2");
  171. this.col1 = col1;
  172. this.row1 = row1;
  173. this.dx1 = x1;
  174. this.dy1 = y1;
  175. this.col2 = col2;
  176. this.row2 = row2;
  177. this.dx2 = x2;
  178. this.dy2 = y2;
  179. }
  180. /**
  181. * @return true if the anchor goes from right to left.
  182. */
  183. public boolean isHorizontallyFlipped()
  184. {
  185. if (col1 == col2) {
  186. return dx1 > dx2;
  187. }
  188. return col1 > col2;
  189. }
  190. /**
  191. * @return true if the anchor goes from bottom to top.
  192. */
  193. public boolean isVerticallyFlipped()
  194. {
  195. if (row1 == row2) {
  196. return dy1 > dy2;
  197. }
  198. return row1 > row2;
  199. }
  200. /**
  201. * Gets the anchor type
  202. * <p>
  203. * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
  204. */
  205. public int getAnchorType()
  206. {
  207. return anchorType;
  208. }
  209. /**
  210. * Sets the anchor type
  211. * <p>
  212. * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
  213. */
  214. public void setAnchorType( int anchorType )
  215. {
  216. this.anchorType = anchorType;
  217. }
  218. private void checkRange( int value, int minRange, int maxRange, String varName )
  219. {
  220. if (value < minRange || value > maxRange)
  221. throw new IllegalArgumentException(varName + " must be between " + minRange + " and " + maxRange);
  222. }
  223. }