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