xref: /trunk/test/testuno/source/testlib/uno/SDUtil.java (revision 42a976b737d4e2b88cfbe2fe1cd610fe957bdc84)
1e6e6073dSLiu Zhe /**************************************************************
2e6e6073dSLiu Zhe  *
3e6e6073dSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4e6e6073dSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5e6e6073dSLiu Zhe  * distributed with this work for additional information
6e6e6073dSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7e6e6073dSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8e6e6073dSLiu Zhe  * "License"); you may not use this file except in compliance
9e6e6073dSLiu Zhe  * with the License.  You may obtain a copy of the License at
10e6e6073dSLiu Zhe  *
11e6e6073dSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12e6e6073dSLiu Zhe  *
13e6e6073dSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14e6e6073dSLiu Zhe  * software distributed under the License is distributed on an
15e6e6073dSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16e6e6073dSLiu Zhe  * KIND, either express or implied.  See the License for the
17e6e6073dSLiu Zhe  * specific language governing permissions and limitations
18e6e6073dSLiu Zhe  * under the License.
19e6e6073dSLiu Zhe  *
20e6e6073dSLiu Zhe  *************************************************************/
21e6e6073dSLiu Zhe package testlib.uno;
22e6e6073dSLiu Zhe 
2343a102b2SLi Feng Wang import java.util.HashMap;
2443a102b2SLi Feng Wang 
2543a102b2SLi Feng Wang import org.openoffice.test.common.Testspace;
2643a102b2SLi Feng Wang 
2743a102b2SLi Feng Wang import com.sun.star.beans.PropertyValue;
28e6e6073dSLiu Zhe import com.sun.star.container.XIndexAccess;
29e6e6073dSLiu Zhe import com.sun.star.drawing.XDrawPage;
30e6e6073dSLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier;
31*28725c19SLi Feng Wang import com.sun.star.drawing.XShape;
32e6e6073dSLiu Zhe import com.sun.star.drawing.XShapes;
3343a102b2SLi Feng Wang import com.sun.star.frame.XStorable;
34e6e6073dSLiu Zhe import com.sun.star.lang.XComponent;
35*28725c19SLi Feng Wang import com.sun.star.uno.Exception;
36e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime;
37e6e6073dSLiu Zhe 
38e6e6073dSLiu Zhe /**
39e6e6073dSLiu Zhe  *
40e6e6073dSLiu Zhe  *
41e6e6073dSLiu Zhe  */
42e6e6073dSLiu Zhe public class SDUtil {
43e6e6073dSLiu Zhe 
4443a102b2SLi Feng Wang     private static HashMap filterName = new HashMap();
4543a102b2SLi Feng Wang 
SDUtil()46e6e6073dSLiu Zhe     private SDUtil() {
47e6e6073dSLiu Zhe 
48e6e6073dSLiu Zhe     }
49e6e6073dSLiu Zhe 
getPageByIndex(XComponent doc, int index)50e6e6073dSLiu Zhe     public static Object getPageByIndex(XComponent doc, int index) throws Exception {
51e6e6073dSLiu Zhe         XDrawPagesSupplier xDrawPagesSupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, doc);
52e6e6073dSLiu Zhe         Object drawPages = xDrawPagesSupplier.getDrawPages();
53e6e6073dSLiu Zhe         XIndexAccess xIndexedDrawPages = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, drawPages);
54e6e6073dSLiu Zhe         return xIndexedDrawPages.getByIndex(index);
55e6e6073dSLiu Zhe     }
56e6e6073dSLiu Zhe 
getShapeOfPageByIndex(Object page, int index)57e6e6073dSLiu Zhe     public static Object getShapeOfPageByIndex(Object page, int index) throws Exception {
58e6e6073dSLiu Zhe         XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, page);
59e6e6073dSLiu Zhe         XShapes m_xdrawShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
60e6e6073dSLiu Zhe         return m_xdrawShapes.getByIndex(index);
61e6e6073dSLiu Zhe     }
62e6e6073dSLiu Zhe 
getShapesOfPageByType(XDrawPage xDrawPage, String shapeType)63*28725c19SLi Feng Wang     public static Object[] getShapesOfPageByType(XDrawPage xDrawPage, String shapeType) throws Exception {
64*28725c19SLi Feng Wang         XShapes m_xdrawShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage);
65*28725c19SLi Feng Wang         int count = m_xdrawShapes.getCount();
66*28725c19SLi Feng Wang         Object[] temp = new Object[count];
67*28725c19SLi Feng Wang         int shapeNum=0;
68*28725c19SLi Feng Wang         for(int i=0;i<count; i++)
69*28725c19SLi Feng Wang         {
70*28725c19SLi Feng Wang             Object shape = m_xdrawShapes.getByIndex(i);
71*28725c19SLi Feng Wang             XShape xshape = (XShape)UnoRuntime.queryInterface(XShape.class, shape);
72*28725c19SLi Feng Wang             String type = xshape.getShapeType();
73*28725c19SLi Feng Wang             if(type.equals(shapeType))
74*28725c19SLi Feng Wang             {
75*28725c19SLi Feng Wang                 temp[shapeNum] = shape;
76*28725c19SLi Feng Wang                 shapeNum++;
77*28725c19SLi Feng Wang             }
78*28725c19SLi Feng Wang         }
79*28725c19SLi Feng Wang 
80*28725c19SLi Feng Wang         Object[] shapes = new Object[shapeNum];
81*28725c19SLi Feng Wang         System.arraycopy(temp, 0, shapes, 0, shapeNum);
82*28725c19SLi Feng Wang         return shapes;
83*28725c19SLi Feng Wang     }
84*28725c19SLi Feng Wang 
saveFileAs(XComponent sdComponent, String fileName, String extName)8543a102b2SLi Feng Wang     public static void saveFileAs(XComponent sdComponent, String fileName, String extName) throws Exception {
8643a102b2SLi Feng Wang 
8743a102b2SLi Feng Wang         initFilterName();
8843a102b2SLi Feng Wang 
8943a102b2SLi Feng Wang         String storeUrl = Testspace.getUrl("temp/" + fileName + "." + extName);
9043a102b2SLi Feng Wang 
9143a102b2SLi Feng Wang         PropertyValue[] storeProps = new PropertyValue[2];
9243a102b2SLi Feng Wang         storeProps[0] = new PropertyValue();
9343a102b2SLi Feng Wang         storeProps[0].Name = "FilterName";
9443a102b2SLi Feng Wang         storeProps[0].Value = filterName.get(extName);
9543a102b2SLi Feng Wang         storeProps[1] = new PropertyValue();
9643a102b2SLi Feng Wang         storeProps[1].Name = "Overwrite";
9743a102b2SLi Feng Wang         storeProps[1].Value = new Boolean(true);
9843a102b2SLi Feng Wang 
9943a102b2SLi Feng Wang         XStorable sdStorable =
10043a102b2SLi Feng Wang                 (XStorable) UnoRuntime.queryInterface(XStorable.class, sdComponent);
10143a102b2SLi Feng Wang         sdStorable.storeAsURL(storeUrl, storeProps);
10243a102b2SLi Feng Wang     }
10343a102b2SLi Feng Wang 
initFilterName()10443a102b2SLi Feng Wang     private static void initFilterName() throws Exception {
10543a102b2SLi Feng Wang         if (filterName.size() > 0) {
10643a102b2SLi Feng Wang             return;
10743a102b2SLi Feng Wang         }
10843a102b2SLi Feng Wang 
10943a102b2SLi Feng Wang         filterName.put("odp", "impress8");
11043a102b2SLi Feng Wang         filterName.put("ppt", "MS PowerPoint 97");
11143a102b2SLi Feng Wang     }
11243a102b2SLi Feng Wang 
113e6e6073dSLiu Zhe }
114