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
|
/* ====================================================================
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.sl.draw;
import static org.apache.poi.sl.draw.DrawPaint.fillPaintWorkaround;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import org.apache.poi.sl.usermodel.GroupShape;
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;
import org.apache.poi.sl.usermodel.TableCell.BorderEdge;
import org.apache.poi.util.Internal;
import org.apache.poi.sl.usermodel.TableShape;
public class DrawTableShape extends DrawShape {
/**
* Additional spacing between cells
*/
@Internal
public static final int borderSize = 2;
public DrawTableShape(TableShape<?,?> shape) {
super(shape);
}
protected Drawable getGroupShape(Graphics2D graphics) {
if (shape instanceof GroupShape) {
DrawFactory df = DrawFactory.getInstance(graphics);
return df.getDrawable((GroupShape<?,?>)shape);
}
return null;
}
public void applyTransform(Graphics2D graphics) {
Drawable d = getGroupShape(graphics);
if (d != null) {
d.applyTransform(graphics);
} else {
super.applyTransform(graphics);
}
}
public void draw(Graphics2D graphics) {
Drawable d = getGroupShape(graphics);
if (d != null) {
d.draw(graphics);
return;
}
TableShape<?,?> ts = getShape();
DrawPaint drawPaint = DrawFactory.getInstance(graphics).getPaint(ts);
final int rows = ts.getNumberOfRows();
final int cols = ts.getNumberOfColumns();
// draw background boxes
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
TableCell<?,?> tc = ts.getCell(row, col);
if (tc == null || tc.isMerged()) {
continue;
}
Paint fillPaint = drawPaint.getPaint(graphics, tc.getFillStyle().getPaint());
graphics.setPaint(fillPaint);
Rectangle2D cellAnc = tc.getAnchor();
fillPaintWorkaround(graphics, cellAnc);
for (BorderEdge edge : BorderEdge.values()) {
StrokeStyle stroke = tc.getBorderStyle(edge);
if (stroke == null) {
continue;
}
graphics.setStroke(getStroke(stroke));
Paint linePaint = drawPaint.getPaint(graphics, stroke.getPaint());
graphics.setPaint(linePaint);
double x=cellAnc.getX(), y=cellAnc.getY(), w=cellAnc.getWidth(), h=cellAnc.getHeight();
Line2D line;
switch (edge) {
default:
case bottom:
line = new Line2D.Double(x-borderSize, y+h, x+w+borderSize, y+h);
break;
case left:
line = new Line2D.Double(x, y, x, y+h+borderSize);
break;
case right:
line = new Line2D.Double(x+w, y, x+w, y+h+borderSize);
break;
case top:
line = new Line2D.Double(x-borderSize, y, x+w+borderSize, y);
break;
}
graphics.draw(line);
}
}
}
// draw text
drawContent(graphics);
}
public void drawContent(Graphics2D graphics) {
Drawable d = getGroupShape(graphics);
if (d != null) {
d.drawContent(graphics);
return;
}
TableShape<?,?> ts = getShape();
DrawFactory df = DrawFactory.getInstance(graphics);
final int rows = ts.getNumberOfRows();
final int cols = ts.getNumberOfColumns();
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
TableCell<?,?> tc = ts.getCell(row, col);
if (tc != null) {
DrawTextShape dts = df.getDrawable(tc);
dts.drawContent(graphics);
}
}
}
}
@Override
protected TableShape<?,?> getShape() {
return (TableShape<?,?>)shape;
}
/**
* Format the table and apply the specified Line to all cell boundaries,
* both outside and inside.
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setAllBorders(Object... args) {
TableShape<?,?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge edges[] = { BorderEdge.top, BorderEdge.left, null, null };
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[2] = (col == cols - 1) ? BorderEdge.right : null;
edges[3] = (row == rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
/**
* Format the outside border using the specified Line object
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setOutsideBorders(Object... args){
if (args.length == 0) return;
TableShape<?,?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge edges[] = new BorderEdge[4];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[0] = (col == 0) ? BorderEdge.left : null;
edges[1] = (col == cols - 1) ? BorderEdge.right : null;
edges[2] = (row == 0) ? BorderEdge.top : null;
edges[3] = (row == rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
/**
* Format the inside border using the specified Line object
* An empty args parameter removes the affected border.
*
* @param args a varargs array possible containing {@link Double} (width),
* {@link LineCompound}, {@link Color}, {@link LineDash}
*/
public void setInsideBorders(Object... args) {
if (args.length == 0) return;
TableShape<?,?> table = getShape();
final int rows = table.getNumberOfRows();
final int cols = table.getNumberOfColumns();
BorderEdge edges[] = new BorderEdge[2];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
edges[0] = (col > 0 && col < cols - 1) ? BorderEdge.right : null;
edges[1] = (row > 0 && row < rows - 1) ? BorderEdge.bottom : null;
setEdges(table.getCell(row, col), edges, args);
}
}
}
/**
* Apply the border attributes (args) to the given cell and edges
*
* @param cell the cell
* @param edges the border edges
* @param args the border attributes
*/
private static void setEdges(TableCell<?,?> cell, BorderEdge edges[], Object... args) {
if (cell == null) {
return;
}
for (BorderEdge be : edges) {
if (be != null) {
if (args.length == 0) {
cell.removeBorder(be);
} else {
for (Object o : args) {
if (o instanceof Double) {
cell.setBorderWidth(be, (Double)o);
} else if (o instanceof Color) {
cell.setBorderColor(be, (Color)o);
} else if (o instanceof LineDash) {
cell.setBorderDash(be, (LineDash)o);
} else if (o instanceof LineCompound) {
cell.setBorderCompound(be, (LineCompound)o);
}
}
}
}
}
}
}
|