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 import static org.junit.Assert.*; 23 import static testlib.uno.GraphicUtil.getSizePixelOfGraphicFile; 24 25 import org.junit.After; 26 import org.junit.AfterClass; 27 import org.junit.Before; 28 import org.junit.BeforeClass; 29 import org.junit.Test; 30 import org.openoffice.test.uno.UnoApp; 31 import org.openoffice.test.common.FileUtil; 32 import org.openoffice.test.common.Testspace; 33 34 import com.sun.star.beans.Property; 35 import com.sun.star.beans.PropertyValue; 36 import com.sun.star.beans.UnknownPropertyException; 37 import com.sun.star.beans.XPropertySet; 38 import com.sun.star.beans.XPropertySetInfo; 39 import com.sun.star.drawing.XDrawPage; 40 import com.sun.star.drawing.XShape; 41 import com.sun.star.drawing.XShapes; 42 import com.sun.star.graphic.XGraphic; 43 import com.sun.star.graphic.XGraphicObject; 44 import com.sun.star.graphic.XGraphicProvider; 45 import com.sun.star.lang.WrappedTargetException; 46 import com.sun.star.lang.XComponent; 47 import com.sun.star.lang.XMultiServiceFactory; 48 import com.sun.star.lang.XServiceName; 49 import com.sun.star.uno.Exception; 50 import com.sun.star.uno.UnoRuntime; 51 import com.sun.star.awt.Point; 52 import com.sun.star.awt.Size; 53 //import com.sun.star.uno.AnyConverter; 54 55 public class GraphicUtil { 56 getUniqueIDbyXGraphic(UnoApp unoApp, XGraphic xgraphic)57 public static String getUniqueIDbyXGraphic(UnoApp unoApp, XGraphic xgraphic) throws Exception{ 58 Object graphicObj = unoApp.getServiceFactory().createInstance("com.sun.star.graphic.GraphicObject"); 59 XGraphicObject xgraphicObj = (XGraphicObject)UnoRuntime.queryInterface(XGraphicObject.class, graphicObj); 60 xgraphicObj.setGraphic(xgraphic); 61 62 return xgraphicObj.getUniqueID(); 63 } 64 65 // String sUrl = "file:///F:/work/36.gif"; getUniqueIDOfGraphicFile(UnoApp unoApp, String sUrl)66 public static String getUniqueIDOfGraphicFile(UnoApp unoApp, String sUrl) throws Exception{ 67 XGraphic xgraphic = getXGraphicOfGraphicFile(unoApp,sUrl); 68 String uniqueID = getUniqueIDbyXGraphic(unoApp, xgraphic); 69 70 return uniqueID; 71 } 72 getXGraphicOfGraphicFile(UnoApp unoApp, String sUrl)73 public static XGraphic getXGraphicOfGraphicFile(UnoApp unoApp, String sUrl) throws Exception 74 { 75 Object graphicObj = unoApp.getServiceFactory().createInstance("com.sun.star.graphic.GraphicProvider"); 76 XGraphicProvider xgraphicProvider = (XGraphicProvider)UnoRuntime.queryInterface(XGraphicProvider.class, graphicObj); 77 PropertyValue[] sourceProps = new PropertyValue[1]; 78 sourceProps[0] = new PropertyValue(); 79 sourceProps[0].Name = "URL"; 80 sourceProps[0].Value = sUrl; 81 return xgraphicProvider.queryGraphic(sourceProps); 82 } 83 getSize100thMMOfGraphicFile(UnoApp unoApp, String sUrl)84 public static Size getSize100thMMOfGraphicFile(UnoApp unoApp, String sUrl) throws Exception 85 { 86 Object graphicObj = unoApp.getServiceFactory().createInstance("com.sun.star.graphic.GraphicProvider"); 87 XGraphicProvider xgraphicProvider = (XGraphicProvider)UnoRuntime.queryInterface(XGraphicProvider.class, graphicObj); 88 PropertyValue[] sourceProps = new PropertyValue[1]; 89 sourceProps[0] = new PropertyValue(); 90 sourceProps[0].Name = "URL"; 91 sourceProps[0].Value = sUrl; 92 XPropertySet xGraphicPro = xgraphicProvider.queryGraphicDescriptor(sourceProps); 93 Size size = (Size)xGraphicPro.getPropertyValue("Size100thMM"); 94 return size; 95 } 96 getSizePixelOfGraphicFile(UnoApp unoApp, String sUrl)97 public static Size getSizePixelOfGraphicFile(UnoApp unoApp, String sUrl) throws Exception 98 { 99 Object graphicObj = unoApp.getServiceFactory().createInstance("com.sun.star.graphic.GraphicProvider"); 100 XGraphicProvider xgraphicProvider = (XGraphicProvider)UnoRuntime.queryInterface(XGraphicProvider.class, graphicObj); 101 PropertyValue[] sourceProps = new PropertyValue[1]; 102 sourceProps[0] = new PropertyValue(); 103 sourceProps[0].Name = "URL"; 104 sourceProps[0].Value = sUrl; 105 XPropertySet xGraphicPro = xgraphicProvider.queryGraphicDescriptor(sourceProps); 106 Size size = (Size)xGraphicPro.getPropertyValue("SizePixel"); 107 return size; 108 } 109 110 111 //GraphicObjectShape getGraphicsOfPage(XDrawPage xDrawPage)112 public static Object[] getGraphicsOfPage(XDrawPage xDrawPage) throws Exception { 113 XShapes m_xdrawShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xDrawPage); 114 int count = m_xdrawShapes.getCount(); 115 Object[] temp = new Object[count]; 116 int graphicNum=0; 117 for(int i=0;i<count; i++) 118 { 119 Object shape = m_xdrawShapes.getByIndex(i); 120 XShape xshape = (XShape)UnoRuntime.queryInterface(XShape.class, shape); 121 String type = xshape.getShapeType(); 122 if(type.equals("com.sun.star.drawing.GraphicObjectShape")) 123 { 124 temp[graphicNum] = shape; 125 graphicNum++; 126 } 127 } 128 129 Object[] graphics = new Object[graphicNum]; 130 System.arraycopy(temp, 0, graphics, 0, graphicNum); 131 return graphics; 132 } 133 134 /*Insert a graphic into a Impress 135 * component: the Impress document 136 * toPage: the page that the graphic will be inserted to 137 * graphicURL: the file path of the graphic, e.g. file:///c:/test.png 138 * size: size of the graphic to be inserted 139 * position: position of the graphic to be inserted 140 * */ insertGraphic(XComponent component, XDrawPage toPage, String graphicURL, Size size, Point position)141 public static void insertGraphic(XComponent component, XDrawPage toPage, String graphicURL, Size size, Point position) throws Exception 142 { 143 XMultiServiceFactory xDrawFactory = 144 (XMultiServiceFactory)UnoRuntime.queryInterface( 145 XMultiServiceFactory.class, component); 146 147 Object oGraphic = xDrawFactory.createInstance("com.sun.star.drawing.GraphicObjectShape"); 148 XPropertySet xGraphicPro = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, oGraphic); 149 xGraphicPro.setPropertyValue("GraphicURL", graphicURL); 150 151 XShape xDrawShape = (XShape)UnoRuntime.queryInterface(XShape.class, oGraphic); 152 153 xDrawShape.setSize(size); 154 xDrawShape.setPosition(position); 155 156 toPage.add(xDrawShape); 157 } 158 159 } 160