1*e6e6073dSLiu Zhe package testlib.uno; 2*e6e6073dSLiu Zhe 3*e6e6073dSLiu Zhe /************************************************************** 4*e6e6073dSLiu Zhe * 5*e6e6073dSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 6*e6e6073dSLiu Zhe * or more contributor license agreements. See the NOTICE file 7*e6e6073dSLiu Zhe * distributed with this work for additional information 8*e6e6073dSLiu Zhe * regarding copyright ownership. The ASF licenses this file 9*e6e6073dSLiu Zhe * to you under the Apache License, Version 2.0 (the 10*e6e6073dSLiu Zhe * "License"); you may not use this file except in compliance 11*e6e6073dSLiu Zhe * with the License. You may obtain a copy of the License at 12*e6e6073dSLiu Zhe * 13*e6e6073dSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 14*e6e6073dSLiu Zhe * 15*e6e6073dSLiu Zhe * Unless required by applicable law or agreed to in writing, 16*e6e6073dSLiu Zhe * software distributed under the License is distributed on an 17*e6e6073dSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18*e6e6073dSLiu Zhe * KIND, either express or implied. See the License for the 19*e6e6073dSLiu Zhe * specific language governing permissions and limitations 20*e6e6073dSLiu Zhe * under the License. 21*e6e6073dSLiu Zhe * 22*e6e6073dSLiu Zhe *************************************************************/ 23*e6e6073dSLiu Zhe 24*e6e6073dSLiu Zhe // __________ Imports __________ 25*e6e6073dSLiu Zhe 26*e6e6073dSLiu Zhe import com.sun.star.uno.Exception; 27*e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime; 28*e6e6073dSLiu Zhe import com.sun.star.lang.WrappedTargetException; 29*e6e6073dSLiu Zhe import com.sun.star.lang.XComponent; 30*e6e6073dSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 31*e6e6073dSLiu Zhe 32*e6e6073dSLiu Zhe import com.sun.star.awt.Point; 33*e6e6073dSLiu Zhe import com.sun.star.awt.Size; 34*e6e6073dSLiu Zhe 35*e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet; 36*e6e6073dSLiu Zhe 37*e6e6073dSLiu Zhe import com.sun.star.container.NoSuchElementException; 38*e6e6073dSLiu Zhe import com.sun.star.container.XEnumeration; 39*e6e6073dSLiu Zhe import com.sun.star.container.XEnumerationAccess; 40*e6e6073dSLiu Zhe 41*e6e6073dSLiu Zhe import com.sun.star.drawing.XShape; 42*e6e6073dSLiu Zhe import com.sun.star.drawing.XShapes; 43*e6e6073dSLiu Zhe 44*e6e6073dSLiu Zhe import com.sun.star.text.ControlCharacter; 45*e6e6073dSLiu Zhe import com.sun.star.text.XText; 46*e6e6073dSLiu Zhe import com.sun.star.text.XTextCursor; 47*e6e6073dSLiu Zhe import com.sun.star.text.XTextContent; 48*e6e6073dSLiu Zhe import com.sun.star.text.XTextRange; 49*e6e6073dSLiu Zhe 50*e6e6073dSLiu Zhe public class ShapeUtil { 51*e6e6073dSLiu Zhe // __________ static helper methods __________ 52*e6e6073dSLiu Zhe // 53*e6e6073dSLiu Zhe public static XPropertySet createAndInsertShape(XComponent xDrawDoc, 54*e6e6073dSLiu Zhe XShapes xShapes, Point aPos, Size aSize, String sShapeType) 55*e6e6073dSLiu Zhe throws java.lang.Exception { 56*e6e6073dSLiu Zhe XShape xShape = createShape(xDrawDoc, aPos, aSize, sShapeType); 57*e6e6073dSLiu Zhe xShapes.add(xShape); 58*e6e6073dSLiu Zhe XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( 59*e6e6073dSLiu Zhe XPropertySet.class, xShape); 60*e6e6073dSLiu Zhe return xPropSet; 61*e6e6073dSLiu Zhe } 62*e6e6073dSLiu Zhe 63*e6e6073dSLiu Zhe /** 64*e6e6073dSLiu Zhe * create a Shape 65*e6e6073dSLiu Zhe */ 66*e6e6073dSLiu Zhe public static XShape createShape(XComponent xDrawDoc, Point aPos, 67*e6e6073dSLiu Zhe Size aSize, String sShapeType) throws java.lang.Exception { 68*e6e6073dSLiu Zhe XShape xShape = null; 69*e6e6073dSLiu Zhe XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime 70*e6e6073dSLiu Zhe .queryInterface(XMultiServiceFactory.class, xDrawDoc); 71*e6e6073dSLiu Zhe Object xObj = xFactory.createInstance(sShapeType); 72*e6e6073dSLiu Zhe xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj); 73*e6e6073dSLiu Zhe xShape.setPosition(aPos); 74*e6e6073dSLiu Zhe xShape.setSize(aSize); 75*e6e6073dSLiu Zhe return xShape; 76*e6e6073dSLiu Zhe } 77*e6e6073dSLiu Zhe 78*e6e6073dSLiu Zhe /** 79*e6e6073dSLiu Zhe * try to get shape according position 80*e6e6073dSLiu Zhe * 81*e6e6073dSLiu Zhe * @param aPos 82*e6e6073dSLiu Zhe * @return 83*e6e6073dSLiu Zhe */ 84*e6e6073dSLiu Zhe public static XShape getShape(XComponent xDrawDoc, Point aPos, 85*e6e6073dSLiu Zhe String sShapeType) { 86*e6e6073dSLiu Zhe XShape xShape = null; 87*e6e6073dSLiu Zhe try { 88*e6e6073dSLiu Zhe XMultiServiceFactory xFactory = (XMultiServiceFactory) UnoRuntime 89*e6e6073dSLiu Zhe .queryInterface(XMultiServiceFactory.class, xDrawDoc); 90*e6e6073dSLiu Zhe Object xObj = xFactory.createInstance(sShapeType); 91*e6e6073dSLiu Zhe xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xObj); 92*e6e6073dSLiu Zhe } catch (Exception e) { 93*e6e6073dSLiu Zhe // TODO Auto-generated catch block 94*e6e6073dSLiu Zhe e.printStackTrace(); 95*e6e6073dSLiu Zhe } 96*e6e6073dSLiu Zhe return xShape; 97*e6e6073dSLiu Zhe } 98*e6e6073dSLiu Zhe 99*e6e6073dSLiu Zhe /** 100*e6e6073dSLiu Zhe * add text to a shape. the return value is the PropertySet of the text 101*e6e6073dSLiu Zhe * range that has been added 102*e6e6073dSLiu Zhe */ 103*e6e6073dSLiu Zhe public static XPropertySet addPortion(XShape xShape, String sText, 104*e6e6073dSLiu Zhe boolean bNewParagraph) 105*e6e6073dSLiu Zhe throws com.sun.star.lang.IllegalArgumentException { 106*e6e6073dSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape); 107*e6e6073dSLiu Zhe 108*e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 109*e6e6073dSLiu Zhe xTextCursor.gotoEnd(false); 110*e6e6073dSLiu Zhe if (bNewParagraph == true) { 111*e6e6073dSLiu Zhe xText.insertControlCharacter(xTextCursor, 112*e6e6073dSLiu Zhe ControlCharacter.PARAGRAPH_BREAK, false); 113*e6e6073dSLiu Zhe xTextCursor.gotoEnd(false); 114*e6e6073dSLiu Zhe } 115*e6e6073dSLiu Zhe XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface( 116*e6e6073dSLiu Zhe XTextRange.class, xTextCursor); 117*e6e6073dSLiu Zhe xTextRange.setString(sText); 118*e6e6073dSLiu Zhe xTextCursor.gotoEnd(true); 119*e6e6073dSLiu Zhe XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( 120*e6e6073dSLiu Zhe XPropertySet.class, xTextRange); 121*e6e6073dSLiu Zhe return xPropSet; 122*e6e6073dSLiu Zhe } 123*e6e6073dSLiu Zhe 124*e6e6073dSLiu Zhe /** 125*e6e6073dSLiu Zhe * get a paragraph in a shape. the return value is the PropertySet of the text 126*e6e6073dSLiu Zhe * range that specified by the index 127*e6e6073dSLiu Zhe */ 128*e6e6073dSLiu Zhe public static XPropertySet getPortion(XShape xShape, int index) throws NoSuchElementException, WrappedTargetException { 129*e6e6073dSLiu Zhe XEnumerationAccess m_paraAccess = (XEnumerationAccess)UnoRuntime.queryInterface(XEnumerationAccess.class, xShape); 130*e6e6073dSLiu Zhe XEnumeration xParaEnum = m_paraAccess.createEnumeration(); 131*e6e6073dSLiu Zhe XPropertySet xPropSet = null; 132*e6e6073dSLiu Zhe int i=0; 133*e6e6073dSLiu Zhe while(xParaEnum.hasMoreElements()) 134*e6e6073dSLiu Zhe { 135*e6e6073dSLiu Zhe if(i == index) 136*e6e6073dSLiu Zhe { 137*e6e6073dSLiu Zhe Object aPortionObj = xParaEnum.nextElement(); 138*e6e6073dSLiu Zhe XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, aPortionObj); 139*e6e6073dSLiu Zhe // System.out.println(xTextRange.getText().getString()); 140*e6e6073dSLiu Zhe xPropSet = (XPropertySet) UnoRuntime.queryInterface( 141*e6e6073dSLiu Zhe XPropertySet.class, xTextRange); 142*e6e6073dSLiu Zhe break; 143*e6e6073dSLiu Zhe } 144*e6e6073dSLiu Zhe else i++; 145*e6e6073dSLiu Zhe } 146*e6e6073dSLiu Zhe return xPropSet; 147*e6e6073dSLiu Zhe } 148*e6e6073dSLiu Zhe 149*e6e6073dSLiu Zhe 150*e6e6073dSLiu Zhe /** 151*e6e6073dSLiu Zhe * try to get text of a shape 152*e6e6073dSLiu Zhe * 153*e6e6073dSLiu Zhe * @return 154*e6e6073dSLiu Zhe */ 155*e6e6073dSLiu Zhe public static String getPortion(XShape xShape) { 156*e6e6073dSLiu Zhe String text = null; 157*e6e6073dSLiu Zhe XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape); 158*e6e6073dSLiu Zhe 159*e6e6073dSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 160*e6e6073dSLiu Zhe XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface( 161*e6e6073dSLiu Zhe XTextRange.class, xTextCursor); 162*e6e6073dSLiu Zhe text = xTextRange.getString(); 163*e6e6073dSLiu Zhe return text; 164*e6e6073dSLiu Zhe 165*e6e6073dSLiu Zhe } 166*e6e6073dSLiu Zhe 167*e6e6073dSLiu Zhe public static void setPropertyForLastParagraph(XShape xText, 168*e6e6073dSLiu Zhe String sPropName, Object aValue) 169*e6e6073dSLiu Zhe throws com.sun.star.beans.UnknownPropertyException, 170*e6e6073dSLiu Zhe com.sun.star.beans.PropertyVetoException, 171*e6e6073dSLiu Zhe com.sun.star.lang.IllegalArgumentException, 172*e6e6073dSLiu Zhe com.sun.star.lang.WrappedTargetException, 173*e6e6073dSLiu Zhe com.sun.star.container.NoSuchElementException { 174*e6e6073dSLiu Zhe XEnumerationAccess xEnumerationAccess = (XEnumerationAccess) UnoRuntime 175*e6e6073dSLiu Zhe .queryInterface(XEnumerationAccess.class, xText); 176*e6e6073dSLiu Zhe if (xEnumerationAccess.hasElements()) { 177*e6e6073dSLiu Zhe XEnumeration xEnumeration = xEnumerationAccess.createEnumeration(); 178*e6e6073dSLiu Zhe while (xEnumeration.hasMoreElements()) { 179*e6e6073dSLiu Zhe Object xObj = xEnumeration.nextElement(); 180*e6e6073dSLiu Zhe if (xEnumeration.hasMoreElements() == false) { 181*e6e6073dSLiu Zhe XTextContent xTextContent = (XTextContent) UnoRuntime 182*e6e6073dSLiu Zhe .queryInterface(XTextContent.class, xObj); 183*e6e6073dSLiu Zhe XPropertySet xParaPropSet = (XPropertySet) UnoRuntime 184*e6e6073dSLiu Zhe .queryInterface(XPropertySet.class, xTextContent); 185*e6e6073dSLiu Zhe xParaPropSet.setPropertyValue(sPropName, aValue); 186*e6e6073dSLiu Zhe } 187*e6e6073dSLiu Zhe } 188*e6e6073dSLiu Zhe } 189*e6e6073dSLiu Zhe } 190*e6e6073dSLiu Zhe } 191