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 
28 package mod._fwk;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XIndexContainer;
33 import com.sun.star.embed.ElementModes;
34 import com.sun.star.embed.XStorage;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.lang.XSingleServiceFactory;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import ifc.ui._XUIConfiguration;
40 import java.io.PrintWriter;
41 import com.sun.star.lang.EventObject;
42 import com.sun.star.ui.ConfigurationEvent;
43 import com.sun.star.ui.UIElementType;
44 import com.sun.star.ui.XUIConfigurationManager;
45 import com.sun.star.ui.XUIConfigurationStorage;
46 import ifc.ui._XUIConfigurationManager;
47 import lib.TestCase;
48 import lib.TestEnvironment;
49 import lib.TestParameters;
50 import util.utils;
51 
52 /**
53  */
54 public class UIConfigurationManager extends TestCase {
55     XUIConfigurationManager xManager = null;
56 
57 
58     /**
59      * Create test environment:
60      * <ul>
61      * <li>create a text doc</li>
62      * <li>get the model from the text doc</li>
63      * <li>query model for XUIConfigurationManagerSupplier interface</li>
64      * <li>get the manager from the supplier</li>
65      * </ul>
66      * @param tParam The test parameters.
67      * @param The log writer.
68      * @return The test environment.
69      */
70     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
71         TestEnvironment tEnv = null;
72         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
73 
74         log.println("Creating instance...");
75 
76         try {
77             xManager = (XUIConfigurationManager)UnoRuntime.queryInterface(
78                     XUIConfigurationManager.class, xMSF.createInstance(
79                     "com.sun.star.comp.framework.UIConfigurationManager"));
80         }
81         catch(com.sun.star.uno.Exception e) {
82         }
83         // just to make sure, it's the right one.
84         log.println("TestObject: " + utils.getImplName(xManager));
85         tEnv = new TestEnvironment(xManager);
86 
87         // create a configuration storage
88         try {
89             String sourceDeleteCfg = util.utils.getFullTestURL("delete.cfg");
90             String deleteCfg = util.utils.getFullURL(util.utils.getOfficeTemp(xMSF) + "delete.cfg");
91             util.utils.copyFile(xMSF, sourceDeleteCfg, deleteCfg);
92             XStorage xSubStorage = null;
93             Object o = (XInterface)xMSF.createInstance("com.sun.star.embed.StorageFactory");
94             XSingleServiceFactory xSSF = (XSingleServiceFactory)UnoRuntime.queryInterface(
95             XSingleServiceFactory.class, o);
96             Object[] props = new Object[2];
97             props[0] = deleteCfg;
98             props[1] = new Integer(ElementModes.READWRITE);
99             XStorage xRootStorage = (XStorage)UnoRuntime.queryInterface(XStorage.class, xSSF.createInstanceWithArguments(props));
100             xSubStorage = xRootStorage.openStorageElement("Configurations2", ElementModes.READWRITE);
101 
102             XUIConfigurationStorage xConfigStorage =(XUIConfigurationStorage)UnoRuntime.queryInterface(XUIConfigurationStorage.class, xManager);
103             xConfigStorage.setStorage(xSubStorage);
104             tEnv.addObjRelation("XUIConfigurationStorage.Storage", xSubStorage);
105         }
106         catch(com.sun.star.uno.Exception e) {
107             log.println("Could not create storage: " + e.toString());
108         }
109         util.dbg.printInterfaces(xManager);
110 
111         tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl",
112             new ConfigurationListener(log, xManager, xMSF));
113 
114         // the short cut manager service name
115         // 2do: correct the service name when it's no longer in
116         tEnv.addObjRelation("XConfigurationManager.ShortCutManager",
117             "com.sun.star.ui.DocumentAcceleratorConfiguration");
118         return tEnv;
119     }
120 
121     /**
122      * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl
123      * interface to trigger the event for a listener call.
124      * @see ifc.ui._XUIConfiguration
125      */
126     public static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl {
127         private boolean triggered = false;
128         private PrintWriter log = null;
129         private XUIConfigurationManager xUIManager = null;
130         private XMultiServiceFactory xMSF = null;
131         private static int iUniqueCounter;
132 
133         public ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) {
134             log = _log;
135             this.xUIManager = xUIManager;
136             this.xMSF = xMSF;
137             iUniqueCounter = 0;
138         }
139         public void reset(){
140             triggered = false;
141         }
142         public void fireEvent() {
143             try {
144                 if (iUniqueCounter == 0) {
145                     iUniqueCounter++;
146                     PropertyValue[][]props = xUIManager.getUIElementsInfo(UIElementType.UNKNOWN);
147                     XIndexAccess xMenuBarSettings = xUIManager.getSettings(
148                     "private:resource/menubar/menubar", true);
149 
150                     PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry(
151                     "Trigger Event", xMenuBarSettings, xMSF, log);
152                     _XUIConfigurationManager.createMenuBarItem("Click for Macro",
153                     (XIndexContainer)UnoRuntime.queryInterface(
154                     XIndexContainer.class, prop[3].Value), log);
155                     XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings);
156                     x.insertByIndex(x.getCount(), prop);
157                     xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings);
158                     xUIManager.reset();
159                 }
160             }
161             catch(com.sun.star.container.NoSuchElementException e) {
162                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
163                 e.printStackTrace(log);
164             }
165             catch(com.sun.star.lang.IllegalArgumentException e) {
166                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
167                 e.printStackTrace(log);
168             }
169             catch(com.sun.star.lang.IllegalAccessException e) {
170                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
171                 e.printStackTrace(log);
172             }
173             catch(com.sun.star.lang.IndexOutOfBoundsException e) {
174                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
175                 e.printStackTrace(log);
176             }
177             catch(com.sun.star.lang.WrappedTargetException e) {
178                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
179                 e.printStackTrace(log);
180             }
181         }
182         public boolean actionWasTriggered(){
183             return triggered;
184         }
185         public void disposing(EventObject e) {
186             triggered = true;
187             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener.");
188         }
189         public void elementInserted(ConfigurationEvent configEvent) {
190             triggered = true;
191             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted.");
192         }
193         public void elementRemoved(ConfigurationEvent configEvent) {
194             triggered = true;
195             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved.");
196         }
197         public void elementReplaced(ConfigurationEvent configEvent) {
198             triggered = true;
199             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced.");
200         }
201     }
202 
203 }
204 
205 
206