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