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