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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.ooxml.util.POIXMLUnits;
  17. import org.apache.poi.ss.usermodel.ClientAnchor;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.Units;
  20. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  22. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  23. /**
  24. * A client anchor is attached to an excel worksheet. It anchors against:
  25. * <ol>
  26. * <li>A fixed position and fixed size
  27. * <li>A position relative to a cell (top-left) and a fixed size
  28. * <li>A position relative to a cell (top-left) and sized relative to another cell (bottom right)
  29. * </ol>
  30. *
  31. * which method is used is determined by the {@link AnchorType}.
  32. */
  33. public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor {
  34. /**
  35. * placeholder for zeros when needed for dynamic position calculations
  36. */
  37. private static final CTMarker EMPTY_MARKER = CTMarker.Factory.newInstance();
  38. private AnchorType anchorType;
  39. /**
  40. * Starting anchor point (top-left cell + relative offset)
  41. * if left null recalculate as needed from point
  42. */
  43. private CTMarker cell1;
  44. /**
  45. * Ending anchor point (bottom-right cell + relative offset)
  46. * if left null, re-calculate as needed from size and cell1
  47. */
  48. private CTMarker cell2;
  49. /**
  50. * if present, fixed size of the object to use instead of cell2, which is inferred instead
  51. */
  52. private CTPositiveSize2D size;
  53. /**
  54. * if present, fixed top-left position to use instead of cell1, which is inferred instead
  55. */
  56. private CTPoint2D position;
  57. /**
  58. * sheet to base dynamic calculations on, if needed. Required if size and/or position or set.
  59. * Not needed if cell1/2 are set explicitly (dynamic sizing and position relative to cells).
  60. */
  61. private XSSFSheet sheet;
  62. /**
  63. * Creates a new client anchor and defaults all the anchor positions to 0.
  64. * Sets the type to {@link AnchorType#MOVE_AND_RESIZE} relative to cell range A1:A1.
  65. */
  66. public XSSFClientAnchor() {
  67. this(0,0,0,0,0,0,0,0);
  68. }
  69. /**
  70. * Creates a new client anchor and sets the top-left and bottom-right
  71. * coordinates of the anchor by cell references and offsets.
  72. * Sets the type to {@link AnchorType#MOVE_AND_RESIZE}.
  73. *
  74. * @param dx1 the x coordinate within the first cell.
  75. * @param dy1 the y coordinate within the first cell.
  76. * @param dx2 the x coordinate within the second cell.
  77. * @param dy2 the y coordinate within the second cell.
  78. * @param col1 the column (0 based) of the first cell.
  79. * @param row1 the row (0 based) of the first cell.
  80. * @param col2 the column (0 based) of the second cell.
  81. * @param row2 the row (0 based) of the second cell.
  82. */
  83. public XSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) {
  84. anchorType = AnchorType.MOVE_AND_RESIZE;
  85. cell1 = CTMarker.Factory.newInstance();
  86. cell1.setCol(col1);
  87. cell1.setColOff(dx1);
  88. cell1.setRow(row1);
  89. cell1.setRowOff(dy1);
  90. cell2 = CTMarker.Factory.newInstance();
  91. cell2.setCol(col2);
  92. cell2.setColOff(dx2);
  93. cell2.setRow(row2);
  94. cell2.setRowOff(dy2);
  95. }
  96. /**
  97. * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
  98. * Sets the type to {@link AnchorType#MOVE_AND_RESIZE}.
  99. * @param cell1 starting anchor point
  100. * @param cell2 ending anchor point
  101. */
  102. protected XSSFClientAnchor(CTMarker cell1, CTMarker cell2) {
  103. anchorType = AnchorType.MOVE_AND_RESIZE;
  104. this.cell1 = cell1;
  105. this.cell2 = cell2;
  106. }
  107. /**
  108. * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
  109. * Sets the type to {@link AnchorType#MOVE_DONT_RESIZE}.
  110. *
  111. * @param sheet needed to calculate ending point based on column/row sizes
  112. * @param cell1 starting anchor point
  113. * @param size object size, to calculate ending anchor point
  114. */
  115. protected XSSFClientAnchor(XSSFSheet sheet, CTMarker cell1, CTPositiveSize2D size) {
  116. anchorType = AnchorType.MOVE_DONT_RESIZE;
  117. this.sheet = sheet;
  118. this.size = size;
  119. this.cell1 = cell1;
  120. // this.cell2 = calcCell(sheet, cell1, size.getCx(), size.getCy());
  121. }
  122. /**
  123. * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
  124. * Sets the type to {@link AnchorType#DONT_MOVE_AND_RESIZE}.
  125. *
  126. * @param sheet needed to calculate starting and ending points based on column/row sizes
  127. * @param position starting absolute position
  128. * @param size object size, to calculate ending position
  129. */
  130. protected XSSFClientAnchor(XSSFSheet sheet, CTPoint2D position, CTPositiveSize2D size) {
  131. anchorType = AnchorType.DONT_MOVE_AND_RESIZE;
  132. this.sheet = sheet;
  133. this.position = position;
  134. this.size = size;
  135. // zeros for row/col/offsets
  136. // this.cell1 = calcCell(sheet, EMPTY_MARKER, position.getCx(), position.getCy());
  137. // this.cell2 = calcCell(sheet, cell1, size.getCx(), size.getCy());
  138. }
  139. private CTMarker calcCell(CTMarker cell, long w, long h) {
  140. CTMarker c2 = CTMarker.Factory.newInstance();
  141. int r = cell.getRow();
  142. int c = cell.getCol();
  143. int cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
  144. // start with width - offset, then keep adding column widths until the next one puts us over w
  145. long wPos = cw - POIXMLUnits.parseLength(cell.xgetColOff());
  146. while (wPos < w) {
  147. c++;
  148. cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
  149. wPos += cw;
  150. }
  151. // now wPos >= w, so end column = c, now figure offset
  152. c2.setCol(c);
  153. c2.setColOff(cw - (wPos - w));
  154. int rh = Units.toEMU(getRowHeight(sheet, r));
  155. // start with height - offset, then keep adding row heights until the next one puts us over h
  156. long hPos = rh - POIXMLUnits.parseLength(cell.xgetRowOff());
  157. while (hPos < h) {
  158. r++;
  159. rh = Units.toEMU(getRowHeight(sheet, r));
  160. hPos += rh;
  161. }
  162. // now hPos >= h, so end row = r, now figure offset
  163. c2.setRow(r);
  164. c2.setRowOff(rh - (hPos - h));
  165. return c2;
  166. }
  167. /**
  168. * @return height in twips (1/20th of point) for row or default
  169. */
  170. private static float getRowHeight(XSSFSheet sheet, int row) {
  171. XSSFRow r = sheet.getRow(row);
  172. return r == null ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints();
  173. }
  174. private CTMarker getCell1() {
  175. return cell1 != null ? cell1 : calcCell(EMPTY_MARKER, POIXMLUnits.parseLength(position.xgetX()), POIXMLUnits.parseLength(position.xgetY()));
  176. }
  177. private CTMarker getCell2() {
  178. return cell2 != null ? cell2 : calcCell(getCell1(), size.getCx(), size.getCy());
  179. }
  180. @Override
  181. public short getCol1() {
  182. return (short)getCell1().getCol();
  183. }
  184. /**
  185. * @throws NullPointerException if cell1 is null (fixed position)
  186. */
  187. @Override
  188. public void setCol1(int col1) {
  189. cell1.setCol(col1);
  190. }
  191. @Override
  192. public short getCol2() {
  193. return (short) getCell2().getCol();
  194. }
  195. /**
  196. * @throws NullPointerException if cell2 is null (fixed size)
  197. */
  198. @Override
  199. public void setCol2(int col2) {
  200. cell2.setCol(col2);
  201. }
  202. @Override
  203. public int getRow1() {
  204. return getCell1().getRow();
  205. }
  206. /**
  207. * @throws NullPointerException if cell1 is null (fixed position)
  208. */
  209. @Override
  210. public void setRow1(int row1) {
  211. cell1.setRow(row1);
  212. }
  213. @Override
  214. public int getRow2() {
  215. return getCell2().getRow();
  216. }
  217. /**
  218. * @throws NullPointerException if cell2 is null (fixed size)
  219. */
  220. @Override
  221. public void setRow2(int row2) {
  222. cell2.setRow(row2);
  223. }
  224. @Override
  225. public int getDx1() {
  226. return Math.toIntExact(POIXMLUnits.parseLength(getCell1().xgetColOff()));
  227. }
  228. /**
  229. * @throws NullPointerException if cell1 is null (fixed position)
  230. * @see org.apache.poi.ss.usermodel.ChildAnchor#setDx1(int)
  231. */
  232. @Override
  233. public void setDx1(int dx1) {
  234. cell1.setColOff(dx1);
  235. }
  236. @Override
  237. public int getDy1() {
  238. return Math.toIntExact(POIXMLUnits.parseLength(getCell1().xgetRowOff()));
  239. }
  240. /**
  241. * @throws NullPointerException if cell1 is null (fixed position)
  242. * @see org.apache.poi.ss.usermodel.ChildAnchor#setDy1(int)
  243. */
  244. @Override
  245. public void setDy1(int dy1) {
  246. cell1.setRowOff(dy1);
  247. }
  248. @Override
  249. public int getDy2() {
  250. return Math.toIntExact(POIXMLUnits.parseLength(getCell2().xgetRowOff()));
  251. }
  252. /**
  253. * @throws NullPointerException if cell2 is null (fixed size)
  254. * @see org.apache.poi.ss.usermodel.ChildAnchor#setDy2(int)
  255. */
  256. @Override
  257. public void setDy2(int dy2) {
  258. cell2.setRowOff(dy2);
  259. }
  260. @Override
  261. public int getDx2() {
  262. return Math.toIntExact(POIXMLUnits.parseLength(getCell2().xgetColOff()));
  263. }
  264. /**
  265. * @throws NullPointerException if cell2 is null (fixed size)
  266. * @see org.apache.poi.ss.usermodel.ChildAnchor#setDx2(int)
  267. */
  268. @Override
  269. public void setDx2(int dx2) {
  270. cell2.setColOff(dx2);
  271. }
  272. @Override
  273. public boolean equals(Object o) {
  274. if (!(o instanceof XSSFClientAnchor)) return false;
  275. XSSFClientAnchor anchor = (XSSFClientAnchor) o;
  276. return getDx1() == anchor.getDx1() &&
  277. getDx2() == anchor.getDx2() &&
  278. getDy1() == anchor.getDy1() &&
  279. getDy2() == anchor.getDy2() &&
  280. getCol1() == anchor.getCol1() &&
  281. getCol2() == anchor.getCol2() &&
  282. getRow1() == anchor.getRow1() &&
  283. getRow2() == anchor.getRow2() ;
  284. }
  285. @Override
  286. public int hashCode() {
  287. assert false : "hashCode not designed";
  288. return 42; // any arbitrary constant will do
  289. }
  290. @Override
  291. public String toString(){
  292. return "from : " + getCell1() + "; to: " + getCell2();
  293. }
  294. /**
  295. * Return starting anchor point
  296. *
  297. * @return starting anchor point
  298. */
  299. @Internal
  300. public CTMarker getFrom(){
  301. return getCell1();
  302. }
  303. protected void setFrom(CTMarker from){
  304. cell1 = from;
  305. }
  306. /**
  307. * Return ending anchor point
  308. *
  309. * @return ending anchor point
  310. */
  311. @Internal
  312. public CTMarker getTo(){
  313. return getCell2();
  314. }
  315. protected void setTo(CTMarker to){
  316. cell2 = to;
  317. }
  318. /**
  319. * @return absolute top-left position, or null if position is determined from the "from" cell
  320. * @since POI 3.17 beta 1
  321. */
  322. public CTPoint2D getPosition() {
  323. return position;
  324. }
  325. /**
  326. * Sets the top-left absolute position of the object. To use this, "from" must be set to null.
  327. * @since POI 3.17 beta 1
  328. */
  329. public void setPosition(CTPoint2D position) {
  330. this.position = position;
  331. }
  332. /**
  333. *
  334. * @return size or null, if size is determined from the to and from cells
  335. * @since POI 3.17 beta 1
  336. */
  337. public CTPositiveSize2D getSize() {
  338. return size;
  339. }
  340. /**
  341. * Sets the size of the object. To use this, "to" must be set to null.
  342. * @since POI 3.17 beta 1
  343. */
  344. public void setSize(CTPositiveSize2D size) {
  345. this.size = size;
  346. }
  347. /**
  348. * Sets the anchor type
  349. * @param anchorType the anchor type to set
  350. * @since POI 3.14
  351. */
  352. @Override
  353. public void setAnchorType( AnchorType anchorType )
  354. {
  355. this.anchorType = anchorType;
  356. }
  357. /**
  358. * Gets the anchor type
  359. * Changed from returning an int to an enum in POI 3.14 beta 1.
  360. * @return the anchor type
  361. */
  362. @Override
  363. public AnchorType getAnchorType()
  364. {
  365. return anchorType;
  366. }
  367. public boolean isSet(){
  368. CTMarker c1 = getCell1();
  369. CTMarker c2 = getCell2();
  370. return !(c1.getCol() == 0 && c2.getCol() == 0 &&
  371. c1.getRow() == 0 && c2.getRow() == 0);
  372. }
  373. }