View | Details | Raw Unified | Return to issue 120527
Collapse All | Expand All

(-)source/testcase/uno/sd/ShapeProperties.java (+282 lines)
Line 0 Link Here
1
/**************************************************************
2
 * 
3
 * Licensed to the Apache Software Foundation (ASF) under one
4
 * or more contributor license agreements.  See the NOTICE file
5
 * distributed with this work for additional information
6
 * regarding copyright ownership.  The ASF licenses this file
7
 * to you under the Apache License, Version 2.0 (the
8
 * "License"); you may not use this file except in compliance
9
 * with the License.  You may obtain a copy of the License at
10
 * 
11
 *   http://www.apache.org/licenses/LICENSE-2.0
12
 * 
13
 * Unless required by applicable law or agreed to in writing,
14
 * software distributed under the License is distributed on an
15
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
 * KIND, either express or implied.  See the License for the
17
 * specific language governing permissions and limitations
18
 * under the License.
19
 * 
20
 *************************************************************/
21
package testcase.uno.sd;
22
23
import java.io.File;
24
import java.io.IOException;
25
26
import org.junit.After;
27
import static org.junit.Assert.*;
28
29
import org.junit.Before;
30
import org.junit.Test;
31
import org.openoffice.test.uno.UnoApp;
32
import org.openoffice.test.common.FileUtil;
33
import org.openoffice.test.common.Testspace;
34
35
import testcase.uno.sd.utils.PageHelper;
36
import testcase.uno.sd.utils.ShapeHelper;
37
38
import com.sun.star.uno.UnoRuntime;
39
import com.sun.star.presentation.XPresentation;
40
import com.sun.star.presentation.XPresentationSupplier;
41
import com.sun.star.awt.Gradient;
42
import com.sun.star.awt.GradientStyle;
43
import com.sun.star.awt.Size;
44
import com.sun.star.awt.Point;
45
import com.sun.star.beans.PropertyValue;
46
import com.sun.star.beans.XPropertySet;
47
import com.sun.star.drawing.FillStyle;
48
import com.sun.star.drawing.Hatch;
49
import com.sun.star.drawing.HatchStyle;
50
import com.sun.star.drawing.XDrawPage;
51
import com.sun.star.drawing.XDrawPages;
52
import com.sun.star.drawing.XDrawPagesSupplier;
53
import com.sun.star.drawing.XShape;
54
import com.sun.star.drawing.XShapes;
55
import com.sun.star.frame.XComponentLoader;
56
import com.sun.star.frame.XStorable;
57
import com.sun.star.lang.XComponent;
58
import com.sun.star.lang.XMultiServiceFactory;
59
60
public class ShapeProperties {
61
	UnoApp unoApp = new UnoApp();
62
	XPresentationSupplier sdDocument = null;
63
	XPresentation pre = null;
64
	XComponent precomp = null;
65
	XComponent impressDocument = null;
66
	XComponent reLoadFile = null;
67
	XDrawPagesSupplier drawsupplier = null;
68
	XDrawPages drawpages = null;
69
	XShapes xShapes = null;
70
	XDrawPage xpage = null;
71
72
	@Before
73
	public void setUp() throws Exception {
74
		unoApp.start();
75
		createDocumentAndSlide();
76
	}
77
78
	@After
79
	public void tearDown() throws Exception {
80
		unoApp.closeDocument(impressDocument);
81
		unoApp.closeDocument(reLoadFile);
82
		unoApp.close();
83
	}
84
85
	/**
86
	 * test Insert a new slide and Insert a new EllipseShape
87
	 * 
88
	 * @throws Exception
89
	 */
90
	@Test
91
	public void testInsertShape() throws Exception {
92
		Point po = new Point(1000, 8000);
93
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
94
		XShape xShape = ShapeHelper.createShape(impressDocument, po, new Size(
95
				5000, 5000), "com.sun.star.drawing.EllipseShape");
96
		xShapes.add(xShape);
97
		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
98
				XPropertySet.class, xShape);
99
		xPropSet.setPropertyValue("Name", "test");
100
		XShape shap = ShapeHelper.getShape(impressDocument, po,
101
				"com.sun.star.drawing.EllipseShape");
102
		XPropertySet xPropSet2 = (XPropertySet) UnoRuntime.queryInterface(
103
				XPropertySet.class, xShape);
104
		assertTrue("Not the same shape",
105
				(xPropSet2.getPropertyValue("Name")).equals("test"));
106
		assertTrue("Not EllopseShape",
107
				shap.getShapeType().equals("com.sun.star.drawing.EllipseShape"));
108
109
	}
110
111
	/**
112
	 * test Insert text to an EllopseShape
113
	 * 
114
	 * @throws Exception
115
	 */
116
	@Test
117
	public void testInsertTextToShape() throws Exception {
118
		Point po = new Point(1000, 8000);
119
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
120
		XShape xShape = ShapeHelper.createShape(impressDocument, po, new Size(
121
				5000, 5000), "com.sun.star.drawing.EllipseShape");
122
		xShapes.add(xShape);
123
		ShapeHelper.addPortion(xShape, "test", false);
124
		assertTrue("Not put text correctly",
125
				"test".equals(ShapeHelper.getPortion(xShape)));
126
127
	}
128
129
	/**
130
	 * test Shape fill with Gradient
131
	 * 
132
	 * @throws Exception
133
	 */
134
	@Test
135
	public void testShapeFillGradient() throws Exception {
136
		Point po = new Point(1000, 8000);
137
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
138
		XShape xShape = ShapeHelper.createShape(impressDocument, po, new Size(
139
				5000, 5000), "com.sun.star.drawing.EllipseShape");
140
		xShapes.add(xShape);
141
		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
142
				XPropertySet.class, xShape);
143
		xPropSet.setPropertyValue("FillStyle", FillStyle.GRADIENT);
144
		Gradient aGradient = new Gradient();
145
		aGradient.Style = GradientStyle.LINEAR;
146
		aGradient.StartColor = 0x00ff00;
147
		aGradient.EndColor = 0xffff00;
148
		aGradient.Angle = 450;
149
		aGradient.Border = 0;
150
		aGradient.XOffset = 0;
151
		aGradient.YOffset = 0;
152
		aGradient.StartIntensity = 100;
153
		aGradient.EndIntensity = 100;
154
		aGradient.StepCount = 10;
155
		xPropSet.setPropertyValue("FillGradient", aGradient);
156
		// --------------------------
157
		xShape = saveAndLoadShape(po, "com.sun.star.drawing.EllipseShape");
158
		// ----------------------------
159
		assertTrue("Not Gradient Fill Style",xPropSet.getPropertyValue("FillStyle").equals(FillStyle.GRADIENT));
160
		aGradient=(Gradient) xPropSet.getPropertyValue("FillGradient");
161
		assertTrue("Not Linear Gradient",aGradient.Style.equals(GradientStyle.LINEAR));
162
		
163
	}
164
165
	/**
166
	 * test Shape fill with yellow color
167
	 * 
168
	 * @throws Exception
169
	 */
170
	@Test
171
	public void testShapeFillColor() throws Exception {
172
		Point po = new Point(1000, 8000);
173
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
174
		XShape xShape = ShapeHelper.createShape(impressDocument, po, new Size(
175
				5000, 5000), "com.sun.star.drawing.EllipseShape");
176
		xShapes.add(xShape);
177
		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
178
				XPropertySet.class, xShape);
179
		xPropSet.setPropertyValue("FillStyle", FillStyle.SOLID);
180
		xPropSet.setPropertyValue("FillColor", 0xffff00);
181
		// --------------------------
182
		xShape = saveAndLoadShape(po, "com.sun.star.drawing.EllipseShape");
183
		// ----------------------------------------------------
184
		assertTrue("Not Color Fill Style",
185
				xPropSet.getPropertyValue("FillStyle").equals(FillStyle.SOLID));
186
		assertTrue("Not Yellow Color Fill ",
187
				xPropSet.getPropertyValue("FillColor").equals(0xffff00));
188
	}
189
	/**
190
	 * test shape fill with Hatch Style(Pattern in MS)
191
	 * @throws Exception
192
	 */
193
	@Test
194
	public void testShapeFillHatch() throws Exception {
195
		Point po = new Point(1000, 8000);
196
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
197
		XShape xShape = ShapeHelper.createShape(impressDocument, po, new Size(
198
				5000, 5000), "com.sun.star.drawing.EllipseShape");
199
		xShapes.add(xShape);
200
		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
201
				XPropertySet.class, xShape);
202
		xPropSet.setPropertyValue("FillStyle", FillStyle.HATCH);
203
		Hatch aHatch=new Hatch();
204
		aHatch.Style=HatchStyle.DOUBLE;
205
		aHatch.Color=0x00ff00;
206
		aHatch.Distance=100;
207
		aHatch.Angle=450;
208
		xPropSet.setPropertyValue("FillHatch", aHatch);
209
		
210
		// --------------------------
211
		xShape = saveAndLoadShape(po, "com.sun.star.drawing.EllipseShape");
212
		// ----------------------------
213
		assertTrue("Not Gradient Fill Style",xPropSet.getPropertyValue("FillStyle").equals(FillStyle.HATCH));
214
		
215
		aHatch=(Hatch) xPropSet.getPropertyValue("FillHatch");
216
		assertTrue("Not Double Hatch",aHatch.Style.equals(HatchStyle.DOUBLE));
217
	}
218
	
219
	/**
220
	 * create a new presentation document and insert a new slide.
221
	 * 
222
	 * @throws Exception
223
	 */
224
	public void createDocumentAndSlide() throws Exception {
225
		impressDocument = (XComponent) UnoRuntime.queryInterface(
226
				XComponent.class, unoApp.newDocument("simpress"));
227
		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
228
				XDrawPagesSupplier.class, impressDocument);
229
		drawpages = drawsupplier.getDrawPages();
230
		drawpages.insertNewByIndex(1);
231
		xpage = PageHelper.getDrawPageByIndex(impressDocument, 1);
232
	}
233
234
	/**
235
	 * Save presentation and reLoad the presentation and shape in it.
236
	 * 
237
	 * @param po
238
	 * @param shapeType
239
	 * @return
240
	 * @throws Exception
241
	 */
242
	public XShape saveAndLoadShape(Point po, String shapeType) throws Exception {
243
		reLoadFile = saveAndReloadDoc(impressDocument,
244
				"StarOffice XML (Impress)", "odp");
245
		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
246
				XDrawPagesSupplier.class, reLoadFile);
247
		drawpages = drawsupplier.getDrawPages();
248
		xpage = PageHelper.getDrawPageByIndex(impressDocument, 1);
249
		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
250
		XShape shap = ShapeHelper.getShape(impressDocument, po,
251
				"com.sun.star.drawing.EllipseShape");
252
		return shap;
253
	}
254
255
	/**
256
	 * save and reload Presentation document.
257
	 * 
258
	 * @param presentationDocument
259
	 * @param sFilter
260
	 * @param sExtension
261
	 * @return
262
	 * @throws Exception
263
	 */
264
	private XComponent saveAndReloadDoc(XComponent presentationDocument,
265
			String sFilter, String sExtension) throws Exception {
266
		String filePath = Testspace.getPath("output/presentationtest."
267
				+ sExtension);
268
		PropertyValue[] aStoreProperties = new PropertyValue[2];
269
		aStoreProperties[0] = new PropertyValue();
270
		aStoreProperties[1] = new PropertyValue();
271
		aStoreProperties[0].Name = "Override";
272
		aStoreProperties[0].Value = true;
273
		aStoreProperties[1].Name = "FilterName";
274
		aStoreProperties[1].Value = sFilter;
275
		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
276
				XStorable.class, presentationDocument);
277
		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
278
279
		return UnoRuntime.queryInterface(XComponent.class,
280
				unoApp.loadDocument(filePath));
281
	}
282
}
(-)source/testcase/uno/sd/utils/PageHelper.java (+231 lines)
Line 0 Link Here
1
/**************************************************************
2
 * 
3
 * Licensed to the Apache Software Foundation (ASF) under one
4
 * or more contributor license agreements.  See the NOTICE file
5
 * distributed with this work for additional information
6
 * regarding copyright ownership.  The ASF licenses this file
7
 * to you under the Apache License, Version 2.0 (the
8
 * "License"); you may not use this file except in compliance
9
 * with the License.  You may obtain a copy of the License at
10
 * 
11
 *   http://www.apache.org/licenses/LICENSE-2.0
12
 * 
13
 * Unless required by applicable law or agreed to in writing,
14
 * software distributed under the License is distributed on an
15
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
 * KIND, either express or implied.  See the License for the
17
 * specific language governing permissions and limitations
18
 * under the License.
19
 * 
20
 *************************************************************/
21
package testcase.uno.sd.utils;
22
23
import com.sun.star.uno.UnoRuntime;
24
import com.sun.star.lang.XComponent;
25
import com.sun.star.lang.XServiceInfo;
26
27
import com.sun.star.awt.Size;
28
29
import com.sun.star.beans.XPropertySet;
30
31
import com.sun.star.drawing.XDrawPage;
32
import com.sun.star.drawing.XDrawPages;
33
import com.sun.star.drawing.XDrawPagesSupplier;
34
import com.sun.star.drawing.XMasterPageTarget;
35
import com.sun.star.drawing.XMasterPagesSupplier;
36
37
import com.sun.star.presentation.XPresentationPage;
38
import com.sun.star.presentation.XHandoutMasterSupplier;
39
40
public class PageHelper {
41
	/**
42
	 * Get the page count for standard pages
43
	 * 
44
	 * @param xComponent
45
	 *            : The presentation document
46
	 * @return slide count
47
	 */
48
	static public int getDrawPageCount(XComponent xComponent) {
49
		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
50
				.queryInterface(XDrawPagesSupplier.class, xComponent);
51
		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
52
		return xDrawPages.getCount();
53
	}
54
55
	/**
56
	 * Get draw page by index
57
	 * 
58
	 * @param xComponent
59
	 *            : The presentation document
60
	 * @param nIndex
61
	 *            : index of slide pages, from 0 or 1?
62
	 * @return Page of corresponding index.
63
	 */
64
	static public XDrawPage getDrawPageByIndex(XComponent xComponent, int nIndex)
65
			throws com.sun.star.lang.IndexOutOfBoundsException,
66
			com.sun.star.lang.WrappedTargetException {
67
		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
68
				.queryInterface(XDrawPagesSupplier.class, xComponent);
69
		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
70
		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
71
				xDrawPages.getByIndex(nIndex));
72
	}
73
74
	/**
75
	 * Create and insert a draw page into the giving position,the method returns
76
	 * the new created page
77
	 * 
78
	 * @param xComponent
79
	 *            :The Presentation Document
80
	 * @param nIndex
81
	 *            :The index at which page will be inserted.
82
	 * @return The newly created page.
83
	 */
84
	static public XDrawPage insertNewDrawPageByIndex(XComponent xComponent,
85
			int nIndex) throws Exception {
86
		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
87
				.queryInterface(XDrawPagesSupplier.class, xComponent);
88
		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
89
		return xDrawPages.insertNewByIndex(nIndex);
90
	}
91
92
	/**
93
	 * Remove the given page
94
	 * 
95
	 * @param xComponent
96
	 *            : The Presentation Document
97
	 * @param xDrawPage
98
	 *            : The page want to be removed.
99
	 */
100
	static public void removeDrawPage(XComponent xComponent, XDrawPage xDrawPage) {
101
		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime
102
				.queryInterface(XDrawPagesSupplier.class, xComponent);
103
		XDrawPages xDrawPages = xDrawPagesSupplier.getDrawPages();
104
		xDrawPages.remove(xDrawPage);
105
	}
106
107
	/**
108
	 * Get size of the given page
109
	 * 
110
	 * @param xDrawPage
111
	 *            : The specified target page
112
	 * @return specifies the 2-dimensional size of the page using width and
113
	 *         height.
114
	 */
115
	static public Size getPageSize(XDrawPage xDrawPage)
116
			throws com.sun.star.beans.UnknownPropertyException,
117
			com.sun.star.lang.WrappedTargetException {
118
		XPropertySet xPageProperties = (XPropertySet) UnoRuntime
119
				.queryInterface(XPropertySet.class, xDrawPage);
120
		return new Size(
121
				((Integer) xPageProperties.getPropertyValue("Width"))
122
						.intValue(),
123
				((Integer) xPageProperties.getPropertyValue("Height"))
124
						.intValue());
125
	}
126
127
	/**
128
	 * Get the page count for master pages
129
	 * 
130
	 * @param xComponent
131
	 *            : The presentation document
132
	 * @return Count of master pages.
133
	 */
134
	static public int getMasterPageCount(XComponent xComponent) {
135
		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
136
				.queryInterface(XMasterPagesSupplier.class, xComponent);
137
		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
138
		return xDrawPages.getCount();
139
	}
140
141
	/**
142
	 * Get master page by index
143
	 * 
144
	 * @param xComponent
145
	 *            : The Presentation document
146
	 * @param nIndex
147
	 *            : Index of target master page.
148
	 * @return Page of
149
	 */
150
	static public XDrawPage getMasterPageByIndex(XComponent xComponent,
151
			int nIndex) throws com.sun.star.lang.IndexOutOfBoundsException,
152
			com.sun.star.lang.WrappedTargetException {
153
		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
154
				.queryInterface(XMasterPagesSupplier.class, xComponent);
155
		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
156
		return (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class,
157
				xDrawPages.getByIndex(nIndex));
158
	}
159
160
	/**
161
	 * creates and inserts a new master page into the giving position, the
162
	 * method returns the new created page
163
	 */
164
	static public XDrawPage insertNewMasterPageByIndex(XComponent xComponent,
165
			int nIndex) {
166
		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
167
				.queryInterface(XMasterPagesSupplier.class, xComponent);
168
		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
169
		return xDrawPages.insertNewByIndex(nIndex);
170
	}
171
172
	/**
173
	 * removes the given page
174
	 */
175
	static public void removeMasterPage(XComponent xComponent,
176
			XDrawPage xDrawPage) {
177
		XMasterPagesSupplier xMasterPagesSupplier = (XMasterPagesSupplier) UnoRuntime
178
				.queryInterface(XMasterPagesSupplier.class, xComponent);
179
		XDrawPages xDrawPages = xMasterPagesSupplier.getMasterPages();
180
		xDrawPages.remove(xDrawPage);
181
	}
182
183
	/**
184
	 * return the corresponding masterpage for the giving drawpage
185
	 */
186
	static public XDrawPage getMasterPage(XDrawPage xDrawPage) {
187
		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
188
				.queryInterface(XMasterPageTarget.class, xDrawPage);
189
		return xMasterPageTarget.getMasterPage();
190
	}
191
192
	/**
193
	 * sets given masterpage at the drawpage
194
	 */
195
	static public void setMasterPage(XDrawPage xDrawPage, XDrawPage xMasterPage) {
196
		XMasterPageTarget xMasterPageTarget = (XMasterPageTarget) UnoRuntime
197
				.queryInterface(XMasterPageTarget.class, xDrawPage);
198
		xMasterPageTarget.setMasterPage(xMasterPage);
199
	}
200
201
	// __________ presentation pages __________
202
203
	/**
204
	 * test if a Presentation Document is supported. This is important, because
205
	 * only presentation documents have notes and handout pages
206
	 */
207
	static public boolean isImpressDocument(XComponent xComponent) {
208
		XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
209
				XServiceInfo.class, xComponent);
210
		return xInfo
211
				.supportsService("com.sun.star.presentation.PresentationDocument");
212
	}
213
214
	/**
215
	 * in impress documents each normal draw page has a corresponding notes page
216
	 */
217
	static public XDrawPage getNotesPage(XDrawPage xDrawPage) {
218
		XPresentationPage aPresentationPage = (XPresentationPage) UnoRuntime
219
				.queryInterface(XPresentationPage.class, xDrawPage);
220
		return aPresentationPage.getNotesPage();
221
	}
222
223
	/**
224
	 * in impress each documents has one handout page
225
	 */
226
	static public XDrawPage getHandoutMasterPage(XComponent xComponent) {
227
		XHandoutMasterSupplier aHandoutMasterSupplier = (XHandoutMasterSupplier) UnoRuntime
228
				.queryInterface(XHandoutMasterSupplier.class, xComponent);
229
		return aHandoutMasterSupplier.getHandoutMasterPage();
230
	}
231
}
(-)source/testcase/uno/sd/utils/ShapeHelper.java (+162 lines)
Line 0 Link Here
1
package testcase.uno.sd.utils;
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 ShapeHelper {
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
}

Return to issue 120527