aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFClientAnchor.java
blob: 02299a2b318f5639c88bb30f837fe017245259b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.xssf.usermodel;

import org.apache.poi.ooxml.util.POIXMLUnits;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;

/**
 * A client anchor is attached to an excel worksheet.  It anchors against:
 * <ol>
 * <li>A fixed position and fixed size
 * <li>A position relative to a cell (top-left) and a fixed size
 * <li>A position relative to a cell (top-left) and sized relative to another cell (bottom right)
 * </ol>
 *
 * which method is used is determined by the {@link AnchorType}.
 */
public class XSSFClientAnchor extends XSSFAnchor implements ClientAnchor {

    /**
     * placeholder for zeros when needed for dynamic position calculations
     */
    private static final CTMarker EMPTY_MARKER = CTMarker.Factory.newInstance();

    private AnchorType anchorType;

    /**
     * Starting anchor point (top-left cell + relative offset)
     * if left null recalculate as needed from point
     */
    private CTMarker cell1;

    /**
     * Ending anchor point (bottom-right cell + relative offset)
     * if left null, re-calculate as needed from size and cell1
     */
    private CTMarker cell2;

    /**
     * if present, fixed size of the object to use instead of cell2, which is inferred instead
     */
    private CTPositiveSize2D size;

    /**
     * if present, fixed top-left position to use instead of cell1, which is inferred instead
     */
    private CTPoint2D position;

    /**
     * sheet to base dynamic calculations on, if needed.  Required if size and/or position or set.
     * Not needed if cell1/2 are set explicitly (dynamic sizing and position relative to cells).
     */
    private XSSFSheet sheet;

    /**
     * Creates a new client anchor and defaults all the anchor positions to 0.
     * Sets the type to {@link AnchorType#MOVE_AND_RESIZE} relative to cell range A1:A1.
     */
    public XSSFClientAnchor() {
        this(0,0,0,0,0,0,0,0);
    }

    /**
     * Creates a new client anchor and sets the top-left and bottom-right
     * coordinates of the anchor by cell references and offsets.
     * Sets the type to {@link AnchorType#MOVE_AND_RESIZE}.
     *
     * @param dx1  the x coordinate within the first cell.
     * @param dy1  the y coordinate within the first cell.
     * @param dx2  the x coordinate within the second cell.
     * @param dy2  the y coordinate within the second cell.
     * @param col1 the column (0 based) of the first cell.
     * @param row1 the row (0 based) of the first cell.
     * @param col2 the column (0 based) of the second cell.
     * @param row2 the row (0 based) of the second cell.
     */
    public XSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) {
        anchorType = AnchorType.MOVE_AND_RESIZE;
        cell1 = CTMarker.Factory.newInstance();
        cell1.setCol(col1);
        cell1.setColOff(dx1);
        cell1.setRow(row1);
        cell1.setRowOff(dy1);
        cell2 = CTMarker.Factory.newInstance();
        cell2.setCol(col2);
        cell2.setColOff(dx2);
        cell2.setRow(row2);
        cell2.setRowOff(dy2);
    }

    /**
     * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
     * Sets the type to {@link AnchorType#MOVE_AND_RESIZE}.
     * @param cell1 starting anchor point
     * @param cell2 ending anchor point
     */
    protected XSSFClientAnchor(CTMarker cell1, CTMarker cell2) {
        anchorType = AnchorType.MOVE_AND_RESIZE;
        this.cell1 = cell1;
        this.cell2 = cell2;
    }

    /**
     * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
     * Sets the type to {@link AnchorType#MOVE_DONT_RESIZE}.
     *
     * @param sheet needed to calculate ending point based on column/row sizes
     * @param cell1 starting anchor point
     * @param size object size, to calculate ending anchor point
     */
    protected XSSFClientAnchor(XSSFSheet sheet, CTMarker cell1, CTPositiveSize2D size) {
        anchorType = AnchorType.MOVE_DONT_RESIZE;
        this.sheet = sheet;
        this.size = size;
        this.cell1 = cell1;
//        this.cell2 = calcCell(sheet, cell1, size.getCx(), size.getCy());
    }

    /**
     * Create XSSFClientAnchor from existing xml beans, sized and positioned relative to a pair of cells.
     * Sets the type to {@link AnchorType#DONT_MOVE_AND_RESIZE}.
     *
     * @param sheet needed to calculate starting and ending points based on column/row sizes
     * @param position starting absolute position
     * @param size object size, to calculate ending position
     */
    protected XSSFClientAnchor(XSSFSheet sheet, CTPoint2D position, CTPositiveSize2D size) {
        anchorType = AnchorType.DONT_MOVE_AND_RESIZE;
        this.sheet = sheet;
        this.position = position;
        this.size = size;
        // zeros for row/col/offsets
//        this.cell1 = calcCell(sheet, EMPTY_MARKER, position.getCx(), position.getCy());
//        this.cell2 = calcCell(sheet, cell1, size.getCx(), size.getCy());
    }

    private CTMarker calcCell(CTMarker cell, long w, long h) {
        CTMarker c2 = CTMarker.Factory.newInstance();

        int r = cell.getRow();
        int c = cell.getCol();

        int cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));

        // start with width - offset, then keep adding column widths until the next one puts us over w
        long wPos = cw - POIXMLUnits.parseLength(cell.xgetColOff());

        while (wPos < w) {
            c++;
            cw = Units.columnWidthToEMU(sheet.getColumnWidth(c));
            wPos += cw;
        }
        // now wPos >= w, so end column = c, now figure offset
        c2.setCol(c);
        c2.setColOff(cw - (wPos - w));

        int rh = Units.toEMU(getRowHeight(sheet, r));
        // start with height - offset, then keep adding row heights until the next one puts us over h
        long hPos = rh - POIXMLUnits.parseLength(cell.xgetRowOff());

        while (hPos < h) {
            r++;
            rh = Units.toEMU(getRowHeight(sheet, r));
            hPos += rh;
        }
        // now hPos >= h, so end row = r, now figure offset
        c2.setRow(r);
        c2.setRowOff(rh - (hPos - h));

        return c2;
    }

    /**
     * @return height in twips (1/20th of point) for row or default
     */
    private static float getRowHeight(XSSFSheet sheet, int row) {
        XSSFRow r = sheet.getRow(row);
        return r == null ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints();
    }

    private CTMarker getCell1() {
        return cell1 != null ? cell1 : calcCell(EMPTY_MARKER, POIXMLUnits.parseLength(position.xgetX()), POIXMLUnits.parseLength(position.xgetY()));
    }

    private CTMarker getCell2() {
        return cell2 != null ? cell2 : calcCell(getCell1(), size.getCx(), size.getCy());
    }

    @Override
    public short getCol1() {
        return (short)getCell1().getCol();
    }

    /**
     * @throws NullPointerException if cell1 is null (fixed position)
     */
    @Override
    public void setCol1(int col1) {
        cell1.setCol(col1);
    }

    @Override
    public short getCol2() {
        return (short) getCell2().getCol();
    }

    /**
     * @throws NullPointerException if cell2 is null (fixed size)
     */
    @Override
    public void setCol2(int col2) {
        cell2.setCol(col2);
    }

    @Override
    public int getRow1() {
        return getCell1().getRow();
    }

    /**
     * @throws NullPointerException if cell1 is null (fixed position)
     */
    @Override
    public void setRow1(int row1) {
        cell1.setRow(row1);
    }

    @Override
    public int getRow2() {
        return getCell2().getRow();
    }

    /**
     * @throws NullPointerException if cell2 is null (fixed size)
     */
    @Override
    public void setRow2(int row2) {
        cell2.setRow(row2);
    }

    @Override
    public int getDx1() {
        return Math.toIntExact(POIXMLUnits.parseLength(getCell1().xgetColOff()));
    }

    /**
     * @throws NullPointerException if cell1 is null (fixed position)
     * @see org.apache.poi.ss.usermodel.ChildAnchor#setDx1(int)
     */
    @Override
    public void setDx1(int dx1) {
        cell1.setColOff(dx1);
    }

    @Override
    public int getDy1() {
        return Math.toIntExact(POIXMLUnits.parseLength(getCell1().xgetRowOff()));
    }

    /**
     * @throws NullPointerException if cell1 is null (fixed position)
     * @see org.apache.poi.ss.usermodel.ChildAnchor#setDy1(int)
     */
    @Override
    public void setDy1(int dy1) {
        cell1.setRowOff(dy1);
    }

    @Override
    public int getDy2() {
        return Math.toIntExact(POIXMLUnits.parseLength(getCell2().xgetRowOff()));
    }

    /**
     * @throws NullPointerException if cell2 is null (fixed size)
     * @see org.apache.poi.ss.usermodel.ChildAnchor#setDy2(int)
     */
    @Override
    public void setDy2(int dy2) {
        cell2.setRowOff(dy2);
    }

    @Override
    public int getDx2() {
        return Math.toIntExact(POIXMLUnits.parseLength(getCell2().xgetColOff()));
    }

    /**
     * @throws NullPointerException if cell2 is null (fixed size)
     * @see org.apache.poi.ss.usermodel.ChildAnchor#setDx2(int)
     */
    @Override
    public void setDx2(int dx2) {
        cell2.setColOff(dx2);
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof XSSFClientAnchor)) return false;

        XSSFClientAnchor anchor = (XSSFClientAnchor) o;
        return  getDx1() == anchor.getDx1() &&
                getDx2() == anchor.getDx2() &&
                getDy1() == anchor.getDy1() &&
                getDy2() == anchor.getDy2() &&
                getCol1() == anchor.getCol1() &&
                getCol2() == anchor.getCol2() &&
                getRow1() == anchor.getRow1() &&
                getRow2() == anchor.getRow2() ;

    }

    @Override
    public int hashCode() {
        assert false : "hashCode not designed";
        return 42; // any arbitrary constant will do
    }

    @Override
    public String toString(){
        return "from : " + getCell1() + "; to: " + getCell2();
    }

    /**
     * Return starting anchor point
     *
     * @return starting anchor point
     */
    @Internal
    public CTMarker getFrom(){
        return getCell1();
    }

    protected void setFrom(CTMarker from){
        cell1 = from;
    }

    /**
     * Return ending anchor point
     *
     * @return ending anchor point
     */
    @Internal
    public CTMarker getTo(){
        return getCell2();
    }

    protected void setTo(CTMarker to){
        cell2 = to;
    }

    /**
     * @return absolute top-left position, or null if position is determined from the "from" cell
     * @since POI 3.17 beta 1
     */
    public CTPoint2D getPosition() {
        return position;
    }

    /**
     * Sets the top-left absolute position of the object.  To use this, "from" must be set to null.
     * @since POI 3.17 beta 1
     */
    public void setPosition(CTPoint2D position) {
        this.position = position;
    }

    /**
     *
     * @return size or null, if size is determined from the to and from cells
     * @since POI 3.17 beta 1
     */
    public CTPositiveSize2D getSize() {
        return size;
    }

    /**
     * Sets the size of the object.  To use this, "to" must be set to null.
     * @since POI 3.17 beta 1
     */
    public void setSize(CTPositiveSize2D size) {
        this.size = size;
    }

    /**
     * Sets the anchor type
     * @param anchorType the anchor type to set
     * @since POI 3.14
     */
    @Override
    public void setAnchorType( AnchorType anchorType )
    {
        this.anchorType = anchorType;
    }

    /**
     * Gets the anchor type
     * Changed from returning an int to an enum in POI 3.14 beta 1.
     * @return the anchor type
     */
    @Override
    public AnchorType getAnchorType()
    {
        return anchorType;
    }

    public boolean isSet(){
        CTMarker c1 = getCell1();
        CTMarker c2 = getCell2();
        return !(c1.getCol() == 0 && c2.getCol() == 0 &&
                 c1.getRow() == 0 && c2.getRow() == 0);
    }
}