Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,49 +295,58 @@ public final boolean executeCommand(final ParameterizedCommand parameterizedComm
final EHandlerService handlerService = getHandlerService();
final Command command = parameterizedCommand.getCommand();

final IEclipseContext staticContext = createContext(trigger);

final boolean commandDefined = command.isDefined();
// boolean commandEnabled;
boolean commandHandled = false;

try {
// commandEnabled = handlerService.canExecute(parameterizedCommand, staticContext);
Object obj = HandlerServiceImpl.lookUpHandler(context, command.getId());
if (obj != null) {
if (obj instanceof IHandler) {
commandHandled = ((IHandler) obj).isHandled();
} else {
commandHandled = true;
}
// commandEnabled = handlerService.canExecute(parameterizedCommand,
// staticContext);
Object obj = HandlerServiceImpl.lookUpHandler(context, command.getId());
if (obj != null) {
if (obj instanceof IHandler handler) {
commandHandled = command.isEnabled() && handler.isHandled();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the command.isEnabled() check added here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the command is not enabled, the code simply registers an execption and proceeds. Later this leads to commandHandled being set to false. Note that this needed to decide if the current handler eats the key. If the eatKey is false then the next handler in the notifyListener handles this event. e.g. CTRL + A can have multiple handler and if the first handler is not enabled and the second one should perform the command. The first one should set the commandHandled to false and not eatKey so that it would be propagated to the second handler. Since we can check this proactively before handlerService#executeHandler, we leverage this to already check if it can execute. Similar check is also performed inside Command#execute.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you for the detailed explanation!

} else {
commandHandled = true;
}
}

if (isTracingEnabled()) {
logger.trace("Command " + parameterizedCommand + ", defined: " + commandDefined + ", handled: " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ commandHandled + " in " + describe(context)); //$NON-NLS-1$
}
if (isTracingEnabled()) {
logger.trace("Command " + parameterizedCommand + ", defined: " + commandDefined + ", handled: " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ commandHandled + " in " + describe(context)); //$NON-NLS-1$
}

if (trigger != null && trigger.widget instanceof Browser) {
getDisplay()
.asyncExec(() -> handleCommandExecution(parameterizedCommand, handlerService, trigger, obj));
} else {
handleCommandExecution(parameterizedCommand, handlerService, trigger, obj);
}
return (commandDefined && commandHandled);
}

private void handleCommandExecution(final ParameterizedCommand parameterizedCommand,
final EHandlerService handlerService, final Event trigger, Object handler) {
final IEclipseContext staticContext = createContext(trigger);
try {
handlerService.executeHandler(parameterizedCommand, staticContext);
final Object commandException = staticContext.get(HandlerServiceImpl.HANDLER_EXCEPTION);
if (commandException instanceof CommandException) {
commandHandled = false;
if (commandException instanceof ExecutionException) {
if (logger != null) {
logger.error((Throwable) commandException,
"Execution exception for: " + parameterizedCommand + " in " //$NON-NLS-1$//$NON-NLS-2$
+ describe(context));
}
} else if (isTracingEnabled()) {
logger.trace((Throwable) commandException,
"Command exception for: " + parameterizedCommand + " in " //$NON-NLS-1$ //$NON-NLS-2$
logger.trace((Throwable) commandException, "Command exception for: " + parameterizedCommand + " in " //$NON-NLS-1$ //$NON-NLS-2$
+ describe(context));
if (handlerService instanceof HandlerServiceImpl serviceImpl) {
IEclipseContext serviceContext = serviceImpl.getContext();
if (serviceContext != null) {
StringBuilder sb = new StringBuilder("\n\tExecution context: "); //$NON-NLS-1$
sb.append(describe(serviceContext));
sb.append("\n\tHandler: "); //$NON-NLS-1$
sb.append(obj);
sb.append(handler);
logger.trace(sb.toString());
}
}
Expand All @@ -352,17 +361,16 @@ public final boolean executeCommand(final ParameterizedCommand parameterizedComm
}
}
}
/*
* Now that the command has executed (and had the opportunity to use the remembered
* state of the dialog), it is safe to delete that information.
*/
if (keyAssistDialog != null) {
keyAssistDialog.clearRememberedState();
}
} finally {
staticContext.dispose();
}
return (commandDefined && commandHandled);
/*
* Now that the command has executed (and had the opportunity to use the
* remembered state of the dialog), it is safe to delete that information.
*/
if (keyAssistDialog != null) {
keyAssistDialog.clearRememberedState();
}
}

private boolean isTracingEnabled() {
Expand Down
Loading