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