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