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 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.presentation.XPresentation;
29 import com.sun.star.presentation.XPresentationSupplier;
30 import com.sun.star.style.LineSpacing;
31 import com.sun.star.style.LineSpacingMode;
32 import com.sun.star.style.ParagraphAdjust;
33 import com.sun.star.text.ControlCharacter;
34 import com.sun.star.text.XText;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextRange;
37 import com.sun.star.uno.UnoRuntime;
38 
39 public class ParagraphStyle {
40 	XPresentationSupplier sdDocument = null;
41 	XPresentation pre = null;
42 	XComponent precomp = null;
43 	XComponent impressDocument = null;
44 	XComponent reLoadFile = null;
45 	XDrawPagesSupplier drawsupplier = null;
46 	XDrawPages drawpages = null;
47 	XShapes xShapes = null;
48 	XDrawPage xpage = null;
49 	String filePath=null;
50 
51     UnoApp unoApp = new UnoApp();
52 
53 	/**
54 	 * @throws java.lang.Exception
55 	 */
56 	@Before
57 	public void setUp() throws Exception {
58 		unoApp.start();
59 		createDocumentAndSlide();
60 	}
61 
62 	@After
63 	public void tearDown() throws Exception {
64 		unoApp.closeDocument(impressDocument);
65 		unoApp.closeDocument(reLoadFile);
66 		unoApp.close();
67 	}
68 
69 	@Test
70 	public void ParaStyle() throws Exception {
71 		Point po = new Point(5000, 5000);
72 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
73 		// create the shape
74 		XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
75 		 xShapes.add(xRectangle);
76 		 XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
77 		 // TextFitToSize
78 		 xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
79 
80 		 XPropertySet xTextPropSet1 = addPortion(xRectangle, "New text paragraph", true);
81 		 xTextPropSet1.setPropertyValue("ParaAdjust", ParagraphAdjust.CENTER);
82 
83 		 //Line Spacing
84 		 LineSpacing xLineSpacing = new LineSpacing(LineSpacingMode.LEADING, (short)1);
85 		 xTextPropSet1.setPropertyValue("ParaLineSpacing",  xLineSpacing);
86 
87 		 //left, right, top and bottom margin
88 		 xTextPropSet1.setPropertyValue("ParaLeftMargin",  1000);
89 		 xTextPropSet1.setPropertyValue("ParaRightMargin",  1000);
90 		 xTextPropSet1.setPropertyValue("ParaTopMargin",  1000);
91 		 xTextPropSet1.setPropertyValue("ParaBottomMargin",  1000);
92 
93 		 XPropertySet xTextPropSet2 = addPortion(xRectangle, "And another text paragraph", true);
94 		 xTextPropSet2.setPropertyValue("CharColor", new Integer(0xff0000));
95 
96 		 xRectangle = saveAndLoadShape(1, 0);
97 
98 
99 		 Assert.assertEquals("Paragraph Left Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaLeftMargin"));
100 		 Assert.assertEquals("Paragraph Right Margin is 1000", 1000,xTextPropSet1.getPropertyValue("ParaRightMargin"));
101 		 Assert.assertEquals("Paragraph Top Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaTopMargin") );
102 		 Assert.assertEquals("Paragraph Bottom Margin is 1000 ",1000, xTextPropSet1.getPropertyValue("ParaBottomMargin"));
103 		 Assert.assertEquals("Text Color is red",0xff0000,xTextPropSet2.getPropertyValue("CharColor"));
104 
105 	}
106 
107 
108 	public static XShape createShape(XComponent xComponent, int x, int y,
109 			int width, int height, String sShapeType)
110 			throws java.lang.Exception {
111 		// query the document for the document-internal service factory
112 		XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
113 				.queryInterface(XMultiServiceFactory.class, xComponent);
114 
115 		// get the given Shape service from the factory
116 		Object xObj = xFactory.createInstance(sShapeType);
117 		Point aPos = new Point(x, y);
118 		Size aSize = new Size(width, height);
119 
120 		// use its XShape interface to determine position and size before
121 		// insertion
122 		XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
123 		xShape.setPosition(aPos);
124 		xShape.setSize(aSize);
125 		return xShape;
126 	}
127 
128 	public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
129 	         throws com.sun.star.lang.IllegalArgumentException {
130 	     XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
131 	     XTextCursor xTextCursor = xText.createTextCursor();
132 	     xTextCursor.gotoEnd(false);
133 	     if (bNewParagraph) {
134 	         xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
135 	         xTextCursor.gotoEnd(false);
136 	     }
137 	     XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
138 	     xTextRange.setString(sText);
139 	     xTextCursor.gotoEnd(true);
140 	     XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
141 	     return xPropSet;
142 	 }
143 
144 	/**
145 	 * create a new presentation document and insert a new slide.
146 	 *
147 	 * @throws Exception
148 	 */
149 	public void createDocumentAndSlide() throws Exception {
150 		impressDocument = (XComponent) UnoRuntime.queryInterface(
151 				XComponent.class, unoApp.newDocument("simpress"));
152 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
153 				XDrawPagesSupplier.class, impressDocument);
154 		drawpages = drawsupplier.getDrawPages();
155 		drawpages.insertNewByIndex(1);
156 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
157 	}
158 
159 	/**
160 	 * Save presentation and reLoad the presentation and shape in it.
161 	 *
162 	 * @param po
163 	 * @param shapeType
164 	 * @return
165 	 * @throws Exception
166 	 */
167 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
168 		reLoadFile = saveAndReloadDoc(impressDocument,
169 				"impress8", "odp");
170 		xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
171 		return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
172 	}
173 
174 	/**
175 	 * save and reload Presentation document.
176 	 *
177 	 * @param presentationDocument
178 	 * @param sFilter
179 	 * @param sExtension
180 	 * @return
181 	 * @throws Exception
182 	 */
183 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
184 			String sFilter, String sExtension) throws Exception {
185 		filePath = Testspace.getPath("tmp/paragraphstyle."
186 				+ sExtension);
187 		PropertyValue[] aStoreProperties = new PropertyValue[2];
188 		aStoreProperties[0] = new PropertyValue();
189 		aStoreProperties[1] = new PropertyValue();
190 		aStoreProperties[0].Name = "Override";
191 		aStoreProperties[0].Value = true;
192 		aStoreProperties[1].Name = "FilterName";
193 		aStoreProperties[1].Value = sFilter;
194 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
195 				XStorable.class, presentationDocument);
196 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
197 
198 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
199 				unoApp.loadDocument(filePath));
200 	}
201 }
202