1*22a547feSLi Feng Wang package fvt.uno.sd.paragraph;
2*22a547feSLi Feng Wang 
3*22a547feSLi Feng Wang import junit.framework.Assert;
4*22a547feSLi Feng Wang 
5*22a547feSLi Feng Wang import org.junit.After;
6*22a547feSLi Feng Wang import org.junit.Before;
7*22a547feSLi Feng Wang import org.junit.Test;
8*22a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
9*22a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
10*22a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
11*22a547feSLi Feng Wang 
12*22a547feSLi Feng Wang import testlib.uno.PageUtil;
13*22a547feSLi Feng Wang import testlib.uno.ShapeUtil;
14*22a547feSLi Feng Wang 
15*22a547feSLi Feng Wang import com.sun.star.awt.Point;
16*22a547feSLi Feng Wang import com.sun.star.awt.Size;
17*22a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
18*22a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
19*22a547feSLi Feng Wang import com.sun.star.drawing.TextFitToSizeType;
20*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
21*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
22*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
23*22a547feSLi Feng Wang import com.sun.star.drawing.XShape;
24*22a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
25*22a547feSLi Feng Wang import com.sun.star.frame.XStorable;
26*22a547feSLi Feng Wang import com.sun.star.lang.XComponent;
27*22a547feSLi Feng Wang 
28*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
29*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
30*22a547feSLi Feng Wang 
31*22a547feSLi Feng Wang import com.sun.star.text.ControlCharacter;
32*22a547feSLi Feng Wang import com.sun.star.text.XText;
33*22a547feSLi Feng Wang import com.sun.star.text.XTextCursor;
34*22a547feSLi Feng Wang import com.sun.star.text.XTextRange;
35*22a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
36*22a547feSLi Feng Wang 
37*22a547feSLi Feng Wang public class ParagraphTextProperty {
38*22a547feSLi Feng Wang 	XPresentationSupplier sdDocument = null;
39*22a547feSLi Feng Wang 	XPresentation pre = null;
40*22a547feSLi Feng Wang 	XComponent precomp = null;
41*22a547feSLi Feng Wang 	XComponent impressDocument = null;
42*22a547feSLi Feng Wang 	XComponent reLoadFile = null;
43*22a547feSLi Feng Wang 	XDrawPagesSupplier drawsupplier = null;
44*22a547feSLi Feng Wang 	XDrawPages drawpages = null;
45*22a547feSLi Feng Wang 	XShapes xShapes = null;
46*22a547feSLi Feng Wang 	XDrawPage xpage = null;
47*22a547feSLi Feng Wang 	String filePath=null;
48*22a547feSLi Feng Wang 
49*22a547feSLi Feng Wang 	UnoApp unoApp = new UnoApp();
50*22a547feSLi Feng Wang 
51*22a547feSLi Feng Wang 	/**
52*22a547feSLi Feng Wang 	 * @throws java.lang.Exception
53*22a547feSLi Feng Wang 	 */
54*22a547feSLi Feng Wang 	@Before
55*22a547feSLi Feng Wang 	public void setUp() throws Exception {
56*22a547feSLi Feng Wang 		unoApp.start();
57*22a547feSLi Feng Wang 		createDocumentAndSlide();
58*22a547feSLi Feng Wang 	}
59*22a547feSLi Feng Wang 
60*22a547feSLi Feng Wang 	@After
61*22a547feSLi Feng Wang 	public void tearDown() throws Exception {
62*22a547feSLi Feng Wang 		unoApp.closeDocument(impressDocument);
63*22a547feSLi Feng Wang 		unoApp.closeDocument(reLoadFile);
64*22a547feSLi Feng Wang 		unoApp.close();
65*22a547feSLi Feng Wang 	}
66*22a547feSLi Feng Wang 
67*22a547feSLi Feng Wang 	@Test
68*22a547feSLi Feng Wang 	public void testParagraphPropertyShape() throws Exception {
69*22a547feSLi Feng Wang 		 Point po = new Point(5000, 5000);
70*22a547feSLi Feng Wang 		 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
71*22a547feSLi Feng Wang 		 // create the shape
72*22a547feSLi Feng Wang 		 XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
73*22a547feSLi Feng Wang 		 xShapes.add(xRectangle);
74*22a547feSLi Feng Wang 
75*22a547feSLi Feng Wang 		 XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
76*22a547feSLi Feng Wang 		 // TextFitToSize
77*22a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
78*22a547feSLi Feng Wang 		 // border size
79*22a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextLeftDistance", new Integer(2500));
80*22a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextRightDistance", new Integer(2500));
81*22a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextUpperDistance", new Integer(2500));
82*22a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextLowerDistance", new Integer(2500));
83*22a547feSLi Feng Wang 		 XPropertySet xTextPropSet = addPortion(xRectangle, "using TextFitToSize", false);
84*22a547feSLi Feng Wang 		 xTextPropSet = addPortion(xRectangle, "and a Border distance of 2,5 cm", true);
85*22a547feSLi Feng Wang 
86*22a547feSLi Feng Wang 		 xRectangle = saveAndLoadShape(1,0);
87*22a547feSLi Feng Wang 
88*22a547feSLi Feng Wang 		 Assert.assertEquals("TextLeftDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLeftDistance"));
89*22a547feSLi Feng Wang 		 Assert.assertEquals("TextRightDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextRightDistance"));
90*22a547feSLi Feng Wang 		 Assert.assertEquals("TextUpperDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextUpperDistance"));
91*22a547feSLi Feng Wang 		 Assert.assertEquals("TextLowerDistance is 2500", 2500, xShapePropSet.getPropertyValue("TextLowerDistance"));
92*22a547feSLi Feng Wang 
93*22a547feSLi Feng Wang 	}
94*22a547feSLi Feng Wang 
95*22a547feSLi Feng Wang 	public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
96*22a547feSLi Feng Wang 	         throws com.sun.star.lang.IllegalArgumentException {
97*22a547feSLi Feng Wang 	     XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
98*22a547feSLi Feng Wang 	     XTextCursor xTextCursor = xText.createTextCursor();
99*22a547feSLi Feng Wang 	     xTextCursor.gotoEnd(false);
100*22a547feSLi Feng Wang 	     if (bNewParagraph) {
101*22a547feSLi Feng Wang 	         xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
102*22a547feSLi Feng Wang 	         xTextCursor.gotoEnd(false);
103*22a547feSLi Feng Wang 	     }
104*22a547feSLi Feng Wang 	     XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
105*22a547feSLi Feng Wang 	     xTextRange.setString(sText);
106*22a547feSLi Feng Wang 	     xTextCursor.gotoEnd(true);
107*22a547feSLi Feng Wang 	     XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
108*22a547feSLi Feng Wang 	     return xPropSet;
109*22a547feSLi Feng Wang 	 }
110*22a547feSLi Feng Wang 
111*22a547feSLi Feng Wang 	/**
112*22a547feSLi Feng Wang 	 * create a new presentation document and insert a new slide.
113*22a547feSLi Feng Wang 	 *
114*22a547feSLi Feng Wang 	 * @throws Exception
115*22a547feSLi Feng Wang 	 */
116*22a547feSLi Feng Wang 	public void createDocumentAndSlide() throws Exception {
117*22a547feSLi Feng Wang 		impressDocument = (XComponent) UnoRuntime.queryInterface(
118*22a547feSLi Feng Wang 				XComponent.class, unoApp.newDocument("simpress"));
119*22a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
120*22a547feSLi Feng Wang 				XDrawPagesSupplier.class, impressDocument);
121*22a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
122*22a547feSLi Feng Wang 		drawpages.insertNewByIndex(1);
123*22a547feSLi Feng Wang 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
124*22a547feSLi Feng Wang 	}
125*22a547feSLi Feng Wang 
126*22a547feSLi Feng Wang 	/**
127*22a547feSLi Feng Wang 	 * Save presentation and reLoad the presentation and shape in it.
128*22a547feSLi Feng Wang 	 *
129*22a547feSLi Feng Wang 	 * @param po
130*22a547feSLi Feng Wang 	 * @param shapeType
131*22a547feSLi Feng Wang 	 * @return
132*22a547feSLi Feng Wang 	 * @throws Exception
133*22a547feSLi Feng Wang 	 */
134*22a547feSLi Feng Wang 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
135*22a547feSLi Feng Wang 		reLoadFile = saveAndReloadDoc(impressDocument,
136*22a547feSLi Feng Wang 				"impress8", "odp");
137*22a547feSLi Feng Wang 		xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
138*22a547feSLi Feng Wang 		return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
139*22a547feSLi Feng Wang 	}
140*22a547feSLi Feng Wang 
141*22a547feSLi Feng Wang 	/**
142*22a547feSLi Feng Wang 	 * save and reload Presentation document.
143*22a547feSLi Feng Wang 	 *
144*22a547feSLi Feng Wang 	 * @param presentationDocument
145*22a547feSLi Feng Wang 	 * @param sFilter
146*22a547feSLi Feng Wang 	 * @param sExtension
147*22a547feSLi Feng Wang 	 * @return
148*22a547feSLi Feng Wang 	 * @throws Exception
149*22a547feSLi Feng Wang 	 */
150*22a547feSLi Feng Wang 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
151*22a547feSLi Feng Wang 			String sFilter, String sExtension) throws Exception {
152*22a547feSLi Feng Wang 		filePath = Testspace.getPath("tmp/paragraphtextproperty."
153*22a547feSLi Feng Wang 				+ sExtension);
154*22a547feSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
155*22a547feSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
156*22a547feSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
157*22a547feSLi Feng Wang 		aStoreProperties[0].Name = "Override";
158*22a547feSLi Feng Wang 		aStoreProperties[0].Value = true;
159*22a547feSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
160*22a547feSLi Feng Wang 		aStoreProperties[1].Value = sFilter;
161*22a547feSLi Feng Wang 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
162*22a547feSLi Feng Wang 				XStorable.class, presentationDocument);
163*22a547feSLi Feng Wang 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
164*22a547feSLi Feng Wang 
165*22a547feSLi Feng Wang 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
166*22a547feSLi Feng Wang 				unoApp.loadDocument(filePath));
167*22a547feSLi Feng Wang 	}
168*22a547feSLi Feng Wang }
169