1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package mod._fwk; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 31*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 32*cdf0e10cSrcweir import com.sun.star.container.XIndexContainer; 33*cdf0e10cSrcweir import com.sun.star.embed.ElementModes; 34*cdf0e10cSrcweir import com.sun.star.embed.XStorage; 35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 36*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 38*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 39*cdf0e10cSrcweir import ifc.ui._XUIConfiguration; 40*cdf0e10cSrcweir import java.io.PrintWriter; 41*cdf0e10cSrcweir import com.sun.star.lang.EventObject; 42*cdf0e10cSrcweir import com.sun.star.ui.ConfigurationEvent; 43*cdf0e10cSrcweir import com.sun.star.ui.UIElementType; 44*cdf0e10cSrcweir import com.sun.star.ui.XUIConfigurationManager; 45*cdf0e10cSrcweir import com.sun.star.ui.XUIConfigurationStorage; 46*cdf0e10cSrcweir import ifc.ui._XUIConfigurationManager; 47*cdf0e10cSrcweir import lib.TestCase; 48*cdf0e10cSrcweir import lib.TestEnvironment; 49*cdf0e10cSrcweir import lib.TestParameters; 50*cdf0e10cSrcweir import util.utils; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir /** 53*cdf0e10cSrcweir */ 54*cdf0e10cSrcweir public class UIConfigurationManager extends TestCase { 55*cdf0e10cSrcweir XUIConfigurationManager xManager = null; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /** 59*cdf0e10cSrcweir * Create test environment: 60*cdf0e10cSrcweir * <ul> 61*cdf0e10cSrcweir * <li>create a text doc</li> 62*cdf0e10cSrcweir * <li>get the model from the text doc</li> 63*cdf0e10cSrcweir * <li>query model for XUIConfigurationManagerSupplier interface</li> 64*cdf0e10cSrcweir * <li>get the manager from the supplier</li> 65*cdf0e10cSrcweir * </ul> 66*cdf0e10cSrcweir * @param tParam The test parameters. 67*cdf0e10cSrcweir * @param The log writer. 68*cdf0e10cSrcweir * @return The test environment. 69*cdf0e10cSrcweir */ 70*cdf0e10cSrcweir protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) { 71*cdf0e10cSrcweir TestEnvironment tEnv = null; 72*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir log.println("Creating instance..."); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir try { 77*cdf0e10cSrcweir xManager = (XUIConfigurationManager)UnoRuntime.queryInterface( 78*cdf0e10cSrcweir XUIConfigurationManager.class, xMSF.createInstance( 79*cdf0e10cSrcweir "com.sun.star.comp.framework.UIConfigurationManager")); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir // just to make sure, it's the right one. 84*cdf0e10cSrcweir log.println("TestObject: " + utils.getImplName(xManager)); 85*cdf0e10cSrcweir tEnv = new TestEnvironment(xManager); 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir // create a configuration storage 88*cdf0e10cSrcweir try { 89*cdf0e10cSrcweir String sourceDeleteCfg = util.utils.getFullTestURL("delete.cfg"); 90*cdf0e10cSrcweir String deleteCfg = util.utils.getFullURL(util.utils.getOfficeTemp(xMSF) + "delete.cfg"); 91*cdf0e10cSrcweir util.utils.copyFile(xMSF, sourceDeleteCfg, deleteCfg); 92*cdf0e10cSrcweir XStorage xSubStorage = null; 93*cdf0e10cSrcweir Object o = (XInterface)xMSF.createInstance("com.sun.star.embed.StorageFactory"); 94*cdf0e10cSrcweir XSingleServiceFactory xSSF = (XSingleServiceFactory)UnoRuntime.queryInterface( 95*cdf0e10cSrcweir XSingleServiceFactory.class, o); 96*cdf0e10cSrcweir Object[] props = new Object[2]; 97*cdf0e10cSrcweir props[0] = deleteCfg; 98*cdf0e10cSrcweir props[1] = new Integer(ElementModes.READWRITE); 99*cdf0e10cSrcweir XStorage xRootStorage = (XStorage)UnoRuntime.queryInterface(XStorage.class, xSSF.createInstanceWithArguments(props)); 100*cdf0e10cSrcweir xSubStorage = xRootStorage.openStorageElement("Configurations2", ElementModes.READWRITE); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir XUIConfigurationStorage xConfigStorage =(XUIConfigurationStorage)UnoRuntime.queryInterface(XUIConfigurationStorage.class, xManager); 103*cdf0e10cSrcweir xConfigStorage.setStorage(xSubStorage); 104*cdf0e10cSrcweir tEnv.addObjRelation("XUIConfigurationStorage.Storage", xSubStorage); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 107*cdf0e10cSrcweir log.println("Could not create storage: " + e.toString()); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir util.dbg.printInterfaces(xManager); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl", 112*cdf0e10cSrcweir new ConfigurationListener(log, xManager, xMSF)); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir // the short cut manager service name 115*cdf0e10cSrcweir // 2do: correct the service name when it's no longer in 116*cdf0e10cSrcweir tEnv.addObjRelation("XConfigurationManager.ShortCutManager", 117*cdf0e10cSrcweir "com.sun.star.ui.DocumentAcceleratorConfiguration"); 118*cdf0e10cSrcweir return tEnv; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir /** 122*cdf0e10cSrcweir * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl 123*cdf0e10cSrcweir * interface to trigger the event for a listener call. 124*cdf0e10cSrcweir * @see ifc.ui._XUIConfiguration 125*cdf0e10cSrcweir */ 126*cdf0e10cSrcweir public static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl { 127*cdf0e10cSrcweir private boolean triggered = false; 128*cdf0e10cSrcweir private PrintWriter log = null; 129*cdf0e10cSrcweir private XUIConfigurationManager xUIManager = null; 130*cdf0e10cSrcweir private XMultiServiceFactory xMSF = null; 131*cdf0e10cSrcweir private static int iUniqueCounter; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir public ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) { 134*cdf0e10cSrcweir log = _log; 135*cdf0e10cSrcweir this.xUIManager = xUIManager; 136*cdf0e10cSrcweir this.xMSF = xMSF; 137*cdf0e10cSrcweir iUniqueCounter = 0; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir public void reset(){ 140*cdf0e10cSrcweir triggered = false; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir public void fireEvent() { 143*cdf0e10cSrcweir try { 144*cdf0e10cSrcweir if (iUniqueCounter == 0) { 145*cdf0e10cSrcweir iUniqueCounter++; 146*cdf0e10cSrcweir PropertyValue[][]props = xUIManager.getUIElementsInfo(UIElementType.UNKNOWN); 147*cdf0e10cSrcweir XIndexAccess xMenuBarSettings = xUIManager.getSettings( 148*cdf0e10cSrcweir "private:resource/menubar/menubar", true); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry( 151*cdf0e10cSrcweir "Trigger Event", xMenuBarSettings, xMSF, log); 152*cdf0e10cSrcweir _XUIConfigurationManager.createMenuBarItem("Click for Macro", 153*cdf0e10cSrcweir (XIndexContainer)UnoRuntime.queryInterface( 154*cdf0e10cSrcweir XIndexContainer.class, prop[3].Value), log); 155*cdf0e10cSrcweir XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings); 156*cdf0e10cSrcweir x.insertByIndex(x.getCount(), prop); 157*cdf0e10cSrcweir xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings); 158*cdf0e10cSrcweir xUIManager.reset(); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir catch(com.sun.star.container.NoSuchElementException e) { 162*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception."); 163*cdf0e10cSrcweir e.printStackTrace(log); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir catch(com.sun.star.lang.IllegalArgumentException e) { 166*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception."); 167*cdf0e10cSrcweir e.printStackTrace(log); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir catch(com.sun.star.lang.IllegalAccessException e) { 170*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception."); 171*cdf0e10cSrcweir e.printStackTrace(log); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir catch(com.sun.star.lang.IndexOutOfBoundsException e) { 174*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception."); 175*cdf0e10cSrcweir e.printStackTrace(log); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir catch(com.sun.star.lang.WrappedTargetException e) { 178*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception."); 179*cdf0e10cSrcweir e.printStackTrace(log); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir public boolean actionWasTriggered(){ 183*cdf0e10cSrcweir return triggered; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir public void disposing(EventObject e) { 186*cdf0e10cSrcweir triggered = true; 187*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener."); 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir public void elementInserted(ConfigurationEvent configEvent) { 190*cdf0e10cSrcweir triggered = true; 191*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted."); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir public void elementRemoved(ConfigurationEvent configEvent) { 194*cdf0e10cSrcweir triggered = true; 195*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved."); 196*cdf0e10cSrcweir } 197*cdf0e10cSrcweir public void elementReplaced(ConfigurationEvent configEvent) { 198*cdf0e10cSrcweir triggered = true; 199*cdf0e10cSrcweir log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced."); 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir 206