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.Property; 30 import com.sun.star.beans.PropertyAttribute; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.container.XIndexAccess; 33 import com.sun.star.container.XNameAccess; 34 import com.sun.star.container.XNameContainer; 35 import com.sun.star.lang.XMultiServiceFactory; 36 import com.sun.star.style.XStyle; 37 import com.sun.star.style.XStyleFamiliesSupplier; 38 import com.sun.star.text.XText; 39 import com.sun.star.text.XTextCursor; 40 import com.sun.star.text.XTextDocument; 41 import com.sun.star.uno.UnoRuntime; 42 import com.sun.star.uno.XInterface; 43 import java.io.PrintWriter; 44 import lib.StatusException; 45 import lib.TestCase; 46 import lib.TestEnvironment; 47 import lib.TestParameters; 48 import util.DesktopTools; 49 import util.SOfficeFactory; 50 import util.utils; 51 52 /** 53 * Test for object which is represented by service 54 * <code>com.sun.star.style.PageStyle</code>. <p> 55 * @see com.sun.star.style.PageStyle 56 */ 57 public class PageStyle extends TestCase { 58 59 XTextDocument xTextDoc; 60 SOfficeFactory SOF = null; 61 62 /** 63 * Creates text document. 64 */ 65 protected void initialize( TestParameters tParam, PrintWriter log ) { 66 SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 67 try { 68 log.println( "creating a textdocument" ); 69 xTextDoc = SOF.createTextDoc( null ); 70 } catch ( com.sun.star.uno.Exception e ) { 71 e.printStackTrace( log ); 72 throw new StatusException( "Couldn't create document", e ); 73 } 74 } 75 76 /** 77 * Disposes text document. 78 */ 79 protected void cleanup( TestParameters tParam, PrintWriter log ) { 80 log.println( " disposing xTextDoc " ); 81 DesktopTools.closeDoc(xTextDoc); 82 } 83 84 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) { 85 TestEnvironment tEnv = null; 86 XNameAccess oSFNA = null; 87 XStyle oStyle = null; 88 XStyle oMyStyle = null; 89 90 log.println("creating a test environment"); 91 92 try { 93 log.println("getting style"); 94 XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier) 95 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, 96 xTextDoc); 97 XNameAccess oSF = oSFS.getStyleFamilies(); 98 oSFNA = (XNameAccess) UnoRuntime.queryInterface( 99 XNameAccess.class,oSF.getByName("PageStyles")); // get the page style 100 XIndexAccess oSFIA = (XIndexAccess) 101 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA); 102 oStyle = (XStyle) UnoRuntime.queryInterface( 103 XStyle.class,oSFIA.getByIndex(0)); 104 log.println("Chosen pool style: "+oStyle.getName()); 105 106 } catch ( com.sun.star.lang.WrappedTargetException e ) { 107 log.println("Error: exception occured."); 108 e.printStackTrace(log); 109 throw new StatusException( "Couldn't create environment ", e ); 110 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) { 111 log.println("Error: exception occured."); 112 e.printStackTrace(log); 113 throw new StatusException( "Couldn't create environment ", e ); 114 } catch ( com.sun.star.container.NoSuchElementException e ) { 115 log.println("Error: exception occured."); 116 e.printStackTrace(log); 117 throw new StatusException( "Couldn't create environment ", e ); 118 } 119 120 try { 121 log.print("Creating a user-defined style... "); 122 XMultiServiceFactory oMSF = (XMultiServiceFactory) 123 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc); 124 XInterface oInt = (XInterface) 125 oMSF.createInstance("com.sun.star.style.PageStyle"); 126 // oMSF.createInstanceWithArguments("com.sun.star.style.PageStyle",new Object[]{oStyle}); 127 oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt); 128 } catch ( com.sun.star.uno.Exception e ) { 129 log.println("Error: exception occured."); 130 e.printStackTrace(log); 131 throw new StatusException( "Couldn't create environment ", e ); 132 } 133 134 135 if (oMyStyle == null) 136 log.println("FAILED"); 137 else 138 log.println("OK"); 139 140 XNameContainer oSFNC = (XNameContainer) 141 UnoRuntime.queryInterface(XNameContainer.class, oSFNA); 142 143 144 try { 145 if ( oSFNC.hasByName("My Style") ) 146 oSFNC.removeByName("My Style"); 147 oSFNC.insertByName("My Style", oMyStyle); 148 } catch ( com.sun.star.lang.WrappedTargetException e ) { 149 e.printStackTrace(log); 150 throw new StatusException( "Couldn't create environment ", e ); 151 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 152 e.printStackTrace(log); 153 throw new StatusException( "Couldn't create environment ", e ); 154 } catch ( com.sun.star.container.NoSuchElementException e ) { 155 e.printStackTrace(log); 156 throw new StatusException( "Couldn't create environment ", e ); 157 } catch ( com.sun.star.container.ElementExistException e ) { 158 e.printStackTrace(log); 159 throw new StatusException( "Couldn't create environment ", e ); 160 } 161 162 XText oText = xTextDoc.getText(); 163 XTextCursor oCursor = oText.createTextCursor(); 164 XPropertySet xProp = (XPropertySet) 165 UnoRuntime.queryInterface(XPropertySet.class, oCursor); 166 Property[] props = xProp.getPropertySetInfo().getProperties(); 167 for (int i=0; i<props.length; i++) 168 System.out.println("# Property: " + props[i].Name + " val: " + props[i].Type.toString() + " attr: " + props[i].Attributes); 169 try { 170 xProp.setPropertyValue("PageDescName", oMyStyle.getName()); 171 } catch ( com.sun.star.lang.WrappedTargetException e ) { 172 e.printStackTrace( log ); 173 throw new StatusException( "Couldn't create environment ", e ); 174 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 175 e.printStackTrace( log ); 176 throw new StatusException( "Couldn't create environment ", e ); 177 } catch ( com.sun.star.beans.PropertyVetoException e ) { 178 e.printStackTrace( log ); 179 throw new StatusException( "Couldn't create environment ", e ); 180 } catch ( com.sun.star.beans.UnknownPropertyException e ) { 181 e.printStackTrace( log ); 182 throw new StatusException( "Couldn't create environment ", e ); 183 } 184 185 // oMyStyle = oStyle; 186 log.println("creating a new environment for object"); 187 tEnv = new TestEnvironment(oMyStyle); 188 tEnv.addObjRelation("PoolStyle", oStyle); 189 190 tEnv.addObjRelation("FollowStyle", "Envelope"); 191 XPropertySet xStyleProp = (XPropertySet) 192 UnoRuntime.queryInterface(XPropertySet.class, oMyStyle); 193 194 short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY; 195 String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude); 196 tEnv.addObjRelation("PropertyNames", names); 197 198 return tEnv; 199 } 200 201 } 202