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.XComponent;
29 import com.sun.star.lang.XMultiServiceFactory;
30 
31 import com.sun.star.awt.Point;
32 import com.sun.star.awt.Size;
33 
34 import com.sun.star.beans.XPropertySet;
35 
36 import com.sun.star.container.XEnumeration;
37 import com.sun.star.container.XEnumerationAccess;
38 
39 import com.sun.star.drawing.XShape;
40 import com.sun.star.drawing.XShapes;
41 
42 import com.sun.star.text.ControlCharacter;
43 import com.sun.star.text.XText;
44 import com.sun.star.text.XTextCursor;
45 import com.sun.star.text.XTextContent;
46 import com.sun.star.text.XTextRange;
47 
48 public class ShapeUtil {
49 	// __________ static helper methods __________
50 	//
51 	public static XPropertySet createAndInsertShape(XComponent xDrawDoc,
52 			XShapes xShapes, Point aPos, Size aSize, String sShapeType)
53 			throws java.lang.Exception {
54 		XShape xShape = createShape(xDrawDoc, aPos, aSize, sShapeType);
55 		xShapes.add(xShape);
56 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
57 				XPropertySet.class, xShape);
58 		return xPropSet;
59 	}
60 
61 	/**
62 	 * create a Shape
63 	 */
64 	public static XShape createShape(XComponent xDrawDoc, Point aPos,
65 			Size aSize, String sShapeType) throws java.lang.Exception {
66 		XShape xShape = null;
67 		XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
68 				.queryInterface(XMultiServiceFactory.class, xDrawDoc);
69 		Object xObj = xFactory.createInstance(sShapeType);
70 		xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
71 		xShape.setPosition(aPos);
72 		xShape.setSize(aSize);
73 		return xShape;
74 	}
75 
76 	/**
77 	 * try to get shape according position
78 	 *
79 	 * @param aPos
80 	 * @return
81 	 */
82 	public static XShape getShape(XComponent xDrawDoc, Point aPos,
83 			String sShapeType) {
84 		XShape xShape = null;
85 		try {
86 			XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime
87 					.queryInterface(XMultiServiceFactory.class, xDrawDoc);
88 			Object xObj = xFactory.createInstance(sShapeType);
89 			xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj);
90 		} catch (Exception e) {
91 			// TODO Auto-generated catch block
92 			e.printStackTrace();
93 		}
94 		return xShape;
95 	}
96 
97 	/**
98 	 * add text to a shape. the return value is the PropertySet of the text
99 	 * range that has been added
100 	 */
101 	public static XPropertySet addPortion(XShape xShape, String sText,
102 			boolean bNewParagraph)
103 			throws com.sun.star.lang.IllegalArgumentException {
104 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
105 
106 		XTextCursor xTextCursor = xText.createTextCursor();
107 		xTextCursor.gotoEnd(false);
108 		if (bNewParagraph == true) {
109 			xText.insertControlCharacter(xTextCursor,
110 					ControlCharacter.PARAGRAPH_BREAK, false);
111 			xTextCursor.gotoEnd(false);
112 		}
113 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
114 				XTextRange.class, xTextCursor);
115 		xTextRange.setString(sText);
116 		xTextCursor.gotoEnd(true);
117 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
118 				XPropertySet.class, xTextRange);
119 		return xPropSet;
120 	}
121 
122 	/**
123 	 * try to get text of a shape
124 	 *
125 	 * @return
126 	 */
127 	public static String getPortion(XShape xShape) {
128 		String text = null;
129 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
130 
131 		XTextCursor xTextCursor = xText.createTextCursor();
132 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
133 				XTextRange.class, xTextCursor);
134 		text = xTextRange.getString();
135 		return text;
136 
137 	}
138 
139 	public static void setPropertyForLastParagraph(XShape xText,
140 			String sPropName, Object aValue)
141 			throws com.sun.star.beans.UnknownPropertyException,
142 			com.sun.star.beans.PropertyVetoException,
143 			com.sun.star.lang.IllegalArgumentException,
144 			com.sun.star.lang.WrappedTargetException,
145 			com.sun.star.container.NoSuchElementException {
146 		XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime
147 				.queryInterface(XEnumerationAccess.class, xText);
148 		if (xEnumerationAccess.hasElements()) {
149 			XEnumeration xEnumeration = xEnumerationAccess.createEnumeration();
150 			while (xEnumeration.hasMoreElements()) {
151 				Object xObj = xEnumeration.nextElement();
152 				if (xEnumeration.hasMoreElements() == false) {
153 					XTextContent xTextContent = (XTextContent) UnoRuntime
154 							.queryInterface(XTextContent.class, xObj);
155 					XPropertySet xParaPropSet = (XPropertySet) UnoRuntime
156 							.queryInterface(XPropertySet.class, xTextContent);
157 					xParaPropSet.setPropertyValue(sPropName, aValue);
158 				}
159 			}
160 		}
161 	}
162 }
163