1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package mod._sw; 28 29 import com.sun.star.beans.XPropertySet; 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 38 import com.sun.star.container.XIndexAccess; 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.lang.XMultiServiceFactory; 41 import com.sun.star.text.XText; 42 import com.sun.star.text.XTextContent; 43 import com.sun.star.text.XTextCursor; 44 import com.sun.star.text.XTextDocument; 45 import com.sun.star.text.XTextEmbeddedObjectsSupplier; 46 import com.sun.star.text.XTextFrame; 47 import com.sun.star.uno.AnyConverter; 48 import com.sun.star.uno.Type; 49 import com.sun.star.uno.UnoRuntime; 50 import com.sun.star.uno.XInterface; 51 import com.sun.star.util.XCloseable; 52 53 54 /** 55 * 56 * initial description 57 * @see com.sun.star.container.XNamed 58 * @see com.sun.star.document.XEmbeddedObjectSupplier 59 * @see com.sun.star.lang.XComponent 60 * @see com.sun.star.text.TextEmbeddedObject 61 * @see com.sun.star.text.XTextContent 62 * @see com.sun.star.text.XTextEmbeddedObject 63 * 64 */ 65 public class SwXTextEmbeddedObject extends TestCase { 66 XTextDocument xTextDoc; 67 68 /** 69 * in general this method disposes the testenvironment and document 70 * 71 * @param tParam class which contains additional test parameters 72 * @param log class to log the test state and result 73 * 74 * 75 * @see TestParameters 76 * @see PrintWriter 77 * 78 */ 79 protected void cleanup(TestParameters tParam, PrintWriter log) { 80 log.println(" disposing xTextDoc "); 81 82 try { 83 XCloseable closer = (XCloseable) UnoRuntime.queryInterface( 84 XCloseable.class, xTextDoc); 85 closer.close(true); 86 } catch (com.sun.star.util.CloseVetoException e) { 87 log.println("couldn't close document"); 88 } catch (com.sun.star.lang.DisposedException e) { 89 log.println("couldn't close document"); 90 } 91 } 92 93 /** 94 * creating a Testenvironment for the interfaces to be tested 95 * 96 * @param tParam class which contains additional test parameters 97 * @param log class to log the test state and result 98 * 99 * @return Status class 100 * 101 * @see TestParameters 102 * @see PrintWriter 103 */ 104 protected TestEnvironment createTestEnvironment(TestParameters tParam, 105 PrintWriter log) { 106 XInterface oObj = null; 107 108 // create testobject here 109 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF()); 110 try { 111 xTextDoc = SOF.createTextDoc(null); 112 } catch (com.sun.star.uno.Exception e) { 113 e.printStackTrace(log); 114 throw new StatusException("Couldn't open document", e); 115 } 116 117 XTextCursor xCursor = xTextDoc.getText().createTextCursor(); 118 try { 119 XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) 120 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc); 121 Object o = xMultiServiceFactory.createInstance("com.sun.star.text.TextEmbeddedObject" ); 122 XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, o); 123 String sChartClassID = "12dcae26-281f-416f-a234-c3086127382e"; 124 XPropertySet xPropertySet = (XPropertySet) 125 UnoRuntime.queryInterface(XPropertySet.class, xTextContent); 126 xPropertySet.setPropertyValue( "CLSID", sChartClassID ); 127 128 xTextDoc.getText().insertTextContent( xCursor, xTextContent, false ); 129 } 130 catch(com.sun.star.uno.Exception e) { 131 e.printStackTrace((java.io.PrintWriter)log); 132 } 133 134 XTextEmbeddedObjectsSupplier oTEOS = (XTextEmbeddedObjectsSupplier) UnoRuntime.queryInterface( 135 XTextEmbeddedObjectsSupplier.class, 136 xTextDoc); 137 138 XNameAccess oEmObj = oTEOS.getEmbeddedObjects(); 139 XIndexAccess oEmIn = (XIndexAccess) UnoRuntime.queryInterface( 140 XIndexAccess.class, oEmObj); 141 142 try { 143 oObj = (XInterface) AnyConverter.toObject( 144 new Type(XInterface.class), oEmIn.getByIndex(0)); 145 } catch (com.sun.star.uno.Exception e) { 146 e.printStackTrace(log); 147 throw new StatusException("Couldn't get Object", e); 148 } 149 150 TestEnvironment tEnv = new TestEnvironment(oObj); 151 152 tEnv.addObjRelation("NoAttach", "SwXTextEmbeddedObject"); 153 154 XTextFrame aFrame = SOF.createTextFrame(xTextDoc, 500, 500); 155 XText oText = xTextDoc.getText(); 156 XTextCursor oCursor = oText.createTextCursor(); 157 XTextContent the_content = (XTextContent) UnoRuntime.queryInterface( 158 XTextContent.class, aFrame); 159 160 try { 161 oText.insertTextContent(oCursor, the_content, true); 162 } catch (com.sun.star.lang.IllegalArgumentException e) { 163 log.println("Couldn't insert frame " + e.getMessage()); 164 } 165 166 tEnv.addObjRelation("TextFrame", aFrame); 167 168 tEnv.addObjRelation("NoSetSize", "SwXTextEmbeddedObject"); 169 tEnv.addObjRelation("NoPos", "SwXTextEmbeddedObject"); 170 171 return tEnv; 172 } // finish method getTestEnvironment 173 } // finish class SwXTextEmbeddedObject 174