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
|
/* ====================================================================
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.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherSpgrRecord;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.EscherAggregate;
import java.lang.reflect.Field;
/**
* @author Evgeniy Berlog
* @date 29.06.12
*/
public class TestShapeGroup extends TestCase{
public void testSetGetCoordinates(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sh = wb.createSheet();
HSSFPatriarch patriarch = sh.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
assertEquals(group.getX1(), 0);
assertEquals(group.getY1(), 0);
assertEquals(group.getX2(), 1023);
assertEquals(group.getY2(), 255);
group.setCoordinates(1,2,3,4);
assertEquals(group.getX1(), 1);
assertEquals(group.getY1(), 2);
assertEquals(group.getX2(), 3);
assertEquals(group.getY2(), 4);
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sh = wb.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
group = (HSSFShapeGroup) patriarch.getChildren().get(0);
assertEquals(group.getX1(), 1);
assertEquals(group.getY1(), 2);
assertEquals(group.getX2(), 3);
assertEquals(group.getY2(), 4);
}
public void testAddToExistingFile(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sh = wb.createSheet();
HSSFPatriarch patriarch = sh.createDrawingPatriarch();
HSSFShapeGroup group1 = patriarch.createGroup(new HSSFClientAnchor());
HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
group1.setCoordinates(1,2,3,4);
group2.setCoordinates(5,6,7,8);
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sh = wb.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
assertEquals(patriarch.getChildren().size(), 2);
HSSFShapeGroup group3 = patriarch.createGroup(new HSSFClientAnchor());
group3.setCoordinates(9,10,11,12);
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sh = wb.getSheetAt(0);
patriarch = sh.getDrawingPatriarch();
assertEquals(patriarch.getChildren().size(), 3);
}
public void testModify() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
// create a sheet with a text box
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group1 = patriarch.createGroup(new
HSSFClientAnchor(0,0,0,0,
(short)0, 0, (short)15, 25));
group1.setCoordinates(0, 0, 792, 612);
HSSFTextbox textbox1 = group1.createTextbox(new
HSSFChildAnchor(100, 100, 300, 300));
HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!");
textbox1.setString(rt1);
// write, read back and check that our text box is there
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
assertEquals(1, group1.getChildren().size());
textbox1 = (HSSFTextbox)group1.getChildren().get(0);
assertEquals("Hello, World!", textbox1.getString().getString());
// modify anchor
assertEquals(new HSSFChildAnchor(100, 100, 300, 300),
textbox1.getAnchor());
HSSFChildAnchor newAnchor = new HSSFChildAnchor(200,200, 400, 400);
textbox1.setAnchor(newAnchor);
// modify text
textbox1.setString(new HSSFRichTextString("Hello, World! (modified)"));
// add a new text box
HSSFTextbox textbox2 = group1.createTextbox(new
HSSFChildAnchor(400, 400, 600, 600));
HSSFRichTextString rt2 = new HSSFRichTextString("Hello, World-2");
textbox2.setString(rt2);
assertEquals(2, group1.getChildren().size());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
assertEquals(2, group1.getChildren().size());
textbox1 = (HSSFTextbox)group1.getChildren().get(0);
assertEquals("Hello, World! (modified)",
textbox1.getString().getString());
assertEquals(new HSSFChildAnchor(200,200, 400, 400),
textbox1.getAnchor());
textbox2 = (HSSFTextbox)group1.getChildren().get(1);
assertEquals("Hello, World-2", textbox2.getString().getString());
assertEquals(new HSSFChildAnchor(400, 400, 600, 600),
textbox2.getAnchor());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
group1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
textbox1 = (HSSFTextbox)group1.getChildren().get(0);
textbox2 = (HSSFTextbox)group1.getChildren().get(1);
HSSFTextbox textbox3 = group1.createTextbox(new
HSSFChildAnchor(400,200, 600, 400));
HSSFRichTextString rt3 = new HSSFRichTextString("Hello, World-3");
textbox3.setString(rt3);
}
public void testAddShapesToGroup(){
HSSFWorkbook wb = new HSSFWorkbook();
// create a sheet with a text box
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
int index = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_JPEG);
group.createPicture(new HSSFChildAnchor(), index);
HSSFPolygon polygon = group.createPolygon(new HSSFChildAnchor());
polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
group.createTextbox(new HSSFChildAnchor());
group.createShape(new HSSFChildAnchor());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(1, patriarch.getChildren().size());
assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
group = (HSSFShapeGroup) patriarch.getChildren().get(0);
assertEquals(group.getChildren().size(), 4);
assertTrue(group.getChildren().get(0) instanceof HSSFPicture);
assertTrue(group.getChildren().get(1) instanceof HSSFPolygon);
assertTrue(group.getChildren().get(2) instanceof HSSFTextbox);
assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape);
HSSFShapeGroup group2 = patriarch.createGroup(new HSSFClientAnchor());
index = wb.addPicture(new byte[]{2,2,2}, HSSFWorkbook.PICTURE_TYPE_JPEG);
group2.createPicture(new HSSFChildAnchor(), index);
polygon = group2.createPolygon(new HSSFChildAnchor());
polygon.setPoints(new int[]{1,100, 1}, new int[]{1, 50, 100});
group2.createTextbox(new HSSFChildAnchor());
group2.createShape(new HSSFChildAnchor());
group2.createShape(new HSSFChildAnchor());
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(2, patriarch.getChildren().size());
group = (HSSFShapeGroup) patriarch.getChildren().get(1);
assertEquals(group.getChildren().size(), 5);
assertTrue(group.getChildren().get(0) instanceof HSSFPicture);
assertTrue(group.getChildren().get(1) instanceof HSSFPolygon);
assertTrue(group.getChildren().get(2) instanceof HSSFTextbox);
assertTrue(group.getChildren().get(3) instanceof HSSFSimpleShape);
assertTrue(group.getChildren().get(4) instanceof HSSFSimpleShape);
group.getShapeId();
}
public void testSpgrRecord(){
HSSFWorkbook wb = new HSSFWorkbook();
// create a sheet with a text box
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
assertSame(((EscherContainerRecord)group.getEscherContainer().getChild(0)).getChildById(EscherSpgrRecord.RECORD_ID), getSpgrRecord(group));
}
private static EscherSpgrRecord getSpgrRecord(HSSFShapeGroup group) {
Field spgrField = null;
try {
spgrField = group.getClass().getDeclaredField("_spgrRecord");
spgrField.setAccessible(true);
return (EscherSpgrRecord) spgrField.get(group);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public void testClearShapes(){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
group.createShape(new HSSFChildAnchor());
group.createShape(new HSSFChildAnchor());
EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
assertEquals(agg.getShapeToObjMapping().size(), 5);
assertEquals(agg.getTailRecords().size(), 0);
assertEquals(group.getChildren().size(), 2);
group.clear();
assertEquals(agg.getShapeToObjMapping().size(), 1);
assertEquals(agg.getTailRecords().size(), 0);
assertEquals(group.getChildren().size(), 0);
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
group = (HSSFShapeGroup) patriarch.getChildren().get(0);
assertEquals(agg.getShapeToObjMapping().size(), 1);
assertEquals(agg.getTailRecords().size(), 0);
assertEquals(group.getChildren().size(), 0);
}
}
|