xref: /AOO41X/test/testuno/source/fvt/uno/sd/paragraph/ParagraphStyle.java (revision b832fe9ee96f9a0260deab239932d14d0af31494)
1*b832fe9eSHerbert Dürr /**************************************************************
2*b832fe9eSHerbert Dürr  *
3*b832fe9eSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
4*b832fe9eSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
5*b832fe9eSHerbert Dürr  * distributed with this work for additional information
6*b832fe9eSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
7*b832fe9eSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
8*b832fe9eSHerbert Dürr  * "License"); you may not use this file except in compliance
9*b832fe9eSHerbert Dürr  * with the License.  You may obtain a copy of the License at
10*b832fe9eSHerbert Dürr  *
11*b832fe9eSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
12*b832fe9eSHerbert Dürr  *
13*b832fe9eSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
14*b832fe9eSHerbert Dürr  * software distributed under the License is distributed on an
15*b832fe9eSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b832fe9eSHerbert Dürr  * KIND, either express or implied.  See the License for the
17*b832fe9eSHerbert Dürr  * specific language governing permissions and limitations
18*b832fe9eSHerbert Dürr  * under the License.
19*b832fe9eSHerbert Dürr  *
20*b832fe9eSHerbert Dürr  *************************************************************/
2122a547feSLi Feng Wang package fvt.uno.sd.paragraph;
2222a547feSLi Feng Wang 
2322a547feSLi Feng Wang import junit.framework.Assert;
2422a547feSLi Feng Wang 
2522a547feSLi Feng Wang import org.junit.After;
2622a547feSLi Feng Wang import org.junit.Before;
2722a547feSLi Feng Wang import org.junit.Test;
2822a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
2922a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
3022a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
3122a547feSLi Feng Wang 
3222a547feSLi Feng Wang import testlib.uno.PageUtil;
3322a547feSLi Feng Wang import testlib.uno.ShapeUtil;
3422a547feSLi Feng Wang 
3522a547feSLi Feng Wang import com.sun.star.awt.Point;
3622a547feSLi Feng Wang import com.sun.star.awt.Size;
3722a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
3822a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
3922a547feSLi Feng Wang import com.sun.star.drawing.TextFitToSizeType;
4022a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
4122a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
4222a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
4322a547feSLi Feng Wang import com.sun.star.drawing.XShape;
4422a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
4522a547feSLi Feng Wang import com.sun.star.frame.XStorable;
4622a547feSLi Feng Wang import com.sun.star.lang.XComponent;
4722a547feSLi Feng Wang import com.sun.star.lang.XMultiServiceFactory;
4822a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
4922a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
5022a547feSLi Feng Wang import com.sun.star.style.LineSpacing;
5122a547feSLi Feng Wang import com.sun.star.style.LineSpacingMode;
5222a547feSLi Feng Wang import com.sun.star.style.ParagraphAdjust;
5322a547feSLi Feng Wang import com.sun.star.text.ControlCharacter;
5422a547feSLi Feng Wang import com.sun.star.text.XText;
5522a547feSLi Feng Wang import com.sun.star.text.XTextCursor;
5622a547feSLi Feng Wang import com.sun.star.text.XTextRange;
5722a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
5822a547feSLi Feng Wang 
5922a547feSLi Feng Wang public class ParagraphStyle {
6022a547feSLi Feng Wang 	XPresentationSupplier sdDocument = null;
6122a547feSLi Feng Wang 	XPresentation pre = null;
6222a547feSLi Feng Wang 	XComponent precomp = null;
6322a547feSLi Feng Wang 	XComponent impressDocument = null;
6422a547feSLi Feng Wang 	XComponent reLoadFile = null;
6522a547feSLi Feng Wang 	XDrawPagesSupplier drawsupplier = null;
6622a547feSLi Feng Wang 	XDrawPages drawpages = null;
6722a547feSLi Feng Wang 	XShapes xShapes = null;
6822a547feSLi Feng Wang 	XDrawPage xpage = null;
6922a547feSLi Feng Wang 	String filePath=null;
7022a547feSLi Feng Wang 
7122a547feSLi Feng Wang     UnoApp unoApp = new UnoApp();
7222a547feSLi Feng Wang 
7322a547feSLi Feng Wang 	/**
7422a547feSLi Feng Wang 	 * @throws java.lang.Exception
7522a547feSLi Feng Wang 	 */
7622a547feSLi Feng Wang 	@Before
setUp()7722a547feSLi Feng Wang 	public void setUp() throws Exception {
7822a547feSLi Feng Wang 		unoApp.start();
7922a547feSLi Feng Wang 		createDocumentAndSlide();
8022a547feSLi Feng Wang 	}
8122a547feSLi Feng Wang 
8222a547feSLi Feng Wang 	@After
tearDown()8322a547feSLi Feng Wang 	public void tearDown() throws Exception {
8422a547feSLi Feng Wang 		unoApp.closeDocument(impressDocument);
8522a547feSLi Feng Wang 		unoApp.closeDocument(reLoadFile);
8622a547feSLi Feng Wang 		unoApp.close();
8722a547feSLi Feng Wang 	}
8822a547feSLi Feng Wang 
8922a547feSLi Feng Wang 	@Test
ParaStyle()9022a547feSLi Feng Wang 	public void ParaStyle() throws Exception {
9122a547feSLi Feng Wang 		Point po = new Point(5000, 5000);
9222a547feSLi Feng Wang 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
9322a547feSLi Feng Wang 		// create the shape
9422a547feSLi Feng Wang 		XShape xRectangle = ShapeUtil.createShape(impressDocument, po, new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
9522a547feSLi Feng Wang 		 xShapes.add(xRectangle);
9622a547feSLi Feng Wang 		 XPropertySet xShapePropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xRectangle);
9722a547feSLi Feng Wang 		 // TextFitToSize
9822a547feSLi Feng Wang 		 xShapePropSet.setPropertyValue("TextFitToSize", TextFitToSizeType.PROPORTIONAL);
9922a547feSLi Feng Wang 
10022a547feSLi Feng Wang 		 XPropertySet xTextPropSet1 = addPortion(xRectangle, "New text paragraph", true);
10122a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaAdjust", ParagraphAdjust.CENTER);
10222a547feSLi Feng Wang 
10322a547feSLi Feng Wang 		 //Line Spacing
10422a547feSLi Feng Wang 		 LineSpacing xLineSpacing = new LineSpacing(LineSpacingMode.LEADING, (short)1);
10522a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaLineSpacing",  xLineSpacing);
10622a547feSLi Feng Wang 
10722a547feSLi Feng Wang 		 //left, right, top and bottom margin
10822a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaLeftMargin",  1000);
10922a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaRightMargin",  1000);
11022a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaTopMargin",  1000);
11122a547feSLi Feng Wang 		 xTextPropSet1.setPropertyValue("ParaBottomMargin",  1000);
11222a547feSLi Feng Wang 
11322a547feSLi Feng Wang 		 XPropertySet xTextPropSet2 = addPortion(xRectangle, "And another text paragraph", true);
11422a547feSLi Feng Wang 		 xTextPropSet2.setPropertyValue("CharColor", new Integer(0xff0000));
11522a547feSLi Feng Wang 
11622a547feSLi Feng Wang 		 xRectangle = saveAndLoadShape(1, 0);
11722a547feSLi Feng Wang 
11822a547feSLi Feng Wang 
11922a547feSLi Feng Wang 		 Assert.assertEquals("Paragraph Left Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaLeftMargin"));
12022a547feSLi Feng Wang 		 Assert.assertEquals("Paragraph Right Margin is 1000", 1000,xTextPropSet1.getPropertyValue("ParaRightMargin"));
12122a547feSLi Feng Wang 		 Assert.assertEquals("Paragraph Top Margin is 1000",1000, xTextPropSet1.getPropertyValue("ParaTopMargin") );
12222a547feSLi Feng Wang 		 Assert.assertEquals("Paragraph Bottom Margin is 1000 ",1000, xTextPropSet1.getPropertyValue("ParaBottomMargin"));
12322a547feSLi Feng Wang 		 Assert.assertEquals("Text Color is red",0xff0000,xTextPropSet2.getPropertyValue("CharColor"));
12422a547feSLi Feng Wang 
12522a547feSLi Feng Wang 	}
12622a547feSLi Feng Wang 
12722a547feSLi Feng Wang 
createShape(XComponent xComponent, int x, int y, int width, int height, String sShapeType)12822a547feSLi Feng Wang 	public static XShape createShape(XComponent xComponent, int x, int y,
12922a547feSLi Feng Wang 			int width, int height, String sShapeType)
13022a547feSLi Feng Wang 			throws java.lang.Exception {
13122a547feSLi Feng Wang 		// query the document for the document-internal service factory
13222a547feSLi Feng Wang 		XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
13322a547feSLi Feng Wang 				.queryInterface(XMultiServiceFactory.class, xComponent);
13422a547feSLi Feng Wang 
13522a547feSLi Feng Wang 		// get the given Shape service from the factory
13622a547feSLi Feng Wang 		Object xObj = xFactory.createInstance(sShapeType);
13722a547feSLi Feng Wang 		Point aPos = new Point(x, y);
13822a547feSLi Feng Wang 		Size aSize = new Size(width, height);
13922a547feSLi Feng Wang 
14022a547feSLi Feng Wang 		// use its XShape interface to determine position and size before
14122a547feSLi Feng Wang 		// insertion
14222a547feSLi Feng Wang 		XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
14322a547feSLi Feng Wang 		xShape.setPosition(aPos);
14422a547feSLi Feng Wang 		xShape.setSize(aSize);
14522a547feSLi Feng Wang 		return xShape;
14622a547feSLi Feng Wang 	}
14722a547feSLi Feng Wang 
addPortion(XShape xShape, String sText, boolean bNewParagraph)14822a547feSLi Feng Wang 	public static XPropertySet addPortion(XShape xShape, String sText, boolean bNewParagraph)
14922a547feSLi Feng Wang 	         throws com.sun.star.lang.IllegalArgumentException {
15022a547feSLi Feng Wang 	     XText xText = (XText)UnoRuntime.queryInterface(XText.class, xShape);
15122a547feSLi Feng Wang 	     XTextCursor xTextCursor = xText.createTextCursor();
15222a547feSLi Feng Wang 	     xTextCursor.gotoEnd(false);
15322a547feSLi Feng Wang 	     if (bNewParagraph) {
15422a547feSLi Feng Wang 	         xText.insertControlCharacter(xTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
15522a547feSLi Feng Wang 	         xTextCursor.gotoEnd(false);
15622a547feSLi Feng Wang 	     }
15722a547feSLi Feng Wang 	     XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
15822a547feSLi Feng Wang 	     xTextRange.setString(sText);
15922a547feSLi Feng Wang 	     xTextCursor.gotoEnd(true);
16022a547feSLi Feng Wang 	     XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextRange);
16122a547feSLi Feng Wang 	     return xPropSet;
16222a547feSLi Feng Wang 	 }
16322a547feSLi Feng Wang 
16422a547feSLi Feng Wang 	/**
16522a547feSLi Feng Wang 	 * create a new presentation document and insert a new slide.
16622a547feSLi Feng Wang 	 *
16722a547feSLi Feng Wang 	 * @throws Exception
16822a547feSLi Feng Wang 	 */
createDocumentAndSlide()16922a547feSLi Feng Wang 	public void createDocumentAndSlide() throws Exception {
17022a547feSLi Feng Wang 		impressDocument = (XComponent) UnoRuntime.queryInterface(
17122a547feSLi Feng Wang 				XComponent.class, unoApp.newDocument("simpress"));
17222a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
17322a547feSLi Feng Wang 				XDrawPagesSupplier.class, impressDocument);
17422a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
17522a547feSLi Feng Wang 		drawpages.insertNewByIndex(1);
17622a547feSLi Feng Wang 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
17722a547feSLi Feng Wang 	}
17822a547feSLi Feng Wang 
17922a547feSLi Feng Wang 	/**
18022a547feSLi Feng Wang 	 * Save presentation and reLoad the presentation and shape in it.
18122a547feSLi Feng Wang 	 *
18222a547feSLi Feng Wang 	 * @param po
18322a547feSLi Feng Wang 	 * @param shapeType
18422a547feSLi Feng Wang 	 * @return
18522a547feSLi Feng Wang 	 * @throws Exception
18622a547feSLi Feng Wang 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)18722a547feSLi Feng Wang 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
18822a547feSLi Feng Wang 		reLoadFile = saveAndReloadDoc(impressDocument,
18922a547feSLi Feng Wang 				"impress8", "odp");
19022a547feSLi Feng Wang 		xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
19122a547feSLi Feng Wang 		return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
19222a547feSLi Feng Wang 	}
19322a547feSLi Feng Wang 
19422a547feSLi Feng Wang 	/**
19522a547feSLi Feng Wang 	 * save and reload Presentation document.
19622a547feSLi Feng Wang 	 *
19722a547feSLi Feng Wang 	 * @param presentationDocument
19822a547feSLi Feng Wang 	 * @param sFilter
19922a547feSLi Feng Wang 	 * @param sExtension
20022a547feSLi Feng Wang 	 * @return
20122a547feSLi Feng Wang 	 * @throws Exception
20222a547feSLi Feng Wang 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)20322a547feSLi Feng Wang 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
20422a547feSLi Feng Wang 			String sFilter, String sExtension) throws Exception {
20522a547feSLi Feng Wang 		filePath = Testspace.getPath("tmp/paragraphstyle."
20622a547feSLi Feng Wang 				+ sExtension);
20722a547feSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
20822a547feSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
20922a547feSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
21022a547feSLi Feng Wang 		aStoreProperties[0].Name = "Override";
21122a547feSLi Feng Wang 		aStoreProperties[0].Value = true;
21222a547feSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
21322a547feSLi Feng Wang 		aStoreProperties[1].Value = sFilter;
21422a547feSLi Feng Wang 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
21522a547feSLi Feng Wang 				XStorable.class, presentationDocument);
21622a547feSLi Feng Wang 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
21722a547feSLi Feng Wang 
21822a547feSLi Feng Wang 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
21922a547feSLi Feng Wang 				unoApp.loadDocument(filePath));
22022a547feSLi Feng Wang 	}
22122a547feSLi Feng Wang }
222