1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package complex.accelerators;
19 
20 import com.sun.star.awt.KeyEvent;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.configuration.theDefaultProvider;
24 import com.sun.star.container.XNameAccess;
25 import com.sun.star.embed.XStorage;
26 import com.sun.star.embed.XTransactedObject;
27 import com.sun.star.lang.XInitialization;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.lang.XSingleServiceFactory;
30 import com.sun.star.ui.XAcceleratorConfiguration;
31 import com.sun.star.ui.XUIConfigurationManager;
32 import com.sun.star.ui.XUIConfigurationPersistence;
33 import com.sun.star.ui.XUIConfigurationStorage;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.util.XChangesBatch;
37 
38 // ---------- junit imports -----------------
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.openoffice.test.FileHelper;
45 import org.openoffice.test.OfficeConnection;
46 import static org.junit.Assert.*;
47 
48 
49 
50 /** @short todo document me
51  */
52 public class AcceleratorsConfigurationTest
53 {
54 
55     /** points to the global uno service manager. */
56     private XMultiServiceFactory m_xSmgr = null;
57     /** the accelerator configuration for testing. */
58     private XAcceleratorConfiguration m_xGlobalAccelCfg = null;
59     private XAcceleratorConfiguration m_xModuleAccelCfg = null;
60     private XAcceleratorConfiguration m_xDocumentAccelCfg = null;
61     /** XCS/XCU based accelerator configuration. */
62     private XNameAccess m_xPrimaryKeys = null;
63     private XNameAccess m_xSecondaryKeys = null;
64 
65 
66     // test environment
67 
68     /** @short Create the environment for following tests.
69      */
70     @Before
before()71     public void before()
72             throws java.lang.Exception
73     {
74         // get uno service manager from global test environment
75         m_xSmgr = getMSF();
76 
77         m_xGlobalAccelCfg = UnoRuntime.queryInterface(XAcceleratorConfiguration.class, m_xSmgr.createInstance("com.sun.star.ui.GlobalAcceleratorConfiguration"));
78         m_xModuleAccelCfg = UnoRuntime.queryInterface(XAcceleratorConfiguration.class, m_xSmgr.createInstance("com.sun.star.ui.ModuleAcceleratorConfiguration"));
79         m_xDocumentAccelCfg = UnoRuntime.queryInterface(XAcceleratorConfiguration.class, m_xSmgr.createInstance("com.sun.star.ui.DocumentAcceleratorConfiguration"));
80 
81         String sConfigPath = "org.openoffice.Office.Accelerators";
82         boolean bReadOnly = false;
83         XNameAccess xConfig2 = openConfig(sConfigPath, bReadOnly);
84         if (xConfig2 != null)
85         {
86             m_xPrimaryKeys = UnoRuntime.queryInterface(XNameAccess.class, xConfig2.getByName("PrimaryKeys"));
87             m_xSecondaryKeys = UnoRuntime.queryInterface(XNameAccess.class, xConfig2.getByName("SecondaryKeys"));
88         }
89     }
90 
91 
92     /** @short  close the environment.
93      */
94     @After
after()95     public void after()
96             throws java.lang.Exception
97     {
98         m_xGlobalAccelCfg = null;
99         m_xModuleAccelCfg = null;
100         m_xDocumentAccelCfg = null;
101         m_xSmgr = null;
102     }
103 
104 
105     /** @todo document me.
106      */
107     @Test
checkGlobalAccelCfg()108     public void checkGlobalAccelCfg()
109             throws java.lang.Exception
110     {
111         System.out.println("\n---- check Global accelerator configuration: ----");
112 
113         String[] sKeys;
114         XNameAccess xPrimaryAccess = UnoRuntime.queryInterface(XNameAccess.class, m_xPrimaryKeys.getByName("Global"));
115         XNameAccess xSecondaryAccess = UnoRuntime.queryInterface(XNameAccess.class, m_xSecondaryKeys.getByName("Global"));
116 
117         sKeys = new String[]
118                 {
119                     "A_MOD1"
120                 };
121         impl_checkGetKeyCommands(m_xGlobalAccelCfg, xPrimaryAccess, sKeys);
122 
123         sKeys = new String[]
124                 {
125                     "PASTE", "X_SHIFT"
126                 };
127         String[] sCommands = new String[]
128         {
129             ".uno:test", ".uno:test"
130         };
131         impl_checkSetKeyCommands(m_xGlobalAccelCfg, xPrimaryAccess, xSecondaryAccess, sKeys, sCommands);
132 
133         sKeys = new String[]
134                 {
135                     "C_MOD1", "CUT"
136                 };
137         impl_checkRemoveKeyCommands(m_xGlobalAccelCfg, xPrimaryAccess, xSecondaryAccess, sKeys);
138 
139         String[] sCommandList = new String[]
140         {
141             ".uno:Paste", ".uno:CloseWin"
142         };
143         impl_checkGetPreferredKeyEventsForCommandList(m_xGlobalAccelCfg, xPrimaryAccess, sCommandList);
144     }
145 
146 
147     /** @todo document me.
148      */
149     @Test
checkModuleAccelCfg()150     public void checkModuleAccelCfg()
151             throws java.lang.Exception
152     {
153         String[] sModules = new String[]
154         {
155             "com.sun.star.frame.StartModule",
156             "com.sun.star.drawing.DrawingDocument",
157             "com.sun.star.presentation.PresentationDocument",
158             "com.sun.star.sheet.SpreadsheetDocument",
159             "com.sun.star.text.TextDocument",
160         // add other modules here
161         };
162 
163         for (int i = 0; i < sModules.length; ++i)
164         {
165             System.out.println("\n---- check accelerator configuration depending module: " + sModules[i] + " ----");
166 
167             PropertyValue[] aProp = new PropertyValue[2];
168             aProp[0] = new PropertyValue();
169             aProp[0].Name = "ModuleIdentifier";
170             aProp[0].Value = sModules[i];
171             aProp[1] = new PropertyValue();
172             aProp[1].Name = "Locale";
173             aProp[1].Value = "en-US";
174 
175             XInitialization xInit = UnoRuntime.queryInterface(XInitialization.class, m_xModuleAccelCfg);
176             xInit.initialize(aProp); // to fill cache
177 
178             XNameAccess xPrimaryModules = UnoRuntime.queryInterface(XNameAccess.class, m_xPrimaryKeys.getByName("Modules"));
179             XNameAccess xSecondaryModules = UnoRuntime.queryInterface(XNameAccess.class, m_xSecondaryKeys.getByName("Modules"));
180 
181             String[] sKeys;
182             XNameAccess xPrimaryAccess = UnoRuntime.queryInterface(XNameAccess.class, xPrimaryModules.getByName(sModules[i]));
183             XNameAccess xSecondaryAccess = UnoRuntime.queryInterface(XNameAccess.class, xSecondaryModules.getByName(sModules[i]));
184 
185 
186             if (sModules[i].equals("com.sun.star.presentation.PresentationDocument"))
187             {
188                 sKeys = new String[]
189                         {
190                             "A_SHIFT_MOD1_MOD2"
191                         };
192             }
193             else if (sModules[i].equals("com.sun.star.sheet.SpreadsheetDocument"))
194             {
195                 sKeys = new String[]
196                         {
197                             "B_MOD1"
198                         };
199             }
200             else if (sModules[i].equals("com.sun.star.text.TextDocument"))
201             {
202                 sKeys = new String[]
203                         {
204                             "F11_MOD1"
205                         };
206             }
207             else
208             {
209                 sKeys = new String[]
210                         {
211                             "A_MOD1"
212                         };
213             }
214             impl_checkGetKeyCommands(m_xModuleAccelCfg, xPrimaryAccess, sKeys);
215 
216 
217             String[] sCommands;
218             if (sModules[i].equals("com.sun.star.presentation.PresentationDocument"))
219             {
220                 sKeys = new String[]
221                         {
222                             "A_SHIFT_MOD1_MOD2"
223                         };
224                 sCommands = new String[]
225                         {
226                             ".uno:test"
227                         };
228             }
229             else if (sModules[i].equals("com.sun.star.sheet.SpreadsheetDocument"))
230             {
231                 sKeys = new String[]
232                         {
233                             "B_MOD1"
234                         };
235                 sCommands = new String[]
236                         {
237                             ".uno:test"
238                         };
239             }
240             else if (sModules[i].equals("com.sun.star.text.TextDocument"))
241             {
242                 sKeys = new String[]
243                         {
244                             "F11_MOD1"
245                         };
246                 sCommands = new String[]
247                         {
248                             ".uno:test"
249                         };
250             }
251             else
252             {
253                 sKeys = new String[]
254                         {
255                             "PASTE"
256                         };
257                 sCommands = new String[]
258                         {
259                             ".uno:test"
260                         };
261             }
262             impl_checkSetKeyCommands(m_xModuleAccelCfg, xPrimaryAccess, xSecondaryAccess, sKeys, sCommands);
263 
264 
265             if (sModules[i].equals("com.sun.star.presentation.PresentationDocument"))
266             {
267                 sKeys = new String[]
268                         {
269                             "A_SHIFT_MOD1_MOD2"
270                         };
271             }
272             else if (sModules[i].equals("com.sun.star.sheet.SpreadsheetDocument"))
273             {
274                 sKeys = new String[]
275                         {
276                             "F5_SHIFT_MOD1"
277                         };
278             }
279             else if (sModules[i].equals("com.sun.star.text.TextDocument"))
280             {
281                 sKeys = new String[]
282                         {
283                             "BACKSPACE_MOD2"
284                         };
285             }
286             else
287             {
288                 sKeys = new String[]
289                         {
290                             "C_MOD1"
291                         };
292             }
293             impl_checkRemoveKeyCommands(m_xModuleAccelCfg, xPrimaryAccess, xSecondaryAccess, sKeys);
294 
295 
296             String[] sCommandList;
297             if (sModules[i].equals("com.sun.star.presentation.PresentationDocument"))
298             {
299                 sCommandList = new String[]
300                         {
301                             ".uno:Presentation"
302                         };
303             }
304             else if (sModules[i].equals("com.sun.star.sheet.SpreadsheetDocument"))
305             {
306                 sCommandList = new String[]
307                         {
308                             ".uno:InsertCell"
309                         };
310             }
311             else if (sModules[i].equals("com.sun.star.text.TextDocument"))
312             {
313                 sCommandList = new String[]
314                         {
315                             ".uno:SelectionModeBlock"
316                         };
317             }
318             else
319             {
320                 sCommandList = new String[]
321                         {
322                             ".uno:Cut"
323                         };
324             }
325             impl_checkGetPreferredKeyEventsForCommandList(m_xModuleAccelCfg, xPrimaryAccess, sCommandList);
326         }
327     }
328 
329 
330     /** @todo document me.
331      */
332     @Test
checkDocumentAccelCfg()333     public void checkDocumentAccelCfg()
334             throws java.lang.Exception
335     {
336         System.out.println("\n---- check Document accelerator configuration: ----");
337 
338         String sDocCfgName;
339 
340         String tempDirURL = util.utils.getOfficeTemp/*Dir*/(getMSF());
341         sDocCfgName = FileHelper.appendPath(tempDirURL, "test.cfg");
342         SaveDocumentAcceleratorConfiguration(sDocCfgName);
343 
344         LoadDocumentAcceleratorConfiguration(sDocCfgName);
345     }
346 
347 
348     /** @todo document me.
349      */
impl_checkGetKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xAccess, String[] sKeys)350     private void impl_checkGetKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xAccess, String[] sKeys)
351             throws java.lang.Exception
352     {
353         System.out.println("check getKeyCommands...");
354 
355         for (int i = 0; i < sKeys.length; ++i)
356         {
357             if (xAccess.hasByName(sKeys[i]) && getCommandFromConfiguration(xAccess, sKeys[i]).length() > 0)
358             {
359                 System.out.println("** get command by " + sKeys[i] + " **");
360 
361                 String sCmdFromCache = ""; // get a value using XAcceleratorConfiguration API
362                 String sCmdFromConfiguration = ""; // get a value using configuration API
363 
364                 // GET shortcuts/commands using XAcceleratorConfiguration API
365                 sCmdFromCache = xAccelCfg.getCommandByKeyEvent(convertShortcut2AWTKey(sKeys[i]));
366                 System.out.println(sKeys[i] + "-->" + sCmdFromCache + ", by XAcceleratorConfiguration API");
367 
368                 // GET shortcuts/commands using configuration API
369                 sCmdFromConfiguration = getCommandFromConfiguration(xAccess, sKeys[i]);
370                 System.out.println(sKeys[i] + "-->" + sCmdFromConfiguration + ", by configuration API");
371 
372                 assertTrue("values are different by XAcceleratorConfiguration API and configuration API!", sCmdFromCache.equals(sCmdFromConfiguration));
373 
374                 String sLocale = "es";
375                 setOfficeLocale(sLocale);
376                 sCmdFromConfiguration = getCommandFromConfiguration(xAccess, sKeys[i]);
377                 System.out.println(sKeys[i] + "-->" + sCmdFromConfiguration + ", by configuration API" + " for locale:" + getOfficeLocale());
378 
379                 sLocale = "en-US";
380                 setOfficeLocale(sLocale); //reset to default locale
381             }
382             else
383             {
384                 System.out.println(sKeys[i] + " doesn't exist!");
385             }
386         }
387     }
388 
389 
390     /** @todo document me.
391      */
impl_checkSetKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, XNameAccess xSecondaryAccess, String[] sKeys, String[] sCommands)392     private void impl_checkSetKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, XNameAccess xSecondaryAccess, String[] sKeys, String[] sCommands)
393             throws java.lang.Exception
394     {
395         System.out.println("check setKeyCommands...");
396 
397         for (int i = 0; i < sKeys.length; ++i)
398         {
399             if (!xPrimaryAccess.hasByName(sKeys[i]) && !xSecondaryAccess.hasByName(sKeys[i]))
400             {
401                 xAccelCfg.setKeyEvent(convertShortcut2AWTKey(sKeys[i]), sCommands[i]);
402                 xAccelCfg.store();
403                 if (xPrimaryAccess.hasByName(sKeys[i]))
404                 {
405                     System.out.println("add " + sKeys[i] + " successfully!");
406                 }
407                 else
408                 {
409                     System.out.println("add " + sKeys[i] + " failed!");
410                 }
411             }
412             else if (xPrimaryAccess.hasByName(sKeys[i]))
413             {
414                 String sOriginalCommand = getCommandFromConfiguration(xPrimaryAccess, sKeys[i]);
415                 if (!sCommands[i].equals(sOriginalCommand))
416                 {
417                     xAccelCfg.setKeyEvent(convertShortcut2AWTKey(sKeys[i]), sCommands[i]);
418                     xAccelCfg.store();
419 
420                     String sChangedCommand = getCommandFromConfiguration(xPrimaryAccess, sKeys[i]);
421                     if (sCommands[i].equals(sChangedCommand))
422                     {
423                         System.out.println("change " + sKeys[i] + " successfully!");
424                     }
425                     else
426                     {
427                         System.out.println("change " + sKeys[i] + " failed!");
428                     }
429                 }
430                 else
431                 {
432                     System.out.println(sKeys[i] + " already exist!");
433                 }
434             }
435             else if (xSecondaryAccess.hasByName(sKeys[i]))
436             {
437                 String sOriginalCommand = getCommandFromConfiguration(xSecondaryAccess, sKeys[i]);
438                 if (!sCommands[i].equals(sOriginalCommand))
439                 {
440                     xAccelCfg.setKeyEvent(convertShortcut2AWTKey(sKeys[i]), sCommands[i]);
441                     xAccelCfg.store();
442 
443                     String sChangedCommand = getCommandFromConfiguration(xPrimaryAccess, sKeys[i]);
444                     if (sCommands[i].equals(sChangedCommand))
445                     {
446                         System.out.println("change " + sKeys[i] + " successfully!");
447                     }
448                     else
449                     {
450                         System.out.println("change " + sKeys[i] + " failed!");
451                     }
452                 }
453                 else
454                 {
455                     System.out.println(sKeys[i] + " already exist!");
456                 }
457             }
458         }
459     }
460 
461 
462     /** @todo document me.
463      */
impl_checkRemoveKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, XNameAccess xSecondaryAccess, String[] sKeys)464     private void impl_checkRemoveKeyCommands(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, XNameAccess xSecondaryAccess, String[] sKeys)
465             throws java.lang.Exception
466     {
467         System.out.println("check removeKeyCommands...");
468 
469         for (int i = 0; i < sKeys.length; i++)
470         {
471             if (!xPrimaryAccess.hasByName(sKeys[i]) && !xSecondaryAccess.hasByName(sKeys[i]))
472             {
473                 System.out.println(sKeys[i] + " doesn't exist!");
474             }
475             else if (xPrimaryAccess.hasByName(sKeys[i]))
476             {
477                 xAccelCfg.removeKeyEvent(convertShortcut2AWTKey(sKeys[i]));
478                 xAccelCfg.store();
479                 if (!xPrimaryAccess.hasByName(sKeys[i]))
480                 {
481                     System.out.println("Remove " + sKeys[i] + " successfully!");
482                 }
483                 else
484                 {
485                     System.out.println("Remove " + sKeys[i] + " failed!");
486                 }
487             }
488             else if (xSecondaryAccess.hasByName(sKeys[i]))
489             {
490                 xAccelCfg.removeKeyEvent(convertShortcut2AWTKey(sKeys[i]));
491                 xAccelCfg.store();
492                 if (!xSecondaryAccess.hasByName(sKeys[i]))
493                 {
494                     System.out.println("Remove " + sKeys[i] + " successfully!");
495                 }
496                 else
497                 {
498                     System.out.println("Remove " + sKeys[i] + " failed!");
499                 }
500             }
501         }
502     }
503 
504 
505     /** @todo document me.
506      */
impl_checkGetPreferredKeyEventsForCommandList(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, String[] sCommandList)507     private void impl_checkGetPreferredKeyEventsForCommandList(XAcceleratorConfiguration xAccelCfg, XNameAccess xPrimaryAccess, String[] sCommandList)
508             throws java.lang.Exception
509     {
510         System.out.println("check getPreferredKeyEventsForCommandList...");
511 
512         Object[] oKeyEvents = xAccelCfg.getPreferredKeyEventsForCommandList(sCommandList);
513         for (int i = 0; i < oKeyEvents.length; i++)
514         {
515             System.out.println("get preferred key for command " + sCommandList[i] + ":");
516 
517             KeyEvent aKeyEvent = (KeyEvent) AnyConverter.toObject(KeyEvent.class, oKeyEvents[i]);
518             String sKeyEvent = convertAWTKey2Shortcut(aKeyEvent);
519             System.out.println(sKeyEvent);
520 
521             String sCmdFromConfiguration = getCommandFromConfiguration(xPrimaryAccess, sKeyEvent);
522             System.out.println(sCmdFromConfiguration);
523             if (sCommandList[i].equals(sCmdFromConfiguration))
524             {
525                 System.out.println("get preferred key correctly!");
526             }
527             else
528             {
529                 System.out.println("get preferred key failed!");
530             }
531         }
532     }
533 
534 
535     /** @todo document me.
536      */
getCommandFromConfiguration(XNameAccess xAccess, String sKey)537     private String getCommandFromConfiguration(XNameAccess xAccess, String sKey)
538             throws java.lang.Exception
539     {
540         String sCommand = "";
541 
542         if (xAccess.hasByName(sKey))
543         {
544             XNameAccess xKey = UnoRuntime.queryInterface(XNameAccess.class, xAccess.getByName(sKey));
545             XNameAccess xCommand = UnoRuntime.queryInterface(XNameAccess.class, xKey.getByName("Command"));
546 
547             String sLocale = getOfficeLocale();
548             if (xCommand.hasByName(sLocale))
549             {
550                 sCommand = UnoRuntime.queryInterface(String.class, xCommand.getByName(sLocale));
551             }
552         }
553 
554         return sCommand;
555     }
556 
557 
558     /** @todo document me.
559      */
LoadDocumentAcceleratorConfiguration(String sDocCfgName)560     private void LoadDocumentAcceleratorConfiguration(String sDocCfgName)
561             throws java.lang.Exception
562     {
563         XSingleServiceFactory xStorageFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, m_xSmgr.createInstance("com.sun.star.embed.StorageFactory"));
564 
565         Object aArgs[] = new Object[2];
566         aArgs[0] = sDocCfgName;
567         aArgs[1] = Integer.valueOf(com.sun.star.embed.ElementModes.READ);
568         XStorage xRootStorage = UnoRuntime.queryInterface(XStorage.class, xStorageFactory.createInstanceWithArguments(aArgs));
569 
570         XStorage xUIConfig = xRootStorage.openStorageElement("Configurations2", com.sun.star.embed.ElementModes.READ);
571 
572         PropertyValue aProp = new PropertyValue();
573         aProp.Name = "DocumentRoot";
574         aProp.Value = xUIConfig;
575         Object[] lArgs = new Object[1];
576         lArgs[0] = aProp;
577 
578         XInitialization xInit = UnoRuntime.queryInterface(XInitialization.class, m_xDocumentAccelCfg);
579         xInit.initialize(lArgs);
580 
581         // TODO: throws css::container::NoSuchElementException
582         try
583         {
584             String test = m_xDocumentAccelCfg.getCommandByKeyEvent(convertShortcut2AWTKey("F2"));
585             System.out.println(test);
586         }
587         catch(com.sun.star.container.NoSuchElementException e)
588         {
589             System.out.println("NoSuchElementException caught: " + e.getMessage());
590         }
591     }
592 
593 
594     /** @todo document me.
595      */
SaveDocumentAcceleratorConfiguration(String sDocCfgName)596     private void SaveDocumentAcceleratorConfiguration(String sDocCfgName)
597             throws java.lang.Exception
598     {
599         XSingleServiceFactory xStorageFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, m_xSmgr.createInstance("com.sun.star.embed.StorageFactory"));
600 
601         Object aArgs[] = new Object[2];
602         aArgs[0] = sDocCfgName;
603         aArgs[1] = Integer.valueOf(com.sun.star.embed.ElementModes.WRITE);
604         XStorage xRootStorage = UnoRuntime.queryInterface(XStorage.class, xStorageFactory.createInstanceWithArguments(aArgs));
605 
606         XStorage xUIConfig = xRootStorage.openStorageElement("Configurations2", com.sun.star.embed.ElementModes.WRITE);
607 
608         XUIConfigurationManager xCfgMgr = UnoRuntime.queryInterface(XUIConfigurationManager.class, m_xSmgr.createInstance("com.sun.star.ui.UIConfigurationManager"));
609 
610         XUIConfigurationStorage xUICfgStore = UnoRuntime.queryInterface(XUIConfigurationStorage.class, xCfgMgr);
611         xUICfgStore.setStorage(xUIConfig);
612 
613         XPropertySet xUIConfigProps = UnoRuntime.queryInterface(XPropertySet.class, xUIConfig);
614         xUIConfigProps.setPropertyValue("MediaType", "application/vnd.sun.xml.ui.configuration");
615 
616         if (xCfgMgr != null)
617         {
618             XAcceleratorConfiguration xTargetAccMgr = UnoRuntime.queryInterface(XAcceleratorConfiguration.class, xCfgMgr.getShortCutManager());
619             XUIConfigurationPersistence xCommit1 = UnoRuntime.queryInterface(XUIConfigurationPersistence.class, xTargetAccMgr);
620             XUIConfigurationPersistence xCommit2 = UnoRuntime.queryInterface(XUIConfigurationPersistence.class, xCfgMgr);
621             xCommit1.store();
622             xCommit2.store();
623 
624             XTransactedObject xCommit3 = UnoRuntime.queryInterface(XTransactedObject.class, xRootStorage);
625             xCommit3.commit();
626         }
627     }
628 
629 
630     /** @todo document me.
631      */
convertShortcut2AWTKey(String sShortcut)632     private com.sun.star.awt.KeyEvent convertShortcut2AWTKey(String sShortcut)
633             throws java.lang.Exception
634     {
635         com.sun.star.awt.KeyEvent aKeyEvent = new com.sun.star.awt.KeyEvent();
636         KeyMapping aKeyMapping = new KeyMapping();
637         String[] sShortcutSplits = sShortcut.split("_");
638 
639         aKeyEvent.KeyCode = aKeyMapping.mapIdentifier2Code(sShortcutSplits[0]);
640         for (int i = 1; i < sShortcutSplits.length; i++)
641         {
642             if (sShortcutSplits[i].equals("SHIFT"))
643             {
644                 aKeyEvent.Modifiers |= com.sun.star.awt.KeyModifier.SHIFT;
645             }
646             else if (sShortcutSplits[i].equals("MOD1"))
647             {
648                 aKeyEvent.Modifiers |= com.sun.star.awt.KeyModifier.MOD1;
649             }
650             else if (sShortcutSplits[i].equals("MOD2"))
651             {
652                 aKeyEvent.Modifiers |= com.sun.star.awt.KeyModifier.MOD2;
653             }
654         }
655 
656         return aKeyEvent;
657     }
658 
659 
660     /** @todo document me.
661      */
convertAWTKey2Shortcut(com.sun.star.awt.KeyEvent aKeyEvent)662     private String convertAWTKey2Shortcut(com.sun.star.awt.KeyEvent aKeyEvent)
663             throws java.lang.Exception
664     {
665         String sShortcut;
666 
667         KeyMapping aKeyMapping = new KeyMapping();
668         sShortcut = aKeyMapping.mapCode2Identifier(aKeyEvent.KeyCode);
669 
670         if ((aKeyEvent.Modifiers & com.sun.star.awt.KeyModifier.SHIFT) == com.sun.star.awt.KeyModifier.SHIFT)
671         {
672             sShortcut += "_SHIFT";
673         }
674         if ((aKeyEvent.Modifiers & com.sun.star.awt.KeyModifier.MOD1) == com.sun.star.awt.KeyModifier.MOD1)
675         {
676             sShortcut += "_MOD1";
677         }
678         if ((aKeyEvent.Modifiers & com.sun.star.awt.KeyModifier.MOD2) == com.sun.star.awt.KeyModifier.MOD2)
679         {
680             sShortcut += "_MOD2";
681         }
682 
683         return sShortcut;
684     }
685 
686 
687     /** @todo document me.
688      */
getOfficeLocale()689     private String getOfficeLocale()
690             throws java.lang.Exception
691     {
692         String sLocale = "";
693 
694         String sConfigPath = "org.openoffice.Setup";
695         boolean bReadOnly = true;
696         XNameAccess xRootConfig = openConfig(sConfigPath, bReadOnly);
697 
698         if (xRootConfig != null)
699         {
700             XNameAccess xLocale = UnoRuntime.queryInterface(XNameAccess.class, xRootConfig.getByName("L10N"));
701             XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xLocale);
702             sLocale = (String) xSet.getPropertyValue("ooLocale");
703         }
704 
705         return sLocale;
706     }
707 
708 
709     /** @todo document me.
710      */
setOfficeLocale(String sLocale)711     private void setOfficeLocale(String sLocale)
712             throws java.lang.Exception
713     {
714         String sConfigPath = "org.openoffice.Setup";
715         boolean bReadOnly = false;
716         XNameAccess xRootConfig = openConfig(sConfigPath, bReadOnly);
717 
718         if (xRootConfig != null)
719         {
720             XNameAccess xLocale = UnoRuntime.queryInterface(XNameAccess.class, xRootConfig.getByName("L10N"));
721             XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xLocale);
722             xSet.setPropertyValue("ooLocale", sLocale);
723             XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xRootConfig);
724             xBatch.commitChanges();
725         }
726     }
727 
728 
729     /** @todo document me.
730      */
openConfig( String sConfigPath, boolean bReadOnly)731     private XNameAccess openConfig(
732             String sConfigPath,
733             boolean bReadOnly)
734             throws java.lang.Exception
735     {
736         XMultiServiceFactory xConfigRoot = theDefaultProvider.get(
737             connection.getComponentContext());
738 
739         PropertyValue[] lParams = new PropertyValue[2];
740         lParams[0] = new PropertyValue();
741         lParams[0].Name = "nodepath";
742         lParams[0].Value = sConfigPath;
743 
744         lParams[1] = new PropertyValue();
745         lParams[1].Name = "locale";
746         lParams[1].Value = "*";
747 
748         Object aConfig;
749         if (bReadOnly)
750         {
751             aConfig = xConfigRoot.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", lParams);
752         }
753         else
754         {
755             aConfig = xConfigRoot.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", lParams);
756         }
757 
758         XNameAccess xConfig = UnoRuntime.queryInterface(XNameAccess.class, aConfig);
759 
760         if (xConfig == null)
761         {
762             throw new com.sun.star.uno.Exception("Could not open configuration \"" + sConfigPath + "\"");
763         }
764 
765         return xConfig;
766     }
767 
getMSF()768     private XMultiServiceFactory getMSF()
769     {
770         return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
771     }
772 
773     // setup and close connections
774     @BeforeClass
setUpConnection()775     public static void setUpConnection() throws Exception
776     {
777         System.out.println("setUpConnection()");
778         connection.setUp();
779     }
780 
781     @AfterClass
tearDownConnection()782     public static void tearDownConnection()
783             throws InterruptedException, com.sun.star.uno.Exception
784     {
785         System.out.println("tearDownConnection()");
786         connection.tearDown();
787     }
788     private static final OfficeConnection connection = new OfficeConnection();
789 }
790