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