1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.StringTokenizer ; 19 import java.util.TreeMap ; 20 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.ui.IMemento; 24 import org.eclipse.ui.commands.IHandler; 25 import org.eclipse.ui.internal.commands.ws.HandlerProxy; 26 import org.eclipse.ui.internal.util.Util; 27 import org.eclipse.ui.keys.KeySequence; 28 import org.eclipse.ui.keys.KeyStroke; 29 import org.eclipse.ui.keys.ParseException; 30 import org.eclipse.ui.keys.SWTKeySupport; 31 32 final class Persistence { 33 34 private static Map stringToValueMap = new TreeMap (); 35 36 static { 37 stringToValueMap.put("BACKSPACE", new Integer (8)); stringToValueMap.put("TAB", new Integer (9)); stringToValueMap.put("RETURN", new Integer (13)); stringToValueMap.put("ENTER", new Integer (13)); stringToValueMap.put("ESCAPE", new Integer (27)); stringToValueMap.put("ESC", new Integer (27)); stringToValueMap.put("DELETE", new Integer (127)); stringToValueMap.put("SPACE", new Integer (' ')); stringToValueMap.put("ARROW_UP", new Integer (SWT.ARROW_UP)); stringToValueMap.put("ARROW_DOWN", new Integer (SWT.ARROW_DOWN)); stringToValueMap.put("ARROW_LEFT", new Integer (SWT.ARROW_LEFT)); stringToValueMap.put("ARROW_RIGHT", new Integer (SWT.ARROW_RIGHT)); stringToValueMap.put("PAGE_UP", new Integer (SWT.PAGE_UP)); stringToValueMap.put("PAGE_DOWN", new Integer (SWT.PAGE_DOWN)); stringToValueMap.put("HOME", new Integer (SWT.HOME)); stringToValueMap.put("END", new Integer (SWT.END)); stringToValueMap.put("INSERT", new Integer (SWT.INSERT)); stringToValueMap.put("F1", new Integer (SWT.F1)); stringToValueMap.put("F2", new Integer (SWT.F2)); stringToValueMap.put("F3", new Integer (SWT.F3)); stringToValueMap.put("F4", new Integer (SWT.F4)); stringToValueMap.put("F5", new Integer (SWT.F5)); stringToValueMap.put("F6", new Integer (SWT.F6)); stringToValueMap.put("F7", new Integer (SWT.F7)); stringToValueMap.put("F8", new Integer (SWT.F8)); stringToValueMap.put("F9", new Integer (SWT.F9)); stringToValueMap.put("F10", new Integer (SWT.F10)); stringToValueMap.put("F11", new Integer (SWT.F11)); stringToValueMap.put("F12", new Integer (SWT.F12)); } 67 68 private final static String ALT = "Alt"; private final static String COMMAND = "Command"; private final static String CTRL = "Ctrl"; private final static String MODIFIER_SEPARATOR = "+"; 73 final static String PACKAGE_BASE = "commands"; final static String PACKAGE_PREFIX = "org.eclipse.ui"; final static String PACKAGE_FULL = PACKAGE_PREFIX + '.' + PACKAGE_BASE; 76 private final static String SHIFT = "Shift"; 78 final static String TAG_ACTIVE_KEY_CONFIGURATION = "activeKeyConfiguration"; final static String TAG_CATEGORY = "category"; final static String TAG_CATEGORY_ID = "categoryId"; final static String TAG_COMMAND = "command"; final static String TAG_COMMAND_ID = "commandId"; final static String TAG_CONTEXT_ID = "contextId"; final static String TAG_DESCRIPTION = "description"; final static String TAG_HANDLER = "handlerSubmission"; final static String TAG_ID = "id"; final static String TAG_KEY_CONFIGURATION = "keyConfiguration"; final static String TAG_KEY_CONFIGURATION_ID = "keyConfigurationId"; final static String TAG_KEY_SEQUENCE = "keySequence"; final static String TAG_KEY_SEQUENCE_BINDING = "keyBinding"; final static String TAG_LOCALE = "locale"; final static String TAG_NAME = "name"; final static String TAG_PARENT_ID = "parentId"; final static String TAG_PLATFORM = "platform"; final static String TAG_SOURCE_ID = "sourceId"; 98 private static KeySequence deprecatedSequenceToKeySequence(int[] sequence) { 99 List keyStrokes = new ArrayList (); 100 101 for (int i = 0; i < sequence.length; i++) 102 keyStrokes.add(deprecatedStrokeToKeyStroke(sequence[i])); 103 104 return KeySequence.getInstance(keyStrokes); 105 } 106 107 private static KeyStroke deprecatedStrokeToKeyStroke(int stroke) { 108 return SWTKeySupport.convertAcceleratorToKeyStroke(stroke); 109 } 110 111 private static int[] parseDeprecatedSequence(String string) { 112 if (string == null) 113 throw new NullPointerException (); 114 115 StringTokenizer stringTokenizer = new StringTokenizer (string); 116 int length = stringTokenizer.countTokens(); 117 int[] strokes = new int[length]; 118 119 for (int i = 0; i < length; i++) 120 strokes[i] = parseDeprecatedStroke(stringTokenizer.nextToken()); 121 122 return strokes; 123 } 124 125 private static int parseDeprecatedStroke(String string) { 126 if (string == null) 127 throw new NullPointerException (); 128 129 List list = new ArrayList (); 130 StringTokenizer stringTokenizer = 131 new StringTokenizer (string, MODIFIER_SEPARATOR, true); 132 133 while (stringTokenizer.hasMoreTokens()) 134 list.add(stringTokenizer.nextToken()); 135 136 int size = list.size(); 137 int value = 0; 138 139 if (size % 2 == 1) { 140 String token = (String ) list.get(size - 1); 141 Integer integer = 142 (Integer ) stringToValueMap.get(token.toUpperCase()); 143 144 if (integer != null) 145 value = integer.intValue(); 146 else if (token.length() == 1) 147 value = token.toUpperCase().charAt(0); 148 149 if (value != 0) { 150 for (int i = 0; i < size - 1; i++) { 151 token = (String ) list.get(i); 152 153 if (i % 2 == 0) { 154 if (token.equalsIgnoreCase(CTRL)) { 155 if ((value & SWT.CTRL) != 0) 156 return 0; 157 158 value |= SWT.CTRL; 159 } else if (token.equalsIgnoreCase(ALT)) { 160 if ((value & SWT.ALT) != 0) 161 return 0; 162 163 value |= SWT.ALT; 164 } else if (token.equalsIgnoreCase(SHIFT)) { 165 if ((value & SWT.SHIFT) != 0) 166 return 0; 167 168 value |= SWT.SHIFT; 169 } else if (token.equalsIgnoreCase(COMMAND)) { 170 if ((value & SWT.COMMAND) != 0) 171 return 0; 172 173 value |= SWT.COMMAND; 174 } else 175 return 0; 176 } else if (!MODIFIER_SEPARATOR.equals(token)) 177 return 0; 178 } 179 } 180 } 181 182 return value; 183 } 184 185 static ActiveKeyConfigurationDefinition readActiveKeyConfigurationDefinition( 186 IMemento memento, 187 String sourceIdOverride) { 188 if (memento == null) 189 throw new NullPointerException (); 190 191 String keyConfigurationId = memento.getString(TAG_KEY_CONFIGURATION_ID); 192 193 if (keyConfigurationId == null) 195 keyConfigurationId = memento.getString("value"); 198 String sourceId = 199 sourceIdOverride != null 200 ? sourceIdOverride 201 : memento.getString(TAG_SOURCE_ID); 202 203 if (sourceIdOverride == null && sourceId == null) 205 sourceId = memento.getString("plugin"); 208 return new ActiveKeyConfigurationDefinition( 209 keyConfigurationId, 210 sourceId); 211 } 212 213 static List readActiveKeyConfigurationDefinitions( 214 IMemento memento, 215 String name, 216 String sourceIdOverride) { 217 if (memento == null || name == null) 218 throw new NullPointerException (); 219 220 IMemento[] mementos = memento.getChildren(name); 221 222 if (mementos == null) 223 throw new NullPointerException (); 224 225 List list = new ArrayList (mementos.length); 226 227 for (int i = 0; i < mementos.length; i++) 228 list.add( 229 readActiveKeyConfigurationDefinition( 230 mementos[i], 231 sourceIdOverride)); 232 233 return list; 234 } 235 236 static CategoryDefinition readCategoryDefinition( 237 IMemento memento, 238 String sourceIdOverride) { 239 if (memento == null) 240 throw new NullPointerException (); 241 242 String description = memento.getString(TAG_DESCRIPTION); 243 String id = memento.getString(TAG_ID); 244 String name = memento.getString(TAG_NAME); 245 String sourceId = 246 sourceIdOverride != null 247 ? sourceIdOverride 248 : memento.getString(TAG_SOURCE_ID); 249 250 if (sourceIdOverride == null && sourceId == null) 252 sourceId = memento.getString("plugin"); 255 return new CategoryDefinition(description, id, name, sourceId); 256 } 257 258 static List readCategoryDefinitions( 259 IMemento memento, 260 String name, 261 String sourceIdOverride) { 262 if (memento == null || name == null) 263 throw new NullPointerException (); 264 265 IMemento[] mementos = memento.getChildren(name); 266 267 if (mementos == null) 268 throw new NullPointerException (); 269 270 List list = new ArrayList (mementos.length); 271 272 for (int i = 0; i < mementos.length; i++) 273 list.add(readCategoryDefinition(mementos[i], sourceIdOverride)); 274 275 return list; 276 } 277 278 static CommandDefinition readCommandDefinition( 279 IMemento memento, 280 String sourceIdOverride) { 281 if (memento == null) 282 throw new NullPointerException (); 283 284 String categoryId = memento.getString(TAG_CATEGORY_ID); 285 286 if (categoryId == null) 288 categoryId = memento.getString("category"); 291 String description = memento.getString(TAG_DESCRIPTION); 292 String id = memento.getString(TAG_ID); 293 String name = memento.getString(TAG_NAME); 294 String sourceId = 295 sourceIdOverride != null 296 ? sourceIdOverride 297 : memento.getString(TAG_SOURCE_ID); 298 299 if (sourceIdOverride == null && sourceId == null) 301 sourceId = memento.getString("plugin"); 304 return new CommandDefinition( 305 categoryId, 306 description, 307 id, 308 name, 309 sourceId); 310 } 311 312 static List readCommandDefinitions( 313 IMemento memento, 314 String name, 315 String sourceIdOverride) { 316 if (memento == null || name == null) 317 throw new NullPointerException (); 318 319 IMemento[] mementos = memento.getChildren(name); 320 321 if (mementos == null) 322 throw new NullPointerException (); 323 324 List list = new ArrayList (mementos.length); 325 326 for (int i = 0; i < mementos.length; i++) 327 list.add(readCommandDefinition(mementos[i], sourceIdOverride)); 328 329 return list; 330 } 331 332 private static int[] readDeprecatedSequence(IMemento memento) { 333 if (memento == null) 334 throw new NullPointerException (); 335 336 IMemento[] mementos = memento.getChildren("stroke"); 338 if (mementos == null) 339 throw new NullPointerException (); 340 341 int[] strokes = new int[mementos.length]; 342 343 for (int i = 0; i < mementos.length; i++) 344 strokes[i] = readDeprecatedStroke(mementos[i]); 345 346 return strokes; 347 } 348 349 private static int readDeprecatedStroke(IMemento memento) { 350 if (memento == null) 351 throw new NullPointerException (); 352 353 Integer value = memento.getInteger("value"); return value != null ? value.intValue() : 0; 355 } 356 357 368 static IHandler readHandlerSubmissionDefinition( 369 IConfigurationElement configurationElement) { 370 final String commandId = configurationElement 371 .getAttribute(TAG_COMMAND_ID); 372 373 return new HandlerProxy(commandId, configurationElement); 374 } 375 376 static KeyConfigurationDefinition readKeyConfigurationDefinition( 377 IMemento memento, 378 String sourceIdOverride) { 379 if (memento == null) 380 throw new NullPointerException (); 381 382 String description = memento.getString(TAG_DESCRIPTION); 383 String id = memento.getString(TAG_ID); 384 String name = memento.getString(TAG_NAME); 385 String parentId = memento.getString(TAG_PARENT_ID); 386 387 if (parentId == null) 389 parentId = memento.getString("parent"); 392 String sourceId = 393 sourceIdOverride != null 394 ? sourceIdOverride 395 : memento.getString(TAG_SOURCE_ID); 396 397 if (sourceIdOverride == null && sourceId == null) 399 sourceId = memento.getString("plugin"); 402 return new KeyConfigurationDefinition( 403 description, 404 id, 405 name, 406 parentId, 407 sourceId); 408 } 409 410 static List readKeyConfigurationDefinitions( 411 IMemento memento, 412 String name, 413 String sourceIdOverride) { 414 if (memento == null || name == null) 415 throw new NullPointerException (); 416 417 IMemento[] mementos = memento.getChildren(name); 418 419 if (mementos == null) 420 throw new NullPointerException (); 421 422 List list = new ArrayList (mementos.length); 423 424 for (int i = 0; i < mementos.length; i++) 425 list.add( 426 readKeyConfigurationDefinition(mementos[i], sourceIdOverride)); 427 428 return list; 429 } 430 431 static KeySequenceBindingDefinition readKeySequenceBindingDefinition( 432 IMemento memento, 433 String sourceIdOverride) { 434 if (memento == null) 435 throw new NullPointerException (); 436 437 String contextId = memento.getString(TAG_CONTEXT_ID); 438 439 if (contextId == null) 441 contextId = memento.getString("scope"); 443 if ("org.eclipse.ui.globalScope".equals(contextId)) contextId = null; 445 447 String commandId = memento.getString(TAG_COMMAND_ID); 448 449 if (commandId == null) 451 commandId = memento.getString("command"); 453 if (commandId == null) 454 commandId = memento.getString("id"); 457 String keyConfigurationId = memento.getString(TAG_KEY_CONFIGURATION_ID); 458 459 if (keyConfigurationId == null) 461 keyConfigurationId = memento.getString("configuration"); 464 KeySequence keySequence = null; 465 String keySequenceAsString = memento.getString(TAG_KEY_SEQUENCE); 466 467 if (keySequenceAsString != null) 468 try { 469 keySequence = KeySequence.getInstance(keySequenceAsString); 470 } catch (ParseException eParse) { 471 } 472 else { 474 IMemento mementoSequence = memento.getChild("sequence"); 476 if (mementoSequence != null) 477 keySequence = 478 deprecatedSequenceToKeySequence( 479 readDeprecatedSequence(mementoSequence)); 480 else { 481 String string = memento.getString("string"); 483 if (string != null) 484 keySequence = 485 deprecatedSequenceToKeySequence( 486 parseDeprecatedSequence(string)); 487 } 488 } 490 491 String locale = memento.getString(TAG_LOCALE); 492 String platform = memento.getString(TAG_PLATFORM); 493 String sourceId = 494 sourceIdOverride != null 495 ? sourceIdOverride 496 : memento.getString(TAG_SOURCE_ID); 497 498 if (sourceIdOverride == null && sourceId == null) 500 sourceId = memento.getString("plugin"); 503 if (contextId == null) { 505 contextId = KeySequenceBinding.DEFAULT_CONTEXT_ID; 506 } 507 508 return new KeySequenceBindingDefinition( 509 contextId, 510 commandId, 511 keyConfigurationId, 512 keySequence, 513 locale, 514 platform, 515 sourceId); 516 } 517 518 static List readKeySequenceBindingDefinitions( 519 IMemento memento, 520 String name, 521 String sourceIdOverride) { 522 if (memento == null || name == null) 523 throw new NullPointerException (); 524 525 IMemento[] mementos = memento.getChildren(name); 526 527 if (mementos == null) 528 throw new NullPointerException (); 529 530 List list = new ArrayList (mementos.length); 531 532 for (int i = 0; i < mementos.length; i++) 533 list.add( 534 readKeySequenceBindingDefinition( 535 mementos[i], 536 sourceIdOverride)); 537 538 return list; 539 } 540 541 static void writeActiveKeyConfigurationDefinition( 542 IMemento memento, 543 ActiveKeyConfigurationDefinition activeKeyConfigurationDefinition) { 544 if (memento == null || activeKeyConfigurationDefinition == null) 545 throw new NullPointerException (); 546 547 memento.putString( 548 TAG_KEY_CONFIGURATION_ID, 549 activeKeyConfigurationDefinition.getKeyConfigurationId()); 550 memento.putString( 551 TAG_SOURCE_ID, 552 activeKeyConfigurationDefinition.getSourceId()); 553 } 554 555 static void writeActiveKeyConfigurationDefinitions( 556 IMemento memento, 557 String name, 558 List activeKeyConfigurationDefinitions) { 559 if (memento == null 560 || name == null 561 || activeKeyConfigurationDefinitions == null) 562 throw new NullPointerException (); 563 564 activeKeyConfigurationDefinitions = 565 new ArrayList (activeKeyConfigurationDefinitions); 566 Iterator iterator = activeKeyConfigurationDefinitions.iterator(); 567 568 while (iterator.hasNext()) 569 Util.assertInstance( 570 iterator.next(), 571 ActiveKeyConfigurationDefinition.class); 572 573 iterator = activeKeyConfigurationDefinitions.iterator(); 574 575 while (iterator.hasNext()) 576 writeActiveKeyConfigurationDefinition( 577 memento.createChild(name), 578 (ActiveKeyConfigurationDefinition) iterator.next()); 579 } 580 581 static void writeCategoryDefinition( 582 IMemento memento, 583 CategoryDefinition categoryDefinition) { 584 if (memento == null || categoryDefinition == null) 585 throw new NullPointerException (); 586 587 memento.putString(TAG_DESCRIPTION, categoryDefinition.getDescription()); 588 memento.putString(TAG_ID, categoryDefinition.getId()); 589 memento.putString(TAG_NAME, categoryDefinition.getName()); 590 memento.putString(TAG_SOURCE_ID, categoryDefinition.getSourceId()); 591 } 592 593 static void writeCategoryDefinitions( 594 IMemento memento, 595 String name, 596 List categoryDefinitions) { 597 if (memento == null || name == null || categoryDefinitions == null) 598 throw new NullPointerException (); 599 600 categoryDefinitions = new ArrayList (categoryDefinitions); 601 Iterator iterator = categoryDefinitions.iterator(); 602 603 while (iterator.hasNext()) 604 Util.assertInstance(iterator.next(), CategoryDefinition.class); 605 606 iterator = categoryDefinitions.iterator(); 607 608 while (iterator.hasNext()) 609 writeCategoryDefinition( 610 memento.createChild(name), 611 (CategoryDefinition) iterator.next()); 612 } 613 614 static void writeCommandDefinition( 615 IMemento memento, 616 CommandDefinition commandDefinition) { 617 if (memento == null || commandDefinition == null) 618 throw new NullPointerException (); 619 620 memento.putString(TAG_CATEGORY_ID, commandDefinition.getCategoryId()); 621 memento.putString(TAG_DESCRIPTION, commandDefinition.getDescription()); 622 memento.putString(TAG_ID, commandDefinition.getId()); 623 memento.putString(TAG_NAME, commandDefinition.getName()); 624 memento.putString(TAG_SOURCE_ID, commandDefinition.getSourceId()); 625 } 626 627 static void writeCommandDefinitions( 628 IMemento memento, 629 String name, 630 List commandDefinitions) { 631 if (memento == null || name == null || commandDefinitions == null) 632 throw new NullPointerException (); 633 634 commandDefinitions = new ArrayList (commandDefinitions); 635 Iterator iterator = commandDefinitions.iterator(); 636 637 while (iterator.hasNext()) 638 Util.assertInstance(iterator.next(), CommandDefinition.class); 639 640 iterator = commandDefinitions.iterator(); 641 642 while (iterator.hasNext()) 643 writeCommandDefinition( 644 memento.createChild(name), 645 (CommandDefinition) iterator.next()); 646 } 647 648 static void writeKeyConfigurationDefinition( 649 IMemento memento, 650 KeyConfigurationDefinition keyConfigurationDefinition) { 651 if (memento == null || keyConfigurationDefinition == null) 652 throw new NullPointerException (); 653 654 memento.putString( 655 TAG_DESCRIPTION, 656 keyConfigurationDefinition.getDescription()); 657 memento.putString(TAG_ID, keyConfigurationDefinition.getId()); 658 memento.putString(TAG_NAME, keyConfigurationDefinition.getName()); 659 memento.putString( 660 TAG_PARENT_ID, 661 keyConfigurationDefinition.getParentId()); 662 memento.putString( 663 TAG_SOURCE_ID, 664 keyConfigurationDefinition.getSourceId()); 665 } 666 667 static void writeKeyConfigurationDefinitions( 668 IMemento memento, 669 String name, 670 List keyConfigurationDefinitions) { 671 if (memento == null 672 || name == null 673 || keyConfigurationDefinitions == null) 674 throw new NullPointerException (); 675 676 keyConfigurationDefinitions = 677 new ArrayList (keyConfigurationDefinitions); 678 Iterator iterator = keyConfigurationDefinitions.iterator(); 679 680 while (iterator.hasNext()) 681 Util.assertInstance( 682 iterator.next(), 683 KeyConfigurationDefinition.class); 684 685 iterator = keyConfigurationDefinitions.iterator(); 686 687 while (iterator.hasNext()) 688 writeKeyConfigurationDefinition( 689 memento.createChild(name), 690 (KeyConfigurationDefinition) iterator.next()); 691 } 692 693 static void writeKeySequenceBindingDefinition( 694 IMemento memento, 695 KeySequenceBindingDefinition keySequenceBindingDefinition) { 696 if (memento == null || keySequenceBindingDefinition == null) 697 throw new NullPointerException (); 698 699 memento.putString( 700 TAG_CONTEXT_ID, 701 keySequenceBindingDefinition.getContextId()); 702 memento.putString( 703 TAG_COMMAND_ID, 704 keySequenceBindingDefinition.getCommandId()); 705 memento.putString( 706 TAG_KEY_CONFIGURATION_ID, 707 keySequenceBindingDefinition.getKeyConfigurationId()); 708 memento.putString( 709 TAG_KEY_SEQUENCE, 710 keySequenceBindingDefinition.getKeySequence() != null 711 ? keySequenceBindingDefinition.getKeySequence().toString() 712 : null); 713 memento.putString(TAG_LOCALE, keySequenceBindingDefinition.getLocale()); 714 memento.putString( 715 TAG_PLATFORM, 716 keySequenceBindingDefinition.getPlatform()); 717 memento.putString( 718 TAG_SOURCE_ID, 719 keySequenceBindingDefinition.getSourceId()); 720 } 721 722 static void writeKeySequenceBindingDefinitions( 723 IMemento memento, 724 String name, 725 List keySequenceBindingDefinitions) { 726 if (memento == null 727 || name == null 728 || keySequenceBindingDefinitions == null) 729 throw new NullPointerException (); 730 731 keySequenceBindingDefinitions = 732 new ArrayList (keySequenceBindingDefinitions); 733 Iterator iterator = keySequenceBindingDefinitions.iterator(); 734 735 while (iterator.hasNext()) 736 Util.assertInstance( 737 iterator.next(), 738 KeySequenceBindingDefinition.class); 739 740 iterator = keySequenceBindingDefinitions.iterator(); 741 742 while (iterator.hasNext()) 743 writeKeySequenceBindingDefinition( 744 memento.createChild(name), 745 (KeySequenceBindingDefinition) iterator.next()); 746 } 747 748 private Persistence() { 749 } 750 } 751 | Popular Tags |