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 testlib.uno;
22 
23 import java.util.HashMap;
24 
25 import org.openoffice.test.common.Testspace;
26 
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.container.XIndexAccess;
29 import com.sun.star.drawing.XDrawPage;
30 import com.sun.star.drawing.XDrawPagesSupplier;
31 import com.sun.star.drawing.XShape;
32 import com.sun.star.drawing.XShapes;
33 import com.sun.star.frame.XStorable;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.uno.Exception;
36 import com.sun.star.uno.UnoRuntime;
37 
38 /**
39  *
40  *
41  */
42 public class SDUtil {
43 
44 	private static HashMap filterName = new HashMap();
45 
SDUtil()46 	private SDUtil() {
47 
48 	}
49 
getPageByIndex(XComponent doc, int index)50 	public static Object getPageByIndex(XComponent doc, int index) throws Exception {
51 		XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, doc);
52 		Object drawPages = xDrawPagesSupplier.getDrawPages();
53 		XIndexAccess xIndexedDrawPages = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, drawPages);
54 		return xIndexedDrawPages.getByIndex(index);
55 	}
56 
getShapeOfPageByIndex(Object page, int index)57 	public static Object getShapeOfPageByIndex(Object page, int index) throws Exception {
58 		XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, page);
59 		XShapes m_xdrawShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
60 		return m_xdrawShapes.getByIndex(index);
61 	}
62 
getShapesOfPageByType(XDrawPage xDrawPage, String shapeType)63 	public static Object[] getShapesOfPageByType(XDrawPage xDrawPage, String shapeType) throws Exception {
64 		XShapes m_xdrawShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
65 		int count = m_xdrawShapes.getCount();
66 		Object[] temp = new Object[count];
67 		int shapeNum=0;
68 		for(int i=0;i<count; i++)
69 		{
70 			Object shape = m_xdrawShapes.getByIndex(i);
71 			XShape xshape = (XShape)UnoRuntime.queryInterface(XShape.class, shape);
72 			String type = xshape.getShapeType();
73 			if(type.equals(shapeType))
74 			{
75 				temp[shapeNum] = shape;
76 				shapeNum++;
77 			}
78 		}
79 
80 		Object[] shapes = new Object[shapeNum];
81 		System.arraycopy(temp, 0, shapes, 0, shapeNum);
82 		return shapes;
83 	}
84 
saveFileAs(XComponent sdComponent, String fileName, String extName)85 	public static void saveFileAs(XComponent sdComponent, String fileName, String extName) throws Exception {
86 
87 		initFilterName();
88 
89 		String storeUrl = Testspace.getUrl("temp/" + fileName + "." + extName);
90 
91 		PropertyValue[] storeProps = new PropertyValue[2];
92 		storeProps[0] = new PropertyValue();
93 		storeProps[0].Name = "FilterName";
94 		storeProps[0].Value = filterName.get(extName);
95 		storeProps[1] = new PropertyValue();
96 		storeProps[1].Name = "Overwrite";
97 		storeProps[1].Value = new Boolean(true);
98 
99 		XStorable sdStorable =
100 				(XStorable) UnoRuntime.queryInterface(XStorable.class, sdComponent);
101 		sdStorable.storeAsURL(storeUrl, storeProps);
102 	}
103 
initFilterName()104 	private static void initFilterName() throws Exception {
105 		if (filterName.size() > 0) {
106 			return;
107 		}
108 
109 		filterName.put("odp", "impress8");
110 		filterName.put("ppt", "MS PowerPoint 97");
111 	}
112 
113 }
114