1 11 12 package org.eclipse.ui.internal.keys; 13 14 import java.io.BufferedWriter ; 15 import java.io.FileWriter ; 16 import java.io.IOException ; 17 import java.io.Writer ; 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Comparator ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.ResourceBundle ; 29 import java.util.Set ; 30 31 import org.eclipse.core.commands.Category; 32 import org.eclipse.core.commands.Command; 33 import org.eclipse.core.commands.CommandManager; 34 import org.eclipse.core.commands.ParameterizedCommand; 35 import org.eclipse.core.commands.common.NotDefinedException; 36 import org.eclipse.core.commands.contexts.Context; 37 import org.eclipse.core.commands.contexts.ContextManager; 38 import org.eclipse.core.runtime.IStatus; 39 import org.eclipse.core.runtime.SafeRunner; 40 import org.eclipse.core.runtime.Status; 41 import org.eclipse.jface.bindings.Binding; 42 import org.eclipse.jface.bindings.BindingManager; 43 import org.eclipse.jface.bindings.Scheme; 44 import org.eclipse.jface.bindings.TriggerSequence; 45 import org.eclipse.jface.bindings.keys.KeyBinding; 46 import org.eclipse.jface.bindings.keys.KeySequence; 47 import org.eclipse.jface.bindings.keys.KeySequenceText; 48 import org.eclipse.jface.bindings.keys.KeyStroke; 49 import org.eclipse.jface.contexts.IContextIds; 50 import org.eclipse.jface.dialogs.IDialogConstants; 51 import org.eclipse.jface.dialogs.MessageDialog; 52 import org.eclipse.jface.preference.IPreferenceStore; 53 import org.eclipse.jface.preference.PreferencePage; 54 import org.eclipse.jface.util.SafeRunnable; 55 import org.eclipse.swt.SWT; 56 import org.eclipse.swt.events.DisposeEvent; 57 import org.eclipse.swt.events.DisposeListener; 58 import org.eclipse.swt.events.FocusEvent; 59 import org.eclipse.swt.events.FocusListener; 60 import org.eclipse.swt.events.ModifyEvent; 61 import org.eclipse.swt.events.ModifyListener; 62 import org.eclipse.swt.events.MouseAdapter; 63 import org.eclipse.swt.events.MouseEvent; 64 import org.eclipse.swt.events.SelectionAdapter; 65 import org.eclipse.swt.events.SelectionEvent; 66 import org.eclipse.swt.events.SelectionListener; 67 import org.eclipse.swt.graphics.Image; 68 import org.eclipse.swt.graphics.Point; 69 import org.eclipse.swt.layout.GridData; 70 import org.eclipse.swt.layout.GridLayout; 71 import org.eclipse.swt.widgets.Button; 72 import org.eclipse.swt.widgets.Combo; 73 import org.eclipse.swt.widgets.Composite; 74 import org.eclipse.swt.widgets.Control; 75 import org.eclipse.swt.widgets.FileDialog; 76 import org.eclipse.swt.widgets.Group; 77 import org.eclipse.swt.widgets.Label; 78 import org.eclipse.swt.widgets.Menu; 79 import org.eclipse.swt.widgets.MenuItem; 80 import org.eclipse.swt.widgets.TabFolder; 81 import org.eclipse.swt.widgets.TabItem; 82 import org.eclipse.swt.widgets.Table; 83 import org.eclipse.swt.widgets.TableColumn; 84 import org.eclipse.swt.widgets.TableItem; 85 import org.eclipse.swt.widgets.Text; 86 import org.eclipse.ui.IWorkbench; 87 import org.eclipse.ui.IWorkbenchPreferencePage; 88 import org.eclipse.ui.PlatformUI; 89 import org.eclipse.ui.activities.IActivityManager; 90 import org.eclipse.ui.commands.ICommandService; 91 import org.eclipse.ui.contexts.IContextService; 92 import org.eclipse.ui.internal.IPreferenceConstants; 93 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 94 import org.eclipse.ui.internal.WorkbenchPlugin; 95 import org.eclipse.ui.internal.misc.StatusUtil; 96 import org.eclipse.ui.internal.util.PrefUtil; 97 import org.eclipse.ui.internal.util.Util; 98 import org.eclipse.ui.keys.IBindingService; 99 import org.eclipse.ui.statushandlers.StatusManager; 100 101 import com.ibm.icu.text.Collator; 102 import com.ibm.icu.text.MessageFormat; 103 104 111 public final class KeysPreferencePage extends PreferencePage implements 112 IWorkbenchPreferencePage { 113 114 121 private class SortOrderSelectionListener extends SelectionAdapter { 122 123 127 private final int columnSelected; 128 129 137 private SortOrderSelectionListener(final int columnSelected) { 138 this.columnSelected = columnSelected; 139 } 140 141 146 public void widgetSelected(SelectionEvent e) { 147 final int oldSortIndex = sortOrder[0]; 149 final TableColumn oldSortColumn = tableBindings 150 .getColumn(oldSortIndex); 151 oldSortColumn.setText(UNSORTED_COLUMN_NAMES[oldSortIndex]); 152 final TableColumn newSortColumn = tableBindings 153 .getColumn(columnSelected); 154 newSortColumn.setText(SORTED_COLUMN_NAMES[columnSelected]); 155 156 boolean columnPlaced = false; 158 boolean enoughRoom = false; 159 int bumpedColumn = -1; 160 for (int i = 0; i < sortOrder.length; i++) { 161 if (sortOrder[i] == columnSelected) { 162 167 enoughRoom = true; 168 if (bumpedColumn != -1) { 169 sortOrder[i] = bumpedColumn; 172 } else { 173 columnPlaced = true; 175 } 176 break; 177 178 } else if (columnPlaced) { 179 int temp = sortOrder[i]; 181 sortOrder[i] = bumpedColumn; 182 bumpedColumn = temp; 183 184 } else { 185 189 bumpedColumn = sortOrder[i]; 190 sortOrder[i] = columnSelected; 191 columnPlaced = true; 192 } 193 } 194 195 if (!enoughRoom) { 197 final int[] newSortOrder = new int[sortOrder.length + 1]; 198 System.arraycopy(sortOrder, 0, newSortOrder, 0, 199 sortOrder.length); 200 newSortOrder[sortOrder.length] = bumpedColumn; 201 sortOrder = newSortOrder; 202 } 203 204 updateViewTab(); 206 } 207 } 208 209 214 private static final String BINDING_KEY = "Binding.bindings.jface.eclipse.org"; 216 220 private static final Image IMAGE_BLANK = ImageFactory.getImage("blank"); 222 225 private static final Image IMAGE_CHANGE = ImageFactory.getImage("change"); 227 231 private static final String ITEM_DATA_KEY = "org.eclipse.jface.bindings"; 233 236 private static final int ITEMS_TO_SHOW = 9; 237 238 241 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle 242 .getBundle(KeysPreferencePage.class.getName()); 243 244 247 private static final int VIEW_TOTAL_COLUMNS = 4; 248 249 253 private static final String [] SORTED_COLUMN_NAMES = new String [VIEW_TOTAL_COLUMNS]; 254 255 260 private static final int TAB_INDEX_MODIFY = 1; 261 262 266 private static final String [] UNSORTED_COLUMN_NAMES = new String [VIEW_TOTAL_COLUMNS]; 267 268 271 private static final int VIEW_CATEGORY_COLUMN_INDEX = 0; 272 273 276 private static final int VIEW_COMMAND_COLUMN_INDEX = 1; 277 278 281 private static final int VIEW_CONTEXT_COLUMN_INDEX = 3; 282 283 286 private static final int VIEW_KEY_SEQUENCE_COLUMN_INDEX = 2; 287 288 static { 289 UNSORTED_COLUMN_NAMES[VIEW_CATEGORY_COLUMN_INDEX] = Util 290 .translateString(RESOURCE_BUNDLE, "tableColumnCategory"); UNSORTED_COLUMN_NAMES[VIEW_COMMAND_COLUMN_INDEX] = Util 292 .translateString(RESOURCE_BUNDLE, "tableColumnCommand"); UNSORTED_COLUMN_NAMES[VIEW_KEY_SEQUENCE_COLUMN_INDEX] = Util 294 .translateString(RESOURCE_BUNDLE, "tableColumnKeySequence"); UNSORTED_COLUMN_NAMES[VIEW_CONTEXT_COLUMN_INDEX] = Util 296 .translateString(RESOURCE_BUNDLE, "tableColumnContext"); 298 SORTED_COLUMN_NAMES[VIEW_CATEGORY_COLUMN_INDEX] = Util.translateString( 299 RESOURCE_BUNDLE, "tableColumnCategorySorted"); SORTED_COLUMN_NAMES[VIEW_COMMAND_COLUMN_INDEX] = Util.translateString( 301 RESOURCE_BUNDLE, "tableColumnCommandSorted"); SORTED_COLUMN_NAMES[VIEW_KEY_SEQUENCE_COLUMN_INDEX] = Util 303 .translateString(RESOURCE_BUNDLE, 304 "tableColumnKeySequenceSorted"); SORTED_COLUMN_NAMES[VIEW_CONTEXT_COLUMN_INDEX] = Util.translateString( 306 RESOURCE_BUNDLE, "tableColumnContextSorted"); } 308 309 313 private IActivityManager activityManager; 314 315 319 private IBindingService bindingService; 320 321 326 private Button buttonAdd; 327 328 332 private Button buttonRemove; 333 334 339 private Button buttonRestore; 340 341 345 private Map categoryIdsByUniqueName; 346 347 351 private Map categoryUniqueNamesById; 352 353 356 private Combo comboCategory; 357 358 362 private Combo comboCommand; 363 364 367 private Combo comboContext; 368 369 372 private Combo comboScheme; 373 374 378 private Map commandIdsByCategoryId; 379 380 386 private ParameterizedCommand[] commands = null; 387 388 392 private ICommandService commandService; 393 394 398 private Map contextIdsByUniqueName; 399 400 404 private IContextService contextService; 405 406 410 private Map contextUniqueNamesById; 411 412 418 424 private Label labelBindingsForCommand; 425 426 431 private Label labelBindingsForTriggerSequence; 432 433 438 private Label labelContextExtends; 439 440 445 private Label labelSchemeExtends; 446 447 455 private final BindingManager localChangeManager = new BindingManager( 456 new ContextManager(), new CommandManager()); 457 458 462 private Map schemeIdsByUniqueName; 463 464 468 private Map schemeUniqueNamesById; 469 470 475 private int[] sortOrder = { VIEW_CATEGORY_COLUMN_INDEX, 476 VIEW_COMMAND_COLUMN_INDEX, VIEW_KEY_SEQUENCE_COLUMN_INDEX, 477 VIEW_CONTEXT_COLUMN_INDEX }; 478 479 483 private TabFolder tabFolder; 484 485 491 private Table tableBindings; 492 493 496 private Table tableBindingsForCommand; 497 498 502 private Table tableBindingsForTriggerSequence; 503 504 509 private Text textTriggerSequence; 510 511 516 private KeySequenceText textTriggerSequenceManager; 517 518 519 522 public void applyData(Object data) { 523 if(data instanceof Binding) { 524 editBinding((Binding) data); 525 } 526 } 527 protected final Control createContents(final Composite parent) { 528 529 PlatformUI.getWorkbench().getHelpSystem() 530 .setHelp(parent, IWorkbenchHelpContextIds.KEYS_PREFERENCE_PAGE); 531 532 tabFolder = new TabFolder(parent, SWT.NULL); 533 534 final TabItem viewTab = new TabItem(tabFolder, SWT.NULL); 536 viewTab.setText(Util.translateString(RESOURCE_BUNDLE, "viewTab.Text")); viewTab.setControl(createViewTab(tabFolder)); 538 539 final TabItem modifyTab = new TabItem(tabFolder, SWT.NULL); 541 modifyTab.setText(Util.translateString(RESOURCE_BUNDLE, 542 "modifyTab.Text")); modifyTab.setControl(createModifyTab(tabFolder)); 544 545 applyDialogFont(tabFolder); 547 final IPreferenceStore store = getPreferenceStore(); 548 final int selectedTab = store 549 .getInt(IPreferenceConstants.KEYS_PREFERENCE_SELECTED_TAB); 550 if ((tabFolder.getItemCount() > selectedTab) && (selectedTab > 0)) { 551 tabFolder.setSelection(selectedTab); 552 } 553 554 return tabFolder; 555 } 556 557 566 private final Composite createModifyTab(final TabFolder parent) { 567 final Composite composite = new Composite(parent, SWT.NULL); 568 composite.setLayout(new GridLayout()); 569 GridData gridData = new GridData(GridData.FILL_BOTH); 570 composite.setLayoutData(gridData); 571 final Composite compositeKeyConfiguration = new Composite(composite, 572 SWT.NULL); 573 GridLayout gridLayout = new GridLayout(); 574 gridLayout.numColumns = 3; 575 compositeKeyConfiguration.setLayout(gridLayout); 576 gridData = new GridData(GridData.FILL_HORIZONTAL); 577 compositeKeyConfiguration.setLayoutData(gridData); 578 final Label labelKeyConfiguration = new Label( 579 compositeKeyConfiguration, SWT.LEFT); 580 labelKeyConfiguration.setText(Util.translateString(RESOURCE_BUNDLE, 581 "labelScheme")); comboScheme = new Combo(compositeKeyConfiguration, SWT.READ_ONLY); 583 gridData = new GridData(); 584 gridData.widthHint = 200; 585 comboScheme.setLayoutData(gridData); 586 comboScheme.setVisibleItemCount(ITEMS_TO_SHOW); 587 588 comboScheme.addSelectionListener(new SelectionAdapter() { 589 public final void widgetSelected(final SelectionEvent e) { 590 selectedComboScheme(); 591 } 592 }); 593 594 labelSchemeExtends = new Label(compositeKeyConfiguration, SWT.LEFT); 595 gridData = new GridData(GridData.FILL_HORIZONTAL); 596 labelSchemeExtends.setLayoutData(gridData); 597 final Control spacer = new Composite(composite, SWT.NULL); 598 gridData = new GridData(); 599 gridData.heightHint = 10; 600 gridData.widthHint = 10; 601 spacer.setLayoutData(gridData); 602 final Group groupCommand = new Group(composite, SWT.SHADOW_NONE); 603 gridLayout = new GridLayout(); 604 gridLayout.numColumns = 3; 605 groupCommand.setLayout(gridLayout); 606 gridData = new GridData(GridData.FILL_BOTH); 607 groupCommand.setLayoutData(gridData); 608 groupCommand.setText(Util.translateString(RESOURCE_BUNDLE, 609 "groupCommand")); final Label labelCategory = new Label(groupCommand, SWT.LEFT); 611 gridData = new GridData(); 612 labelCategory.setLayoutData(gridData); 613 labelCategory.setText(Util.translateString(RESOURCE_BUNDLE, 614 "labelCategory")); comboCategory = new Combo(groupCommand, SWT.READ_ONLY); 616 gridData = new GridData(); 617 gridData.horizontalSpan = 2; 618 gridData.widthHint = 200; 619 comboCategory.setLayoutData(gridData); 620 comboCategory.setVisibleItemCount(ITEMS_TO_SHOW); 621 622 comboCategory.addSelectionListener(new SelectionAdapter() { 623 public final void widgetSelected(final SelectionEvent e) { 624 update(); 625 } 626 }); 627 628 final Label labelCommand = new Label(groupCommand, SWT.LEFT); 629 gridData = new GridData(); 630 labelCommand.setLayoutData(gridData); 631 labelCommand.setText(Util.translateString(RESOURCE_BUNDLE, 632 "labelCommand")); comboCommand = new Combo(groupCommand, SWT.READ_ONLY); 634 gridData = new GridData(); 635 gridData.horizontalSpan = 2; 636 gridData.widthHint = 300; 637 comboCommand.setLayoutData(gridData); 638 comboCommand.setVisibleItemCount(9); 639 640 comboCommand.addSelectionListener(new SelectionAdapter() { 641 public final void widgetSelected(final SelectionEvent e) { 642 update(); 643 } 644 }); 645 646 labelBindingsForCommand = new Label(groupCommand, SWT.LEFT); 647 gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 648 gridData.verticalAlignment = GridData.FILL_VERTICAL; 649 labelBindingsForCommand.setLayoutData(gridData); 650 labelBindingsForCommand.setText(Util.translateString(RESOURCE_BUNDLE, 651 "labelAssignmentsForCommand")); tableBindingsForCommand = new Table(groupCommand, SWT.BORDER 653 | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 654 tableBindingsForCommand.setHeaderVisible(true); 655 gridData = new GridData(GridData.FILL_BOTH); 656 gridData.heightHint = 60; 657 gridData.horizontalSpan = 2; 658 gridData.widthHint = "carbon".equals(SWT.getPlatform()) ? 620 : 520; tableBindingsForCommand.setLayoutData(gridData); 660 TableColumn tableColumnDelta = new TableColumn(tableBindingsForCommand, 661 SWT.NULL, 0); 662 tableColumnDelta.setResizable(false); 663 tableColumnDelta.setText(Util.ZERO_LENGTH_STRING); 664 tableColumnDelta.setWidth(20); 665 TableColumn tableColumnContext = new TableColumn( 666 tableBindingsForCommand, SWT.NULL, 1); 667 tableColumnContext.setResizable(true); 668 tableColumnContext.setText(Util.translateString(RESOURCE_BUNDLE, 669 "tableColumnContext")); tableColumnContext.pack(); 671 tableColumnContext.setWidth(200); 672 final TableColumn tableColumnKeySequence = new TableColumn( 673 tableBindingsForCommand, SWT.NULL, 2); 674 tableColumnKeySequence.setResizable(true); 675 tableColumnKeySequence.setText(Util.translateString(RESOURCE_BUNDLE, 676 "tableColumnKeySequence")); tableColumnKeySequence.pack(); 678 tableColumnKeySequence.setWidth(300); 679 680 tableBindingsForCommand.addMouseListener(new MouseAdapter() { 681 682 public void mouseDoubleClick(MouseEvent mouseEvent) { 683 update(); 684 } 685 }); 686 687 tableBindingsForCommand.addSelectionListener(new SelectionAdapter() { 688 689 public void widgetSelected(SelectionEvent selectionEvent) { 690 selectedTableBindingsForCommand(); 691 } 692 }); 693 694 final Group groupKeySequence = new Group(composite, SWT.SHADOW_NONE); 695 gridLayout = new GridLayout(); 696 gridLayout.numColumns = 4; 697 groupKeySequence.setLayout(gridLayout); 698 gridData = new GridData(GridData.FILL_BOTH); 699 groupKeySequence.setLayoutData(gridData); 700 groupKeySequence.setText(Util.translateString(RESOURCE_BUNDLE, 701 "groupKeySequence")); final Label labelKeySequence = new Label(groupKeySequence, SWT.LEFT); 703 gridData = new GridData(); 704 labelKeySequence.setLayoutData(gridData); 705 labelKeySequence.setText(Util.translateString(RESOURCE_BUNDLE, 706 "labelKeySequence")); 708 textTriggerSequence = new Text(groupKeySequence, SWT.BORDER); 710 textTriggerSequence.setFont(groupKeySequence.getFont()); 712 gridData = new GridData(); 713 gridData.horizontalSpan = 2; 714 gridData.widthHint = 300; 715 textTriggerSequence.setLayoutData(gridData); 716 textTriggerSequence.addModifyListener(new ModifyListener() { 717 public void modifyText(ModifyEvent e) { 718 update(); 719 } 720 }); 721 textTriggerSequence.addFocusListener(new FocusListener() { 722 public void focusGained(FocusEvent e) { 723 bindingService.setKeyFilterEnabled(false); 724 } 725 726 public void focusLost(FocusEvent e) { 727 bindingService.setKeyFilterEnabled(true); 728 } 729 }); 730 textTriggerSequence.addDisposeListener(new DisposeListener() { 731 public void widgetDisposed(DisposeEvent e) { 732 if (!bindingService.isKeyFilterEnabled()) { 733 bindingService.setKeyFilterEnabled(true); 734 } 735 } 736 }); 737 738 textTriggerSequenceManager = new KeySequenceText(textTriggerSequence); 740 textTriggerSequenceManager.setKeyStrokeLimit(4); 741 742 final Button buttonAddKey = new Button(groupKeySequence, SWT.LEFT 744 | SWT.ARROW); 745 buttonAddKey.setToolTipText(Util.translateString(RESOURCE_BUNDLE, 746 "buttonAddKey.ToolTipText")); gridData = new GridData(); 748 gridData.heightHint = comboCategory.getTextHeight(); 749 buttonAddKey.setLayoutData(gridData); 750 751 final Control[] tabStops = groupKeySequence.getTabList(); 753 final ArrayList newTabStops = new ArrayList (); 754 for (int i = 0; i < tabStops.length; i++) { 755 Control tabStop = tabStops[i]; 756 newTabStops.add(tabStop); 757 if (textTriggerSequence.equals(tabStop)) { 758 newTabStops.add(buttonAddKey); 759 } 760 } 761 final Control[] newTabStopArray = (Control[]) newTabStops 762 .toArray(new Control[newTabStops.size()]); 763 groupKeySequence.setTabList(newTabStopArray); 764 765 final Menu menuButtonAddKey = new Menu(buttonAddKey); 767 final Iterator trappedKeyItr = KeySequenceText.TRAPPED_KEYS.iterator(); 768 while (trappedKeyItr.hasNext()) { 769 final KeyStroke trappedKey = (KeyStroke) trappedKeyItr.next(); 770 final MenuItem menuItem = new MenuItem(menuButtonAddKey, SWT.PUSH); 771 menuItem.setText(trappedKey.format()); 772 menuItem.addSelectionListener(new SelectionAdapter() { 773 774 public void widgetSelected(SelectionEvent e) { 775 textTriggerSequenceManager.insert(trappedKey); 776 textTriggerSequence.setFocus(); 777 textTriggerSequence.setSelection(textTriggerSequence 778 .getTextLimit()); 779 } 780 }); 781 } 782 buttonAddKey.addSelectionListener(new SelectionAdapter() { 783 784 public void widgetSelected(SelectionEvent selectionEvent) { 785 Point buttonLocation = buttonAddKey.getLocation(); 786 buttonLocation = groupKeySequence.toDisplay(buttonLocation.x, 787 buttonLocation.y); 788 Point buttonSize = buttonAddKey.getSize(); 789 menuButtonAddKey.setLocation(buttonLocation.x, buttonLocation.y 790 + buttonSize.y); 791 menuButtonAddKey.setVisible(true); 792 } 793 }); 794 795 labelBindingsForTriggerSequence = new Label(groupKeySequence, SWT.LEFT); 796 gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 797 gridData.verticalAlignment = GridData.FILL_VERTICAL; 798 labelBindingsForTriggerSequence.setLayoutData(gridData); 799 labelBindingsForTriggerSequence.setText(Util.translateString( 800 RESOURCE_BUNDLE, "labelAssignmentsForKeySequence")); tableBindingsForTriggerSequence = new Table(groupKeySequence, 802 SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 803 tableBindingsForTriggerSequence.setHeaderVisible(true); 804 gridData = new GridData(GridData.FILL_BOTH); 805 gridData.heightHint = 60; 806 gridData.horizontalSpan = 3; 807 gridData.widthHint = "carbon".equals(SWT.getPlatform()) ? 620 : 520; tableBindingsForTriggerSequence.setLayoutData(gridData); 809 tableColumnDelta = new TableColumn(tableBindingsForTriggerSequence, 810 SWT.NULL, 0); 811 tableColumnDelta.setResizable(false); 812 tableColumnDelta.setText(Util.ZERO_LENGTH_STRING); 813 tableColumnDelta.setWidth(20); 814 tableColumnContext = new TableColumn(tableBindingsForTriggerSequence, 815 SWT.NULL, 1); 816 tableColumnContext.setResizable(true); 817 tableColumnContext.setText(Util.translateString(RESOURCE_BUNDLE, 818 "tableColumnContext")); tableColumnContext.pack(); 820 tableColumnContext.setWidth(200); 821 final TableColumn tableColumnCommand = new TableColumn( 822 tableBindingsForTriggerSequence, SWT.NULL, 2); 823 tableColumnCommand.setResizable(true); 824 tableColumnCommand.setText(Util.translateString(RESOURCE_BUNDLE, 825 "tableColumnCommand")); tableColumnCommand.pack(); 827 tableColumnCommand.setWidth(300); 828 829 tableBindingsForTriggerSequence.addMouseListener(new MouseAdapter() { 830 831 public void mouseDoubleClick(MouseEvent mouseEvent) { 832 update(); 833 } 834 }); 835 836 tableBindingsForTriggerSequence 837 .addSelectionListener(new SelectionAdapter() { 838 839 public void widgetSelected(SelectionEvent selectionEvent) { 840 selectedTableBindingsForTriggerSequence(); 841 } 842 }); 843 844 final Composite compositeContext = new Composite(composite, SWT.NULL); 845 gridLayout = new GridLayout(); 846 gridLayout.numColumns = 3; 847 compositeContext.setLayout(gridLayout); 848 gridData = new GridData(GridData.FILL_HORIZONTAL); 849 compositeContext.setLayoutData(gridData); 850 final Label labelContext = new Label(compositeContext, SWT.LEFT); 851 labelContext.setText(Util.translateString(RESOURCE_BUNDLE, 852 "labelContext")); comboContext = new Combo(compositeContext, SWT.READ_ONLY); 854 gridData = new GridData(); 855 gridData.widthHint = 250; 856 comboContext.setLayoutData(gridData); 857 comboContext.setVisibleItemCount(ITEMS_TO_SHOW); 858 859 comboContext.addSelectionListener(new SelectionAdapter() { 860 public final void widgetSelected(final SelectionEvent e) { 861 update(); 862 } 863 }); 864 865 labelContextExtends = new Label(compositeContext, SWT.LEFT); 866 gridData = new GridData(GridData.FILL_HORIZONTAL); 867 labelContextExtends.setLayoutData(gridData); 868 final Composite compositeButton = new Composite(composite, SWT.NULL); 869 gridLayout = new GridLayout(); 870 gridLayout.marginHeight = 20; 871 gridLayout.marginWidth = 0; 872 gridLayout.numColumns = 3; 873 compositeButton.setLayout(gridLayout); 874 gridData = new GridData(); 875 compositeButton.setLayoutData(gridData); 876 buttonAdd = new Button(compositeButton, SWT.CENTER | SWT.PUSH); 877 gridData = new GridData(); 878 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 879 buttonAdd.setText(Util.translateString(RESOURCE_BUNDLE, "buttonAdd")); gridData.widthHint = Math.max(widthHint, buttonAdd.computeSize( 881 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5; 882 buttonAdd.setLayoutData(gridData); 883 884 buttonAdd.addSelectionListener(new SelectionAdapter() { 885 886 public void widgetSelected(SelectionEvent selectionEvent) { 887 selectedButtonAdd(); 888 } 889 }); 890 891 buttonRemove = new Button(compositeButton, SWT.CENTER | SWT.PUSH); 892 gridData = new GridData(); 893 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 894 buttonRemove.setText(Util.translateString(RESOURCE_BUNDLE, 895 "buttonRemove")); gridData.widthHint = Math.max(widthHint, buttonRemove.computeSize( 897 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5; 898 buttonRemove.setLayoutData(gridData); 899 900 buttonRemove.addSelectionListener(new SelectionAdapter() { 901 902 public void widgetSelected(SelectionEvent selectionEvent) { 903 selectedButtonRemove(); 904 } 905 }); 906 907 buttonRestore = new Button(compositeButton, SWT.CENTER | SWT.PUSH); 908 gridData = new GridData(); 909 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 910 buttonRestore.setText(Util.translateString(RESOURCE_BUNDLE, 911 "buttonRestore")); gridData.widthHint = Math.max(widthHint, buttonRestore.computeSize( 913 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5; 914 buttonRestore.setLayoutData(gridData); 915 916 buttonRestore.addSelectionListener(new SelectionAdapter() { 917 918 public void widgetSelected(SelectionEvent selectionEvent) { 919 selectedButtonRestore(); 920 } 921 }); 922 923 return composite; 924 } 925 926 939 private final Composite createViewTab(final TabFolder parent) { 940 GridData gridData = null; 941 int widthHint; 942 943 final Composite composite = new Composite(parent, SWT.NONE); 945 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 946 composite.setLayout(new GridLayout()); 947 948 tableBindings = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION 950 | SWT.H_SCROLL | SWT.V_SCROLL); 951 tableBindings.setHeaderVisible(true); 952 gridData = new GridData(GridData.FILL_BOTH); 953 gridData.heightHint = 400; 954 gridData.horizontalSpan = 2; 955 tableBindings.setLayoutData(gridData); 956 final TableColumn tableColumnCategory = new TableColumn(tableBindings, 957 SWT.NONE, VIEW_CATEGORY_COLUMN_INDEX); 958 tableColumnCategory 959 .setText(SORTED_COLUMN_NAMES[VIEW_CATEGORY_COLUMN_INDEX]); 960 tableColumnCategory 961 .addSelectionListener(new SortOrderSelectionListener( 962 VIEW_CATEGORY_COLUMN_INDEX)); 963 final TableColumn tableColumnCommand = new TableColumn(tableBindings, 964 SWT.NONE, VIEW_COMMAND_COLUMN_INDEX); 965 tableColumnCommand 966 .setText(UNSORTED_COLUMN_NAMES[VIEW_COMMAND_COLUMN_INDEX]); 967 tableColumnCommand.addSelectionListener(new SortOrderSelectionListener( 968 VIEW_COMMAND_COLUMN_INDEX)); 969 final TableColumn tableColumnKeySequence = new TableColumn( 970 tableBindings, SWT.NONE, VIEW_KEY_SEQUENCE_COLUMN_INDEX); 971 tableColumnKeySequence 972 .setText(UNSORTED_COLUMN_NAMES[VIEW_KEY_SEQUENCE_COLUMN_INDEX]); 973 tableColumnKeySequence 974 .addSelectionListener(new SortOrderSelectionListener( 975 VIEW_KEY_SEQUENCE_COLUMN_INDEX)); 976 final TableColumn tableColumnContext = new TableColumn(tableBindings, 977 SWT.NONE, VIEW_CONTEXT_COLUMN_INDEX); 978 tableColumnContext 979 .setText(UNSORTED_COLUMN_NAMES[VIEW_CONTEXT_COLUMN_INDEX]); 980 tableColumnContext.addSelectionListener(new SortOrderSelectionListener( 981 VIEW_CONTEXT_COLUMN_INDEX)); 982 tableBindings.addSelectionListener(new SelectionAdapter() { 983 public final void widgetDefaultSelected(final SelectionEvent e) { 984 selectedTableKeyBindings(); 985 } 986 }); 987 988 final Composite buttonBar = new Composite(composite, SWT.NONE); 990 buttonBar.setLayout(new GridLayout(2, false)); 991 gridData = new GridData(); 992 gridData.horizontalAlignment = GridData.END; 993 buttonBar.setLayoutData(gridData); 994 995 final Button editButton = new Button(buttonBar, SWT.PUSH); 997 gridData = new GridData(); 998 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 999 editButton.setText(Util.translateString(RESOURCE_BUNDLE, "buttonEdit")); gridData.widthHint = Math.max(widthHint, editButton.computeSize( 1001 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5; 1002 editButton.setLayoutData(gridData); 1003 editButton.addSelectionListener(new SelectionListener() { 1004 1005 1010 public final void widgetDefaultSelected(final SelectionEvent event) { 1011 selectedTableKeyBindings(); 1012 } 1013 1014 1019 public void widgetSelected(SelectionEvent e) { 1020 widgetDefaultSelected(e); 1021 } 1022 }); 1023 1024 final Button buttonExport = new Button(buttonBar, SWT.PUSH); 1026 gridData = new GridData(); 1027 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 1028 buttonExport.setText(Util.translateString(RESOURCE_BUNDLE, 1029 "buttonExport")); gridData.widthHint = Math.max(widthHint, buttonExport.computeSize( 1031 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5; 1032 buttonExport.setLayoutData(gridData); 1033 buttonExport.addSelectionListener(new SelectionListener() { 1034 1035 1040 public final void widgetDefaultSelected(final SelectionEvent event) { 1041 selectedButtonExport(); 1042 } 1043 1044 1049 public void widgetSelected(SelectionEvent e) { 1050 widgetDefaultSelected(e); 1051 } 1052 }); 1053 1054 return composite; 1055 } 1056 1057 protected IPreferenceStore doGetPreferenceStore() { 1058 return PrefUtil.getInternalPreferenceStore(); 1059 } 1060 1061 1074 public final void editBinding(final Binding binding) { 1075 tabFolder.setSelection(TAB_INDEX_MODIFY); 1077 1078 if (binding == null) { 1080 return; 1081 } 1082 1083 1088 final ParameterizedCommand command = binding.getParameterizedCommand(); 1089 String categoryName = null; 1090 String commandName = null; 1091 try { 1092 categoryName = command.getCommand().getCategory().getName(); 1093 commandName = command.getName(); 1094 } catch (final NotDefinedException e) { 1095 return; } 1097 1098 final String [] categoryNames = comboCategory.getItems(); 1100 int i = 0; 1101 for (; i < categoryNames.length; i++) { 1102 if (categoryName.equals(categoryNames[i])) { 1103 break; 1104 } 1105 } 1106 if (i >= comboCategory.getItemCount()) { 1107 return; 1109 } 1110 comboCategory.select(i); 1111 1112 updateComboCommand(); 1114 1115 final String [] commandNames = comboCommand.getItems(); 1117 int j = 0; 1118 for (; j < commandNames.length; j++) { 1119 if (commandName.equals(commandNames[j])) { 1120 if (comboCommand.getSelectionIndex() != j) { 1121 comboCommand.select(j); 1122 } 1123 break; 1124 } 1125 } 1126 if (j >= comboCommand.getItemCount()) { 1127 if (comboCommand.getSelectionIndex() != 0) { 1129 comboCommand.select(0); 1130 } 1131 update(); 1132 return; 1133 } 1134 1135 1139 update(); 1140 1141 final TableItem[] items = tableBindingsForCommand.getItems(); 1143 int k = 0; 1144 for (; k < items.length; k++) { 1145 final String currentKeySequence = items[k].getText(2); 1146 if (binding.getTriggerSequence().format() 1147 .equals(currentKeySequence)) { 1148 break; 1149 } 1150 } 1151 if (k < tableBindingsForCommand.getItemCount()) { 1152 tableBindingsForCommand.select(k); 1153 tableBindingsForCommand.notifyListeners(SWT.Selection, null); 1154 textTriggerSequence.setFocus(); 1155 } 1156 } 1157 1158 1163 private final String getCategoryId() { 1164 return !commandIdsByCategoryId.containsKey(null) 1165 || comboCategory.getSelectionIndex() > 0 ? (String ) categoryIdsByUniqueName 1166 .get(comboCategory.getText()) 1167 : null; 1168 } 1169 1170 1175 private final String getContextId() { 1176 return comboContext.getSelectionIndex() >= 0 ? (String ) contextIdsByUniqueName 1177 .get(comboContext.getText()) 1178 : null; 1179 } 1180 1181 1186 private final KeySequence getKeySequence() { 1187 return textTriggerSequenceManager.getKeySequence(); 1188 } 1189 1190 1196 private final ParameterizedCommand getParameterizedCommand() { 1197 final int selectionIndex = comboCommand.getSelectionIndex(); 1198 if ((selectionIndex >= 0) && (commands != null) 1199 && (selectionIndex < commands.length)) { 1200 return commands[selectionIndex]; 1201 } 1202 1203 return null; 1204 } 1205 1206 1211 private final String getSchemeId() { 1212 return comboScheme.getSelectionIndex() >= 0 ? (String ) schemeIdsByUniqueName 1213 .get(comboScheme.getText()) 1214 : null; 1215 } 1216 1217 public final void init(final IWorkbench workbench) { 1218 activityManager = workbench.getActivitySupport().getActivityManager(); 1219 bindingService = (IBindingService) workbench.getService(IBindingService.class); 1220 commandService = (ICommandService) workbench.getService(ICommandService.class); 1221 contextService = (IContextService) workbench.getService(IContextService.class); 1222 } 1223 1224 1235 private final boolean isActive(final Command command) { 1236 return activityManager.getIdentifier(command.getId()).isEnabled(); 1237 } 1238 1239 1247 private final void logPreferenceStoreException(final Throwable exception) { 1248 final String message = Util.translateString(RESOURCE_BUNDLE, 1249 "PreferenceStoreError.Message"); String exceptionMessage = exception.getMessage(); 1251 if (exceptionMessage == null) { 1252 exceptionMessage = message; 1253 } 1254 final IStatus status = new Status(IStatus.ERROR, 1255 WorkbenchPlugin.PI_WORKBENCH, 0, exceptionMessage, exception); 1256 WorkbenchPlugin.log(message, status); 1257 StatusUtil.handleStatus(message, exception, StatusManager.SHOW); 1258 } 1259 1260 public final boolean performCancel() { 1261 persistSelectedTab(); 1263 1264 return super.performCancel(); 1265 } 1266 1267 protected final void performDefaults() { 1268 final String title = Util.translateString(RESOURCE_BUNDLE, 1270 "restoreDefaultsMessageBoxText"); final String message = Util.translateString(RESOURCE_BUNDLE, 1272 "restoreDefaultsMessageBoxMessage"); final boolean confirmed = MessageDialog.openConfirm(getShell(), title, 1274 message); 1275 1276 if (confirmed) { 1277 final String defaultSchemeId = bindingService.getDefaultSchemeId(); 1279 final Scheme defaultScheme = localChangeManager 1280 .getScheme(defaultSchemeId); 1281 try { 1282 localChangeManager.setActiveScheme(defaultScheme); 1283 } catch (final NotDefinedException e) { 1284 } 1286 1287 final Binding[] currentBindings = localChangeManager.getBindings(); 1289 final int currentBindingsLength = currentBindings.length; 1290 final Set trimmedBindings = new HashSet (); 1291 for (int i = 0; i < currentBindingsLength; i++) { 1292 final Binding binding = currentBindings[i]; 1293 if (binding.getType() != Binding.USER) { 1294 trimmedBindings.add(binding); 1295 } 1296 } 1297 final Binding[] trimmedBindingArray = (Binding[]) trimmedBindings 1298 .toArray(new Binding[trimmedBindings.size()]); 1299 localChangeManager.setBindings(trimmedBindingArray); 1300 1301 try { 1303 bindingService.savePreferences(defaultScheme, 1304 trimmedBindingArray); 1305 } catch (final IOException e) { 1306 logPreferenceStoreException(e); 1307 } 1308 } 1309 1310 setScheme(localChangeManager.getActiveScheme()); update(true); 1312 super.performDefaults(); 1313 } 1314 1315 public final boolean performOk() { 1316 try { 1318 bindingService.savePreferences( 1319 localChangeManager.getActiveScheme(), localChangeManager 1320 .getBindings()); 1321 } catch (final IOException e) { 1322 logPreferenceStoreException(e); 1323 } 1324 1325 persistSelectedTab(); 1327 1328 return super.performOk(); 1329 } 1330 1331 1335 private final void persistSelectedTab() { 1336 final IPreferenceStore store = getPreferenceStore(); 1337 store.setValue(IPreferenceConstants.KEYS_PREFERENCE_SELECTED_TAB, 1338 tabFolder.getSelectionIndex()); 1339 } 1340 1341 1346 private final void selectedButtonAdd() { 1347 final ParameterizedCommand command = getParameterizedCommand(); 1348 final String contextId = getContextId(); 1349 final String schemeId = getSchemeId(); 1350 final KeySequence keySequence = getKeySequence(); 1351 localChangeManager.removeBindings(keySequence, schemeId, contextId, 1352 null, null, null, Binding.USER); 1353 localChangeManager.addBinding(new KeyBinding(keySequence, command, 1354 schemeId, contextId, null, null, null, Binding.USER)); 1355 update(true); 1356 } 1357 1358 1366 private final void selectedButtonExport() { 1367 final FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE); 1368 fileDialog.setFilterExtensions(new String [] { "*.csv" }); fileDialog.setFilterNames(new String [] { Util.translateString( 1370 RESOURCE_BUNDLE, "csvFilterName") }); final String filePath = fileDialog.open(); 1372 if (filePath == null) { 1373 return; 1374 } 1375 1376 final SafeRunnable runnable = new SafeRunnable() { 1377 public final void run() throws IOException { 1378 Writer fileWriter = null; 1379 try { 1380 fileWriter = new BufferedWriter (new FileWriter (filePath)); 1381 final TableItem[] items = tableBindings.getItems(); 1382 final int numColumns = tableBindings.getColumnCount(); 1383 for (int i = 0; i < items.length; i++) { 1384 final TableItem item = items[i]; 1385 for (int j = 0; j < numColumns; j++) { 1386 String buf = Util.replaceAll(item.getText(j), "\"", "\"\""); fileWriter.write("\"" + buf + "\""); if (j < numColumns - 1) { 1390 fileWriter.write(','); 1391 } 1392 } 1393 fileWriter.write(System.getProperty("line.separator")); } 1395 1396 } finally { 1397 if (fileWriter != null) { 1398 try { 1399 fileWriter.close(); 1400 } catch (final IOException e) { 1401 } 1403 } 1404 1405 } 1406 } 1407 }; 1408 SafeRunner.run(runnable); 1409 } 1410 1411 1417 private final void selectedButtonRemove() { 1418 final String contextId = getContextId(); 1419 final String schemeId = getSchemeId(); 1420 final KeySequence keySequence = getKeySequence(); 1421 localChangeManager.removeBindings(keySequence, schemeId, contextId, 1422 null, null, null, Binding.USER); 1423 localChangeManager.addBinding(new KeyBinding(keySequence, null, 1424 schemeId, contextId, null, null, null, Binding.USER)); 1425 update(true); 1426 } 1427 1428 1433 private final void selectedButtonRestore() { 1434 String contextId = getContextId(); 1435 String schemeId = getSchemeId(); 1436 KeySequence keySequence = getKeySequence(); 1437 localChangeManager.removeBindings(keySequence, schemeId, contextId, 1438 null, null, null, Binding.USER); 1439 update(true); 1440 } 1441 1442 1445 private final void selectedComboScheme() { 1446 final String activeSchemeId = getSchemeId(); 1447 final Scheme activeScheme = localChangeManager 1448 .getScheme(activeSchemeId); 1449 try { 1450 localChangeManager.setActiveScheme(activeScheme); 1451 } catch (final NotDefinedException e) { 1452 } 1454 update(true); 1455 } 1456 1457 1462 private final void selectedTableBindingsForCommand() { 1463 final int selection = tableBindingsForCommand.getSelectionIndex(); 1464 if ((selection >= 0) 1465 && (selection < tableBindingsForCommand.getItemCount())) { 1466 final TableItem item = tableBindingsForCommand.getItem(selection); 1467 final KeyBinding binding = (KeyBinding) item.getData(ITEM_DATA_KEY); 1468 setContextId(binding.getContextId()); 1469 setKeySequence(binding.getKeySequence()); 1470 } 1471 1472 update(); 1473 } 1474 1475 1480 private final void selectedTableBindingsForTriggerSequence() { 1481 final int selection = tableBindingsForTriggerSequence 1482 .getSelectionIndex(); 1483 if ((selection >= 0) 1484 && (selection < tableBindingsForTriggerSequence.getItemCount())) { 1485 final TableItem item = tableBindingsForTriggerSequence 1486 .getItem(selection); 1487 final Binding binding = (Binding) item.getData(ITEM_DATA_KEY); 1488 setContextId(binding.getContextId()); 1489 } 1490 1491 update(); 1492 } 1493 1494 1501 private final void selectedTableKeyBindings() { 1502 final int selectionIndex = tableBindings.getSelectionIndex(); 1503 if (selectionIndex != -1) { 1504 final TableItem item = tableBindings.getItem(selectionIndex); 1505 final Binding binding = (Binding) item.getData(BINDING_KEY); 1506 editBinding(binding); 1507 1508 } else { 1509 editBinding(null); 1510 } 1511 } 1512 1513 1523 private final void setContextId(final String contextId) { 1524 comboContext.clearSelection(); 1526 comboContext.deselectAll(); 1527 1528 String contextName = (String ) contextUniqueNamesById.get(contextId); 1530 if (contextName == null) { 1531 contextName = (String ) contextUniqueNamesById 1532 .get(IContextIds.CONTEXT_ID_WINDOW); 1533 } 1534 if (contextName == null) { 1535 contextName = Util.ZERO_LENGTH_STRING; 1536 } 1537 1538 final String [] items = comboContext.getItems(); 1540 boolean found = false; 1541 for (int i = 0; i < items.length; i++) { 1542 if (contextName.equals(items[i])) { 1543 comboContext.select(i); 1544 found = true; 1545 break; 1546 } 1547 } 1548 1549 if ((!found) && (items.length > 0)) { 1551 comboContext.select(0); 1552 } 1553 } 1554 1555 1561 private final void setKeySequence(final KeySequence keySequence) { 1562 textTriggerSequenceManager.setKeySequence(keySequence); 1563 } 1564 1565 1572 private final void setParameterizedCommand( 1573 final ParameterizedCommand command) { 1574 int i = 0; 1575 if (commands != null) { 1576 final int commandCount = commands.length; 1577 for (; i < commandCount; i++) { 1578 if (commands[i].equals(command)) { 1579 if ((comboCommand.getSelectionIndex() != i) 1580 && (i < comboCommand.getItemCount())) { 1581 comboCommand.select(i); 1582 } 1583 break; 1584 } 1585 } 1586 if ((i >= comboCommand.getItemCount()) 1587 && (comboCommand.getSelectionIndex() != 0)) { 1588 comboCommand.select(0); 1589 } 1590 } 1591 } 1592 1593 1599 private final void setScheme(final Scheme scheme) { 1600 comboScheme.clearSelection(); 1601 comboScheme.deselectAll(); 1602 final String schemeUniqueName = (String ) schemeUniqueNamesById 1603 .get(scheme.getId()); 1604 1605 if (schemeUniqueName != null) { 1606 final String items[] = comboScheme.getItems(); 1607 1608 for (int i = 0; i < items.length; i++) { 1609 if (schemeUniqueName.equals(items[i])) { 1610 comboScheme.select(i); 1611 break; 1612 } 1613 } 1614 } 1615 } 1616 1617 1621 public final void setVisible(final boolean visible) { 1622 if (visible == true) { 1623 Map contextsByName = new HashMap (); 1624 1625 for (Iterator iterator = contextService.getDefinedContextIds() 1626 .iterator(); iterator.hasNext();) { 1627 Context context = contextService.getContext((String ) iterator 1628 .next()); 1629 try { 1630 String name = context.getName(); 1631 Collection contexts = (Collection ) contextsByName.get(name); 1632 1633 if (contexts == null) { 1634 contexts = new HashSet (); 1635 contextsByName.put(name, contexts); 1636 } 1637 1638 contexts.add(context); 1639 } catch (final NotDefinedException e) { 1640 } 1642 } 1643 1644 Map commandsByName = new HashMap (); 1645 1646 for (Iterator iterator = commandService.getDefinedCommandIds() 1647 .iterator(); iterator.hasNext();) { 1648 Command command = commandService.getCommand((String ) iterator 1649 .next()); 1650 if (!isActive(command)) { 1651 continue; 1652 } 1653 1654 try { 1655 String name = command.getName(); 1656 Collection commands = (Collection ) commandsByName.get(name); 1657 1658 if (commands == null) { 1659 commands = new HashSet (); 1660 commandsByName.put(name, commands); 1661 } 1662 1663 commands.add(command); 1664 } catch (NotDefinedException eNotDefined) { 1665 } 1667 } 1668 1669 commandIdsByCategoryId = new HashMap (); 1671 1672 for (Iterator iterator = commandService.getDefinedCommandIds() 1673 .iterator(); iterator.hasNext();) { 1674 final Command command = commandService 1675 .getCommand((String ) iterator.next()); 1676 if (!isActive(command)) { 1677 continue; 1678 } 1679 1680 try { 1681 String categoryId = command.getCategory().getId(); 1682 Collection commandIds = (Collection ) commandIdsByCategoryId 1683 .get(categoryId); 1684 1685 if (commandIds == null) { 1686 commandIds = new HashSet (); 1687 commandIdsByCategoryId.put(categoryId, commandIds); 1688 } 1689 1690 commandIds.add(command.getId()); 1691 } catch (NotDefinedException eNotDefined) { 1692 } 1694 } 1695 1696 Map categoriesByName = new HashMap (); 1697 1698 for (Iterator iterator = commandService.getDefinedCategoryIds() 1699 .iterator(); iterator.hasNext();) { 1700 Category category = commandService 1701 .getCategory((String ) iterator.next()); 1702 1703 try { 1704 if (commandIdsByCategoryId.containsKey(category.getId())) { 1705 String name = category.getName(); 1706 Collection categories = (Collection ) categoriesByName 1707 .get(name); 1708 1709 if (categories == null) { 1710 categories = new HashSet (); 1711 categoriesByName.put(name, categories); 1712 } 1713 1714 categories.add(category); 1715 } 1716 } catch (NotDefinedException eNotDefined) { 1717 } 1719 } 1720 1721 Map schemesByName = new HashMap (); 1722 1723 final Scheme[] definedSchemes = bindingService.getDefinedSchemes(); 1724 for (int i = 0; i < definedSchemes.length; i++) { 1725 final Scheme scheme = definedSchemes[i]; 1726 try { 1727 String name = scheme.getName(); 1728 Collection schemes = (Collection ) schemesByName.get(name); 1729 1730 if (schemes == null) { 1731 schemes = new HashSet (); 1732 schemesByName.put(name, schemes); 1733 } 1734 1735 schemes.add(scheme); 1736 } catch (final NotDefinedException e) { 1737 } 1739 } 1740 1741 contextIdsByUniqueName = new HashMap (); 1742 contextUniqueNamesById = new HashMap (); 1743 1744 for (Iterator iterator = contextsByName.entrySet().iterator(); iterator 1745 .hasNext();) { 1746 Map.Entry entry = (Map.Entry ) iterator.next(); 1747 String name = (String ) entry.getKey(); 1748 Set contexts = (Set ) entry.getValue(); 1749 Iterator iterator2 = contexts.iterator(); 1750 1751 if (contexts.size() == 1) { 1752 Context context = (Context) iterator2.next(); 1753 contextIdsByUniqueName.put(name, context.getId()); 1754 contextUniqueNamesById.put(context.getId(), name); 1755 } else { 1756 while (iterator2.hasNext()) { 1757 Context context = (Context) iterator2.next(); 1758 String uniqueName = MessageFormat.format( 1759 Util.translateString(RESOURCE_BUNDLE, 1760 "uniqueName"), new Object [] { name, context.getId() }); 1762 contextIdsByUniqueName.put(uniqueName, context.getId()); 1763 contextUniqueNamesById.put(context.getId(), uniqueName); 1764 } 1765 } 1766 } 1767 1768 categoryIdsByUniqueName = new HashMap (); 1769 categoryUniqueNamesById = new HashMap (); 1770 1771 for (Iterator iterator = categoriesByName.entrySet().iterator(); iterator 1772 .hasNext();) { 1773 Map.Entry entry = (Map.Entry ) iterator.next(); 1774 String name = (String ) entry.getKey(); 1775 Set categories = (Set ) entry.getValue(); 1776 Iterator iterator2 = categories.iterator(); 1777 1778 if (categories.size() == 1) { 1779 Category category = (Category) iterator2.next(); 1780 categoryIdsByUniqueName.put(name, category.getId()); 1781 categoryUniqueNamesById.put(category.getId(), name); 1782 } else { 1783 while (iterator2.hasNext()) { 1784 Category category = (Category) iterator2.next(); 1785 String uniqueName = MessageFormat.format( 1786 Util.translateString(RESOURCE_BUNDLE, 1787 "uniqueName"), new Object [] { name, category.getId() }); 1789 categoryIdsByUniqueName.put(uniqueName, category 1790 .getId()); 1791 categoryUniqueNamesById.put(category.getId(), 1792 uniqueName); 1793 } 1794 } 1795 } 1796 1797 schemeIdsByUniqueName = new HashMap (); 1798 schemeUniqueNamesById = new HashMap (); 1799 1800 for (Iterator iterator = schemesByName.entrySet().iterator(); iterator 1801 .hasNext();) { 1802 Map.Entry entry = (Map.Entry ) iterator.next(); 1803 String name = (String ) entry.getKey(); 1804 Set keyConfigurations = (Set ) entry.getValue(); 1805 Iterator iterator2 = keyConfigurations.iterator(); 1806 1807 if (keyConfigurations.size() == 1) { 1808 Scheme scheme = (Scheme) iterator2.next(); 1809 schemeIdsByUniqueName.put(name, scheme.getId()); 1810 schemeUniqueNamesById.put(scheme.getId(), name); 1811 } else { 1812 while (iterator2.hasNext()) { 1813 Scheme scheme = (Scheme) iterator2.next(); 1814 String uniqueName = MessageFormat.format( 1815 Util.translateString(RESOURCE_BUNDLE, 1816 "uniqueName"), new Object [] { name, scheme.getId() }); 1818 schemeIdsByUniqueName.put(uniqueName, scheme.getId()); 1819 schemeUniqueNamesById.put(scheme.getId(), uniqueName); 1820 } 1821 } 1822 } 1823 1824 Scheme activeScheme = bindingService.getActiveScheme(); 1825 1826 try { 1828 for (int i = 0; i < definedSchemes.length; i++) { 1829 final Scheme scheme = definedSchemes[i]; 1830 final Scheme copy = localChangeManager.getScheme(scheme 1831 .getId()); 1832 copy.define(scheme.getName(), scheme.getDescription(), 1833 scheme.getParentId()); 1834 } 1835 localChangeManager.setActiveScheme(bindingService 1836 .getActiveScheme()); 1837 } catch (final NotDefinedException e) { 1838 throw new Error ( 1839 "There is a programmer error in the keys preference page"); } 1841 localChangeManager.setLocale(bindingService.getLocale()); 1842 localChangeManager.setPlatform(bindingService.getPlatform()); 1843 localChangeManager.setBindings(bindingService.getBindings()); 1844 1845 List categoryNames = new ArrayList (categoryIdsByUniqueName.keySet()); 1847 Collections.sort(categoryNames, Collator.getInstance()); 1848 if (commandIdsByCategoryId.containsKey(null)) { 1849 categoryNames.add(0, Util.translateString(RESOURCE_BUNDLE, 1850 "other")); } 1852 comboCategory.setItems((String []) categoryNames 1853 .toArray(new String [categoryNames.size()])); 1854 comboCategory.clearSelection(); 1855 comboCategory.deselectAll(); 1856 if (commandIdsByCategoryId.containsKey(null) 1857 || !categoryNames.isEmpty()) { 1858 comboCategory.select(0); 1859 } 1860 1861 List schemeNames = new ArrayList (schemeIdsByUniqueName.keySet()); 1863 Collections.sort(schemeNames, Collator.getInstance()); 1864 comboScheme.setItems((String []) schemeNames 1865 .toArray(new String [schemeNames.size()])); 1866 setScheme(activeScheme); 1867 1868 update(true); 1870 } 1871 1872 super.setVisible(visible); 1873 } 1874 1875 1883 private final void update() { 1884 update(false); 1885 } 1886 1887 1897 private final void update(final boolean updateViewTab) { 1898 if (updateViewTab) { 1899 updateViewTab(); 1900 } 1901 updateComboCommand(); 1902 updateComboContext(); 1903 final TriggerSequence triggerSequence = getKeySequence(); 1904 updateTableBindingsForTriggerSequence(triggerSequence); 1905 final ParameterizedCommand command = getParameterizedCommand(); 1906 updateTableBindingsForCommand(command); 1907 final String contextId = getContextId(); 1908 updateSelection(tableBindingsForTriggerSequence, contextId, 1909 triggerSequence); 1910 updateSelection(tableBindingsForCommand, contextId, triggerSequence); 1911 updateLabelSchemeExtends(); 1912 updateLabelContextExtends(); 1913 updateEnabled(triggerSequence, command); 1914 } 1915 1916 1920 private final void updateComboCommand() { 1921 final ParameterizedCommand command = getParameterizedCommand(); 1923 1924 final String categoryId = getCategoryId(); 1926 Set commandIds = (Set ) commandIdsByCategoryId.get(categoryId); 1927 if (commandIds==null) { 1928 commandIds = Collections.EMPTY_SET; 1929 } 1930 1931 1936 List commands = new ArrayList (); 1937 final Iterator commandIdItr = commandIds.iterator(); 1938 while (commandIdItr.hasNext()) { 1939 final String currentCommandId = (String ) commandIdItr.next(); 1940 final Command currentCommand = commandService 1941 .getCommand(currentCommandId); 1942 try { 1943 commands.addAll(ParameterizedCommand 1944 .generateCombinations(currentCommand)); 1945 } catch (final NotDefinedException e) { 1946 } 1948 } 1949 1950 commands = sortParameterizedCommands(commands); 1953 1954 final int commandCount = commands.size(); 1955 this.commands = (ParameterizedCommand[]) commands 1956 .toArray(new ParameterizedCommand[commandCount]); 1957 1958 1962 final String [] commandNames = new String [commandCount]; 1963 for (int i = 0; i < commandCount; i++) { 1964 try { 1965 commandNames[i] = this.commands[i].getName(); 1966 } catch (final NotDefinedException e) { 1967 throw new Error ( 1968 "Concurrent modification of the command's defined state"); } 1970 } 1971 1972 1978 final String [] currentItems = comboCommand.getItems(); 1979 if (!Arrays.equals(currentItems, commandNames)) { 1980 comboCommand.setItems(commandNames); 1981 } 1982 1983 setParameterizedCommand(command); 1985 1986 1991 if ((comboCommand.getSelectionIndex() == -1) && (commandCount > 0)) { 1992 comboCommand.select(0); 1993 } 1994 } 1995 1996 2001 private List sortParameterizedCommands(List commands) { 2002 final Collator collator = Collator.getInstance(); 2003 2004 Comparator comparator = new Comparator () { 2007 public int compare(Object o1, Object o2) { 2008 String name1 = null; 2009 String name2 = null; 2010 try { 2011 name1 = ((ParameterizedCommand) o1).getName(); 2012 } catch (NotDefinedException e) { 2013 return -1; 2014 } 2015 try { 2016 name2 = ((ParameterizedCommand) o2).getName(); 2017 } catch (NotDefinedException e) { 2018 return 1; 2019 } 2020 int rc = collator.compare(name1, name2); 2021 if (rc != 0) { 2022 return rc; 2023 } 2024 2025 String id1 = ((ParameterizedCommand) o1).getId(); 2026 String id2 = ((ParameterizedCommand) o2).getId(); 2027 return collator.compare(id1, id2); 2028 } 2029 }; 2030 Collections.sort(commands, comparator); 2031 return commands; 2032 } 2033 2034 2037 private final void updateComboContext() { 2038 final String contextId = getContextId(); 2039 final Map contextIdsByName = new HashMap (contextIdsByUniqueName); 2040 2041 final List contextNames = new ArrayList (contextIdsByName.keySet()); 2042 Collections.sort(contextNames, Collator.getInstance()); 2043 2044 comboContext.setItems((String []) contextNames 2045 .toArray(new String [contextNames.size()])); 2046 setContextId(contextId); 2047 2048 if (comboContext.getSelectionIndex() == -1 && !contextNames.isEmpty()) { 2049 comboContext.select(0); 2050 } 2051 } 2052 2053 2065 private final void updateEnabled(final TriggerSequence triggerSequence, 2066 final ParameterizedCommand command) { 2067 final boolean commandSelected = command != null; 2068 labelBindingsForCommand.setEnabled(commandSelected); 2069 tableBindingsForCommand.setEnabled(commandSelected); 2070 2071 final boolean triggerSequenceSelected = !triggerSequence.isEmpty(); 2072 labelBindingsForTriggerSequence.setEnabled(triggerSequenceSelected); 2073 tableBindingsForTriggerSequence.setEnabled(triggerSequenceSelected); 2074 2075 2078 final boolean buttonsEnabled = commandSelected 2079 && triggerSequenceSelected; 2080 buttonAdd.setEnabled(buttonsEnabled); 2081 buttonRemove.setEnabled(buttonsEnabled); 2082 buttonRestore.setEnabled(buttonsEnabled); 2083 } 2084 2085 2090 private final void updateLabelContextExtends() { 2091 final String contextId = getContextId(); 2092 2093 if (contextId != null) { 2094 final Context context = contextService.getContext(getContextId()); 2095 if (context.isDefined()) { 2096 try { 2097 final String parentId = context.getParentId(); 2098 if (parentId != null) { 2099 final String name = (String ) contextUniqueNamesById 2100 .get(parentId); 2101 if (name != null) { 2102 labelContextExtends.setText(MessageFormat.format( 2103 Util.translateString(RESOURCE_BUNDLE, 2104 "extends"), new Object [] { name })); 2106 return; 2107 } 2108 } 2109 } catch (final NotDefinedException e) { 2110 } 2112 } 2113 } 2114 2115 labelContextExtends.setText(Util.ZERO_LENGTH_STRING); 2116 } 2117 2118 2123 private final void updateLabelSchemeExtends() { 2124 final String schemeId = getSchemeId(); 2125 2126 if (schemeId != null) { 2127 final Scheme scheme = bindingService.getScheme(schemeId); 2128 try { 2129 final String name = (String ) schemeUniqueNamesById.get(scheme 2130 .getParentId()); 2131 if (name != null) { 2132 labelSchemeExtends.setText(MessageFormat.format(Util 2133 .translateString(RESOURCE_BUNDLE, "extends"), new Object [] { name })); 2135 return; 2136 } 2137 } catch (final NotDefinedException e) { 2138 } 2140 } 2141 2142 labelSchemeExtends.setText(Util.ZERO_LENGTH_STRING); 2143 } 2144 2145 2160 private final void updateSelection(final Table table, 2161 final String contextId, final TriggerSequence triggerSequence) { 2162 if (table.getSelectionCount() > 1) { 2163 table.deselectAll(); 2164 } 2165 2166 final TableItem[] items = table.getItems(); 2167 int selection = -1; 2168 for (int i = 0; i < items.length; i++) { 2169 final Binding binding = (Binding) items[i].getData(ITEM_DATA_KEY); 2170 if ((Util.equals(contextId, binding.getContextId())) 2171 && (Util.equals(triggerSequence, binding 2172 .getTriggerSequence()))) { 2173 selection = i; 2174 break; 2175 } 2176 } 2177 2178 if (selection != -1) { 2179 table.select(selection); 2180 } 2181 } 2182 2183 2191 private final void updateTableBindingsForCommand( 2192 final ParameterizedCommand parameterizedCommand) { 2193 tableBindingsForCommand.removeAll(); 2195 2196 final Collection bindings = localChangeManager 2198 .getActiveBindingsDisregardingContextFlat(); 2199 final Iterator bindingItr = bindings.iterator(); 2200 while (bindingItr.hasNext()) { 2201 final Binding binding = (Binding) bindingItr.next(); 2202 if (!Util.equals(parameterizedCommand, binding 2203 .getParameterizedCommand())) { 2204 continue; } 2206 2207 final TableItem tableItem = new TableItem(tableBindingsForCommand, 2208 SWT.NULL); 2209 tableItem.setData(ITEM_DATA_KEY, binding); 2210 2211 2217 if (binding.getType() == Binding.SYSTEM) { 2218 tableItem.setImage(0, IMAGE_BLANK); 2219 } else { 2220 tableItem.setImage(0, IMAGE_CHANGE); 2221 } 2222 2223 String contextName = (String ) contextUniqueNamesById.get(binding 2224 .getContextId()); 2225 if (contextName == null) { 2226 contextName = Util.ZERO_LENGTH_STRING; 2227 } 2228 tableItem.setText(1, contextName); 2229 tableItem.setText(2, binding.getTriggerSequence().format()); 2230 } 2231 } 2232 2233 2241 private final void updateTableBindingsForTriggerSequence( 2242 final TriggerSequence triggerSequence) { 2243 tableBindingsForTriggerSequence.removeAll(); 2245 2246 final Map activeBindings = localChangeManager 2248 .getActiveBindingsDisregardingContext(); 2249 final Collection bindings = (Collection ) activeBindings 2250 .get(triggerSequence); 2251 if (bindings == null) { 2252 return; 2253 } 2254 2255 final Iterator bindingItr = bindings.iterator(); 2257 while (bindingItr.hasNext()) { 2258 final Binding binding = (Binding) bindingItr.next(); 2259 final Context context = contextService.getContext(binding 2260 .getContextId()); 2261 final ParameterizedCommand parameterizedCommand = binding 2262 .getParameterizedCommand(); 2263 final Command command = parameterizedCommand.getCommand(); 2264 if ((!context.isDefined()) && (!command.isDefined())) { 2265 continue; 2266 } 2267 2268 final TableItem tableItem = new TableItem( 2269 tableBindingsForTriggerSequence, SWT.NULL); 2270 tableItem.setData(ITEM_DATA_KEY, binding); 2271 2272 2278 if (binding.getType() == Binding.SYSTEM) { 2279 tableItem.setImage(0, IMAGE_BLANK); 2280 } else { 2281 tableItem.setImage(0, IMAGE_CHANGE); 2282 } 2283 2284 try { 2285 tableItem.setText(1, context.getName()); 2286 tableItem.setText(2, parameterizedCommand.getName()); 2287 } catch (final NotDefinedException e) { 2288 throw new Error ( 2289 "Context or command became undefined on a non-UI thread while the UI thread was processing."); } 2291 } 2292 } 2293 2294 2301 private final void updateViewTab() { 2302 tableBindings.removeAll(); 2304 2305 final List bindings = new ArrayList (localChangeManager 2307 .getActiveBindingsDisregardingContextFlat()); 2308 Collections.sort(bindings, new Comparator () { 2309 2323 public final int compare(final Object object1, final Object object2) { 2324 final Binding binding1 = (Binding) object1; 2325 final Binding binding2 = (Binding) object2; 2326 2327 2331 final Command command1 = binding1.getParameterizedCommand() 2332 .getCommand(); 2333 String categoryName1 = Util.ZERO_LENGTH_STRING; 2334 String commandName1 = Util.ZERO_LENGTH_STRING; 2335 try { 2336 commandName1 = command1.getName(); 2337 categoryName1 = command1.getCategory().getName(); 2338 } catch (final NotDefinedException e) { 2339 } 2341 final String triggerSequence1 = binding1.getTriggerSequence() 2342 .format(); 2343 final String contextId1 = binding1.getContextId(); 2344 String contextName1 = Util.ZERO_LENGTH_STRING; 2345 if (contextId1 != null) { 2346 final Context context = contextService 2347 .getContext(contextId1); 2348 try { 2349 contextName1 = context.getName(); 2350 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 2351 } 2353 } 2354 2355 2359 final Command command2 = binding2.getParameterizedCommand() 2360 .getCommand(); 2361 String categoryName2 = Util.ZERO_LENGTH_STRING; 2362 String commandName2 = Util.ZERO_LENGTH_STRING; 2363 try { 2364 commandName2 = command2.getName(); 2365 categoryName2 = command2.getCategory().getName(); 2366 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 2367 } 2369 final String keySequence2 = binding2.getTriggerSequence() 2370 .format(); 2371 final String contextId2 = binding2.getContextId(); 2372 String contextName2 = Util.ZERO_LENGTH_STRING; 2373 if (contextId2 != null) { 2374 final Context context = contextService 2375 .getContext(contextId2); 2376 try { 2377 contextName2 = context.getName(); 2378 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 2379 } 2381 } 2382 2383 int compare = 0; 2385 for (int i = 0; i < sortOrder.length; i++) { 2386 switch (sortOrder[i]) { 2387 case VIEW_CATEGORY_COLUMN_INDEX: 2388 compare = Util.compare(categoryName1, categoryName2); 2389 if (compare != 0) { 2390 return compare; 2391 } 2392 break; 2393 case VIEW_COMMAND_COLUMN_INDEX: 2394 compare = Util.compare(commandName1, commandName2); 2395 if (compare != 0) { 2396 return compare; 2397 } 2398 break; 2399 case VIEW_KEY_SEQUENCE_COLUMN_INDEX: 2400 compare = Util.compare(triggerSequence1, keySequence2); 2401 if (compare != 0) { 2402 return compare; 2403 } 2404 break; 2405 case VIEW_CONTEXT_COLUMN_INDEX: 2406 compare = Util.compare(contextName1, contextName2); 2407 if (compare != 0) { 2408 return compare; 2409 } 2410 break; 2411 default: 2412 throw new Error ( 2413 "Programmer error: added another sort column without modifying the comparator."); } 2415 } 2416 2417 return compare; 2418 } 2419 2420 2423 public final boolean equals(final Object object) { 2424 return super.equals(object); 2425 } 2426 }); 2427 2428 final Iterator keyBindingItr = bindings.iterator(); 2430 while (keyBindingItr.hasNext()) { 2431 final Binding binding = (Binding) keyBindingItr.next(); 2432 2433 final ParameterizedCommand command = binding 2435 .getParameterizedCommand(); 2436 String commandName = Util.ZERO_LENGTH_STRING; 2437 String categoryName = Util.ZERO_LENGTH_STRING; 2438 try { 2439 commandName = command.getName(); 2440 categoryName = command.getCommand().getCategory().getName(); 2441 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 2442 } 2444 2445 if ((commandName == null) || (commandName.length() == 0)) { 2447 continue; 2448 } 2449 2450 final String contextId = binding.getContextId(); 2452 String contextName = Util.ZERO_LENGTH_STRING; 2453 if (contextId != null) { 2454 final Context context = contextService.getContext(contextId); 2455 try { 2456 contextName = context.getName(); 2457 } catch (final org.eclipse.core.commands.common.NotDefinedException e) { 2458 } 2460 } 2461 2462 final TableItem item = new TableItem(tableBindings, SWT.NONE); 2464 item.setText(VIEW_CATEGORY_COLUMN_INDEX, categoryName); 2465 item.setText(VIEW_COMMAND_COLUMN_INDEX, commandName); 2466 item.setText(VIEW_KEY_SEQUENCE_COLUMN_INDEX, binding 2467 .getTriggerSequence().format()); 2468 item.setText(VIEW_CONTEXT_COLUMN_INDEX, contextName); 2469 item.setData(BINDING_KEY, binding); 2470 } 2471 2472 for (int i = 0; i < tableBindings.getColumnCount(); i++) { 2474 tableBindings.getColumn(i).pack(); 2475 } 2476 } 2477 2478 2479} 2480 | Popular Tags |