Debian Codegraph @

framework/qa/complex/accelerators/AcceleratorsConfigurationTest.java

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