1645d376bSLiu Zhe package testlib.uno; 2645d376bSLiu Zhe 3645d376bSLiu Zhe /************************************************************** 4645d376bSLiu Zhe * 5645d376bSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 6645d376bSLiu Zhe * or more contributor license agreements. See the NOTICE file 7645d376bSLiu Zhe * distributed with this work for additional information 8645d376bSLiu Zhe * regarding copyright ownership. The ASF licenses this file 9645d376bSLiu Zhe * to you under the Apache License, Version 2.0 (the 10645d376bSLiu Zhe * "License"); you may not use this file except in compliance 11645d376bSLiu Zhe * with the License. You may obtain a copy of the License at 12645d376bSLiu Zhe * 13645d376bSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 14645d376bSLiu Zhe * 15645d376bSLiu Zhe * Unless required by applicable law or agreed to in writing, 16645d376bSLiu Zhe * software distributed under the License is distributed on an 17645d376bSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18645d376bSLiu Zhe * KIND, either express or implied. See the License for the 19645d376bSLiu Zhe * specific language governing permissions and limitations 20645d376bSLiu Zhe * under the License. 21645d376bSLiu Zhe * 22645d376bSLiu Zhe *************************************************************/ 23645d376bSLiu Zhe 24645d376bSLiu Zhe // __________ Imports __________ 25645d376bSLiu Zhe 26645d376bSLiu Zhe import com.sun.star.uno.Exception; 27645d376bSLiu Zhe import com.sun.star.uno.UnoRuntime; 283908dc91SLiu Zhe import com.sun.star.lang.WrappedTargetException; 29645d376bSLiu Zhe import com.sun.star.lang.XComponent; 30645d376bSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 31645d376bSLiu Zhe 32645d376bSLiu Zhe import com.sun.star.awt.Point; 33645d376bSLiu Zhe import com.sun.star.awt.Size; 34645d376bSLiu Zhe 35645d376bSLiu Zhe import com.sun.star.beans.XPropertySet; 36645d376bSLiu Zhe 373908dc91SLiu Zhe import com.sun.star.container.NoSuchElementException; 38645d376bSLiu Zhe import com.sun.star.container.XEnumeration; 39645d376bSLiu Zhe import com.sun.star.container.XEnumerationAccess; 40*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPage; 41*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPages; 42*85b88695SLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier; 43645d376bSLiu Zhe import com.sun.star.drawing.XShape; 44645d376bSLiu Zhe import com.sun.star.drawing.XShapes; 45645d376bSLiu Zhe 46645d376bSLiu Zhe import com.sun.star.text.ControlCharacter; 47645d376bSLiu Zhe import com.sun.star.text.XText; 48645d376bSLiu Zhe import com.sun.star.text.XTextCursor; 49645d376bSLiu Zhe import com.sun.star.text.XTextContent; 50645d376bSLiu Zhe import com.sun.star.text.XTextRange; 51645d376bSLiu Zhe 52645d376bSLiu Zhe public class ShapeUtil { 53645d376bSLiu Zhe // __________ static helper methods __________ 54645d376bSLiu Zhe // createAndInsertShape(XComponent xDrawDoc, XShapes xShapes, Point aPos, Size aSize, String sShapeType)55645d376bSLiu Zhe public static XPropertySet createAndInsertShape(XComponent xDrawDoc, 56645d376bSLiu Zhe XShapes xShapes, Point aPos, Size aSize, String sShapeType) 57645d376bSLiu Zhe throws java.lang.Exception { 58645d376bSLiu Zhe XShape xShape = createShape(xDrawDoc, aPos, aSize, sShapeType); 59645d376bSLiu Zhe xShapes.add(xShape); 60645d376bSLiu Zhe XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( 61645d376bSLiu Zhe XPropertySet.class, xShape); 62645d376bSLiu Zhe return xPropSet; 63645d376bSLiu Zhe } 64645d376bSLiu Zhe 65645d376bSLiu Zhe /** 66645d376bSLiu Zhe * create a Shape 67645d376bSLiu Zhe */ createShape(XComponent xDrawDoc, Point aPos, Size aSize, String sShapeType)68645d376bSLiu Zhe public static XShape createShape(XComponent xDrawDoc, Point aPos, 69645d376bSLiu Zhe Size aSize, String sShapeType) throws java.lang.Exception { 70645d376bSLiu Zhe XShape xShape = null; 71645d376bSLiu Zhe XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime 72645d376bSLiu Zhe .queryInterface(XMultiServiceFactory.class, xDrawDoc); 73645d376bSLiu Zhe Object xObj = xFactory.createInstance(sShapeType); 74645d376bSLiu Zhe xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj); 75645d376bSLiu Zhe xShape.setPosition(aPos); 76645d376bSLiu Zhe xShape.setSize(aSize); 77645d376bSLiu Zhe return xShape; 78645d376bSLiu Zhe } 79645d376bSLiu Zhe 80645d376bSLiu Zhe 81645d376bSLiu Zhe /** 82645d376bSLiu Zhe * add text to a shape. the return value is the PropertySet of the text 83645d376bSLiu Zhe * range that has been added 84645d376bSLiu Zhe */ addPortion(XShape xShape, String sText, boolean bNewParagraph)85645d376bSLiu Zhe public static XPropertySet addPortion(XShape xShape, String sText, 86645d376bSLiu Zhe boolean bNewParagraph) 87645d376bSLiu Zhe throws com.sun.star.lang.IllegalArgumentException { 88645d376bSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape); 89645d376bSLiu Zhe 90645d376bSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 91645d376bSLiu Zhe xTextCursor.gotoEnd(false); 92645d376bSLiu Zhe if (bNewParagraph == true) { 93645d376bSLiu Zhe xText.insertControlCharacter(xTextCursor, 94645d376bSLiu Zhe ControlCharacter.PARAGRAPH_BREAK, false); 95645d376bSLiu Zhe xTextCursor.gotoEnd(false); 96645d376bSLiu Zhe } 97645d376bSLiu Zhe XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface( 98645d376bSLiu Zhe XTextRange.class, xTextCursor); 99645d376bSLiu Zhe xTextRange.setString(sText); 100645d376bSLiu Zhe xTextCursor.gotoEnd(true); 101645d376bSLiu Zhe XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( 102645d376bSLiu Zhe XPropertySet.class, xTextRange); 103645d376bSLiu Zhe return xPropSet; 104645d376bSLiu Zhe } 1053908dc91SLiu Zhe 1063908dc91SLiu Zhe /** 1073908dc91SLiu Zhe * get a paragraph in a shape. the return value is the PropertySet of the text 1083908dc91SLiu Zhe * range that specified by the index 1093908dc91SLiu Zhe */ getPortion(XShape xShape, int index)1103908dc91SLiu Zhe public static XPropertySet getPortion(XShape xShape, int index) throws NoSuchElementException, WrappedTargetException { 1113908dc91SLiu Zhe XEnumerationAccess m_paraAccess = (XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, xShape); 1123908dc91SLiu Zhe XEnumeration xParaEnum = m_paraAccess.createEnumeration(); 1133908dc91SLiu Zhe XPropertySet xPropSet = null; 1143908dc91SLiu Zhe int i=0; 1153908dc91SLiu Zhe while(xParaEnum.hasMoreElements()) 1163908dc91SLiu Zhe { 1173908dc91SLiu Zhe if(i == index) 1183908dc91SLiu Zhe { 1193908dc91SLiu Zhe Object aPortionObj = xParaEnum.nextElement(); 1203908dc91SLiu Zhe XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, aPortionObj); 1213908dc91SLiu Zhe // System.out.println(xTextRange.getText().getString()); 1223908dc91SLiu Zhe xPropSet = (XPropertySet) UnoRuntime.queryInterface( 1233908dc91SLiu Zhe XPropertySet.class, xTextRange); 1243908dc91SLiu Zhe break; 1253908dc91SLiu Zhe } 1263908dc91SLiu Zhe else i++; 1273908dc91SLiu Zhe } 1283908dc91SLiu Zhe return xPropSet; 1293908dc91SLiu Zhe } 1303908dc91SLiu Zhe 131645d376bSLiu Zhe 132645d376bSLiu Zhe /** 133645d376bSLiu Zhe * try to get text of a shape 134645d376bSLiu Zhe * 135645d376bSLiu Zhe * @return 136645d376bSLiu Zhe */ getPortion(XShape xShape)137645d376bSLiu Zhe public static String getPortion(XShape xShape) { 138645d376bSLiu Zhe String text = null; 139645d376bSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape); 140645d376bSLiu Zhe 141645d376bSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 142645d376bSLiu Zhe XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface( 143645d376bSLiu Zhe XTextRange.class, xTextCursor); 144645d376bSLiu Zhe text = xTextRange.getString(); 145645d376bSLiu Zhe return text; 146645d376bSLiu Zhe 147645d376bSLiu Zhe } 148645d376bSLiu Zhe setPropertyForLastParagraph(XShape xText, String sPropName, Object aValue)149645d376bSLiu Zhe public static void setPropertyForLastParagraph(XShape xText, 150645d376bSLiu Zhe String sPropName, Object aValue) 151645d376bSLiu Zhe throws com.sun.star.beans.UnknownPropertyException, 152645d376bSLiu Zhe com.sun.star.beans.PropertyVetoException, 153645d376bSLiu Zhe com.sun.star.lang.IllegalArgumentException, 154645d376bSLiu Zhe com.sun.star.lang.WrappedTargetException, 155645d376bSLiu Zhe com.sun.star.container.NoSuchElementException { 156645d376bSLiu Zhe XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime 157645d376bSLiu Zhe .queryInterface(XEnumerationAccess.class, xText); 158645d376bSLiu Zhe if (xEnumerationAccess.hasElements()) { 159645d376bSLiu Zhe XEnumeration xEnumeration = xEnumerationAccess.createEnumeration(); 160645d376bSLiu Zhe while (xEnumeration.hasMoreElements()) { 161645d376bSLiu Zhe Object xObj = xEnumeration.nextElement(); 162645d376bSLiu Zhe if (xEnumeration.hasMoreElements() == false) { 163645d376bSLiu Zhe XTextContent xTextContent = (XTextContent) UnoRuntime 164645d376bSLiu Zhe .queryInterface(XTextContent.class, xObj); 165645d376bSLiu Zhe XPropertySet xParaPropSet = (XPropertySet) UnoRuntime 166645d376bSLiu Zhe .queryInterface(XPropertySet.class, xTextContent); 167645d376bSLiu Zhe xParaPropSet.setPropertyValue(sPropName, aValue); 168645d376bSLiu Zhe } 169645d376bSLiu Zhe } 170645d376bSLiu Zhe } 171645d376bSLiu Zhe } 172*85b88695SLiu Zhe /** 173*85b88695SLiu Zhe * Get shapes in specific page 174*85b88695SLiu Zhe * @param impressDocument 175*85b88695SLiu Zhe * @param pageIndex 176*85b88695SLiu Zhe * @return 177*85b88695SLiu Zhe * @throws Exception 178*85b88695SLiu Zhe */ getShapes(XComponent impressDocument, int pageIndex)179*85b88695SLiu Zhe public static XShapes getShapes(XComponent impressDocument, int pageIndex) throws Exception{ 180*85b88695SLiu Zhe 181*85b88695SLiu Zhe XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface( 182*85b88695SLiu Zhe XDrawPagesSupplier.class, impressDocument); 183*85b88695SLiu Zhe XDrawPages drawpages = drawsupplier.getDrawPages(); 184*85b88695SLiu Zhe XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex)); 185*85b88695SLiu Zhe XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage); 186*85b88695SLiu Zhe return xShapes; 187*85b88695SLiu Zhe 188*85b88695SLiu Zhe } 189*85b88695SLiu Zhe 190*85b88695SLiu Zhe /** 191*85b88695SLiu Zhe * Remove the specific shape in specific page 192*85b88695SLiu Zhe * @param impressDocument 193*85b88695SLiu Zhe * @param pageIndex 194*85b88695SLiu Zhe * @param shapeIndex 195*85b88695SLiu Zhe * @throws Exception 196*85b88695SLiu Zhe */ removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex)197*85b88695SLiu Zhe public static void removeOneShape(XComponent impressDocument, int pageIndex, int shapeIndex) throws Exception{ 198*85b88695SLiu Zhe XDrawPagesSupplier drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface( 199*85b88695SLiu Zhe XDrawPagesSupplier.class, impressDocument); 200*85b88695SLiu Zhe XDrawPages drawpages = drawsupplier.getDrawPages(); 201*85b88695SLiu Zhe XDrawPage xpage=(XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawpages.getByIndex(pageIndex)); 202*85b88695SLiu Zhe XShapes xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage); 203*85b88695SLiu Zhe XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex)); 204*85b88695SLiu Zhe xShapes.remove(xShape); 205*85b88695SLiu Zhe 206*85b88695SLiu Zhe } 207645d376bSLiu Zhe } 208