* @return The integer value representing the comparison. The
* comparison is based on the current sort order.
* @since 3.1
*/
public final int compare(final Object object1, final Object object2) {
final Binding binding1 = (Binding) object1;
final Binding binding2 = (Binding) object2;
/*
* Get the category name, command name, formatted key sequence
* and context name for the first binding.
*/
final Command command1 = binding1.getParameterizedCommand()
.getCommand();
String categoryName1 = Util.ZERO_LENGTH_STRING;
String commandName1 = Util.ZERO_LENGTH_STRING;
try {
commandName1 = command1.getName();
categoryName1 = command1.getCategory().getName();
} catch (final NotDefinedException e) {
// Just use the zero-length string.
}
final String triggerSequence1 = binding1.getTriggerSequence()
.format();
final String contextId1 = binding1.getContextId();
String contextName1 = Util.ZERO_LENGTH_STRING;
if (contextId1 != null) {
final Context context = contextService
.getContext(contextId1);
try {
contextName1 = context.getName();
} catch (final org.eclipse.core.commands.common.NotDefinedException e) {
// Just use the zero-length string.
}
}
/*
* Get the category name, command name, formatted key sequence
* and context name for the first binding.
*/
final Command command2 = binding2.getParameterizedCommand()
.getCommand();
String categoryName2 = Util.ZERO_LENGTH_STRING;
String commandName2 = Util.ZERO_LENGTH_STRING;
try {
commandName2 = command2.getName();
categoryName2 = command2.getCategory().getName();
} catch (final org.eclipse.core.commands.common.NotDefinedException e) {
// Just use the zero-length string.
}
final String keySequence2 = binding2.getTriggerSequence()
.format();
final String contextId2 = binding2.getContextId();
String contextName2 = Util.ZERO_LENGTH_STRING;
if (contextId2 != null) {
final Context context = contextService
.getContext(contextId2);
try {
contextName2 = context.getName();
} catch (final org.eclipse.core.commands.common.NotDefinedException e) {
// Just use the zero-length string.
}
}
// Compare the items in the current sort order.
int compare = 0;
for (int i = 0; i < sortOrder.length; i++) {
switch (sortOrder[i]) {
case VIEW_CATEGORY_COLUMN_INDEX:
compare = Util.compare(categoryName1, categoryName2);
if (compare != 0) {
return compare;
}
break;
case VIEW_COMMAND_COLUMN_INDEX:
compare = Util.compare(commandName1, commandName2);
if (compare != 0) {
return compare;
}
break;
case VIEW_KEY_SEQUENCE_COLUMN_INDEX:
compare = Util.compare(triggerSequence1, keySequence2);
if (compare != 0) {
return compare;
}
break;
case VIEW_CONTEXT_COLUMN_INDEX:
compare = Util.compare(contextName1, contextName2);
if (compare != 0) {
return compare;
}
break;
default:
throw new Error(
"Programmer error: added another sort column without modifying the comparator."); //$NON-NLS-1$
}
}
return compare;
}
/**
* @see Object#equals(java.lang.Object)
*/
public final boolean equals(final Object object) {
return super.equals(object);
}
});
// Add a table item for each item in the list.
final Iterator keyBindingItr = bindings.iterator();
while (keyBindingItr.hasNext()) {
final Binding binding = (Binding) keyBindingItr.next();
// Get the command and category name.
final ParameterizedCommand command = binding
.getParameterizedCommand();
String commandName = Util.ZERO_LENGTH_STRING;
String categoryName = Util.ZERO_LENGTH_STRING;
try {
commandName = command.getName();
categoryName = command.getCommand().getCategory().getName();
} catch (final org.eclipse.core.commands.common.NotDefinedException e) {
// Just use the zero-length string.
}
// Ignore items with a meaningless command name.
if ((commandName == null) || (commandName.length() == 0)) {
continue;
}
// Get the context name.
final String contextId = binding.getContextId();
String contextName = Util.ZERO_LENGTH_STRING;
if (contextId != null) {
final Context context = contextService.getContext(contextId);
try {
contextName = context.getName();
} catch (final org.eclipse.core.commands.common.NotDefinedException e) {
// Just use the zero-length string.
}
}
// Create the table item.
final TableItem item = new TableItem(tableBindings, SWT.NONE);
item.setText(VIEW_CATEGORY_COLUMN_INDEX, categoryName);
item.setText(VIEW_COMMAND_COLUMN_INDEX, commandName);
item.setText(VIEW_KEY_SEQUENCE_COLUMN_INDEX, binding
.getTriggerSequence().format());
item.setText(VIEW_CONTEXT_COLUMN_INDEX, contextName);
item.setData(BINDING_KEY, binding);
}