1*eba4d44aSLiu Zhe package fvt.uno.sw.puretext;
2352c0eacSLiu Zhe 
3352c0eacSLiu Zhe import static org.junit.Assert.*;
4352c0eacSLiu Zhe 
5352c0eacSLiu Zhe import org.junit.After;
6352c0eacSLiu Zhe import org.junit.Before;
7352c0eacSLiu Zhe import org.junit.Test;
8352c0eacSLiu Zhe import org.openoffice.test.common.FileUtil;
9352c0eacSLiu Zhe import org.openoffice.test.common.Testspace;
10352c0eacSLiu Zhe import org.openoffice.test.uno.UnoApp;
11352c0eacSLiu Zhe 
12352c0eacSLiu Zhe import com.sun.star.uno.UnoRuntime;
13352c0eacSLiu Zhe import com.sun.star.text.*;
14352c0eacSLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
15352c0eacSLiu Zhe import com.sun.star.beans.PropertyValue;
16352c0eacSLiu Zhe import com.sun.star.container.XIndexAccess;
17352c0eacSLiu Zhe import com.sun.star.frame.XStorable;
18352c0eacSLiu Zhe 
19352c0eacSLiu Zhe public class InsertCharacterToTable {
20352c0eacSLiu Zhe 
21352c0eacSLiu Zhe 	private static final UnoApp app = new UnoApp();
22352c0eacSLiu Zhe 	private XTextDocument xTextDocument = null;
23352c0eacSLiu Zhe 	private XMultiServiceFactory xWriterFactory = null;
24352c0eacSLiu Zhe 	private XText xText = null;
25352c0eacSLiu Zhe 
26352c0eacSLiu Zhe 	@Before
27352c0eacSLiu Zhe 	public void setUp() throws Exception {
28352c0eacSLiu Zhe 		app.start();
29352c0eacSLiu Zhe 	}
30352c0eacSLiu Zhe 
31352c0eacSLiu Zhe 	@After
32352c0eacSLiu Zhe 	public void tearDown() throws Exception {
33352c0eacSLiu Zhe 		app.close();
34352c0eacSLiu Zhe 	}
35352c0eacSLiu Zhe 
36352c0eacSLiu Zhe 	@Test
37352c0eacSLiu Zhe 	public void testCreateTable() throws Exception {
38352c0eacSLiu Zhe 		xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
39352c0eacSLiu Zhe 		xText = xTextDocument.getText();
40352c0eacSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
41352c0eacSLiu Zhe 		// get internal service factory of the document
42352c0eacSLiu Zhe 		xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
43352c0eacSLiu Zhe 		// Create a new table from the document's factory
44352c0eacSLiu Zhe 		XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class,xWriterFactory.createInstance("com.sun.star.text.TextTable"));
45352c0eacSLiu Zhe 		xTable.initialize(4, 4);
46352c0eacSLiu Zhe 		xText.insertTextContent(xTextCursor, xTable, false);
47352c0eacSLiu Zhe         //insert text in to table cell
48352c0eacSLiu Zhe 		insertIntoCell( "A1","test", xTable );
49352c0eacSLiu Zhe 		insertIntoCell( "C4","123", xTable );
50352c0eacSLiu Zhe 		insertIntoCell( "D2","fsdf132134", xTable );
51352c0eacSLiu Zhe 		insertIntoCell( "B3","*^$%^$^$", xTable );
52352c0eacSLiu Zhe 
53352c0eacSLiu Zhe 		//save to odt
54352c0eacSLiu Zhe 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
55352c0eacSLiu Zhe 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
56352c0eacSLiu Zhe 		aStoreProperties_odt[0] = new PropertyValue();
57352c0eacSLiu Zhe 		aStoreProperties_odt[1] = new PropertyValue();
58352c0eacSLiu Zhe 		aStoreProperties_odt[0].Name = "Override";
59352c0eacSLiu Zhe 		aStoreProperties_odt[0].Value = true;
60352c0eacSLiu Zhe 		aStoreProperties_odt[1].Name = "FilterName";
61352c0eacSLiu Zhe 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
62352c0eacSLiu Zhe 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
63352c0eacSLiu Zhe 		//save to doc
64352c0eacSLiu Zhe 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
65352c0eacSLiu Zhe 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
66352c0eacSLiu Zhe 		aStoreProperties_doc[0] = new PropertyValue();
67352c0eacSLiu Zhe 		aStoreProperties_doc[1] = new PropertyValue();
68352c0eacSLiu Zhe 		aStoreProperties_doc[0].Name = "Override";
69352c0eacSLiu Zhe 		aStoreProperties_doc[0].Value = true;
70352c0eacSLiu Zhe 		aStoreProperties_doc[1].Name = "FilterName";
71352c0eacSLiu Zhe 		aStoreProperties_doc[1].Value = "MS Word 97";
72352c0eacSLiu Zhe 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
73352c0eacSLiu Zhe 		app.closeDocument(xTextDocument);
74352c0eacSLiu Zhe 
75352c0eacSLiu Zhe 		// reopen the document and assert create table successfully
76352c0eacSLiu Zhe 		XTextDocument assertDocument_odt = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.odt")));
77352c0eacSLiu Zhe 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt);
78352c0eacSLiu Zhe 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
79352c0eacSLiu Zhe 		Object xTable_obj_odt = xIndexedTables_odt.getByIndex(0);
80352c0eacSLiu Zhe 		XTextTable xTable_Assert_odt = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
81352c0eacSLiu Zhe 		assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_odt));
82352c0eacSLiu Zhe 		assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_odt));
83352c0eacSLiu Zhe 		assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_odt));
84352c0eacSLiu Zhe 		assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_odt));
85352c0eacSLiu Zhe 
86352c0eacSLiu Zhe 		// reopen the document and assert create table successfully
87352c0eacSLiu Zhe 		XTextDocument assertDocument_doc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.doc")));
88352c0eacSLiu Zhe 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc);
89352c0eacSLiu Zhe 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
90352c0eacSLiu Zhe 		Object xTable_obj_doc = xIndexedTables_doc.getByIndex(0);
91352c0eacSLiu Zhe 		XTextTable xTable_Assert_doc = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
92352c0eacSLiu Zhe 		assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_doc));
93352c0eacSLiu Zhe 		assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_doc));
94352c0eacSLiu Zhe 		assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_doc));
95352c0eacSLiu Zhe 		assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_doc));
96352c0eacSLiu Zhe 	}
97352c0eacSLiu Zhe 	// This method is inserts string sText in table cell by sCellName.
98352c0eacSLiu Zhe 	public static void insertIntoCell(String sCellName, String sText, XTextTable xTable) {
99352c0eacSLiu Zhe 		// Access the XText interface of the cell referred to by sCellName
100352c0eacSLiu Zhe 		XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
101352c0eacSLiu Zhe 		// Set the text in the cell to sText
102352c0eacSLiu Zhe 		xCellText.setString(sText);
103352c0eacSLiu Zhe 	}
104352c0eacSLiu Zhe 	// This method is get string sText in table cell by sCellName.
105352c0eacSLiu Zhe 	public static String getFromCell(String sCellName, XTextTable xTable) {
106352c0eacSLiu Zhe 		// Access the XText interface of the cell referred to by sCellName
107352c0eacSLiu Zhe 		XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
108352c0eacSLiu Zhe 		// Set the text in the cell to sText
109352c0eacSLiu Zhe 		return xCellText.getString();
110352c0eacSLiu Zhe 	}
111352c0eacSLiu Zhe }