1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._fwk;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
28cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
29cdf0e10cSrcweir import com.sun.star.embed.ElementModes;
30cdf0e10cSrcweir import com.sun.star.embed.XStorage;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.XInterface;
35cdf0e10cSrcweir import ifc.ui._XUIConfiguration;
36cdf0e10cSrcweir import java.io.PrintWriter;
37cdf0e10cSrcweir import com.sun.star.lang.EventObject;
38cdf0e10cSrcweir import com.sun.star.ui.ConfigurationEvent;
39cdf0e10cSrcweir import com.sun.star.ui.UIElementType;
40cdf0e10cSrcweir import com.sun.star.ui.XUIConfigurationManager;
41cdf0e10cSrcweir import com.sun.star.ui.XUIConfigurationStorage;
42cdf0e10cSrcweir import ifc.ui._XUIConfigurationManager;
43cdf0e10cSrcweir import lib.TestCase;
44cdf0e10cSrcweir import lib.TestEnvironment;
45cdf0e10cSrcweir import lib.TestParameters;
46cdf0e10cSrcweir import util.utils;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /**
49cdf0e10cSrcweir  */
50cdf0e10cSrcweir public class UIConfigurationManager extends TestCase {
51cdf0e10cSrcweir     XUIConfigurationManager xManager = null;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /**
55cdf0e10cSrcweir      * Create test environment:
56cdf0e10cSrcweir      * <ul>
57cdf0e10cSrcweir      * <li>create a text doc</li>
58cdf0e10cSrcweir      * <li>get the model from the text doc</li>
59cdf0e10cSrcweir      * <li>query model for XUIConfigurationManagerSupplier interface</li>
60cdf0e10cSrcweir      * <li>get the manager from the supplier</li>
61cdf0e10cSrcweir      * </ul>
62cdf0e10cSrcweir      * @param tParam The test parameters.
63*e6b649b5SPedro Giffuni      * @param log The log writer.
64cdf0e10cSrcweir      * @return The test environment.
65cdf0e10cSrcweir      */
createTestEnvironment(TestParameters tParam, PrintWriter log)66cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
67cdf0e10cSrcweir         TestEnvironment tEnv = null;
68cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         log.println("Creating instance...");
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         try {
73cdf0e10cSrcweir             xManager = (XUIConfigurationManager)UnoRuntime.queryInterface(
74cdf0e10cSrcweir                     XUIConfigurationManager.class, xMSF.createInstance(
75cdf0e10cSrcweir                     "com.sun.star.comp.framework.UIConfigurationManager"));
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e) {
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir         // just to make sure, it's the right one.
80cdf0e10cSrcweir         log.println("TestObject: " + utils.getImplName(xManager));
81cdf0e10cSrcweir         tEnv = new TestEnvironment(xManager);
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         // create a configuration storage
84cdf0e10cSrcweir         try {
85cdf0e10cSrcweir             String sourceDeleteCfg = util.utils.getFullTestURL("delete.cfg");
86cdf0e10cSrcweir             String deleteCfg = util.utils.getFullURL(util.utils.getOfficeTemp(xMSF) + "delete.cfg");
87cdf0e10cSrcweir             util.utils.copyFile(xMSF, sourceDeleteCfg, deleteCfg);
88cdf0e10cSrcweir             XStorage xSubStorage = null;
89cdf0e10cSrcweir             Object o = (XInterface)xMSF.createInstance("com.sun.star.embed.StorageFactory");
90cdf0e10cSrcweir             XSingleServiceFactory xSSF = (XSingleServiceFactory)UnoRuntime.queryInterface(
91cdf0e10cSrcweir             XSingleServiceFactory.class, o);
92cdf0e10cSrcweir             Object[] props = new Object[2];
93cdf0e10cSrcweir             props[0] = deleteCfg;
94cdf0e10cSrcweir             props[1] = new Integer(ElementModes.READWRITE);
95cdf0e10cSrcweir             XStorage xRootStorage = (XStorage)UnoRuntime.queryInterface(XStorage.class, xSSF.createInstanceWithArguments(props));
96cdf0e10cSrcweir             xSubStorage = xRootStorage.openStorageElement("Configurations2", ElementModes.READWRITE);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             XUIConfigurationStorage xConfigStorage =(XUIConfigurationStorage)UnoRuntime.queryInterface(XUIConfigurationStorage.class, xManager);
99cdf0e10cSrcweir             xConfigStorage.setStorage(xSubStorage);
100cdf0e10cSrcweir             tEnv.addObjRelation("XUIConfigurationStorage.Storage", xSubStorage);
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e) {
103cdf0e10cSrcweir             log.println("Could not create storage: " + e.toString());
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir         util.dbg.printInterfaces(xManager);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl",
108cdf0e10cSrcweir             new ConfigurationListener(log, xManager, xMSF));
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         // the short cut manager service name
111cdf0e10cSrcweir         // 2do: correct the service name when it's no longer in
112cdf0e10cSrcweir         tEnv.addObjRelation("XConfigurationManager.ShortCutManager",
113cdf0e10cSrcweir             "com.sun.star.ui.DocumentAcceleratorConfiguration");
114cdf0e10cSrcweir         return tEnv;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /**
118cdf0e10cSrcweir      * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl
119cdf0e10cSrcweir      * interface to trigger the event for a listener call.
120cdf0e10cSrcweir      * @see ifc.ui._XUIConfiguration
121cdf0e10cSrcweir      */
122cdf0e10cSrcweir     public static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl {
123cdf0e10cSrcweir         private boolean triggered = false;
124cdf0e10cSrcweir         private PrintWriter log = null;
125cdf0e10cSrcweir         private XUIConfigurationManager xUIManager = null;
126cdf0e10cSrcweir         private XMultiServiceFactory xMSF = null;
127cdf0e10cSrcweir         private static int iUniqueCounter;
128cdf0e10cSrcweir 
ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF)129cdf0e10cSrcweir         public ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) {
130cdf0e10cSrcweir             log = _log;
131cdf0e10cSrcweir             this.xUIManager = xUIManager;
132cdf0e10cSrcweir             this.xMSF = xMSF;
133cdf0e10cSrcweir             iUniqueCounter = 0;
134cdf0e10cSrcweir         }
reset()135cdf0e10cSrcweir         public void reset(){
136cdf0e10cSrcweir             triggered = false;
137cdf0e10cSrcweir         }
fireEvent()138cdf0e10cSrcweir         public void fireEvent() {
139cdf0e10cSrcweir             try {
140cdf0e10cSrcweir                 if (iUniqueCounter == 0) {
141cdf0e10cSrcweir                     iUniqueCounter++;
142cdf0e10cSrcweir                     PropertyValue[][]props = xUIManager.getUIElementsInfo(UIElementType.UNKNOWN);
143cdf0e10cSrcweir                     XIndexAccess xMenuBarSettings = xUIManager.getSettings(
144cdf0e10cSrcweir                     "private:resource/menubar/menubar", true);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                     PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry(
147cdf0e10cSrcweir                     "Trigger Event", xMenuBarSettings, xMSF, log);
148cdf0e10cSrcweir                     _XUIConfigurationManager.createMenuBarItem("Click for Macro",
149cdf0e10cSrcweir                     (XIndexContainer)UnoRuntime.queryInterface(
150cdf0e10cSrcweir                     XIndexContainer.class, prop[3].Value), log);
151cdf0e10cSrcweir                     XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings);
152cdf0e10cSrcweir                     x.insertByIndex(x.getCount(), prop);
153cdf0e10cSrcweir                     xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings);
154cdf0e10cSrcweir                     xUIManager.reset();
155cdf0e10cSrcweir                 }
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir             catch(com.sun.star.container.NoSuchElementException e) {
158cdf0e10cSrcweir                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
159cdf0e10cSrcweir                 e.printStackTrace(log);
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir             catch(com.sun.star.lang.IllegalArgumentException e) {
162cdf0e10cSrcweir                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
163cdf0e10cSrcweir                 e.printStackTrace(log);
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir             catch(com.sun.star.lang.IllegalAccessException e) {
166cdf0e10cSrcweir                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
167cdf0e10cSrcweir                 e.printStackTrace(log);
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir             catch(com.sun.star.lang.IndexOutOfBoundsException e) {
170cdf0e10cSrcweir                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
171cdf0e10cSrcweir                 e.printStackTrace(log);
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir             catch(com.sun.star.lang.WrappedTargetException e) {
174cdf0e10cSrcweir                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
175cdf0e10cSrcweir                 e.printStackTrace(log);
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir         }
actionWasTriggered()178cdf0e10cSrcweir         public boolean actionWasTriggered(){
179cdf0e10cSrcweir             return triggered;
180cdf0e10cSrcweir         }
disposing(EventObject e)181cdf0e10cSrcweir         public void disposing(EventObject e) {
182cdf0e10cSrcweir             triggered = true;
183cdf0e10cSrcweir             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener.");
184cdf0e10cSrcweir         }
elementInserted(ConfigurationEvent configEvent)185cdf0e10cSrcweir         public void elementInserted(ConfigurationEvent configEvent) {
186cdf0e10cSrcweir             triggered = true;
187cdf0e10cSrcweir             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted.");
188cdf0e10cSrcweir         }
elementRemoved(ConfigurationEvent configEvent)189cdf0e10cSrcweir         public void elementRemoved(ConfigurationEvent configEvent) {
190cdf0e10cSrcweir             triggered = true;
191cdf0e10cSrcweir             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved.");
192cdf0e10cSrcweir         }
elementReplaced(ConfigurationEvent configEvent)193cdf0e10cSrcweir         public void elementReplaced(ConfigurationEvent configEvent) {
194cdf0e10cSrcweir             triggered = true;
195cdf0e10cSrcweir             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced.");
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202