Skip to content

Commit 9913907

Browse files
Copilotlaeubi
andcommitted
Fix dependency issue by introducing IHandlerWithExpression interface
Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com>
1 parent be26995 commit 9913907

4 files changed

Lines changed: 58 additions & 18 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Eclipse Platform and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Eclipse Platform - initial API and implementation
13+
******************************************************************************/
14+
15+
package org.eclipse.e4.core.commands;
16+
17+
import org.eclipse.e4.core.contexts.IEclipseContext;
18+
19+
/**
20+
* A marker interface for handlers that have enabledWhen expressions that need to be
21+
* evaluated before @CanExecute.
22+
*
23+
* @since 1.1
24+
* @noimplement This interface is not intended to be implemented by clients outside of the
25+
* workbench.
26+
*/
27+
public interface IHandlerWithExpression {
28+
/**
29+
* Evaluates the enabledWhen expression if present.
30+
*
31+
* @param context the eclipse context for evaluation
32+
* @return true if no expression is defined or if the expression evaluates to true
33+
*/
34+
boolean evaluateEnabledWhen(IEclipseContext context);
35+
36+
/**
37+
* Returns the actual handler object that should be used for execution.
38+
*
39+
* @return the handler object
40+
*/
41+
Object getHandler();
42+
}

bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.core.commands.State;
2626
import org.eclipse.core.expressions.IEvaluationContext;
2727
import org.eclipse.e4.core.commands.ExpressionContext;
28+
import org.eclipse.e4.core.commands.IHandlerWithExpression;
2829
import org.eclipse.e4.core.commands.internal.HandlerServiceImpl.ExecutionContexts;
2930
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
3031
import org.eclipse.e4.core.contexts.EclipseContextFactory;
@@ -67,14 +68,13 @@ public boolean isEnabled() {
6768

6869
// Check for enabledWhen expression first (takes precedence over @CanExecute)
6970
Object actualHandler = handler;
70-
if (handler instanceof org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) {
71-
org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper wrapper =
72-
(org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) handler;
73-
if (!wrapper.evaluateEnabledWhen(executionContext)) {
71+
if (handler instanceof IHandlerWithExpression) {
72+
IHandlerWithExpression handlerWithExpr = (IHandlerWithExpression) handler;
73+
if (!handlerWithExpr.evaluateEnabledWhen(executionContext)) {
7474
setBaseEnabled(false);
7575
return super.isEnabled();
7676
}
77-
actualHandler = wrapper.getHandler();
77+
actualHandler = handlerWithExpr.getHandler();
7878
}
7979

8080
IEclipseContext staticContext = contexts.staticContext; // getStaticContext(contexts);
@@ -99,8 +99,8 @@ public void setEnabled(Object evaluationContext) {
9999

100100
// Unwrap handler if needed
101101
Object actualHandler = handler;
102-
if (handler instanceof org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) {
103-
actualHandler = ((org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) handler).getHandler();
102+
if (handler instanceof IHandlerWithExpression) {
103+
actualHandler = ((IHandlerWithExpression) handler).getHandler();
104104
}
105105

106106
IEclipseContext staticContext = getStaticContext(executionContext);
@@ -155,8 +155,8 @@ public boolean isHandled() {
155155

156156
// Unwrap handler if needed
157157
Object actualHandler = handler;
158-
if (handler instanceof org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) {
159-
actualHandler = ((org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) handler).getHandler();
158+
if (handler instanceof IHandlerWithExpression) {
159+
actualHandler = ((IHandlerWithExpression) handler).getHandler();
160160
}
161161

162162
if (actualHandler instanceof IHandler) {
@@ -184,8 +184,8 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
184184

185185
// Unwrap handler if needed
186186
Object actualHandler = handler;
187-
if (handler instanceof org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) {
188-
actualHandler = ((org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper) handler).getHandler();
187+
if (handler instanceof IHandlerWithExpression) {
188+
actualHandler = ((IHandlerWithExpression) handler).getHandler();
189189
}
190190

191191
IEclipseContext staticContext = getStaticContext(executionContext);

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/addons/HandlerEnabledWhenWrapper.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.core.expressions.Expression;
1919
import org.eclipse.core.expressions.ReferenceExpression;
2020
import org.eclipse.e4.core.commands.ExpressionContext;
21+
import org.eclipse.e4.core.commands.IHandlerWithExpression;
2122
import org.eclipse.e4.core.contexts.IEclipseContext;
2223
import org.eclipse.e4.ui.model.application.commands.MHandler;
2324
import org.eclipse.e4.ui.model.application.ui.MCoreExpression;
@@ -29,7 +30,7 @@
2930
*
3031
* @since 1.4
3132
*/
32-
public class HandlerEnabledWhenWrapper {
33+
public class HandlerEnabledWhenWrapper implements IHandlerWithExpression {
3334
private final Object handler;
3435
private final MHandler handlerModel;
3536

@@ -38,6 +39,7 @@ public HandlerEnabledWhenWrapper(Object handler, MHandler handlerModel) {
3839
this.handlerModel = handlerModel;
3940
}
4041

42+
@Override
4143
public Object getHandler() {
4244
return handler;
4345
}
@@ -46,12 +48,7 @@ public MHandler getHandlerModel() {
4648
return handlerModel;
4749
}
4850

49-
/**
50-
* Evaluates the enabledWhen expression if present.
51-
*
52-
* @param context the eclipse context for evaluation
53-
* @return true if no expression is defined or if the expression evaluates to true
54-
*/
51+
@Override
5552
public boolean evaluateEnabledWhen(IEclipseContext context) {
5653
MExpression enabledWhen = handlerModel.getEnabledWhen();
5754
if (enabledWhen == null) {

tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/HandlerEnabledWhenTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void testEnabledWhenTrue() throws Exception {
135135
MHandler handlerModel = createHandlerWithExpression(handler, new TrueExpression());
136136

137137
// Use the wrapper created by HandlerProcessingAddon simulation
138+
// The wrapper implements IHandlerWithExpression interface
138139
org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper wrapper =
139140
new org.eclipse.e4.ui.internal.workbench.addons.HandlerEnabledWhenWrapper(handler, handlerModel);
140141
service.activateHandler(TEST_COMMAND_ID, wrapper);

0 commit comments

Comments
 (0)