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.container.XNameAccess;
30 import com.sun.star.embed.ElementModes;
31 import com.sun.star.embed.XStorage;
32 import com.sun.star.embed.XTransactedObject;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.lang.XSingleServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.ui.XModuleUIConfigurationManagerSupplier;
38 import ifc.ui._XUIConfiguration;
39 import java.io.PrintWriter;
40 import com.sun.star.lang.EventObject;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.util.XCloseable;
43 import com.sun.star.ui.ConfigurationEvent;
44 import com.sun.star.ui.XUIConfigurationManager;
45 import ifc.ui._XUIConfigurationManager;
46 import lib.StatusException;
47 import lib.TestCase;
48 import lib.TestEnvironment;
49 import lib.TestParameters;
50 import util.WriterTools;
51 import util.utils;
52 
53 /**
54  */
55 public class ModuleUIConfigurationManager extends TestCase {
56     XInterface oObj = null;
57     XMultiServiceFactory xMSF = null;
58     XTextDocument xTextDoc = null;
59     XStorage xStore = null;
60 
61     /**
62      * Cleanup: close the created document
63      * @param tParam The test parameters.
64      * @param The log writer.
65      * @return The test environment.
66      */
67     protected void cleanup(TestParameters tParam, PrintWriter log) {
68         log.println("    disposing xTextDoc ");
69         if (xTextDoc != null) {
70             try {
71                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
72                     XCloseable.class, xTextDoc);
73                 closer.close(true);
74             } catch (com.sun.star.util.CloseVetoException e) {
75                 log.println("couldn't close document");
76             } catch (com.sun.star.lang.DisposedException e) {
77                 log.println("couldn't close document");
78             }
79         }
80         log.println("   disposing storage");
81         if (xStore != null) {
82             xStore.dispose();
83         }
84     }
85 
86     /**
87      * Create environment.
88      */
89     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
90         TestEnvironment tEnv = null;
91 
92         try {
93             xMSF = (XMultiServiceFactory)tParam.getMSF();
94 
95             log.println("Creating instance...");
96             xTextDoc = WriterTools.createTextDoc(xMSF);
97 
98             Object o = (XInterface)xMSF.createInstance("com.sun.star.ui.ModuleUIConfigurationManagerSupplier");
99             XModuleUIConfigurationManagerSupplier xMUICMS = (XModuleUIConfigurationManagerSupplier)
100             UnoRuntime.queryInterface(XModuleUIConfigurationManagerSupplier.class, o);
101 
102             util.dbg.printInterfaces(xMUICMS);
103             oObj = xMUICMS.getUIConfigurationManager("com.sun.star.text.TextDocument");
104 
105             log.println("TestObject: " + utils.getImplName(oObj));
106             tEnv = new TestEnvironment(oObj);
107 
108             XNameAccess xMM = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, xMSF.createInstance("com.sun.star.comp.framework.ModuleManager"));
109             String[] names = xMM.getElementNames();
110 
111             o = xMSF.createInstance("com.sun.star.embed.StorageFactory");
112             XSingleServiceFactory xStorageService = (XSingleServiceFactory)
113                     UnoRuntime.queryInterface(XSingleServiceFactory.class, o);
114             Object[]props = new Object[2];
115 
116             String aFile = util.utils.getOfficeTempDir(xMSF) + "dummyFile.dat";
117             log.println("storage file : '"+ aFile + "'");
118 
119             props[0] = aFile;
120             props[1] = new Integer(ElementModes.READWRITE);
121             xStore = (XStorage)UnoRuntime.queryInterface(XStorage.class, xStorageService.createInstanceWithArguments(props));
122 
123             PropertyValue[] initProps = new PropertyValue[4];
124             PropertyValue propVal = new PropertyValue();
125             propVal.Name = "DefaultConfigStorage";
126             propVal.Value = xStore;
127             initProps[0] = propVal;
128             propVal = new PropertyValue();
129             propVal.Name = "UserConfigStorage";
130             propVal.Value = xStore;
131             initProps[1] = propVal;
132             propVal = new PropertyValue();
133             propVal.Name = "ModuleIdentifier";
134             propVal.Value = "swriter";
135             initProps[2] = propVal;
136             propVal = new PropertyValue();
137             propVal.Name = "UserRootCommit";
138             propVal.Value = (XTransactedObject)UnoRuntime.queryInterface(XTransactedObject.class, xStore);
139             initProps[3] = propVal;
140 
141 
142             tEnv.addObjRelation("XInitialization.args", initProps);
143 
144             // the short cut manager service name
145             // 2do: correct the service name when it's no longer in
146             tEnv.addObjRelation("XConfigurationManager.ShortCutManager",
147                 "com.sun.star.ui.ModuleAcceleratorConfiguration");
148 
149             // the resourceURL
150             tEnv.addObjRelation("XModuleUIConfigurationManager.ResourceURL",
151                                             "private:resource/menubar/menubar");
152             tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl",
153                             new ConfigurationListener(log,
154                             (XUIConfigurationManager)UnoRuntime.queryInterface(
155                             XUIConfigurationManager.class, oObj), xMSF));
156             tEnv.addObjRelation("XModuleUIConfigurationManagerSupplier.ConfigManagerImplementationName",
157                         "com.sun.star.comp.framework.ModuleUIConfigurationManager");
158         }
159         catch(com.sun.star.uno.Exception e) {
160             e.printStackTrace(log);
161             throw new StatusException("Cannot create test object", e);
162         }
163         return tEnv;
164     }
165 
166     /**
167      * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl
168      * interface to trigger the event for a listener call.
169      * @see ifc.ui._XUIConfiguration
170      */
171     public static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl {
172         private boolean triggered = false;
173         private PrintWriter log = null;
174         private XUIConfigurationManager xUIManager = null;
175         private XMultiServiceFactory xMSF = null;
176 
177         public ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) {
178             log = _log;
179             this.xUIManager = xUIManager;
180             this.xMSF = xMSF;
181         }
182         public void reset(){
183             triggered = false;
184         }
185         public void fireEvent() {
186             try {
187                 XIndexAccess xMenuBarSettings = xUIManager.getSettings(
188                                         "private:resource/menubar/menubar", true);
189 
190                 PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry(
191                                         "Trigger Event", xMenuBarSettings, xMSF, log);
192                 _XUIConfigurationManager.createMenuBarItem("Click for Macro",
193                                 (XIndexContainer)UnoRuntime.queryInterface(
194                                 XIndexContainer.class, prop[3].Value), log);
195                 XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings);
196                 x.insertByIndex(x.getCount(), prop);
197                 xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings);
198                 xUIManager.reset();
199             }
200             catch(com.sun.star.container.NoSuchElementException e) {
201                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
202                 e.printStackTrace(log);
203             }
204             catch(com.sun.star.lang.IllegalArgumentException e) {
205                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
206                 e.printStackTrace(log);
207             }
208             catch(com.sun.star.lang.IllegalAccessException e) {
209                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
210                 e.printStackTrace(log);
211             }
212             catch(com.sun.star.lang.IndexOutOfBoundsException e) {
213                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
214                 e.printStackTrace(log);
215             }
216             catch(com.sun.star.lang.WrappedTargetException e) {
217                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
218                 e.printStackTrace(log);
219             }
220         }
221         public boolean actionWasTriggered(){
222             return triggered;
223         }
224         public void disposing(EventObject e) {
225             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener.");
226         }
227         public void elementInserted(ConfigurationEvent configEvent) {
228             triggered = true;
229             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted.");
230         }
231         public void elementRemoved(ConfigurationEvent configEvent) {
232             triggered = true;
233             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved.");
234         }
235         public void elementReplaced(ConfigurationEvent configEvent) {
236             triggered = true;
237             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced.");
238         }
239     }
240 
241 }
242