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.

XSSFClientAnchor.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.xssf.usermodel;
  16. import org.apache.poi.ss.usermodel.ClientAnchor;
  17. import org.apache.poi.util.Internal;
  18. import org.apache.poi.util.Removal;
  19. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  20. /**
  21. * A client anchor is attached to an excel worksheet. It anchors against
  22. * top-left and bottom-right cells.
  23. *
  24. * @author Yegor Kozlov
  25. */
  26. public final class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor {
  27. private AnchorType DEFAULT_ANCHOR_TYPE = AnchorType.MOVE_AND_RESIZE;
  28. private AnchorType anchorType;
  29. /**
  30. * Starting anchor point
  31. */
  32. private CTMarker cell1;
  33. /**
  34. * Ending anchor point
  35. */
  36. private CTMarker cell2;
  37. /**
  38. * Creates a new client anchor and defaults all the anchor positions to 0.
  39. */
  40. public XSSFClientAnchor() {
  41. anchorType = DEFAULT_ANCHOR_TYPE;
  42. cell1 = CTMarker.Factory.newInstance();
  43. cell1.setCol(0);
  44. cell1.setColOff(0);
  45. cell1.setRow(0);
  46. cell1.setRowOff(0);
  47. cell2 = CTMarker.Factory.newInstance();
  48. cell2.setCol(0);
  49. cell2.setColOff(0);
  50. cell2.setRow(0);
  51. cell2.setRowOff(0);
  52. }
  53. /**
  54. * Creates a new client anchor and sets the top-left and bottom-right
  55. * coordinates of the anchor.
  56. *
  57. * @param dx1 the x coordinate within the first cell.
  58. * @param dy1 the y coordinate within the first cell.
  59. * @param dx2 the x coordinate within the second cell.
  60. * @param dy2 the y coordinate within the second cell.
  61. * @param col1 the column (0 based) of the first cell.
  62. * @param row1 the row (0 based) of the first cell.
  63. * @param col2 the column (0 based) of the second cell.
  64. * @param row2 the row (0 based) of the second cell.
  65. */
  66. public XSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) {
  67. this();
  68. cell1.setCol(col1);
  69. cell1.setColOff(dx1);
  70. cell1.setRow(row1);
  71. cell1.setRowOff(dy1);
  72. cell2.setCol(col2);
  73. cell2.setColOff(dx2);
  74. cell2.setRow(row2);
  75. cell2.setRowOff(dy2);
  76. }
  77. /**
  78. * Create XSSFClientAnchor from existing xml beans
  79. *
  80. * @param cell1 starting anchor point
  81. * @param cell2 ending anchor point
  82. */
  83. protected XSSFClientAnchor(CTMarker cell1, CTMarker cell2) {
  84. anchorType = DEFAULT_ANCHOR_TYPE;
  85. this.cell1 = cell1;
  86. this.cell2 = cell2;
  87. }
  88. public short getCol1() {
  89. return (short)cell1.getCol();
  90. }
  91. public void setCol1(int col1) {
  92. cell1.setCol(col1);
  93. }
  94. public short getCol2() {
  95. return (short)cell2.getCol();
  96. }
  97. public void setCol2(int col2) {
  98. cell2.setCol(col2);
  99. }
  100. public int getRow1() {
  101. return cell1.getRow();
  102. }
  103. public void setRow1(int row1) {
  104. cell1.setRow(row1);
  105. }
  106. public int getRow2() {
  107. return cell2.getRow();
  108. }
  109. public void setRow2(int row2) {
  110. cell2.setRow(row2);
  111. }
  112. public int getDx1() {
  113. return (int)cell1.getColOff();
  114. }
  115. public void setDx1(int dx1) {
  116. cell1.setColOff(dx1);
  117. }
  118. public int getDy1() {
  119. return (int)cell1.getRowOff();
  120. }
  121. public void setDy1(int dy1) {
  122. cell1.setRowOff(dy1);
  123. }
  124. public int getDy2() {
  125. return (int)cell2.getRowOff();
  126. }
  127. public void setDy2(int dy2) {
  128. cell2.setRowOff(dy2);
  129. }
  130. public int getDx2() {
  131. return (int)cell2.getColOff();
  132. }
  133. public void setDx2(int dx2) {
  134. cell2.setColOff(dx2);
  135. }
  136. @Override
  137. public boolean equals(Object o) {
  138. if (o == null || !(o instanceof XSSFClientAnchor)) return false;
  139. XSSFClientAnchor anchor = (XSSFClientAnchor) o;
  140. return getDx1() == anchor.getDx1() &&
  141. getDx2() == anchor.getDx2() &&
  142. getDy1() == anchor.getDy1() &&
  143. getDy2() == anchor.getDy2() &&
  144. getCol1() == anchor.getCol1() &&
  145. getCol2() == anchor.getCol2() &&
  146. getRow1() == anchor.getRow1() &&
  147. getRow2() == anchor.getRow2() ;
  148. }
  149. @Override
  150. public int hashCode() {
  151. assert false : "hashCode not designed";
  152. return 42; // any arbitrary constant will do
  153. }
  154. @Override
  155. public String toString(){
  156. return "from : " + cell1.toString() + "; to: " + cell2.toString();
  157. }
  158. /**
  159. * Return starting anchor point
  160. *
  161. * @return starting anchor point
  162. */
  163. @Internal
  164. public CTMarker getFrom(){
  165. return cell1;
  166. }
  167. protected void setFrom(CTMarker from){
  168. cell1 = from;
  169. }
  170. /**
  171. * Return ending anchor point
  172. *
  173. * @return ending anchor point
  174. */
  175. @Internal
  176. public CTMarker getTo(){
  177. return cell2;
  178. }
  179. protected void setTo(CTMarker to){
  180. cell2 = to;
  181. }
  182. /**
  183. * Sets the anchor type
  184. * @param anchorType the anchor type to set
  185. * @since POI 3.14
  186. */
  187. @Override
  188. public void setAnchorType( AnchorType anchorType )
  189. {
  190. this.anchorType = anchorType;
  191. }
  192. /**
  193. * Sets the anchor type
  194. * @param anchorType the anchor type to set
  195. * @deprecated POI 3.15. Use {@link #setAnchorType(AnchorType)} instead
  196. */
  197. @Removal(version="3.17")
  198. @Override
  199. public void setAnchorType( int anchorType )
  200. {
  201. this.anchorType = AnchorType.byId(anchorType);
  202. }
  203. /**
  204. * Gets the anchor type
  205. * Changed from returning an int to an enum in POI 3.14 beta 1.
  206. * @return the anchor type
  207. */
  208. @Override
  209. public AnchorType getAnchorType()
  210. {
  211. return anchorType;
  212. }
  213. public boolean isSet(){
  214. return !(cell1.getCol() == 0 && cell2.getCol() == 0 &&
  215. cell1.getRow() == 0 && cell2.getRow() == 0);
  216. }
  217. }