1 package testlib.uno;
2 
3 /**************************************************************
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements.  See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership.  The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License.  You may obtain a copy of the License at
12  *
13  *   http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied.  See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  *
22  *************************************************************/
23 
24 // __________ Imports __________
25 
26 import com.sun.star.uno.Exception;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.lang.WrappedTargetException;
29 import com.sun.star.lang.XComponent;
30 import com.sun.star.lang.XMultiServiceFactory;
31 
32 import com.sun.star.awt.Point;
33 import com.sun.star.awt.Size;
34 
35 import com.sun.star.beans.XPropertySet;
36 
37 import com.sun.star.container.NoSuchElementException;
38 import com.sun.star.container.XEnumeration;
39 import com.sun.star.container.XEnumerationAccess;
40 import com.sun.star.drawing.XDrawPage;
41 import com.sun.star.drawing.XDrawPages;
42 import com.sun.star.drawing.XDrawPagesSupplier;
43 import com.sun.star.drawing.XShape;
44 import com.sun.star.drawing.XShapes;
45 
46 import com.sun.star.text.ControlCharacter;
47 import com.sun.star.text.XText;
48 import com.sun.star.text.XTextCursor;
49 import com.sun.star.text.XTextContent;
50 import com.sun.star.text.XTextRange;
51 
52 public class ShapeUtil {
53 	// __________ static helper methods __________
54 	//
createAndInsertShape(XComponent xDrawDoc, XShapes xShapes, Point aPos, Size aSize, String sShapeType)55 	public static XPropertySet createAndInsertShape(XComponent xDrawDoc,
56 			XShapes xShapes, Point aPos, Size aSize, String sShapeType)
57 			throws java.lang.Exception {
58 		XShape xShape = createShape(xDrawDoc, aPos, aSize, sShapeType);
59 		xShapes.add(xShape);
60 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
61 				XPropertySet.class, xShape);
62 		return xPropSet;
63 	}
64 
65 	/**
66 	 * create a Shape
67 	 */
createShape(XComponent xDrawDoc, Point aPos, Size aSize, String sShapeType)68 	public static XShape createShape(XComponent xDrawDoc, Point aPos,
69 			Size aSize, String sShapeType) throws java.lang.Exception {
70 		XShape xShape = null;
71 		XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
72 				.queryInterface(XMultiServiceFactory.class, xDrawDoc);
73 		Object xObj = xFactory.createInstance(sShapeType);
74 		xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
75 		xShape.setPosition(aPos);
76 		xShape.setSize(aSize);
77 		return xShape;
78 	}
79 
80 
81 	/**
82 	 * add text to a shape. the return value is the PropertySet of the text
83 	 * range that has been added
84 	 */
addPortion(XShape xShape, String sText, boolean bNewParagraph)85 	public static XPropertySet addPortion(XShape xShape, String sText,
86 			boolean bNewParagraph)
87 			throws com.sun.star.lang.IllegalArgumentException {
88 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
89 
90 		XTextCursor xTextCursor = xText.createTextCursor();
91 		xTextCursor.gotoEnd(false);
92 		if (bNewParagraph == true) {
93 			xText.insertControlCharacter(xTextCursor,
94 					ControlCharacter.PARAGRAPH_BREAK, false);
95 			xTextCursor.gotoEnd(false);
96 		}
97 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
98 				XTextRange.class, xTextCursor);
99 		xTextRange.setString(sText);
100 		xTextCursor.gotoEnd(true);
101 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
102 				XPropertySet.class, xTextRange);
103 		return xPropSet;
104 	}
105 
106 	/**
107 	 * get a paragraph in a shape. the return value is the PropertySet of the text
108 	 * range that specified by the index
109 	 */
getPortion(XShape xShape, int index)110 	public static XPropertySet getPortion(XShape xShape, int index) throws NoSuchElementException, WrappedTargetException {
111 		XEnumerationAccess m_paraAccess = (XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, xShape);
112 		XEnumeration xParaEnum = m_paraAccess.createEnumeration();
113 		XPropertySet xPropSet = null;
114 		int i=0;
115 		while(xParaEnum.hasMoreElements())
116 		{
117 			if(i == index)
118 			{
119 				Object aPortionObj = xParaEnum.nextElement();
120 				XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, aPortionObj);
121 //				System.out.println(xTextRange.getText().getString());
122 				xPropSet = (XPropertySet) UnoRuntime.queryInterface(
123 						XPropertySet.class, xTextRange);
124 				break;
125 			}
126 			else i++;
127 		}
128 		return xPropSet;
129 	}
130 
131 
132 	/**
133 	 * try to get text of a shape
134 	 *
135 	 * @return
136 	 */
getPortion(XShape xShape)137 	public static String getPortion(XShape xShape) {
138 		String text = null;
139 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
140 
141 		XTextCursor xTextCursor = xText.createTextCursor();
142 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
143 				XTextRange.class, xTextCursor);
144 		text = xTextRange.getString();
145 		return text;
146 
147 	}
148 
setPropertyForLastParagraph(XShape xText, String sPropName, Object aValue)149 	public static void setPropertyForLastParagraph(XShape xText,
150 			String sPropName, Object aValue)
151 			throws com.sun.star.beans.UnknownPropertyException,
152 			com.sun.star.beans.PropertyVetoException,
153 			com.sun.star.lang.IllegalArgumentException,
154 			com.sun.star.lang.WrappedTargetException,
155 			com.sun.star.container.NoSuchElementException {
156 		XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime
157 				.queryInterface(XEnumerationAccess.class, xText);
158 		if (xEnumerationAccess.hasElements()) {
159 			XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
160 			while (xEnumeration.hasMoreElements()) {
161 				Object xObj = xEnumeration.nextElement();
162 				if (xEnumeration.hasMoreElements() == false) {
163 					XTextContent xTextContent = (XTextContent) UnoRuntime
164 							.queryInterface(XTextContent.class, xObj);
165 					XPropertySet xParaPropSet = (XPropertySet) UnoRuntime
166 							.queryInterface(XPropertySet.class, xTextContent);
167 					xParaPropSet.setPropertyValue(sPropName, aValue);
168 				}
169 			}
170 		}
171 	}
172 	/**
173 	 * Get shapes in specific page
174 	 * @param impressDocument
175 	 * @param pageIndex
176 	 * @return
177 	 * @throws Exception
178 	 */
getShapes(XComponent impressDocument, int pageIndex)179 	public static XShapes getShapes(XComponent impressDocument, int pageIndex) throws Exception{
180 
181 		XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
182 				XDrawPagesSupplier.class, impressDocument);
183 		XDrawPages drawpages = drawsupplier.getDrawPages();
184 		XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex));
185 		XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
186 		return xShapes;
187 
188 	}
189 
190 	/**
191 	 * Remove the specific shape in specific page
192 	 * @param impressDocument
193 	 * @param pageIndex
194 	 * @param shapeIndex
195 	 * @throws Exception
196 	 */
removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex)197 	public static void removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex) throws Exception{
198 		XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
199 				XDrawPagesSupplier.class, impressDocument);
200 		XDrawPages drawpages = drawsupplier.getDrawPages();
201 		XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex));
202 		XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
203 		XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
204 		xShapes.remove(xShape);
205 
206 	}
207 }
208