2004-04-09 09:05:39 -04:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
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
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
==================================================================== */
|
2004-08-23 04:52:54 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import java.awt.*;
|
2008-01-15 11:31:29 -05:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the capabilities of the EscherGraphics class.
|
2008-01-15 11:31:29 -05:00
|
|
|
*
|
|
|
|
* All tests have two escher groups available to them,
|
|
|
|
* one anchored at 0,0,1022,255 and another anchored
|
|
|
|
* at 20,30,500,200
|
2004-04-09 07:45:38 -04:00
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
|
|
|
*/
|
|
|
|
public class TestEscherGraphics extends TestCase
|
|
|
|
{
|
2008-01-15 11:31:29 -05:00
|
|
|
private HSSFWorkbook workbook;
|
|
|
|
private HSSFPatriarch patriarch;
|
|
|
|
private HSSFShapeGroup escherGroupA;
|
|
|
|
private HSSFShapeGroup escherGroupB;
|
2004-04-09 07:45:38 -04:00
|
|
|
private EscherGraphics graphics;
|
|
|
|
|
|
|
|
protected void setUp() throws Exception
|
|
|
|
{
|
2008-01-15 11:31:29 -05:00
|
|
|
workbook = new HSSFWorkbook();
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
HSSFSheet sheet = workbook.createSheet("test");
|
2008-01-15 11:31:29 -05:00
|
|
|
patriarch = sheet.createDrawingPatriarch();
|
|
|
|
escherGroupA = patriarch.createGroup(new HSSFClientAnchor(0,0,1022,255,(short)0,0,(short) 0,0));
|
|
|
|
escherGroupB = patriarch.createGroup(new HSSFClientAnchor(20,30,500,200,(short)0,0,(short) 0,0));
|
|
|
|
// escherGroup = new HSSFShapeGroup(null, new HSSFChildAnchor());
|
|
|
|
graphics = new EscherGraphics(this.escherGroupA, workbook, Color.black, 1.0f);
|
2004-04-09 07:45:38 -04:00
|
|
|
super.setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetFont() throws Exception
|
|
|
|
{
|
|
|
|
Font f = graphics.getFont();
|
2006-06-16 11:21:30 -04:00
|
|
|
if (f.toString().indexOf("dialog") == -1 && f.toString().indexOf("Dialog") == -1)
|
2004-04-09 08:19:08 -04:00
|
|
|
assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", f.toString());
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetFontMetrics() throws Exception
|
|
|
|
{
|
2004-04-09 08:19:08 -04:00
|
|
|
Font f = graphics.getFont();
|
2006-06-16 11:21:30 -04:00
|
|
|
if (f.toString().indexOf("dialog") != -1 || f.toString().indexOf("Dialog") != -1)
|
2004-04-09 08:19:08 -04:00
|
|
|
return;
|
2004-04-09 07:45:38 -04:00
|
|
|
FontMetrics fontMetrics = graphics.getFontMetrics(graphics.getFont());
|
|
|
|
assertEquals(7, fontMetrics.charWidth('X'));
|
|
|
|
assertEquals("java.awt.Font[family=Arial,name=Arial,style=plain,size=10]", fontMetrics.getFont().toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSetFont() throws Exception
|
|
|
|
{
|
|
|
|
Font f = new Font("Helvetica", 0, 12);
|
|
|
|
graphics.setFont(f);
|
|
|
|
assertEquals(f, graphics.getFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSetColor() throws Exception
|
|
|
|
{
|
|
|
|
graphics.setColor(Color.red);
|
|
|
|
assertEquals(Color.red, graphics.getColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testFillRect() throws Exception
|
|
|
|
{
|
|
|
|
graphics.fillRect( 10, 10, 20, 20 );
|
2008-01-15 11:31:29 -05:00
|
|
|
HSSFSimpleShape s = (HSSFSimpleShape) escherGroupA.getChildren().get(0);
|
2004-04-09 07:45:38 -04:00
|
|
|
assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, s.getShapeType());
|
|
|
|
assertEquals(10, s.getAnchor().getDx1());
|
|
|
|
assertEquals(10, s.getAnchor().getDy1());
|
|
|
|
assertEquals(30, s.getAnchor().getDy2());
|
|
|
|
assertEquals(30, s.getAnchor().getDx2());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testDrawString() throws Exception
|
|
|
|
{
|
|
|
|
graphics.drawString("This is a test", 10, 10);
|
2008-01-15 11:31:29 -05:00
|
|
|
HSSFTextbox t = (HSSFTextbox) escherGroupA.getChildren().get(0);
|
2005-08-18 03:06:44 -04:00
|
|
|
assertEquals("This is a test", t.getString().getString().toString());
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2008-01-15 11:31:29 -05:00
|
|
|
public void testGetDataBackAgain() throws Exception {
|
|
|
|
HSSFSheet s;
|
|
|
|
HSSFShapeGroup s1;
|
|
|
|
HSSFShapeGroup s2;
|
|
|
|
|
|
|
|
patriarch.setCoordinates(10, 20, 30, 40);
|
|
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
workbook.write(baos);
|
|
|
|
workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
s = workbook.getSheetAt(0);
|
|
|
|
|
|
|
|
patriarch = s.getDrawingPatriarch();
|
|
|
|
|
|
|
|
assertNotNull(patriarch);
|
|
|
|
assertEquals(10, patriarch.getX1());
|
|
|
|
assertEquals(20, patriarch.getY1());
|
|
|
|
assertEquals(30, patriarch.getX2());
|
|
|
|
assertEquals(40, patriarch.getY2());
|
|
|
|
|
|
|
|
// Check the two groups too
|
|
|
|
assertEquals(2, patriarch.countOfAllChildren());
|
|
|
|
assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
|
|
|
|
assertTrue(patriarch.getChildren().get(1) instanceof HSSFShapeGroup);
|
|
|
|
|
|
|
|
s1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
|
|
|
|
s2 = (HSSFShapeGroup)patriarch.getChildren().get(1);
|
|
|
|
|
|
|
|
assertEquals(0, s1.getX1());
|
|
|
|
assertEquals(0, s1.getY1());
|
|
|
|
assertEquals(1023, s1.getX2());
|
|
|
|
assertEquals(255, s1.getY2());
|
|
|
|
assertEquals(0, s2.getX1());
|
|
|
|
assertEquals(0, s2.getY1());
|
|
|
|
assertEquals(1023, s2.getX2());
|
|
|
|
assertEquals(255, s2.getY2());
|
|
|
|
|
|
|
|
assertEquals(0, s1.getAnchor().getDx1());
|
|
|
|
assertEquals(0, s1.getAnchor().getDy1());
|
|
|
|
assertEquals(1022, s1.getAnchor().getDx2());
|
|
|
|
assertEquals(255, s1.getAnchor().getDy2());
|
|
|
|
assertEquals(20, s2.getAnchor().getDx1());
|
|
|
|
assertEquals(30, s2.getAnchor().getDy1());
|
|
|
|
assertEquals(500, s2.getAnchor().getDx2());
|
|
|
|
assertEquals(200, s2.getAnchor().getDy2());
|
|
|
|
|
|
|
|
|
|
|
|
// Write and re-load once more, to check that's ok
|
|
|
|
baos = new ByteArrayOutputStream();
|
|
|
|
workbook.write(baos);
|
|
|
|
workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
s = workbook.getSheetAt(0);
|
|
|
|
patriarch = s.getDrawingPatriarch();
|
|
|
|
|
|
|
|
assertNotNull(patriarch);
|
|
|
|
assertEquals(10, patriarch.getX1());
|
|
|
|
assertEquals(20, patriarch.getY1());
|
|
|
|
assertEquals(30, patriarch.getX2());
|
|
|
|
assertEquals(40, patriarch.getY2());
|
|
|
|
|
|
|
|
// Check the two groups too
|
|
|
|
assertEquals(2, patriarch.countOfAllChildren());
|
|
|
|
assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
|
|
|
|
assertTrue(patriarch.getChildren().get(1) instanceof HSSFShapeGroup);
|
|
|
|
|
|
|
|
s1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
|
|
|
|
s2 = (HSSFShapeGroup)patriarch.getChildren().get(1);
|
|
|
|
|
|
|
|
assertEquals(0, s1.getX1());
|
|
|
|
assertEquals(0, s1.getY1());
|
|
|
|
assertEquals(1023, s1.getX2());
|
|
|
|
assertEquals(255, s1.getY2());
|
|
|
|
assertEquals(0, s2.getX1());
|
|
|
|
assertEquals(0, s2.getY1());
|
|
|
|
assertEquals(1023, s2.getX2());
|
|
|
|
assertEquals(255, s2.getY2());
|
|
|
|
|
|
|
|
assertEquals(0, s1.getAnchor().getDx1());
|
|
|
|
assertEquals(0, s1.getAnchor().getDy1());
|
|
|
|
assertEquals(1022, s1.getAnchor().getDx2());
|
|
|
|
assertEquals(255, s1.getAnchor().getDy2());
|
|
|
|
assertEquals(20, s2.getAnchor().getDx1());
|
|
|
|
assertEquals(30, s2.getAnchor().getDy1());
|
|
|
|
assertEquals(500, s2.getAnchor().getDx2());
|
|
|
|
assertEquals(200, s2.getAnchor().getDy2());
|
|
|
|
|
|
|
|
// Change the positions of the first groups,
|
|
|
|
// but not of their anchors
|
|
|
|
s1.setCoordinates(2, 3, 1021, 242);
|
|
|
|
|
|
|
|
baos = new ByteArrayOutputStream();
|
|
|
|
workbook.write(baos);
|
|
|
|
workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
s = workbook.getSheetAt(0);
|
|
|
|
patriarch = s.getDrawingPatriarch();
|
|
|
|
|
|
|
|
assertNotNull(patriarch);
|
|
|
|
assertEquals(10, patriarch.getX1());
|
|
|
|
assertEquals(20, patriarch.getY1());
|
|
|
|
assertEquals(30, patriarch.getX2());
|
|
|
|
assertEquals(40, patriarch.getY2());
|
|
|
|
|
|
|
|
// Check the two groups too
|
|
|
|
assertEquals(2, patriarch.countOfAllChildren());
|
|
|
|
assertEquals(2, patriarch.getChildren().size());
|
|
|
|
assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
|
|
|
|
assertTrue(patriarch.getChildren().get(1) instanceof HSSFShapeGroup);
|
|
|
|
|
|
|
|
s1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
|
|
|
|
s2 = (HSSFShapeGroup)patriarch.getChildren().get(1);
|
|
|
|
|
|
|
|
assertEquals(2, s1.getX1());
|
|
|
|
assertEquals(3, s1.getY1());
|
|
|
|
assertEquals(1021, s1.getX2());
|
|
|
|
assertEquals(242, s1.getY2());
|
|
|
|
assertEquals(0, s2.getX1());
|
|
|
|
assertEquals(0, s2.getY1());
|
|
|
|
assertEquals(1023, s2.getX2());
|
|
|
|
assertEquals(255, s2.getY2());
|
|
|
|
|
|
|
|
assertEquals(0, s1.getAnchor().getDx1());
|
|
|
|
assertEquals(0, s1.getAnchor().getDy1());
|
|
|
|
assertEquals(1022, s1.getAnchor().getDx2());
|
|
|
|
assertEquals(255, s1.getAnchor().getDy2());
|
|
|
|
assertEquals(20, s2.getAnchor().getDx1());
|
|
|
|
assertEquals(30, s2.getAnchor().getDy1());
|
|
|
|
assertEquals(500, s2.getAnchor().getDx2());
|
|
|
|
assertEquals(200, s2.getAnchor().getDy2());
|
|
|
|
|
|
|
|
|
|
|
|
// Now add some text to one group, and some more
|
|
|
|
// to the base, and check we can get it back again
|
|
|
|
HSSFTextbox tbox1 =
|
|
|
|
patriarch.createTextbox(new HSSFClientAnchor(1,2,3,4, (short)0,0,(short)0,0));
|
|
|
|
tbox1.setString(new HSSFRichTextString("I am text box 1"));
|
|
|
|
HSSFTextbox tbox2 =
|
|
|
|
s2.createTextbox(new HSSFChildAnchor(41,42,43,44));
|
|
|
|
tbox2.setString(new HSSFRichTextString("This is text box 2"));
|
|
|
|
|
|
|
|
assertEquals(3, patriarch.getChildren().size());
|
|
|
|
|
|
|
|
|
|
|
|
baos = new ByteArrayOutputStream();
|
|
|
|
workbook.write(baos);
|
|
|
|
workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray()));
|
|
|
|
s = workbook.getSheetAt(0);
|
|
|
|
|
|
|
|
patriarch = s.getDrawingPatriarch();
|
|
|
|
|
|
|
|
assertNotNull(patriarch);
|
|
|
|
assertEquals(10, patriarch.getX1());
|
|
|
|
assertEquals(20, patriarch.getY1());
|
|
|
|
assertEquals(30, patriarch.getX2());
|
|
|
|
assertEquals(40, patriarch.getY2());
|
|
|
|
|
|
|
|
// Check the two groups and the text
|
|
|
|
assertEquals(3, patriarch.countOfAllChildren());
|
|
|
|
assertEquals(2, patriarch.getChildren().size());
|
|
|
|
|
|
|
|
// Should be two groups and a text
|
|
|
|
assertTrue(patriarch.getChildren().get(0) instanceof HSSFShapeGroup);
|
|
|
|
assertTrue(patriarch.getChildren().get(1) instanceof HSSFTextbox);
|
|
|
|
// assertTrue(patriarch.getChildren().get(2) instanceof HSSFShapeGroup);
|
|
|
|
|
|
|
|
s1 = (HSSFShapeGroup)patriarch.getChildren().get(0);
|
|
|
|
tbox1 = (HSSFTextbox)patriarch.getChildren().get(1);
|
|
|
|
|
|
|
|
// s2 = (HSSFShapeGroup)patriarch.getChildren().get(1);
|
|
|
|
|
|
|
|
assertEquals(2, s1.getX1());
|
|
|
|
assertEquals(3, s1.getY1());
|
|
|
|
assertEquals(1021, s1.getX2());
|
|
|
|
assertEquals(242, s1.getY2());
|
|
|
|
assertEquals(0, s2.getX1());
|
|
|
|
assertEquals(0, s2.getY1());
|
|
|
|
assertEquals(1023, s2.getX2());
|
|
|
|
assertEquals(255, s2.getY2());
|
|
|
|
|
|
|
|
assertEquals(0, s1.getAnchor().getDx1());
|
|
|
|
assertEquals(0, s1.getAnchor().getDy1());
|
|
|
|
assertEquals(1022, s1.getAnchor().getDx2());
|
|
|
|
assertEquals(255, s1.getAnchor().getDy2());
|
|
|
|
assertEquals(20, s2.getAnchor().getDx1());
|
|
|
|
assertEquals(30, s2.getAnchor().getDy1());
|
|
|
|
assertEquals(500, s2.getAnchor().getDx2());
|
|
|
|
assertEquals(200, s2.getAnchor().getDy2());
|
|
|
|
|
|
|
|
// Not working just yet
|
|
|
|
//assertEquals("I am text box 1", tbox1.getString().getString());
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|