aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFTableCell.java
blob: 0956e6f31d1f8bbeaa9925ed1d7fe5069c1c67cf (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* ====================================================================
   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.hslf.usermodel;

import java.awt.Color;
import java.awt.geom.Rectangle2D;

import org.apache.poi.ddf.AbstractEscherOptRecord;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherPropertyTypes;
import org.apache.poi.sl.draw.DrawPaint;
import org.apache.poi.sl.usermodel.PaintStyle;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.StrokeStyle;
import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
import org.apache.poi.sl.usermodel.TableCell;

/**
 * Represents a cell in a ppt table
 */
public final class HSLFTableCell extends HSLFTextBox implements TableCell<HSLFShape,HSLFTextParagraph> {
    protected static final int DEFAULT_WIDTH = 100;
    protected static final int DEFAULT_HEIGHT = 40;

    /* package */ HSLFLine borderLeft;
    /* package */ HSLFLine borderRight;
    /* package */ HSLFLine borderTop;
    /* package */ HSLFLine borderBottom;

    /**
     * The number of columns to be spanned/merged
     */
    private int gridSpan = 1;

    /**
     * The number of columns to be spanned/merged
     */
    private int rowSpan = 1;

    /**
     * Create a TableCell object and initialize it from the supplied Record container.
     *
     * @param escherRecord       EscherSpContainer which holds information about this shape
     * @param parent    the parent of the shape
     */
   protected HSLFTableCell(EscherContainerRecord escherRecord, HSLFTable parent){
        super(escherRecord, parent);
    }

    /**
     * Create a new TableCell. This constructor is used when a new shape is created.
     *
     * @param parent    the parent of this Shape. For example, if this text box is a cell
     * in a table then the parent is Table.
     */
    public HSLFTableCell(HSLFTable parent){
        super(parent);

        setShapeType(ShapeType.RECT);
        //_txtrun.setRunType(TextHeaderAtom.HALF_BODY_TYPE);
        //_txtrun.getRichTextRuns()[0].setFlag(false, 0, false);
    }

    @Override
    protected EscherContainerRecord createSpContainer(boolean isChild){
        EscherContainerRecord ecr = super.createSpContainer(isChild);
        AbstractEscherOptRecord opt = getEscherOptRecord();
        setEscherProperty(opt, EscherPropertyTypes.TEXT__TEXTID, 0);
        setEscherProperty(opt, EscherPropertyTypes.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x20000);
        setEscherProperty(opt, EscherPropertyTypes.FILL__NOFILLHITTEST, 0x150001);
        setEscherProperty(opt, EscherPropertyTypes.SHADOWSTYLE__SHADOWOBSURED, 0x20000);
        setEscherProperty(opt, EscherPropertyTypes.PROTECTION__LOCKAGAINSTGROUPING, 0x40000);

        return ecr;
    }

    private void anchorBorder(BorderEdge edge, final HSLFLine line) {
        if (line == null) {
            return;
        }
        Rectangle2D cellAnchor = getAnchor();
        double x,y,w,h;
        switch(edge){
            case top:
                x = cellAnchor.getX();
                y = cellAnchor.getY();
                w = cellAnchor.getWidth();
                h = 0;
                break;
            case right:
                x = cellAnchor.getX() + cellAnchor.getWidth();
                y = cellAnchor.getY();
                w = 0;
                h = cellAnchor.getHeight();
                break;
            case bottom:
                x = cellAnchor.getX();
                y = cellAnchor.getY() + cellAnchor.getHeight();
                w = cellAnchor.getWidth();
                h = 0;
                break;
            case left:
                x = cellAnchor.getX();
                y = cellAnchor.getY();
                w = 0;
                h = cellAnchor.getHeight();
                break;
            default:
                throw new IllegalArgumentException();
        }
        line.setAnchor(new Rectangle2D.Double(x,y,w,h));
    }

    @Override
    public void setAnchor(Rectangle2D anchor){
        super.setAnchor(anchor);

        anchorBorder(BorderEdge.top, borderTop);
        anchorBorder(BorderEdge.right, borderRight);
        anchorBorder(BorderEdge.bottom, borderBottom);
        anchorBorder(BorderEdge.left, borderLeft);
    }

    @Override
    public StrokeStyle getBorderStyle(final BorderEdge edge) {
        final Double width = getBorderWidth(edge);
        return (width == null) ? null : new StrokeStyle() {
            @Override
            public PaintStyle getPaint() {
                return DrawPaint.createSolidPaint(getBorderColor(edge));
            }

            @Override
            public LineCap getLineCap() {
                return null;
            }

            @Override
            public LineDash getLineDash() {
                return getBorderDash(edge);
            }

            @Override
            public LineCompound getLineCompound() {
                return getBorderCompound(edge);
            }

            @Override
            public double getLineWidth() {
                return width;
            }
        };
    }

    @Override
    public void setBorderStyle(BorderEdge edge, StrokeStyle style) {
        if (style == null) {
            throw new IllegalArgumentException("StrokeStyle needs to be specified.");
        }

        // setting the line cap is not implemented, as the border lines aren't connected

        LineCompound compound = style.getLineCompound();
        if (compound != null) {
            setBorderCompound(edge, compound);
        }

        LineDash dash = style.getLineDash();
        if (dash != null) {
            setBorderDash(edge, dash);
        }

        double width = style.getLineWidth();
        setBorderWidth(edge, width);
    }


    public Double getBorderWidth(BorderEdge edge) {
        HSLFLine l;
        switch (edge) {
            case bottom: l = borderBottom; break;
            case top: l = borderTop; break;
            case right: l = borderRight; break;
            case left: l = borderLeft; break;
            default: throw new IllegalArgumentException();
        }
        return (l == null) ? null : l.getLineWidth();
    }

    @Override
    public void setBorderWidth(BorderEdge edge, double width) {
        HSLFLine l = addLine(edge);
        l.setLineWidth(width);
    }

    public Color getBorderColor(BorderEdge edge) {
        HSLFLine l;
        switch (edge) {
            case bottom: l = borderBottom; break;
            case top: l = borderTop; break;
            case right: l = borderRight; break;
            case left: l = borderLeft; break;
            default: throw new IllegalArgumentException();
        }
        return (l == null) ? null : l.getLineColor();
    }

    @Override
    public void setBorderColor(BorderEdge edge, Color color) {
        if (edge == null || color == null) {
            throw new IllegalArgumentException("BorderEdge and/or Color need to be specified.");
        }

        HSLFLine l = addLine(edge);
        l.setLineColor(color);
    }

    public LineDash getBorderDash(BorderEdge edge) {
        HSLFLine l;
        switch (edge) {
            case bottom: l = borderBottom; break;
            case top: l = borderTop; break;
            case right: l = borderRight; break;
            case left: l = borderLeft; break;
            default: throw new IllegalArgumentException();
        }
        return (l == null) ? null : l.getLineDash();
    }

    @Override
    public void setBorderDash(BorderEdge edge, LineDash dash) {
        if (edge == null || dash == null) {
            throw new IllegalArgumentException("BorderEdge and/or LineDash need to be specified.");
        }

        HSLFLine l = addLine(edge);
        l.setLineDash(dash);
    }

    public LineCompound getBorderCompound(BorderEdge edge) {
        HSLFLine l;
        switch (edge) {
            case bottom: l = borderBottom; break;
            case top: l = borderTop; break;
            case right: l = borderRight; break;
            case left: l = borderLeft; break;
            default: throw new IllegalArgumentException();
        }
        return (l == null) ? null : l.getLineCompound();
    }

    @Override
    public void setBorderCompound(BorderEdge edge, LineCompound compound) {
        if (edge == null || compound == null) {
            throw new IllegalArgumentException("BorderEdge and/or LineCompound need to be specified.");
        }

        HSLFLine l = addLine(edge);
        l.setLineCompound(compound);
    }


    protected HSLFLine addLine(BorderEdge edge) {
        switch (edge) {
            case bottom: {
                if (borderBottom == null) {
                    borderBottom = createBorder(edge);
                    HSLFTableCell c = getSiblingCell(1,0);
                    if (c != null) {
                        assert(c.borderTop == null);
                        c.borderTop = borderBottom;
                    }
                }
                return borderBottom;
            }
            case top: {
                if (borderTop == null) {
                    borderTop = createBorder(edge);
                    HSLFTableCell c = getSiblingCell(-1,0);
                    if (c != null) {
                        assert(c.borderBottom == null);
                        c.borderBottom = borderTop;
                    }
                }
                return borderTop;
            }
            case right: {
                if (borderRight == null) {
                    borderRight = createBorder(edge);
                    HSLFTableCell c = getSiblingCell(0,1);
                    if (c != null) {
                        assert(c.borderLeft == null);
                        c.borderLeft = borderRight;
                    }
                }
                return borderRight;
            }
            case left: {
                if (borderLeft == null) {
                    borderLeft = createBorder(edge);
                    HSLFTableCell c = getSiblingCell(0,-1);
                    if (c != null) {
                        assert(c.borderRight == null);
                        c.borderRight = borderLeft;
                    }
                }
                return borderLeft;
            }
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public void removeBorder(BorderEdge edge) {
        switch (edge) {
            case bottom: {
                if (borderBottom == null) break;
                getParent().removeShape(borderBottom);
                borderBottom = null;
                HSLFTableCell c = getSiblingCell(1,0);
                if (c != null) {
                    c.borderTop = null;
                }
                break;
            }
            case top: {
                if (borderTop == null) break;
                getParent().removeShape(borderTop);
                borderTop = null;
                HSLFTableCell c = getSiblingCell(-1,0);
                if (c != null) {
                    c.borderBottom = null;
                }
                break;
            }
            case right: {
                if (borderRight == null) break;
                getParent().removeShape(borderRight);
                borderRight = null;
                HSLFTableCell c = getSiblingCell(0,1);
                if (c != null) {
                    c.borderLeft = null;
                }
                break;
            }
            case left: {
                if (borderLeft == null) break;
                getParent().removeShape(borderLeft);
                borderLeft = null;
                HSLFTableCell c = getSiblingCell(0,-1);
                if (c != null) {
                    c.borderRight = null;
                }
                break;
            }
            default:
                throw new IllegalArgumentException();
        }
    }

    protected HSLFTableCell getSiblingCell(int row, int col) {
        return getParent().getRelativeCell(this, row, col);
    }

    /**
     * Create a border to format this table
     *
     * @return the created border
     */
    private HSLFLine createBorder(BorderEdge edge) {
        HSLFTable table = getParent();
        HSLFLine line = new HSLFLine(table);
        table.addShape(line);

        AbstractEscherOptRecord opt = getEscherOptRecord();
        setEscherProperty(opt, EscherPropertyTypes.GEOMETRY__SHAPEPATH, -1);
        setEscherProperty(opt, EscherPropertyTypes.GEOMETRY__FILLOK, -1);
        setEscherProperty(opt, EscherPropertyTypes.SHADOWSTYLE__SHADOWOBSURED, 0x20000);
        setEscherProperty(opt, EscherPropertyTypes.THREED__LIGHTFACE, 0x80000);

        anchorBorder(edge, line);

        return line;
    }

    protected void applyLineProperties(BorderEdge edge, HSLFLine other) {
        HSLFLine line = addLine(edge);
        line.setLineWidth(other.getLineWidth());
        line.setLineColor(other.getLineColor());
        // line.setLineCompound(other.getLineCompound());
        // line.setLineDashing(other.getLineDashing());
    }

    @Override
    public HSLFTable getParent() {
        return (HSLFTable)super.getParent();
    }

    /**
     * Set the gridSpan (aka col-span)
     *
     * @param gridSpan the number of columns to be spanned/merged
     *
     * @since POI 3.15-beta2
     */
    protected void setGridSpan(int gridSpan) {
        this.gridSpan = gridSpan;
    }

    /**
     * Set the rowSpan
     *
     * @param rowSpan the number of rows to be spanned/merged
     *
     * @since POI 3.15-beta2
     */
    protected void setRowSpan(int rowSpan) {
        this.rowSpan = rowSpan;
    }

    @Override
    public int getGridSpan() {
        return gridSpan;
    }

    @Override
    public int getRowSpan() {
        return rowSpan;
    }

    @Override
    public boolean isMerged() {
        // if a hslf cell is merged, it won't appear in the cell matrix, i.e. it doesn't exist
        // therefore this is always false 
        return false;
    }
}