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