KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > KeysPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.commands;
13
14 import java.io.IOException JavaDoc;
15 import java.text.Collator JavaDoc;
16 import java.text.MessageFormat JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.SortedMap JavaDoc;
28 import java.util.TreeMap JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 import org.eclipse.jface.dialogs.IDialogConstants;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.preference.FieldEditor;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.preference.IntegerFieldEditor;
36 import org.eclipse.jface.preference.StringFieldEditor;
37 import org.eclipse.jface.util.IPropertyChangeListener;
38 import org.eclipse.jface.util.PropertyChangeEvent;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.FocusEvent;
41 import org.eclipse.swt.events.FocusListener;
42 import org.eclipse.swt.events.ModifyEvent;
43 import org.eclipse.swt.events.ModifyListener;
44 import org.eclipse.swt.events.MouseAdapter;
45 import org.eclipse.swt.events.MouseEvent;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.graphics.Color;
49 import org.eclipse.swt.graphics.Image;
50 import org.eclipse.swt.graphics.Point;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Combo;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Control;
57 import org.eclipse.swt.widgets.Group;
58 import org.eclipse.swt.widgets.Label;
59 import org.eclipse.swt.widgets.Menu;
60 import org.eclipse.swt.widgets.MenuItem;
61 import org.eclipse.swt.widgets.TabFolder;
62 import org.eclipse.swt.widgets.TabItem;
63 import org.eclipse.swt.widgets.Table;
64 import org.eclipse.swt.widgets.TableColumn;
65 import org.eclipse.swt.widgets.TableItem;
66 import org.eclipse.swt.widgets.Text;
67 import org.eclipse.ui.IWorkbench;
68 import org.eclipse.ui.IWorkbenchPreferencePage;
69 import org.eclipse.ui.PlatformUI;
70 import org.eclipse.ui.commands.ICategory;
71 import org.eclipse.ui.commands.ICommand;
72 import org.eclipse.ui.commands.IKeyConfiguration;
73 import org.eclipse.ui.contexts.IContext;
74 import org.eclipse.ui.contexts.IContextManager;
75 import org.eclipse.ui.contexts.IWorkbenchContextSupport;
76 import org.eclipse.ui.internal.IPreferenceConstants;
77 import org.eclipse.ui.internal.IWorkbenchConstants;
78 import org.eclipse.ui.internal.WorkbenchPlugin;
79 import org.eclipse.ui.internal.keys.KeySequenceText;
80 import org.eclipse.ui.internal.util.Util;
81 import org.eclipse.ui.keys.KeySequence;
82 import org.eclipse.ui.keys.KeyStroke;
83
84 public class KeysPreferencePage extends
85         org.eclipse.jface.preference.PreferencePage implements
86         IWorkbenchPreferencePage {
87
88     private final static class CommandAssignment implements Comparable JavaDoc {
89
90         private KeySequenceBindingNode.Assignment assignment;
91
92         private String JavaDoc contextId;
93
94         private KeySequence keySequence;
95
96         public int compareTo(Object JavaDoc object) {
97             CommandAssignment castedObject = (CommandAssignment) object;
98             int compareTo = Util.compare(contextId, castedObject.contextId);
99
100             if (compareTo == 0) {
101                 compareTo = Util.compare(keySequence, castedObject.keySequence);
102
103                 if (compareTo == 0)
104                         compareTo = Util.compare(assignment,
105                                 castedObject.assignment);
106             }
107
108             return compareTo;
109         }
110
111         public boolean equals(Object JavaDoc object) {
112             if (!(object instanceof CommandAssignment)) return false;
113
114             CommandAssignment castedObject = (CommandAssignment) object;
115             boolean equals = true;
116             equals &= Util.equals(assignment, castedObject.assignment);
117             equals &= Util.equals(contextId, castedObject.contextId);
118             equals &= Util.equals(keySequence, castedObject.keySequence);
119             return equals;
120         }
121     }
122
123     private final static class KeySequenceAssignment implements Comparable JavaDoc {
124
125         private KeySequenceBindingNode.Assignment assignment;
126
127         private String JavaDoc contextId;
128
129         public int compareTo(Object JavaDoc object) {
130             KeySequenceAssignment castedObject = (KeySequenceAssignment) object;
131             int compareTo = Util.compare(contextId, castedObject.contextId);
132
133             if (compareTo == 0)
134                     compareTo = Util.compare(assignment,
135                             castedObject.assignment);
136
137             return compareTo;
138         }
139
140         public boolean equals(Object JavaDoc object) {
141             if (!(object instanceof CommandAssignment)) return false;
142
143             KeySequenceAssignment castedObject = (KeySequenceAssignment) object;
144             boolean equals = true;
145             equals &= Util.equals(assignment, castedObject.assignment);
146             equals &= Util.equals(contextId, castedObject.contextId);
147             return equals;
148         }
149     }
150
151     private final static int DIFFERENCE_ADD = 0;
152
153     private final static int DIFFERENCE_CHANGE = 1;
154
155     private final static int DIFFERENCE_MINUS = 2;
156
157     private final static int DIFFERENCE_NONE = 3;
158
159     private final static Image IMAGE_BLANK = ImageFactory.getImage("blank"); //$NON-NLS-1$
160

161     private final static Image IMAGE_CHANGE = ImageFactory.getImage("change"); //$NON-NLS-1$
162

163     private final static Image IMAGE_MINUS = ImageFactory.getImage("minus"); //$NON-NLS-1$
164

165     private final static Image IMAGE_PLUS = ImageFactory.getImage("plus"); //$NON-NLS-1$
166

167     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
168             .getBundle(KeysPreferencePage.class.getName());
169
170     private Map JavaDoc assignmentsByContextIdByKeySequence;
171
172     private Button buttonAdd;
173
174     private Button buttonAddKey;
175
176     private Button buttonRemove;
177
178     private Button buttonRestore;
179
180     private Map JavaDoc categoryIdsByUniqueName;
181
182     private Map JavaDoc categoryUniqueNamesById;
183
184     private Button checkBoxMultiKeyAssist;
185
186     private Combo comboCategory;
187
188     private Combo comboCommand;
189
190     private Combo comboContext;
191
192     private Combo comboKeyConfiguration;
193
194     private Set JavaDoc commandAssignments;
195
196     private Map JavaDoc commandIdsByCategoryId;
197
198     private Map JavaDoc commandIdsByUniqueName;
199
200     private MutableCommandManager commandManager;
201
202     private Map JavaDoc commandUniqueNamesById;
203
204     private Map JavaDoc contextIdsByUniqueName;
205
206     private IContextManager contextManager;
207
208     private Map JavaDoc contextUniqueNamesById;
209
210     private Group groupCommand;
211
212     private Group groupKeySequence;
213
214     private Map JavaDoc keyConfigurationIdsByUniqueName;
215
216     private Map JavaDoc keyConfigurationUniqueNamesById;
217
218     private Set JavaDoc keySequenceAssignments;
219
220     private Label labelAssignmentsForCommand;
221
222     private Label labelAssignmentsForKeySequence;
223
224     private Label labelCategory;
225
226     private Label labelCommand;
227
228     private Label labelContext;
229
230     private Label labelContextExtends;
231
232     private Label labelKeyConfiguration;
233
234     private Label labelKeyConfigurationExtends;
235
236     private Label labelKeySequence;
237
238     private Menu menuButtonAddKey;
239
240     private Color minusColour;
241
242     private Table tableAssignmentsForCommand;
243
244     private Table tableAssignmentsForKeySequence;
245
246     private Text textKeySequence;
247
248     private KeySequenceText textKeySequenceManager;
249
250     private IntegerFieldEditor textMultiKeyAssistTime;
251
252     private SortedMap JavaDoc tree;
253
254     private void buildCommandAssignmentsTable() {
255         tableAssignmentsForCommand.removeAll();
256         boolean matchFoundInFirstKeyConfiguration = false;
257
258         for (Iterator JavaDoc iterator = commandAssignments.iterator(); iterator
259                 .hasNext();) {
260             boolean createTableItem = true;
261             CommandAssignment commandAssignment = (CommandAssignment) iterator
262                     .next();
263             KeySequenceBindingNode.Assignment assignment = commandAssignment.assignment;
264             KeySequence keySequence = commandAssignment.keySequence;
265             String JavaDoc commandString = null;
266             int difference = DIFFERENCE_NONE;
267
268             if (assignment.hasPreferenceCommandIdInFirstKeyConfiguration
269                     || assignment.hasPreferenceCommandIdInInheritedKeyConfiguration) {
270                 String JavaDoc preferenceCommandId;
271
272                 if (assignment.hasPreferenceCommandIdInFirstKeyConfiguration)
273                     preferenceCommandId = assignment.preferenceCommandIdInFirstKeyConfiguration;
274                 else
275                     preferenceCommandId = assignment.preferenceCommandIdInInheritedKeyConfiguration;
276
277                 if (assignment.hasPluginCommandIdInFirstKeyConfiguration
278                         || assignment.hasPluginCommandIdInInheritedKeyConfiguration) {
279                     String JavaDoc pluginCommandId;
280
281                     if (assignment.hasPluginCommandIdInFirstKeyConfiguration)
282                         pluginCommandId = assignment.pluginCommandIdInFirstKeyConfiguration;
283                     else
284                         pluginCommandId = assignment.pluginCommandIdInInheritedKeyConfiguration;
285                     if (preferenceCommandId != null) {
286                         difference = DIFFERENCE_CHANGE;
287                         commandString =
288                         /* commandUniqueNamesById.get(preferenceCommandId) */
289                         keySequence.format() + ""; //$NON-NLS-1$
290
} else {
291                         difference = DIFFERENCE_MINUS;
292                         commandString = /* "Unassigned" */
293                         keySequence.format();
294                     }
295
296                     if (pluginCommandId != null)
297                         commandString += " (was: " //$NON-NLS-1$
298
+ commandUniqueNamesById.get(pluginCommandId)
299                                 + ")"; //$NON-NLS-1$
300
else
301                         commandString += " (was: " + "Unassigned" + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
302
} else {
303                     if (preferenceCommandId != null) {
304                         difference = DIFFERENCE_ADD;
305                         commandString =
306                         /* commandUniqueNamesById.get(preferenceCommandId) */
307                         keySequence.format() + ""; //$NON-NLS-1$
308
} else {
309                         difference = DIFFERENCE_MINUS;
310                         commandString = /* "Unassigned" */
311                         keySequence.format();
312                     }
313                 }
314             } else {
315                 String JavaDoc pluginCommandId = null;
316                 if (assignment.hasPluginCommandIdInFirstKeyConfiguration) {
317                     pluginCommandId = assignment.pluginCommandIdInFirstKeyConfiguration;
318                     if (pluginCommandId != null) {
319                         matchFoundInFirstKeyConfiguration = true;
320                     }
321                 } else if (!matchFoundInFirstKeyConfiguration) {
322                     pluginCommandId = assignment.pluginCommandIdInInheritedKeyConfiguration;
323                 } else {
324                     createTableItem = false;
325                     iterator.remove();
326                 }
327
328                 if (pluginCommandId != null) {
329                     difference = DIFFERENCE_NONE;
330                     commandString =
331                     /* commandUniqueNamesById.get(preferenceCommandId) */
332                     keySequence.format() + ""; //$NON-NLS-1$
333
} else {
334                     difference = DIFFERENCE_MINUS;
335                     commandString = /* "Unassigned" */
336                     keySequence.format();
337                 }
338             }
339
340             if (createTableItem) {
341                 TableItem tableItem = new TableItem(tableAssignmentsForCommand,
342                         SWT.NULL);
343
344                 switch (difference) {
345                 case DIFFERENCE_ADD:
346                     tableItem.setImage(0, IMAGE_PLUS);
347                     break;
348
349                 case DIFFERENCE_CHANGE:
350                     tableItem.setImage(0, IMAGE_CHANGE);
351                     break;
352
353                 case DIFFERENCE_MINUS:
354                     tableItem.setImage(0, IMAGE_MINUS);
355                     break;
356
357                 case DIFFERENCE_NONE:
358                     tableItem.setImage(0, IMAGE_BLANK);
359                     break;
360                 }
361
362                 String JavaDoc contextId = commandAssignment.contextId;
363
364                 if (contextId == null) {
365                     // This should never happen.
366
tableItem.setText(1, Util.ZERO_LENGTH_STRING);
367                 } else
368                     tableItem.setText(1, (String JavaDoc) contextUniqueNamesById
369                             .get(contextId)); //$NON-NLS-1$
370

371                 tableItem.setText(2, commandString);
372
373                 if (difference == DIFFERENCE_MINUS) {
374                     tableItem.setForeground(minusColour);
375                 }
376             }
377         }
378     }
379
380     private void buildKeySequenceAssignmentsTable() {
381         tableAssignmentsForKeySequence.removeAll();
382         boolean matchFoundInFirstKeyConfiguration = false;
383         
384         for (Iterator JavaDoc iterator = keySequenceAssignments.iterator(); iterator
385                 .hasNext();) {
386             boolean createTableItem = true;
387             KeySequenceAssignment keySequenceAssignment = (KeySequenceAssignment) iterator
388                     .next();
389             KeySequenceBindingNode.Assignment assignment = keySequenceAssignment.assignment;
390             String JavaDoc commandString = null;
391             int difference = DIFFERENCE_NONE;
392
393             if (assignment.hasPreferenceCommandIdInFirstKeyConfiguration
394                     || assignment.hasPreferenceCommandIdInInheritedKeyConfiguration) {
395                 String JavaDoc preferenceCommandId;
396
397                 if (assignment.hasPreferenceCommandIdInFirstKeyConfiguration)
398                     preferenceCommandId = assignment.preferenceCommandIdInFirstKeyConfiguration;
399                 else
400                     preferenceCommandId = assignment.preferenceCommandIdInInheritedKeyConfiguration;
401
402                 if (assignment.hasPluginCommandIdInFirstKeyConfiguration
403                         || assignment.hasPluginCommandIdInInheritedKeyConfiguration) {
404                     String JavaDoc pluginCommandId;
405
406                     if (assignment.hasPluginCommandIdInFirstKeyConfiguration)
407                         pluginCommandId = assignment.pluginCommandIdInFirstKeyConfiguration;
408                     else
409                         pluginCommandId = assignment.pluginCommandIdInInheritedKeyConfiguration;
410
411                     if (preferenceCommandId != null) {
412                         difference = DIFFERENCE_CHANGE;
413                         commandString = commandUniqueNamesById
414                                 .get(preferenceCommandId)
415                                 + ""; //$NON-NLS-1$
416
} else {
417                         difference = DIFFERENCE_MINUS;
418                         commandString = "Unassigned"; //$NON-NLS-1$
419
}
420
421                     if (pluginCommandId != null)
422                         commandString += " (was: " //$NON-NLS-1$
423
+ commandUniqueNamesById.get(pluginCommandId)
424                                 + ")"; //$NON-NLS-1$
425
else
426                         commandString += " (was: " + "Unassigned" + ")"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
427
} else {
428                     if (preferenceCommandId != null) {
429                         difference = DIFFERENCE_ADD;
430                         commandString = commandUniqueNamesById
431                                 .get(preferenceCommandId)
432                                 + ""; //$NON-NLS-1$
433
} else {
434                         difference = DIFFERENCE_MINUS;
435                         commandString = "Unassigned"; //$NON-NLS-1$
436
}
437                 }
438             } else {
439                 String JavaDoc pluginCommandId = null;
440
441                 if (assignment.hasPluginCommandIdInFirstKeyConfiguration) {
442                     pluginCommandId = assignment.pluginCommandIdInFirstKeyConfiguration;
443                     if (pluginCommandId != null) {
444                         matchFoundInFirstKeyConfiguration = true;
445                     }
446                 } else if (!matchFoundInFirstKeyConfiguration) {
447                     pluginCommandId = assignment.pluginCommandIdInInheritedKeyConfiguration;
448                 } else {
449                     createTableItem = false;
450                     iterator.remove();
451                 }
452
453                 if (pluginCommandId != null) {
454                     difference = DIFFERENCE_NONE;
455                     commandString = commandUniqueNamesById.get(pluginCommandId)
456                             + ""; //$NON-NLS-1$
457
} else {
458                     difference = DIFFERENCE_MINUS;
459                     commandString = "Unassigned"; //$NON-NLS-1$
460
}
461             }
462
463             if (createTableItem) {
464                 TableItem tableItem = new TableItem(
465                         tableAssignmentsForKeySequence, SWT.NULL);
466
467                 switch (difference) {
468                 case DIFFERENCE_ADD:
469                     tableItem.setImage(0, IMAGE_PLUS);
470                     break;
471
472                 case DIFFERENCE_CHANGE:
473                     tableItem.setImage(0, IMAGE_CHANGE);
474                     break;
475
476                 case DIFFERENCE_MINUS:
477                     tableItem.setImage(0, IMAGE_MINUS);
478                     break;
479
480                 case DIFFERENCE_NONE:
481                     tableItem.setImage(0, IMAGE_BLANK);
482                     break;
483                 }
484
485                 String JavaDoc contextId = keySequenceAssignment.contextId;
486
487                 if (contextId == null) {
488                     // This should never happen.
489
tableItem.setText(1, Util.ZERO_LENGTH_STRING);
490                 } else {
491                     tableItem.setText(1, (String JavaDoc) contextUniqueNamesById
492                             .get(contextId)); //$NON-NLS-1$
493
}
494
495                 tableItem.setText(2, commandString);
496
497                 if (difference == DIFFERENCE_MINUS) {
498                     tableItem.setForeground(minusColour);
499                 }
500             }
501         }
502     }
503
504     private Composite createAdvancedTab(TabFolder parent) {
505         GridData gridData = null;
506
507         // The composite for this tab.
508
final Composite composite = new Composite(parent, SWT.NULL);
509         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
510
511         // The multi-key assist button.
512
checkBoxMultiKeyAssist = new Button(composite, SWT.CHECK);
513         checkBoxMultiKeyAssist.setText(Util.translateString(RESOURCE_BUNDLE,
514                 "checkBoxMultiKeyAssist.Text")); //$NON-NLS-1$
515
checkBoxMultiKeyAssist.setToolTipText(Util.translateString(
516                 RESOURCE_BUNDLE, "checkBoxMultiKeyAssist.ToolTipText")); //$NON-NLS-1$
517
checkBoxMultiKeyAssist.setSelection(getPreferenceStore().getBoolean(
518                 IPreferenceConstants.MULTI_KEY_ASSIST));
519         gridData = new GridData(GridData.FILL_HORIZONTAL);
520         gridData.horizontalSpan = 2;
521         checkBoxMultiKeyAssist.setLayoutData(gridData);
522
523         // The multi key assist time.
524
final IPreferenceStore store = WorkbenchPlugin.getDefault()
525                 .getPreferenceStore();
526         textMultiKeyAssistTime = new IntegerFieldEditor(
527                 IPreferenceConstants.MULTI_KEY_ASSIST_TIME, Util
528                         .translateString(RESOURCE_BUNDLE,
529                                 "textMultiKeyAssistTime.Text"), composite); //$NON-NLS-1$
530
textMultiKeyAssistTime.setPreferenceStore(store);
531         textMultiKeyAssistTime.setPreferencePage(this);
532         textMultiKeyAssistTime.setTextLimit(9);
533         textMultiKeyAssistTime.setErrorMessage(Util.translateString(
534                 RESOURCE_BUNDLE, "textMultiKeyAssistTime.ErrorMessage")); //$NON-NLS-1$
535
textMultiKeyAssistTime
536                 .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);
537         textMultiKeyAssistTime.setValidRange(1, Integer.MAX_VALUE);
538         textMultiKeyAssistTime.setStringValue(Integer.toString(store
539                 .getInt(IPreferenceConstants.MULTI_KEY_ASSIST_TIME)));
540         textMultiKeyAssistTime
541                 .setPropertyChangeListener(new IPropertyChangeListener() {
542
543                     public void propertyChange(PropertyChangeEvent event) {
544                         if (event.getProperty().equals(FieldEditor.IS_VALID))
545                                 setValid(textMultiKeyAssistTime.isValid());
546                     }
547                 });
548
549         // Conigure the layout of the composite.
550
final GridLayout gridLayout = new GridLayout();
551         gridLayout.marginHeight = 5;
552         gridLayout.marginWidth = 5;
553         gridLayout.numColumns = 2;
554         composite.setLayout(gridLayout);
555
556         return composite;
557     }
558
559     private Composite createBasicTab(TabFolder parent) {
560         Composite composite = new Composite(parent, SWT.NULL);
561         composite.setLayout(new GridLayout());
562         GridData gridData = new GridData(GridData.FILL_BOTH);
563         composite.setLayoutData(gridData);
564         Composite compositeKeyConfiguration = new Composite(composite, SWT.NULL);
565         GridLayout gridLayout = new GridLayout();
566         gridLayout.numColumns = 3;
567         compositeKeyConfiguration.setLayout(gridLayout);
568         gridData = new GridData(GridData.FILL_HORIZONTAL);
569         compositeKeyConfiguration.setLayoutData(gridData);
570         labelKeyConfiguration = new Label(compositeKeyConfiguration, SWT.LEFT);
571         labelKeyConfiguration.setText(Util.translateString(RESOURCE_BUNDLE,
572                 "labelKeyConfiguration")); //$NON-NLS-1$
573
comboKeyConfiguration = new Combo(compositeKeyConfiguration,
574                 SWT.READ_ONLY);
575         gridData = new GridData();
576         gridData.widthHint = 200;
577         comboKeyConfiguration.setLayoutData(gridData);
578
579         comboKeyConfiguration.addSelectionListener(new SelectionAdapter() {
580
581             public void widgetSelected(SelectionEvent selectionEvent) {
582                 selectedComboKeyConfiguration();
583             }
584         });
585
586         labelKeyConfigurationExtends = new Label(compositeKeyConfiguration,
587                 SWT.LEFT);
588         gridData = new GridData(GridData.FILL_HORIZONTAL);
589         labelKeyConfigurationExtends.setLayoutData(gridData);
590         Control spacer = new Composite(composite, SWT.NULL);
591         gridData = new GridData();
592         gridData.heightHint = 10;
593         gridData.widthHint = 10;
594         spacer.setLayoutData(gridData);
595         groupCommand = new Group(composite, SWT.SHADOW_NONE);
596         gridLayout = new GridLayout();
597         gridLayout.numColumns = 3;
598         groupCommand.setLayout(gridLayout);
599         gridData = new GridData(GridData.FILL_BOTH);
600         groupCommand.setLayoutData(gridData);
601         groupCommand.setText(Util.translateString(RESOURCE_BUNDLE,
602                 "groupCommand")); //$NON-NLS-1$
603
labelCategory = new Label(groupCommand, SWT.LEFT);
604         gridData = new GridData();
605         labelCategory.setLayoutData(gridData);
606         labelCategory.setText(Util.translateString(RESOURCE_BUNDLE,
607                 "labelCategory")); //$NON-NLS-1$
608
comboCategory = new Combo(groupCommand, SWT.READ_ONLY);
609         gridData = new GridData();
610         gridData.horizontalSpan = 2;
611         gridData.widthHint = 200;
612         comboCategory.setLayoutData(gridData);
613
614         comboCategory.addSelectionListener(new SelectionAdapter() {
615
616             public void widgetSelected(SelectionEvent selectionEvent) {
617                 selectedComboCategory();
618             }
619         });
620
621         labelCommand = new Label(groupCommand, SWT.LEFT);
622         gridData = new GridData();
623         labelCommand.setLayoutData(gridData);
624         labelCommand.setText(Util.translateString(RESOURCE_BUNDLE,
625                 "labelCommand")); //$NON-NLS-1$
626
comboCommand = new Combo(groupCommand, SWT.READ_ONLY);
627         gridData = new GridData();
628         gridData.horizontalSpan = 2;
629         gridData.widthHint = 300;
630         comboCommand.setLayoutData(gridData);
631
632         comboCommand.addSelectionListener(new SelectionAdapter() {
633
634             public void widgetSelected(SelectionEvent selectionEvent) {
635                 selectedComboCommand();
636             }
637         });
638
639         labelAssignmentsForCommand = new Label(groupCommand, SWT.LEFT);
640         gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
641         gridData.verticalAlignment = GridData.FILL_VERTICAL;
642         labelAssignmentsForCommand.setLayoutData(gridData);
643         labelAssignmentsForCommand.setText(Util.translateString(
644                 RESOURCE_BUNDLE, "labelAssignmentsForCommand")); //$NON-NLS-1$
645
tableAssignmentsForCommand = new Table(groupCommand, SWT.BORDER
646                 | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
647         tableAssignmentsForCommand.setHeaderVisible(true);
648         gridData = new GridData(GridData.FILL_BOTH);
649         gridData.heightHint = 60;
650         gridData.horizontalSpan = 2;
651         gridData.widthHint = "carbon".equals(SWT.getPlatform()) ? 620 : 520; //$NON-NLS-1$
652
tableAssignmentsForCommand.setLayoutData(gridData);
653         TableColumn tableColumnDelta = new TableColumn(
654                 tableAssignmentsForCommand, SWT.NULL, 0);
655         tableColumnDelta.setResizable(false);
656         tableColumnDelta.setText(Util.ZERO_LENGTH_STRING);
657         tableColumnDelta.setWidth(20);
658         TableColumn tableColumnContext = new TableColumn(
659                 tableAssignmentsForCommand, SWT.NULL, 1);
660         tableColumnContext.setResizable(true);
661         tableColumnContext.setText(Util.translateString(RESOURCE_BUNDLE,
662                 "tableColumnContext")); //$NON-NLS-1$
663
tableColumnContext.pack();
664         tableColumnContext.setWidth(200);
665         TableColumn tableColumnKeySequence = new TableColumn(
666                 tableAssignmentsForCommand, SWT.NULL, 2);
667         tableColumnKeySequence.setResizable(true);
668         tableColumnKeySequence.setText(Util.translateString(RESOURCE_BUNDLE,
669                 "tableColumnKeySequence")); //$NON-NLS-1$
670
tableColumnKeySequence.pack();
671         tableColumnKeySequence.setWidth(300);
672
673         tableAssignmentsForCommand.addMouseListener(new MouseAdapter() {
674
675             public void mouseDoubleClick(MouseEvent mouseEvent) {
676                 doubleClickedAssignmentsForCommand();
677             }
678         });
679
680         tableAssignmentsForCommand.addSelectionListener(new SelectionAdapter() {
681
682             public void widgetSelected(SelectionEvent selectionEvent) {
683                 selectedTableAssignmentsForCommand();
684             }
685         });
686
687         groupKeySequence = new Group(composite, SWT.SHADOW_NONE);
688         gridLayout = new GridLayout();
689         gridLayout.numColumns = 4;
690         groupKeySequence.setLayout(gridLayout);
691         gridData = new GridData(GridData.FILL_BOTH);
692         groupKeySequence.setLayoutData(gridData);
693         groupKeySequence.setText(Util.translateString(RESOURCE_BUNDLE,
694                 "groupKeySequence")); //$NON-NLS-1$
695
labelKeySequence = new Label(groupKeySequence, SWT.LEFT);
696         gridData = new GridData();
697         labelKeySequence.setLayoutData(gridData);
698         labelKeySequence.setText(Util.translateString(RESOURCE_BUNDLE,
699                 "labelKeySequence")); //$NON-NLS-1$
700

701         // The text widget into which the key strokes will be entered.
702
textKeySequence = new Text(groupKeySequence, SWT.BORDER);
703         // On MacOS X, this font will be changed by KeySequenceText
704
textKeySequence.setFont(groupKeySequence.getFont());
705         gridData = new GridData();
706         gridData.horizontalSpan = 2;
707         gridData.widthHint = 300;
708         textKeySequence.setLayoutData(gridData);
709         textKeySequence.addModifyListener(new ModifyListener() {
710
711             public void modifyText(ModifyEvent e) {
712                 modifiedTextKeySequence();
713             }
714         });
715         textKeySequence.addFocusListener(new FocusListener() {
716
717             /*
718              * (non-Javadoc)
719              *
720              * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
721              */

722             public void focusGained(FocusEvent e) {
723                 PlatformUI.getWorkbench().getContextSupport()
724                         .setKeyFilterEnabled(false);
725             }
726
727             /*
728              * (non-Javadoc)
729              *
730              * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
731              */

732             public void focusLost(FocusEvent e) {
733                 PlatformUI.getWorkbench().getContextSupport()
734                         .setKeyFilterEnabled(true);
735             }
736         });
737
738         // The manager for the key sequence text widget.
739
textKeySequenceManager = new KeySequenceText(textKeySequence);
740         textKeySequenceManager.setKeyStrokeLimit(4);
741
742         // Button for adding trapped key strokes
743
buttonAddKey = new Button(groupKeySequence, SWT.LEFT | SWT.ARROW);
744         buttonAddKey.setToolTipText(Util.translateString(RESOURCE_BUNDLE,
745                 "buttonAddKey.ToolTipText")); //$NON-NLS-1$
746
gridData = new GridData();
747         gridData.heightHint = comboCategory.getTextHeight();
748         buttonAddKey.setLayoutData(gridData);
749         buttonAddKey.addSelectionListener(new SelectionAdapter() {
750
751             public void widgetSelected(SelectionEvent selectionEvent) {
752                 Point buttonLocation = buttonAddKey.getLocation();
753                 buttonLocation = groupKeySequence.toDisplay(buttonLocation.x,
754                         buttonLocation.y);
755                 Point buttonSize = buttonAddKey.getSize();
756                 menuButtonAddKey.setLocation(buttonLocation.x, buttonLocation.y
757                         + buttonSize.y);
758                 menuButtonAddKey.setVisible(true);
759             }
760         });
761
762         // Arrow buttons aren't normally added to the tab list. Let's fix that.
763
Control[] tabStops = groupKeySequence.getTabList();
764         ArrayList JavaDoc newTabStops = new ArrayList JavaDoc();
765         for (int i = 0; i < tabStops.length; i++) {
766             Control tabStop = tabStops[i];
767             newTabStops.add(tabStop);
768             if (textKeySequence.equals(tabStop)) {
769                 newTabStops.add(buttonAddKey);
770             }
771         }
772         Control[] newTabStopArray = (Control[]) newTabStops
773                 .toArray(new Control[newTabStops.size()]);
774         groupKeySequence.setTabList(newTabStopArray);
775
776         // Construct the menu to attach to the above button.
777
menuButtonAddKey = new Menu(buttonAddKey);
778         Iterator JavaDoc trappedKeyItr = KeySequenceText.TRAPPED_KEYS.iterator();
779         while (trappedKeyItr.hasNext()) {
780             final KeyStroke trappedKey = (KeyStroke) trappedKeyItr.next();
781             MenuItem menuItem = new MenuItem(menuButtonAddKey, SWT.PUSH);
782             menuItem.setText(trappedKey.format());
783             menuItem.addSelectionListener(new SelectionAdapter() {
784
785                 public void widgetSelected(SelectionEvent e) {
786                     textKeySequenceManager.insert(trappedKey);
787                     textKeySequence.setFocus();
788                     textKeySequence
789                             .setSelection(textKeySequence.getTextLimit());
790                 }
791             });
792         }
793
794         labelAssignmentsForKeySequence = new Label(groupKeySequence, SWT.LEFT);
795         gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
796         gridData.verticalAlignment = GridData.FILL_VERTICAL;
797         labelAssignmentsForKeySequence.setLayoutData(gridData);
798         labelAssignmentsForKeySequence.setText(Util.translateString(
799                 RESOURCE_BUNDLE, "labelAssignmentsForKeySequence")); //$NON-NLS-1$
800
tableAssignmentsForKeySequence = new Table(groupKeySequence, SWT.BORDER
801                 | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
802         tableAssignmentsForKeySequence.setHeaderVisible(true);
803         gridData = new GridData(GridData.FILL_BOTH);
804         gridData.heightHint = 60;
805         gridData.horizontalSpan = 3;
806         gridData.widthHint = "carbon".equals(SWT.getPlatform()) ? 620 : 520; //$NON-NLS-1$
807
tableAssignmentsForKeySequence.setLayoutData(gridData);
808         tableColumnDelta = new TableColumn(tableAssignmentsForKeySequence,
809                 SWT.NULL, 0);
810         tableColumnDelta.setResizable(false);
811         tableColumnDelta.setText(Util.ZERO_LENGTH_STRING);
812         tableColumnDelta.setWidth(20);
813         tableColumnContext = new TableColumn(tableAssignmentsForKeySequence,
814                 SWT.NULL, 1);
815         tableColumnContext.setResizable(true);
816         tableColumnContext.setText(Util.translateString(RESOURCE_BUNDLE,
817                 "tableColumnContext")); //$NON-NLS-1$
818
tableColumnContext.pack();
819         tableColumnContext.setWidth(200);
820         TableColumn tableColumnCommand = new TableColumn(
821                 tableAssignmentsForKeySequence, SWT.NULL, 2);
822         tableColumnCommand.setResizable(true);
823         tableColumnCommand.setText(Util.translateString(RESOURCE_BUNDLE,
824                 "tableColumnCommand")); //$NON-NLS-1$
825
tableColumnCommand.pack();
826         tableColumnCommand.setWidth(300);
827
828         tableAssignmentsForKeySequence.addMouseListener(new MouseAdapter() {
829
830             public void mouseDoubleClick(MouseEvent mouseEvent) {
831                 doubleClickedTableAssignmentsForKeySequence();
832             }
833         });
834
835         tableAssignmentsForKeySequence
836                 .addSelectionListener(new SelectionAdapter() {
837
838                     public void widgetSelected(SelectionEvent selectionEvent) {
839                         selectedTableAssignmentsForKeySequence();
840                     }
841                 });
842
843         Composite compositeContext = new Composite(composite, SWT.NULL);
844         gridLayout = new GridLayout();
845         gridLayout.numColumns = 3;
846         compositeContext.setLayout(gridLayout);
847         gridData = new GridData(GridData.FILL_HORIZONTAL);
848         compositeContext.setLayoutData(gridData);
849         labelContext = new Label(compositeContext, SWT.LEFT);
850         labelContext.setText(Util.translateString(RESOURCE_BUNDLE,
851                 "labelContext")); //$NON-NLS-1$
852
comboContext = new Combo(compositeContext, SWT.READ_ONLY);
853         gridData = new GridData();
854         gridData.widthHint = 250;
855         comboContext.setLayoutData(gridData);
856
857         comboContext.addSelectionListener(new SelectionAdapter() {
858
859             public void widgetSelected(SelectionEvent selectionEvent) {
860                 selectedComboContext();
861             }
862         });
863
864         labelContextExtends = new Label(compositeContext, SWT.LEFT);
865         gridData = new GridData(GridData.FILL_HORIZONTAL);
866         labelContextExtends.setLayoutData(gridData);
867         Composite compositeButton = new Composite(composite, SWT.NULL);
868         gridLayout = new GridLayout();
869         gridLayout.marginHeight = 20;
870         gridLayout.marginWidth = 0;
871         gridLayout.numColumns = 3;
872         compositeButton.setLayout(gridLayout);
873         gridData = new GridData();
874         compositeButton.setLayoutData(gridData);
875         buttonAdd = new Button(compositeButton, SWT.CENTER | SWT.PUSH);
876         gridData = new GridData();
877         gridData.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
878         int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
879         buttonAdd.setText(Util.translateString(RESOURCE_BUNDLE, "buttonAdd")); //$NON-NLS-1$
880
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         gridData.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
894         widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
895         buttonRemove.setText(Util.translateString(RESOURCE_BUNDLE,
896                 "buttonRemove")); //$NON-NLS-1$
897
gridData.widthHint = Math.max(widthHint, buttonRemove.computeSize(
898                 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5;
899         buttonRemove.setLayoutData(gridData);
900
901         buttonRemove.addSelectionListener(new SelectionAdapter() {
902
903             public void widgetSelected(SelectionEvent selectionEvent) {
904                 selectedButtonRemove();
905             }
906         });
907
908         buttonRestore = new Button(compositeButton, SWT.CENTER | SWT.PUSH);
909         gridData = new GridData();
910         gridData.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
911         widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
912         buttonRestore.setText(Util.translateString(RESOURCE_BUNDLE,
913                 "buttonRestore")); //$NON-NLS-1$
914
gridData.widthHint = Math.max(widthHint, buttonRestore.computeSize(
915                 SWT.DEFAULT, SWT.DEFAULT, true).x) + 5;
916         buttonRestore.setLayoutData(gridData);
917
918         buttonRestore.addSelectionListener(new SelectionAdapter() {
919
920             public void widgetSelected(SelectionEvent selectionEvent) {
921                 selectedButtonRestore();
922             }
923         });
924
925         // TODO WorkbenchHelp.setHelp(parent,
926
// IHelpContextIds.WORKBENCH_KEY_PREFERENCE_PAGE);
927
return composite;
928     }
929
930     protected Control createContents(Composite parent) {
931         // Initialize the minus colour.
932
minusColour = getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
933         
934         final TabFolder tabFolder = new TabFolder(parent, SWT.NULL);
935
936         // Basic tab
937
final TabItem basicTab = new TabItem(tabFolder, SWT.NULL);
938         basicTab
939                 .setText(Util.translateString(RESOURCE_BUNDLE, "basicTab.Text")); //$NON-NLS-1$
940
basicTab.setControl(createBasicTab(tabFolder));
941
942         // Advanced tab
943
final TabItem advancedTab = new TabItem(tabFolder, SWT.NULL);
944         advancedTab.setText(Util.translateString(RESOURCE_BUNDLE,
945                 "advancedTab.Text")); //$NON-NLS-1$
946
advancedTab.setControl(createAdvancedTab(tabFolder));
947
948         applyDialogFont(tabFolder);
949         return tabFolder;
950     }
951
952     protected IPreferenceStore doGetPreferenceStore() {
953         return PlatformUI.getWorkbench().getPreferenceStore();
954     }
955
956     private void doubleClickedAssignmentsForCommand() {
957         update();
958     }
959
960     private void doubleClickedTableAssignmentsForKeySequence() {
961         update();
962     }
963
964     private String JavaDoc getCategoryId() {
965         return !commandIdsByCategoryId.containsKey(null)
966                 || comboCategory.getSelectionIndex() > 0 ? (String JavaDoc) categoryIdsByUniqueName
967                 .get(comboCategory.getText())
968                 : null;
969     }
970
971     private String JavaDoc getCommandId() {
972         return (String JavaDoc) commandIdsByUniqueName.get(comboCommand.getText());
973     }
974
975     private String JavaDoc getContextId() {
976         return comboContext.getSelectionIndex() >= 0 ? (String JavaDoc) contextIdsByUniqueName
977                 .get(comboContext.getText())
978                 : null;
979     }
980
981     private String JavaDoc getKeyConfigurationId() {
982         return comboKeyConfiguration.getSelectionIndex() >= 0 ? (String JavaDoc) keyConfigurationIdsByUniqueName
983                 .get(comboKeyConfiguration.getText())
984                 : null;
985     }
986
987     private KeySequence getKeySequence() {
988         return textKeySequenceManager.getKeySequence();
989     }
990
991     public void init(IWorkbench workbench) {
992         IWorkbenchContextSupport workbenchContextSupport = workbench
993                 .getContextSupport();
994         contextManager = workbenchContextSupport.getContextManager();
995         // TODO remove blind cast
996
commandManager = (MutableCommandManager) workbench.getCommandSupport()
997                 .getCommandManager();
998         commandAssignments = new TreeSet JavaDoc();
999         keySequenceAssignments = new TreeSet JavaDoc();
1000    }
1001
1002    private void modifiedTextKeySequence() {
1003        update();
1004    }
1005
1006    protected void performDefaults() {
1007        String JavaDoc activeKeyConfigurationId = getKeyConfigurationId();
1008        List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc();
1009        KeySequenceBindingNode.getKeySequenceBindingDefinitions(tree,
1010                KeySequence.getInstance(), 0,
1011                preferenceKeySequenceBindingDefinitions);
1012
1013        if (activeKeyConfigurationId != null
1014                || !preferenceKeySequenceBindingDefinitions.isEmpty()) {
1015            final String JavaDoc title = Util.translateString(RESOURCE_BUNDLE,
1016                    "restoreDefaultsMessageBoxText"); //$NON-NLS-1$
1017
final String JavaDoc message = Util.translateString(RESOURCE_BUNDLE,
1018                    "restoreDefaultsMessageBoxMessage"); //$NON-NLS-1$
1019
final boolean confirmed = MessageDialog.openConfirm(getShell(),
1020                    title, message);
1021
1022            if (confirmed) {
1023                setKeyConfigurationId(IWorkbenchConstants.DEFAULT_ACCELERATOR_CONFIGURATION_ID);
1024                Iterator JavaDoc iterator = preferenceKeySequenceBindingDefinitions
1025                        .iterator();
1026
1027                while (iterator.hasNext()) {
1028                    KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
1029                            .next();
1030                    KeySequenceBindingNode.remove(tree,
1031                            keySequenceBindingDefinition.getKeySequence(),
1032                            keySequenceBindingDefinition.getContextId(),
1033                            keySequenceBindingDefinition
1034                                    .getKeyConfigurationId(), 0,
1035                            keySequenceBindingDefinition.getPlatform(),
1036                            keySequenceBindingDefinition.getLocale(),
1037                            keySequenceBindingDefinition.getCommandId());
1038                }
1039            }
1040        }
1041
1042        // Set the defaults on the advanced tab.
1043
IPreferenceStore store = getPreferenceStore();
1044        checkBoxMultiKeyAssist.setSelection(store
1045                .getDefaultBoolean(IPreferenceConstants.MULTI_KEY_ASSIST));
1046        textMultiKeyAssistTime.setStringValue(Integer.toString(store
1047                .getDefaultInt(IPreferenceConstants.MULTI_KEY_ASSIST_TIME)));
1048
1049        update();
1050    }
1051
1052    public boolean performOk() {
1053        List JavaDoc preferenceActiveKeyConfigurationDefinitions = new ArrayList JavaDoc();
1054        preferenceActiveKeyConfigurationDefinitions
1055                .add(new ActiveKeyConfigurationDefinition(
1056                        getKeyConfigurationId(), null));
1057        PreferenceCommandRegistry preferenceCommandRegistry = (PreferenceCommandRegistry) commandManager
1058                .getMutableCommandRegistry();
1059        preferenceCommandRegistry
1060                .setActiveKeyConfigurationDefinitions(preferenceActiveKeyConfigurationDefinitions);
1061        List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc();
1062        KeySequenceBindingNode.getKeySequenceBindingDefinitions(tree,
1063                KeySequence.getInstance(), 0,
1064                preferenceKeySequenceBindingDefinitions);
1065        preferenceCommandRegistry
1066                .setKeySequenceBindingDefinitions(preferenceKeySequenceBindingDefinitions);
1067
1068        try {
1069            preferenceCommandRegistry.save();
1070        } catch (IOException JavaDoc eIO) {
1071            // Do nothing
1072
}
1073
1074        // Save the advanced settings.
1075
IPreferenceStore store = getPreferenceStore();
1076        store.setValue(IPreferenceConstants.MULTI_KEY_ASSIST,
1077                checkBoxMultiKeyAssist.getSelection());
1078        store.setValue(IPreferenceConstants.MULTI_KEY_ASSIST_TIME,
1079                textMultiKeyAssistTime.getIntValue());
1080        return super.performOk();
1081    }
1082
1083    private void selectAssignmentForCommand(String JavaDoc contextId) {
1084        if (tableAssignmentsForCommand.getSelectionCount() > 1)
1085                tableAssignmentsForCommand.deselectAll();
1086
1087        int i = 0;
1088        int selection = -1;
1089        KeySequence keySequence = getKeySequence();
1090
1091        for (Iterator JavaDoc iterator = commandAssignments.iterator(); iterator
1092                .hasNext(); i++) {
1093            CommandAssignment commandAssignment = (CommandAssignment) iterator
1094                    .next();
1095
1096            if (Util.equals(contextId, commandAssignment.contextId)
1097                    && Util.equals(keySequence, commandAssignment.keySequence)) {
1098                selection = i;
1099                break;
1100            }
1101        }
1102
1103        if (selection != tableAssignmentsForCommand.getSelectionIndex()) {
1104            if (selection == -1
1105                    || selection >= tableAssignmentsForCommand.getItemCount())
1106                tableAssignmentsForCommand.deselectAll();
1107            else
1108                tableAssignmentsForCommand.select(selection);
1109        }
1110    }
1111
1112    private void selectAssignmentForKeySequence(String JavaDoc contextId) {
1113        if (tableAssignmentsForKeySequence.getSelectionCount() > 1)
1114                tableAssignmentsForKeySequence.deselectAll();
1115
1116        int i = 0;
1117        int selection = -1;
1118
1119        for (Iterator JavaDoc iterator = keySequenceAssignments.iterator(); iterator
1120                .hasNext(); i++) {
1121            KeySequenceAssignment keySequenceAssignment = (KeySequenceAssignment) iterator
1122                    .next();
1123
1124            if (Util.equals(contextId, keySequenceAssignment.contextId)) {
1125                selection = i;
1126                break;
1127            }
1128        }
1129
1130        if (selection != tableAssignmentsForKeySequence.getSelectionIndex()) {
1131            if (selection == -1
1132                    || selection >= tableAssignmentsForKeySequence
1133                            .getItemCount())
1134                tableAssignmentsForKeySequence.deselectAll();
1135            else
1136                tableAssignmentsForKeySequence.select(selection);
1137        }
1138    }
1139
1140    private void selectedButtonAdd() {
1141        String JavaDoc commandId = getCommandId();
1142        String JavaDoc contextId = getContextId();
1143        String JavaDoc keyConfigurationId = getKeyConfigurationId();
1144        KeySequence keySequence = getKeySequence();
1145        KeySequenceBindingNode.remove(tree, keySequence, contextId,
1146                keyConfigurationId, 0, null, null);
1147        KeySequenceBindingNode.add(tree, keySequence, contextId,
1148                keyConfigurationId, 0, null, null, commandId);
1149        List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc();
1150        KeySequenceBindingNode.getKeySequenceBindingDefinitions(tree,
1151                KeySequence.getInstance(), 0,
1152                preferenceKeySequenceBindingDefinitions);
1153        update();
1154    }
1155
1156    private void selectedButtonRemove() {
1157        String JavaDoc contextId = getContextId();
1158        String JavaDoc keyConfigurationId = getKeyConfigurationId();
1159        KeySequence keySequence = getKeySequence();
1160        KeySequenceBindingNode.remove(tree, keySequence, contextId,
1161                keyConfigurationId, 0, null, null);
1162        KeySequenceBindingNode.add(tree, keySequence, contextId,
1163                keyConfigurationId, 0, null, null, null);
1164        List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc();
1165        KeySequenceBindingNode.getKeySequenceBindingDefinitions(tree,
1166                KeySequence.getInstance(), 0,
1167                preferenceKeySequenceBindingDefinitions);
1168        update();
1169    }
1170
1171    private void selectedButtonRestore() {
1172        String JavaDoc contextId = getContextId();
1173        String JavaDoc keyConfigurationId = getKeyConfigurationId();
1174        KeySequence keySequence = getKeySequence();
1175        KeySequenceBindingNode.remove(tree, keySequence, contextId,
1176                keyConfigurationId, 0, null, null);
1177        List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc();
1178        KeySequenceBindingNode.getKeySequenceBindingDefinitions(tree,
1179                KeySequence.getInstance(), 0,
1180                preferenceKeySequenceBindingDefinitions);
1181        update();
1182    }
1183
1184    private void selectedComboCategory() {
1185        update();
1186    }
1187
1188    private void selectedComboCommand() {
1189        update();
1190    }
1191
1192    private void selectedComboContext() {
1193        update();
1194    }
1195
1196    private void selectedComboKeyConfiguration() {
1197        update();
1198    }
1199
1200    private void selectedTableAssignmentsForCommand() {
1201        int selection = tableAssignmentsForCommand.getSelectionIndex();
1202        List JavaDoc commandAssignmentsAsList = new ArrayList JavaDoc(commandAssignments);
1203
1204        if (selection >= 0 && selection < commandAssignmentsAsList.size()
1205                && tableAssignmentsForCommand.getSelectionCount() == 1) {
1206            CommandAssignment commandAssignment = (CommandAssignment) commandAssignmentsAsList
1207                    .get(selection);
1208            String JavaDoc contextId = commandAssignment.contextId;
1209            KeySequence keySequence = commandAssignment.keySequence;
1210            setContextId(contextId);
1211            setKeySequence(keySequence);
1212        }
1213
1214        update();
1215    }
1216
1217    private void selectedTableAssignmentsForKeySequence() {
1218        int selection = tableAssignmentsForKeySequence.getSelectionIndex();
1219        List JavaDoc keySequenceAssignmentsAsList = new ArrayList JavaDoc(
1220                keySequenceAssignments);
1221
1222        if (selection >= 0 && selection < keySequenceAssignmentsAsList.size()
1223                && tableAssignmentsForKeySequence.getSelectionCount() == 1) {
1224            KeySequenceAssignment keySequenceAssignment = (KeySequenceAssignment) keySequenceAssignmentsAsList
1225                    .get(selection);
1226            String JavaDoc contextId = keySequenceAssignment.contextId;
1227            setContextId(contextId);
1228        }
1229
1230        update();
1231    }
1232
1233    private void setAssignmentsForCommand() {
1234        commandAssignments.clear();
1235        String JavaDoc commandId = getCommandId();
1236
1237        for (Iterator JavaDoc iterator = assignmentsByContextIdByKeySequence.entrySet()
1238                .iterator(); iterator.hasNext();) {
1239            Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1240            KeySequence keySequence = (KeySequence) entry.getKey();
1241            Map JavaDoc assignmentsByContextId = (Map JavaDoc) entry.getValue();
1242
1243            if (assignmentsByContextId != null)
1244                    for (Iterator JavaDoc iterator2 = assignmentsByContextId.entrySet()
1245                            .iterator(); iterator2.hasNext();) {
1246                        Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc) iterator2.next();
1247                        CommandAssignment commandAssignment = new CommandAssignment();
1248                        commandAssignment.assignment = (KeySequenceBindingNode.Assignment) entry2
1249                                .getValue();
1250                        commandAssignment.contextId = (String JavaDoc) entry2.getKey();
1251                        commandAssignment.keySequence = keySequence;
1252
1253                        if (commandAssignment.assignment.contains(commandId))
1254                                commandAssignments.add(commandAssignment);
1255                    }
1256        }
1257
1258        buildCommandAssignmentsTable();
1259    }
1260
1261    private void setAssignmentsForKeySequence() {
1262        keySequenceAssignments.clear();
1263        KeySequence keySequence = getKeySequence();
1264        Map JavaDoc assignmentsByContextId = (Map JavaDoc) assignmentsByContextIdByKeySequence
1265                .get(keySequence);
1266
1267        if (assignmentsByContextId != null)
1268                for (Iterator JavaDoc iterator = assignmentsByContextId.entrySet()
1269                        .iterator(); iterator.hasNext();) {
1270                    Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1271                    KeySequenceAssignment keySequenceAssignment = new KeySequenceAssignment();
1272                    keySequenceAssignment.assignment = (KeySequenceBindingNode.Assignment) entry
1273                            .getValue();
1274                    keySequenceAssignment.contextId = (String JavaDoc) entry.getKey();
1275                    keySequenceAssignments.add(keySequenceAssignment);
1276                }
1277
1278        buildKeySequenceAssignmentsTable();
1279    }
1280
1281    private void setCommandId(String JavaDoc commandId) {
1282        comboCommand.clearSelection();
1283        comboCommand.deselectAll();
1284        String JavaDoc commandUniqueName = (String JavaDoc) commandUniqueNamesById
1285                .get(commandId);
1286
1287        if (commandUniqueName != null) {
1288            String JavaDoc items[] = comboCommand.getItems();
1289
1290            for (int i = 0; i < items.length; i++)
1291                if (commandUniqueName.equals(items[i])) {
1292                    comboCommand.select(i);
1293                    break;
1294                }
1295        }
1296    }
1297
1298    private void setCommandsForCategory() {
1299        String JavaDoc categoryId = getCategoryId();
1300        String JavaDoc commandId = getCommandId();
1301        Set JavaDoc commandIds = (Set JavaDoc) commandIdsByCategoryId.get(categoryId);
1302        Map JavaDoc commandIdsByName = new HashMap JavaDoc(commandIdsByUniqueName);
1303        if (commandIds == null) {
1304            commandIdsByName = new HashMap JavaDoc();
1305        } else {
1306            commandIdsByName.values().retainAll(commandIds);
1307        }
1308        List JavaDoc commandNames = new ArrayList JavaDoc(commandIdsByName.keySet());
1309        Collections.sort(commandNames, Collator.getInstance());
1310        comboCommand.setItems((String JavaDoc[]) commandNames
1311                .toArray(new String JavaDoc[commandNames.size()]));
1312        setCommandId(commandId);
1313
1314        if (comboCommand.getSelectionIndex() == -1 && !commandNames.isEmpty())
1315                comboCommand.select(0);
1316    }
1317
1318    /**
1319     * Changes the selected context name in the context combo box. The context
1320     * selected is either the one matching the identifier provided (if
1321     * possible), or the default context identifier. If no matching name can be
1322     * found in the combo, then the first item is selected.
1323     *
1324     * @param contextId
1325     * The context identifier for the context to be selected in the
1326     * combo box; may be <code>null</code>.
1327     */

1328    private void setContextId(String JavaDoc contextId) {
1329        // Clear the current selection.
1330
comboContext.clearSelection();
1331        comboContext.deselectAll();
1332
1333        // Figure out which name to look for.
1334
String JavaDoc contextName = (String JavaDoc) contextUniqueNamesById.get(contextId);
1335        if (contextName == null) {
1336            contextName = (String JavaDoc) contextUniqueNamesById
1337                    .get(KeySequenceBinding.DEFAULT_CONTEXT_ID);
1338        }
1339        if (contextName == null) {
1340            contextName = Util.ZERO_LENGTH_STRING;
1341        }
1342
1343        // Scan the list for the selection we're looking for.
1344
final String JavaDoc[] items = comboContext.getItems();
1345        boolean found = false;
1346        for (int i = 0; i < items.length; i++) {
1347            if (contextName.equals(items[i])) {
1348                comboContext.select(i);
1349                found = true;
1350                break;
1351            }
1352        }
1353
1354        // If we didn't find an item, then set the first item as selected.
1355
if ((!found) && (items.length > 0)) {
1356            comboContext.select(0);
1357        }
1358    }
1359
1360    private void setContextsForCommand() {
1361        String JavaDoc commandId = getCommandId();
1362        String JavaDoc contextId = getContextId();
1363        Map JavaDoc contextIdsByName = new HashMap JavaDoc(contextIdsByUniqueName);
1364
1365        List JavaDoc contextNames = new ArrayList JavaDoc(contextIdsByName.keySet());
1366        Collections.sort(contextNames, Collator.getInstance());
1367
1368        comboContext.setItems((String JavaDoc[]) contextNames
1369                .toArray(new String JavaDoc[contextNames.size()]));
1370        setContextId(contextId);
1371
1372        if (comboContext.getSelectionIndex() == -1 && !contextNames.isEmpty())
1373                comboContext.select(0);
1374    }
1375
1376    private void setKeyConfigurationId(String JavaDoc keyConfigurationId) {
1377        comboKeyConfiguration.clearSelection();
1378        comboKeyConfiguration.deselectAll();
1379        String JavaDoc keyConfigurationUniqueName = (String JavaDoc) keyConfigurationUniqueNamesById
1380                .get(keyConfigurationId);
1381
1382        if (keyConfigurationUniqueName != null) {
1383            String JavaDoc items[] = comboKeyConfiguration.getItems();
1384
1385            for (int i = 0; i < items.length; i++)
1386                if (keyConfigurationUniqueName.equals(items[i])) {
1387                    comboKeyConfiguration.select(i);
1388                    break;
1389                }
1390        }
1391    }
1392
1393    private void setKeySequence(KeySequence keySequence) {
1394        textKeySequenceManager.setKeySequence(keySequence);
1395    }
1396
1397    public void setVisible(boolean visible) {
1398        if (visible == true) {
1399            Map JavaDoc contextsByName = new HashMap JavaDoc();
1400
1401            for (Iterator JavaDoc iterator = contextManager.getDefinedContextIds()
1402                    .iterator(); iterator.hasNext();) {
1403                IContext context = contextManager.getContext((String JavaDoc) iterator
1404                        .next());
1405
1406                try {
1407                    String JavaDoc name = context.getName();
1408                    Collection JavaDoc contexts = (Collection JavaDoc) contextsByName.get(name);
1409
1410                    if (contexts == null) {
1411                        contexts = new HashSet JavaDoc();
1412                        contextsByName.put(name, contexts);
1413                    }
1414
1415                    contexts.add(context);
1416                } catch (org.eclipse.ui.contexts.NotDefinedException eNotDefined) {
1417                    // Do nothing
1418
}
1419            }
1420
1421            Map JavaDoc categoriesByName = new HashMap JavaDoc();
1422
1423            for (Iterator JavaDoc iterator = commandManager.getDefinedCategoryIds()
1424                    .iterator(); iterator.hasNext();) {
1425                ICategory category = commandManager
1426                        .getCategory((String JavaDoc) iterator.next());
1427
1428                try {
1429                    String JavaDoc name = category.getName();
1430                    Collection JavaDoc categories = (Collection JavaDoc) categoriesByName
1431                            .get(name);
1432
1433                    if (categories == null) {
1434                        categories = new HashSet JavaDoc();
1435                        categoriesByName.put(name, categories);
1436                    }
1437
1438                    categories.add(category);
1439                } catch (org.eclipse.ui.commands.NotDefinedException eNotDefined) {
1440                    // Do nothing
1441
}
1442            }
1443
1444            Map JavaDoc commandsByName = new HashMap JavaDoc();
1445
1446            for (Iterator JavaDoc iterator = commandManager.getDefinedCommandIds()
1447                    .iterator(); iterator.hasNext();) {
1448                ICommand command = commandManager.getCommand((String JavaDoc) iterator
1449                        .next());
1450
1451                try {
1452                    String JavaDoc name = command.getName();
1453                    Collection JavaDoc commands = (Collection JavaDoc) commandsByName.get(name);
1454
1455                    if (commands == null) {
1456                        commands = new HashSet JavaDoc();
1457                        commandsByName.put(name, commands);
1458                    }
1459
1460                    commands.add(command);
1461                } catch (org.eclipse.ui.commands.NotDefinedException eNotDefined) {
1462                    // Do nothing
1463
}
1464            }
1465
1466            Map JavaDoc keyConfigurationsByName = new HashMap JavaDoc();
1467
1468            for (Iterator JavaDoc iterator = commandManager
1469                    .getDefinedKeyConfigurationIds().iterator(); iterator
1470                    .hasNext();) {
1471                IKeyConfiguration keyConfiguration = commandManager
1472                        .getKeyConfiguration((String JavaDoc) iterator.next());
1473
1474                try {
1475                    String JavaDoc name = keyConfiguration.getName();
1476                    Collection JavaDoc keyConfigurations = (Collection JavaDoc) keyConfigurationsByName
1477                            .get(name);
1478
1479                    if (keyConfigurations == null) {
1480                        keyConfigurations = new HashSet JavaDoc();
1481                        keyConfigurationsByName.put(name, keyConfigurations);
1482                    }
1483
1484                    keyConfigurations.add(keyConfiguration);
1485                } catch (org.eclipse.ui.commands.NotDefinedException eNotDefined) {
1486                    // Do nothing
1487
}
1488            }
1489
1490            contextIdsByUniqueName = new HashMap JavaDoc();
1491            contextUniqueNamesById = new HashMap JavaDoc();
1492
1493            for (Iterator JavaDoc iterator = contextsByName.entrySet().iterator(); iterator
1494                    .hasNext();) {
1495                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1496                String JavaDoc name = (String JavaDoc) entry.getKey();
1497                Set JavaDoc contexts = (Set JavaDoc) entry.getValue();
1498                Iterator JavaDoc iterator2 = contexts.iterator();
1499
1500                if (contexts.size() == 1) {
1501                    IContext context = (IContext) iterator2.next();
1502                    contextIdsByUniqueName.put(name, context.getId());
1503                    contextUniqueNamesById.put(context.getId(), name);
1504                } else
1505                    while (iterator2.hasNext()) {
1506                        IContext context = (IContext) iterator2.next();
1507                        String JavaDoc uniqueName = MessageFormat.format(
1508                                Util.translateString(RESOURCE_BUNDLE,
1509                                        "uniqueName"), new Object JavaDoc[] { name, //$NON-NLS-1$
1510
context.getId()});
1511                        contextIdsByUniqueName.put(uniqueName, context.getId());
1512                        contextUniqueNamesById.put(context.getId(), uniqueName);
1513                    }
1514            }
1515
1516            categoryIdsByUniqueName = new HashMap JavaDoc();
1517            categoryUniqueNamesById = new HashMap JavaDoc();
1518
1519            for (Iterator JavaDoc iterator = categoriesByName.entrySet().iterator(); iterator
1520                    .hasNext();) {
1521                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1522                String JavaDoc name = (String JavaDoc) entry.getKey();
1523                Set JavaDoc categories = (Set JavaDoc) entry.getValue();
1524                Iterator JavaDoc iterator2 = categories.iterator();
1525
1526                if (categories.size() == 1) {
1527                    ICategory category = (ICategory) iterator2.next();
1528                    categoryIdsByUniqueName.put(name, category.getId());
1529                    categoryUniqueNamesById.put(category.getId(), name);
1530                } else
1531                    while (iterator2.hasNext()) {
1532                        ICategory category = (ICategory) iterator2.next();
1533                        String JavaDoc uniqueName = MessageFormat.format(
1534                                Util.translateString(RESOURCE_BUNDLE,
1535                                        "uniqueName"), new Object JavaDoc[] { name, //$NON-NLS-1$
1536
category.getId()});
1537                        categoryIdsByUniqueName.put(uniqueName, category
1538                                .getId());
1539                        categoryUniqueNamesById.put(category.getId(),
1540                                uniqueName);
1541                    }
1542            }
1543
1544            commandIdsByUniqueName = new HashMap JavaDoc();
1545            commandUniqueNamesById = new HashMap JavaDoc();
1546
1547            for (Iterator JavaDoc iterator = commandsByName.entrySet().iterator(); iterator
1548                    .hasNext();) {
1549                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1550                String JavaDoc name = (String JavaDoc) entry.getKey();
1551                Set JavaDoc commands = (Set JavaDoc) entry.getValue();
1552                Iterator JavaDoc iterator2 = commands.iterator();
1553
1554                if (commands.size() == 1) {
1555                    ICommand command = (ICommand) iterator2.next();
1556                    commandIdsByUniqueName.put(name, command.getId());
1557                    commandUniqueNamesById.put(command.getId(), name);
1558                } else
1559                    while (iterator2.hasNext()) {
1560                        ICommand command = (ICommand) iterator2.next();
1561                        String JavaDoc uniqueName = MessageFormat.format(
1562                                Util.translateString(RESOURCE_BUNDLE,
1563                                        "uniqueName"), new Object JavaDoc[] { name, //$NON-NLS-1$
1564
command.getId()});
1565                        commandIdsByUniqueName.put(uniqueName, command.getId());
1566                        commandUniqueNamesById.put(command.getId(), uniqueName);
1567                    }
1568            }
1569
1570            keyConfigurationIdsByUniqueName = new HashMap JavaDoc();
1571            keyConfigurationUniqueNamesById = new HashMap JavaDoc();
1572
1573            for (Iterator JavaDoc iterator = keyConfigurationsByName.entrySet()
1574                    .iterator(); iterator.hasNext();) {
1575                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
1576                String JavaDoc name = (String JavaDoc) entry.getKey();
1577                Set JavaDoc keyConfigurations = (Set JavaDoc) entry.getValue();
1578                Iterator JavaDoc iterator2 = keyConfigurations.iterator();
1579
1580                if (keyConfigurations.size() == 1) {
1581                    IKeyConfiguration keyConfiguration = (IKeyConfiguration) iterator2
1582                            .next();
1583                    keyConfigurationIdsByUniqueName.put(name, keyConfiguration
1584                            .getId());
1585                    keyConfigurationUniqueNamesById.put(keyConfiguration
1586                            .getId(), name);
1587                } else
1588                    while (iterator2.hasNext()) {
1589                        IKeyConfiguration keyConfiguration = (IKeyConfiguration) iterator2
1590                                .next();
1591                        String JavaDoc uniqueName = MessageFormat.format(
1592                                Util.translateString(RESOURCE_BUNDLE,
1593                                        "uniqueName"), new Object JavaDoc[] { name, //$NON-NLS-1$
1594
keyConfiguration.getId()});
1595                        keyConfigurationIdsByUniqueName.put(uniqueName,
1596                                keyConfiguration.getId());
1597                        keyConfigurationUniqueNamesById.put(keyConfiguration
1598                                .getId(), uniqueName);
1599                    }
1600            }
1601
1602            String JavaDoc activeKeyConfigurationId = commandManager
1603                    .getActiveKeyConfigurationId();
1604            commandIdsByCategoryId = new HashMap JavaDoc();
1605
1606            for (Iterator JavaDoc iterator = commandManager.getDefinedCommandIds()
1607                    .iterator(); iterator.hasNext();) {
1608                ICommand command = commandManager.getCommand((String JavaDoc) iterator
1609                        .next());
1610
1611                try {
1612                    String JavaDoc categoryId = command.getCategoryId();
1613                    Collection JavaDoc commandIds = (Collection JavaDoc) commandIdsByCategoryId
1614                            .get(categoryId);
1615
1616                    if (commandIds == null) {
1617                        commandIds = new HashSet JavaDoc();
1618                        commandIdsByCategoryId.put(categoryId, commandIds);
1619                    }
1620
1621                    commandIds.add(command.getId());
1622                } catch (org.eclipse.ui.commands.NotDefinedException eNotDefined) {
1623                    // Do nothing
1624
}
1625            }
1626
1627            ICommandRegistry commandRegistry = commandManager
1628                    .getCommandRegistry();
1629            ICommandRegistry mutableCommandRegistry = commandManager
1630                    .getMutableCommandRegistry();
1631
1632            List JavaDoc pluginKeySequenceBindingDefinitions = new ArrayList JavaDoc(
1633                    commandRegistry.getKeySequenceBindingDefinitions());
1634
1635            for (Iterator JavaDoc iterator = pluginKeySequenceBindingDefinitions
1636                    .iterator(); iterator.hasNext();) {
1637                KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
1638                        .next();
1639                KeySequence keySequence = keySequenceBindingDefinition
1640                        .getKeySequence();
1641                String JavaDoc commandId = keySequenceBindingDefinition.getCommandId();
1642                String JavaDoc contextId = keySequenceBindingDefinition.getContextId();
1643                String JavaDoc keyConfigurationId = keySequenceBindingDefinition
1644                        .getKeyConfigurationId();
1645                boolean validKeySequence = keySequence != null
1646                        && MutableCommandManager.validateKeySequence(keySequence);
1647                boolean validContextId = contextId == null
1648                        || contextManager.getDefinedContextIds().contains(
1649                                contextId);
1650                boolean validCommandId = commandId == null
1651                        || commandManager.getDefinedCommandIds().contains(
1652                                commandId);
1653                boolean validKeyConfigurationId = keyConfigurationId == null
1654                        || commandManager.getDefinedKeyConfigurationIds()
1655                                .contains(keyConfigurationId);
1656
1657                if (!validKeySequence || !validCommandId || !validContextId
1658                        || !validKeyConfigurationId) iterator.remove();
1659            }
1660
1661            List JavaDoc preferenceKeySequenceBindingDefinitions = new ArrayList JavaDoc(
1662                    mutableCommandRegistry.getKeySequenceBindingDefinitions());
1663
1664            for (Iterator JavaDoc iterator = preferenceKeySequenceBindingDefinitions
1665                    .iterator(); iterator.hasNext();) {
1666                KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
1667                        .next();
1668                KeySequence keySequence = keySequenceBindingDefinition
1669                        .getKeySequence();
1670                String JavaDoc commandId = keySequenceBindingDefinition.getCommandId();
1671                String JavaDoc contextId = keySequenceBindingDefinition.getContextId();
1672                String JavaDoc keyConfigurationId = keySequenceBindingDefinition
1673                        .getKeyConfigurationId();
1674                boolean validKeySequence = keySequence != null
1675                        && MutableCommandManager.validateKeySequence(keySequence);
1676                boolean validContextId = contextId == null
1677                        || contextManager.getDefinedContextIds().contains(
1678                                contextId);
1679                boolean validCommandId = commandId == null
1680                        || commandManager.getDefinedCommandIds().contains(
1681                                commandId);
1682                boolean validKeyConfigurationId = keyConfigurationId == null
1683                        || commandManager.getDefinedKeyConfigurationIds()
1684                                .contains(keyConfigurationId);
1685
1686                if (!validKeySequence || !validCommandId || !validContextId
1687                        || !validKeyConfigurationId) iterator.remove();
1688            }
1689
1690            tree = new TreeMap JavaDoc();
1691
1692            for (Iterator JavaDoc iterator = pluginKeySequenceBindingDefinitions
1693                    .iterator(); iterator.hasNext();) {
1694                KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
1695                        .next();
1696                KeySequenceBindingNode.add(tree, keySequenceBindingDefinition
1697                        .getKeySequence(), keySequenceBindingDefinition
1698                        .getContextId(), keySequenceBindingDefinition
1699                        .getKeyConfigurationId(), 1,
1700                        keySequenceBindingDefinition.getPlatform(),
1701                        keySequenceBindingDefinition.getLocale(),
1702                        keySequenceBindingDefinition.getCommandId());
1703            }
1704
1705            for (Iterator JavaDoc iterator = preferenceKeySequenceBindingDefinitions
1706                    .iterator(); iterator.hasNext();) {
1707                KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
1708                        .next();
1709                KeySequenceBindingNode.add(tree, keySequenceBindingDefinition
1710                        .getKeySequence(), keySequenceBindingDefinition
1711                        .getContextId(), keySequenceBindingDefinition
1712                        .getKeyConfigurationId(), 0,
1713                        keySequenceBindingDefinition.getPlatform(),
1714                        keySequenceBindingDefinition.getLocale(),
1715                        keySequenceBindingDefinition.getCommandId());
1716            }
1717
1718            // TODO?
1719
//HashSet categoryIdsReferencedByCommandDefinitions = new
1720
// HashSet();
1721
//categoryDefinitionsById.keySet().retainAll(categoryIdsReferencedByCommandDefinitions);
1722

1723            /*
1724             * TODO rich client platform. simplify UI if possible boolean
1725             * showCategory = !categoryIdsByUniqueName.isEmpty();
1726             * labelCategory.setVisible(showCategory);
1727             * comboCategory.setVisible(showCategory); boolean showContext =
1728             * !contextIdsByUniqueName.isEmpty();
1729             * labelContext.setVisible(showContext);
1730             * comboContext.setVisible(showContext);
1731             * labelContextExtends.setVisible(showContext); boolean
1732             * showKeyConfiguration =
1733             * !keyConfigurationIdsByUniqueName.isEmpty();
1734             * labelKeyConfiguration.setVisible(showKeyConfiguration);
1735             * comboKeyConfiguration.setVisible(showKeyConfiguration);
1736             * labelKeyConfigurationExtends.setVisible(showKeyConfiguration);
1737             */

1738
1739            List JavaDoc categoryNames = new ArrayList JavaDoc(categoryIdsByUniqueName.keySet());
1740            Collections.sort(categoryNames, Collator.getInstance());
1741
1742            if (commandIdsByCategoryId.containsKey(null))
1743                    categoryNames.add(0, Util.translateString(RESOURCE_BUNDLE,
1744                            "other")); //$NON-NLS-1$
1745

1746            comboCategory.setItems((String JavaDoc[]) categoryNames
1747                    .toArray(new String JavaDoc[categoryNames.size()]));
1748            comboCategory.clearSelection();
1749            comboCategory.deselectAll();
1750
1751            if (commandIdsByCategoryId.containsKey(null)
1752                    || !categoryNames.isEmpty()) comboCategory.select(0);
1753
1754            List JavaDoc keyConfigurationNames = new ArrayList JavaDoc(
1755                    keyConfigurationIdsByUniqueName.keySet());
1756            Collections.sort(keyConfigurationNames, Collator.getInstance());
1757            comboKeyConfiguration.setItems((String JavaDoc[]) keyConfigurationNames
1758                    .toArray(new String JavaDoc[keyConfigurationNames.size()]));
1759            setKeyConfigurationId(activeKeyConfigurationId);
1760            update();
1761        }
1762
1763        super.setVisible(visible);
1764    }
1765
1766    private void update() {
1767        setCommandsForCategory();
1768        setContextsForCommand();
1769        String JavaDoc keyConfigurationId = getKeyConfigurationId();
1770        KeySequence keySequence = getKeySequence();
1771        String JavaDoc[] activeKeyConfigurationIds = MutableCommandManager
1772                .extend(commandManager
1773                        .getKeyConfigurationIds(keyConfigurationId));
1774        String JavaDoc[] activeLocales = MutableCommandManager.extend(MutableCommandManager.getPath(
1775                commandManager.getActiveLocale(), MutableCommandManager.SEPARATOR));
1776        String JavaDoc[] activePlatforms = MutableCommandManager.extend(MutableCommandManager
1777                .getPath(commandManager.getActivePlatform(),
1778                        MutableCommandManager.SEPARATOR));
1779        KeySequenceBindingNode.solve(tree, activeKeyConfigurationIds,
1780                activePlatforms, activeLocales);
1781        assignmentsByContextIdByKeySequence = KeySequenceBindingNode
1782                .getAssignmentsByContextIdKeySequence(tree, KeySequence
1783                        .getInstance());
1784        setAssignmentsForKeySequence();
1785        setAssignmentsForCommand();
1786        String JavaDoc commandId = getCommandId();
1787        String JavaDoc contextId = getContextId();
1788        selectAssignmentForKeySequence(contextId);
1789        selectAssignmentForCommand(contextId);
1790        updateLabelKeyConfigurationExtends();
1791        updateLabelContextExtends();
1792        labelAssignmentsForKeySequence.setEnabled(keySequence != null
1793                && !keySequence.getKeyStrokes().isEmpty());
1794        tableAssignmentsForKeySequence.setEnabled(keySequence != null
1795                && !keySequence.getKeyStrokes().isEmpty());
1796        labelAssignmentsForCommand.setEnabled(commandId != null);
1797        tableAssignmentsForCommand.setEnabled(commandId != null);
1798        boolean buttonsEnabled = commandId != null && keySequence != null
1799                && !keySequence.getKeyStrokes().isEmpty();
1800        boolean buttonAddEnabled = buttonsEnabled;
1801        boolean buttonRemoveEnabled = buttonsEnabled;
1802        boolean buttonRestoreEnabled = buttonsEnabled;
1803        // TODO better button enablement
1804
buttonAdd.setEnabled(buttonAddEnabled);
1805        buttonRemove.setEnabled(buttonRemoveEnabled);
1806        buttonRestore.setEnabled(buttonRestoreEnabled);
1807    }
1808
1809    private void updateLabelContextExtends() {
1810        String JavaDoc contextId = getContextId();
1811
1812        if (contextId != null) {
1813            IContext context = contextManager.getContext(getContextId());
1814
1815            if (context.isDefined()) {
1816                try {
1817                    String JavaDoc parentId = context.getParentId();
1818                    if (parentId != null) {
1819                        String JavaDoc name = (String JavaDoc) contextUniqueNamesById
1820                                .get(parentId);
1821
1822                        if (name != null) {
1823                            labelContextExtends.setText(MessageFormat.format(
1824                                    Util.translateString(RESOURCE_BUNDLE,
1825                                            "extends"), //$NON-NLS-1$
1826
new Object JavaDoc[] { name}));
1827
1828                            return;
1829                        }
1830                    }
1831                } catch (org.eclipse.ui.contexts.NotDefinedException eNotDefined) {
1832                    // Do nothing
1833
}
1834            }
1835        }
1836
1837        labelContextExtends.setText(Util.ZERO_LENGTH_STRING);
1838    }
1839
1840    private void updateLabelKeyConfigurationExtends() {
1841        String JavaDoc keyConfigurationId = getKeyConfigurationId();
1842
1843        if (keyConfigurationId != null) {
1844            IKeyConfiguration keyConfiguration = commandManager
1845                    .getKeyConfiguration(keyConfigurationId);
1846
1847            try {
1848                String JavaDoc name = (String JavaDoc) keyConfigurationUniqueNamesById
1849                        .get(keyConfiguration.getParentId());
1850
1851                if (name != null) {
1852                    labelKeyConfigurationExtends.setText(MessageFormat.format(
1853                            Util.translateString(RESOURCE_BUNDLE, "extends"), //$NON-NLS-1$
1854
new Object JavaDoc[] { name}));
1855                    return;
1856                }
1857            } catch (org.eclipse.ui.commands.NotDefinedException eNotDefined) {
1858                // Do nothing
1859
}
1860        }
1861
1862        labelKeyConfigurationExtends.setText(Util.ZERO_LENGTH_STRING);
1863    }
1864
1865    /*
1866     * private void selectedButtonChange() { KeySequence keySequence =
1867     * getKeySequence(); boolean validKeySequence = keySequence != null &&
1868     * validateSequence(keySequence); String scopeId = getScopeId(); boolean
1869     * validScopeId = scopeId != null && contextsDefinitionsById.get(scopeId) !=
1870     * null; String keyConfigurationId = getKeyConfigurationId(); boolean
1871     * validKeyConfigurationId = keyConfigurationId != null &&
1872     * keyConfigurationsById.get(keyConfigurationId) != null; if
1873     * (validKeySequence && validScopeId && validKeyConfigurationId) { String
1874     * commandId = null; ISelection selection =
1875     * treeViewerCommands.getSelection(); if (selection instanceof
1876     * IStructuredSelection && !selection.isEmpty()) { Object object =
1877     * ((IStructuredSelection) selection).getFirstElement(); if (object
1878     * instanceof ICommandDefinition) commandId = ((ICommandDefinition)
1879     * object).getId(); } CommandRecord commandRecord =
1880     * getSelectedCommandRecord(); if (commandRecord == null) set(tree,
1881     * keySequence, scopeId, keyConfigurationId, commandId); else { if
1882     * (!commandRecord.customSet.isEmpty()) clear(tree, keySequence, scopeId,
1883     * keyConfigurationId); else set(tree, keySequence, scopeId,
1884     * keyConfigurationId, null); } commandRecords.clear();
1885     * buildCommandRecords(tree, commandId, commandRecords);
1886     * buildTableCommand(); selectTableCommand(scopeId, keyConfigurationId,
1887     * keySequence); keySequenceRecords.clear(); buildSequenceRecords(tree,
1888     * keySequence, keySequenceRecords); buildTableKeySequence();
1889     * selectTableKeySequence(scopeId, keyConfigurationId); update(); } }
1890     * private void buildTableCommand() { tableSequencesForCommand.removeAll();
1891     * for (int i = 0; i < commandRecords.size(); i++) { CommandRecord
1892     * commandRecord = (CommandRecord) commandRecords.get(i); Set customSet =
1893     * commandRecord.customSet; Set defaultSet = commandRecord.defaultSet; int
1894     * difference = DIFFERENCE_NONE; //String commandId = null; // // boolean
1895     * commandConflict = false; String alternateCommandId = null; boolean
1896     * alternateCommandConflict = false; if (customSet.isEmpty()) { if
1897     * (defaultSet.contains(commandRecord.command)) { //commandId // // =
1898     * commandRecord.commandId; commandConflict = commandRecord.defaultConflict; } }
1899     * else { if (defaultSet.isEmpty()) { if
1900     * (customSet.contains(commandRecord.command)) { difference =
1901     * DIFFERENCE_ADD; //commandId = // // commandRecord.commandId; // //
1902     * commandConflict = commandRecord.customConflict; } } else { if
1903     * (customSet.contains(commandRecord.command)) { difference =
1904     * DIFFERENCE_CHANGE; //commandId = // // commandRecord.commandId;
1905     * commandConflict = commandRecord.customConflict; alternateCommandId =
1906     * commandRecord.defaultCommand; alternateCommandConflict =
1907     * commandRecord.defaultConflict; } else { if
1908     * (defaultSet.contains(commandRecord.command)) { difference =
1909     * DIFFERENCE_MINUS; //commandId = // // commandRecord.commandId; // //
1910     * commandConflict = commandRecord.defaultConflict; alternateCommandId =
1911     * commandRecord.customCommand; alternateCommandConflict =
1912     * commandRecord.customConflict; } } } } TableItem tableItem = new
1913     * TableItem(tableSequencesForCommand, SWT.NULL); switch (difference) { case
1914     * DIFFERENCE_ADD : tableItem.setImage(0, IMAGE_PLUS); break; case
1915     * DIFFERENCE_CHANGE : tableItem.setImage(0, IMAGE_CHANGE); break; case
1916     * DIFFERENCE_MINUS : tableItem.setImage(0, IMAGE_MINUS); break; case
1917     * DIFFERENCE_NONE : tableItem.setImage(0, IMAGE_BLANK); break; }
1918     * IContextDefinition scope = (IContextDefinition)
1919     * contextsById.get(commandRecord.scope); tableItem.setText(1, scope != null ?
1920     * scope.getName() : bracket(commandRecord.scope)); Configuration
1921     * keyConfiguration = (Configuration)
1922     * keyConfigurationsById.get(commandRecord.configuration);
1923     * tableItem.setText(2, keyConfiguration != null ?
1924     * keyConfiguration.getName() : bracket(commandRecord.configuration));
1925     * boolean conflict = commandConflict || alternateCommandConflict;
1926     * StringBuffer stringBuffer = new StringBuffer(); if
1927     * (commandRecord.sequence != null)
1928     * stringBuffer.append(KeySupport.formatSequence(commandRecord.sequence,
1929     * true)); if (commandConflict) stringBuffer.append(SPACE +
1930     * COMMAND_CONFLICT); String alternateCommandName = null; if
1931     * (alternateCommandId == null) alternateCommandName = COMMAND_UNDEFINED;
1932     * else if (alternateCommandId.length() == 0) alternateCommandName =
1933     * COMMAND_NOTHING; else { ICommandDefinition command = (ICommandDefinition)
1934     * commandsById.get(alternateCommandId); if (command != null)
1935     * alternateCommandName = command.getName(); else alternateCommandName =
1936     * bracket(alternateCommandId); } if (alternateCommandConflict)
1937     * alternateCommandName += SPACE + COMMAND_CONFLICT;
1938     * stringBuffer.append(SPACE); if (difference == DIFFERENCE_CHANGE)
1939     * stringBuffer.append(MessageFormat.format(Util.getString(resourceBundle,
1940     * "was"), new Object[] { alternateCommandName })); //$NON-NLS-1$ else if
1941     * (difference == DIFFERENCE_MINUS)
1942     * stringBuffer.append(MessageFormat.format(Util.getString(resourceBundle,
1943     * "now"), new Object[] { alternateCommandName })); //$NON-NLS-1$
1944     * tableItem.setText(3, stringBuffer.toString()); if (difference ==
1945     * DIFFERENCE_MINUS) { if (conflict) tableItem.setForeground(new
1946     * Color(getShell().getDisplay(), RGB_CONFLICT_MINUS)); else
1947     * tableItem.setForeground(new Color(getShell().getDisplay(), RGB_MINUS)); }
1948     * else if (conflict) tableItem.setForeground(new
1949     * Color(getShell().getDisplay(), RGB_CONFLICT)); } } private void
1950     * buildTableKeySequence() { tableCommandsForSequence.removeAll(); for (int
1951     * i = 0; i < keySequenceRecords.size(); i++) { KeySequenceRecord
1952     * keySequenceRecord = (KeySequenceRecord) keySequenceRecords.get(i); int
1953     * difference = DIFFERENCE_NONE; String commandId = null; boolean
1954     * commandConflict = false; String alternateCommandId = null; boolean
1955     * alternateCommandConflict = false; if
1956     * (keySequenceRecord.customSet.isEmpty()) { commandId =
1957     * keySequenceRecord.defaultCommand; commandConflict =
1958     * keySequenceRecord.defaultConflict; } else { commandId =
1959     * keySequenceRecord.customCommand; commandConflict =
1960     * keySequenceRecord.customConflict; if
1961     * (keySequenceRecord.defaultSet.isEmpty()) difference = DIFFERENCE_ADD;
1962     * else { difference = DIFFERENCE_CHANGE; alternateCommandId =
1963     * keySequenceRecord.defaultCommand; alternateCommandConflict =
1964     * keySequenceRecord.defaultConflict; } } TableItem tableItem = new
1965     * TableItem(tableCommandsForSequence, SWT.NULL); switch (difference) { case
1966     * DIFFERENCE_ADD : tableItem.setImage(0, IMAGE_PLUS); break; case
1967     * DIFFERENCE_CHANGE : tableItem.setImage(0, IMAGE_CHANGE); break; case
1968     * DIFFERENCE_MINUS : tableItem.setImage(0, IMAGE_MINUS); break; case
1969     * DIFFERENCE_NONE : tableItem.setImage(0, IMAGE_BLANK); break; }
1970     * IContextDefinition scope = (IContextDefinition)
1971     * contextsById.get(keySequenceRecord.scope); tableItem.setText(1, scope !=
1972     * null ? scope.getName() : bracket(keySequenceRecord.scope)); Configuration
1973     * keyConfiguration = (Configuration)
1974     * keyConfigurationsById.get(keySequenceRecord.configuration);
1975     * tableItem.setText(2, keyConfiguration != null ?
1976     * keyConfiguration.getName() : bracket(keySequenceRecord.configuration));
1977     * boolean conflict = commandConflict || alternateCommandConflict;
1978     * StringBuffer stringBuffer = new StringBuffer(); String commandName =
1979     * null; if (commandId == null) commandName = COMMAND_UNDEFINED; else if
1980     * (commandId.length() == 0) commandName = COMMAND_NOTHING; else {
1981     * ICommandDefinition command = (ICommandDefinition)
1982     * commandsById.get(commandId); if (command != null) commandName =
1983     * command.getName(); else commandName = bracket(commandId); }
1984     * stringBuffer.append(commandName); if (commandConflict)
1985     * stringBuffer.append(SPACE + COMMAND_CONFLICT); String
1986     * alternateCommandName = null; if (alternateCommandId == null)
1987     * alternateCommandName = COMMAND_UNDEFINED; else if
1988     * (alternateCommandId.length() == 0) alternateCommandName =
1989     * COMMAND_NOTHING; else { ICommandDefinition command = (ICommandDefinition)
1990     * commandsById.get(alternateCommandId); if (command != null)
1991     * alternateCommandName = command.getName(); else alternateCommandName =
1992     * bracket(alternateCommandId); } if (alternateCommandConflict)
1993     * alternateCommandName += SPACE + COMMAND_CONFLICT;
1994     * stringBuffer.append(SPACE); if (difference == DIFFERENCE_CHANGE)
1995     * stringBuffer.append(MessageFormat.format(Util.getString(resourceBundle,
1996     * "was"), new Object[] { alternateCommandName })); //$NON-NLS-1$
1997     * tableItem.setText(3, stringBuffer.toString()); if (difference ==
1998     * DIFFERENCE_MINUS) { if (conflict) tableItem.setForeground(new
1999     * Color(getShell().getDisplay(), RGB_CONFLICT_MINUS)); else
2000     * tableItem.setForeground(new Color(getShell().getDisplay(), RGB_MINUS)); }
2001     * else if (conflict) tableItem.setForeground(new
2002     * Color(getShell().getDisplay(), RGB_CONFLICT)); }
2003     */

2004}
2005
Popular Tags