Skip to content

Commit e6a3f72

Browse files
committed
Add Basic Support for Named Command Handlers
Fixes Regression: #1314 With this a basic support for named handlers is introduced, currently this is for compatibility between ActionHandlers and E4.
1 parent b72dc9b commit e6a3f72

File tree

8 files changed

+91
-17
lines changed

8 files changed

+91
-17
lines changed

bundles/org.eclipse.core.commands/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.core.commands
5-
Bundle-Version: 3.11.200.qualifier
5+
Bundle-Version: 3.12.0.qualifier
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.core.commands,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.eclipse.core.commands;
2+
3+
/**
4+
* @since 3.12
5+
*
6+
*/
7+
public interface INamedHandler {
8+
9+
String getHandlerName();
10+
11+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class HandlerServiceHandler extends AbstractHandlerWithState {
4444

4545
protected final String commandId;
4646
// Remove state from currentStateHandler when it goes out of scope
47-
private WeakReference<IObjectWithState> currentStateHandler = new WeakReference<IObjectWithState>(null);
47+
protected WeakReference<IObjectWithState> currentStateHandler = new WeakReference<IObjectWithState>(null);
4848

4949
public HandlerServiceHandler(String commandId) {
5050
this.commandId = commandId;
@@ -265,6 +265,8 @@ private void switchHandler(Object handler) {
265265
typed.addState(id, getState(id));
266266
}
267267
}
268+
269+
fireHandlerChanged(new HandlerEvent(this, isEnabled(), isHandled()));
268270
}
269271

270272
}

bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/HandledContributionItem.java

+26-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.HashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import org.eclipse.core.commands.Command;
2728
import org.eclipse.core.commands.ExecutionException;
29+
import org.eclipse.core.commands.INamedHandler;
2830
import org.eclipse.core.commands.IStateListener;
2931
import org.eclipse.core.commands.ParameterizedCommand;
3032
import org.eclipse.core.commands.State;
@@ -122,11 +124,10 @@ public void setModel(MItem item) {
122124
}
123125

124126
/**
125-
* This method seems to be necessary for calls via reflection when called
126-
* with MHandledItem parameter.
127+
* This method seems to be necessary for calls via reflection when called with
128+
* MHandledItem parameter.
127129
*
128-
* @param item
129-
* The model item
130+
* @param item The model item
130131
*/
131132
public void setModel(MHandledItem item) {
132133
setModel((MItem) item);
@@ -153,10 +154,8 @@ private void generateCommand() {
153154
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
154155
}
155156
if (parmCmd == null) {
156-
logger.error(
157-
"Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
158-
+ parameters
159-
+ " parameter(s). Model details: " + getModel());//$NON-NLS-1$
157+
logger.error("Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
158+
+ parameters + " parameter(s). Model details: " + getModel());//$NON-NLS-1$
160159
return;
161160
}
162161

@@ -174,6 +173,12 @@ private void generateCommand() {
174173
} else if (radioState != null) {
175174
radioState.addListener(stateListener);
176175
}
176+
177+
parmCmd.getCommand().addCommandListener(event -> {
178+
if (event.isHandledChanged()) {
179+
update();
180+
}
181+
});
177182
}
178183
}
179184

@@ -268,8 +273,7 @@ protected void updateMenuItem() {
268273
if (mnemonics != null && !mnemonics.isEmpty()) {
269274
int idx = text.indexOf(mnemonics);
270275
if (idx != -1) {
271-
text = text.substring(0, idx) + '&'
272-
+ text.substring(idx);
276+
text = text.substring(0, idx) + '&' + text.substring(idx);
273277
}
274278
}
275279
}
@@ -311,6 +315,7 @@ protected void updateToolItem() {
311315
item.setToolTipText(tooltip);
312316
item.setSelection(getModel().isSelected());
313317
item.setEnabled(getModel().isEnabled());
318+
314319
}
315320

316321
private String getToolTipText(boolean attachKeybinding) {
@@ -321,6 +326,8 @@ private String getToolTipText(boolean attachKeybinding) {
321326
parmCmd = getModel().getWbCommand();
322327
}
323328

329+
text = legacyActionLabelSupport(text, parmCmd);
330+
324331
if (parmCmd != null && text == null) {
325332
try {
326333
text = parmCmd.getName();
@@ -336,6 +343,14 @@ private String getToolTipText(boolean attachKeybinding) {
336343
return text;
337344
}
338345

346+
private String legacyActionLabelSupport(String text, ParameterizedCommand command) {
347+
348+
return java.util.Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
349+
.filter(INamedHandler.class::isInstance).map(INamedHandler.class::cast)
350+
.map(INamedHandler::getHandlerName).orElse(text);
351+
352+
}
353+
339354
@Override
340355
protected void handleWidgetDispose(Event event) {
341356
if (event.widget == widget) {
@@ -394,7 +409,7 @@ public void dispose() {
394409
@Override
395410
@SuppressWarnings("restriction")
396411
protected void handleHelpRequest() {
397-
if(helpService==null)
412+
if (helpService == null)
398413
return;
399414
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
400415
if (helpContextId != null) {

bundles/org.eclipse.jface/src/org/eclipse/jface/commands/ActionHandler.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import org.eclipse.core.commands.ExecutionException;
2020
import org.eclipse.core.commands.HandlerEvent;
2121
import org.eclipse.core.commands.IHandlerListener;
22+
import org.eclipse.core.commands.INamedHandler;
2223
import org.eclipse.jface.action.IAction;
24+
import org.eclipse.jface.action.LegacyActionTools;
2325
import org.eclipse.jface.util.IPropertyChangeListener;
2426
import org.eclipse.swt.widgets.Event;
2527

@@ -30,7 +32,7 @@
3032
*
3133
* @since 3.1
3234
*/
33-
public final class ActionHandler extends AbstractHandler {
35+
public final class ActionHandler extends AbstractHandler implements INamedHandler {
3436

3537
/**
3638
* The wrapped action. This value is never <code>null</code>.
@@ -168,4 +170,9 @@ public final String toString() {
168170

169171
return buffer.toString();
170172
}
173+
174+
@Override
175+
public String getHandlerName() {
176+
return LegacyActionTools.removeAcceleratorText(action.getText());
177+
}
171178
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchHandlerServiceHandler.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package org.eclipse.ui.internal;
1616

1717
import java.util.Map;
18+
import org.eclipse.core.commands.INamedHandler;
19+
import org.eclipse.core.commands.IObjectWithState;
1820
import org.eclipse.e4.core.commands.internal.HandlerServiceHandler;
1921
import org.eclipse.e4.core.commands.internal.HandlerServiceImpl;
2022
import org.eclipse.e4.core.contexts.IEclipseContext;
@@ -25,7 +27,7 @@
2527
* @since 3.5
2628
*
2729
*/
28-
public class WorkbenchHandlerServiceHandler extends HandlerServiceHandler implements IElementUpdater {
30+
public class WorkbenchHandlerServiceHandler extends HandlerServiceHandler implements IElementUpdater, INamedHandler {
2931

3032
/**
3133
* @param commandId
@@ -46,4 +48,15 @@ public void updateElement(UIElement element, Map parameters) {
4648
}
4749
}
4850

51+
@Override
52+
public String getHandlerName() {
53+
54+
IObjectWithState handler = currentStateHandler.get();
55+
56+
if (handler != null && handler instanceof INamedHandler namedHandler) {
57+
return namedHandler.getHandlerName();
58+
}
59+
60+
return null;
61+
}
4962
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/handlers/E4HandlerProxy.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.core.commands.IHandler;
2525
import org.eclipse.core.commands.IHandler2;
2626
import org.eclipse.core.commands.IHandlerListener;
27+
import org.eclipse.core.commands.INamedHandler;
2728
import org.eclipse.core.commands.IObjectWithState;
2829
import org.eclipse.core.commands.NotHandledException;
2930
import org.eclipse.core.commands.State;
@@ -52,7 +53,7 @@
5253
* @since 3.5
5354
*
5455
*/
55-
public class E4HandlerProxy implements IHandler2, IHandlerListener, IElementUpdater, IObjectWithState {
56+
public class E4HandlerProxy implements IHandler2, IHandlerListener, IElementUpdater, IObjectWithState, INamedHandler {
5657
public HandlerActivation activation;
5758
private final Command command;
5859
private final IHandler handler;
@@ -227,4 +228,12 @@ public void removeState(String stateId) {
227228
}
228229
}
229230

231+
@Override
232+
public String getHandlerName() {
233+
if (handler instanceof INamedHandler namedHandler) {
234+
return namedHandler.getHandlerName();
235+
}
236+
return null;
237+
}
238+
230239
}

bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/menus/CommandContributionItem.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
package org.eclipse.ui.menus;
1616

1717
import java.util.Map;
18+
import java.util.Optional;
1819
import org.eclipse.core.commands.Command;
1920
import org.eclipse.core.commands.CommandEvent;
2021
import org.eclipse.core.commands.ExecutionException;
2122
import org.eclipse.core.commands.ICommandListener;
2223
import org.eclipse.core.commands.IHandler;
24+
import org.eclipse.core.commands.INamedHandler;
2325
import org.eclipse.core.commands.NotEnabledException;
2426
import org.eclipse.core.commands.NotHandledException;
2527
import org.eclipse.core.commands.ParameterizedCommand;
@@ -494,6 +496,9 @@ private void updateMenuItem() {
494496
MenuItem item = (MenuItem) widget;
495497

496498
String text = label;
499+
500+
text = legacyActionLabelSupport(text);
501+
497502
if (text == null) {
498503
if (command != null) {
499504
try {
@@ -535,11 +540,21 @@ private void updateMenuItem() {
535540
}
536541
}
537542

543+
private String legacyActionLabelSupport(String text) {
544+
return Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
545+
.filter(INamedHandler.class::isInstance).map(INamedHandler.class::cast)
546+
.map(INamedHandler::getHandlerName)
547+
.orElse(text);
548+
}
549+
538550
private void updateToolItem() {
539551
ToolItem item = (ToolItem) widget;
540552

541553
String text = label;
542-
String tooltip = label;
554+
555+
text = legacyActionLabelSupport(text);
556+
557+
String tooltip = text;
543558

544559
if (text == null) {
545560
if (command != null) {
@@ -579,6 +594,8 @@ private void updateButton() {
579594
Button item = (Button) widget;
580595

581596
String text = label;
597+
text = legacyActionLabelSupport(text);
598+
582599
if (text == null) {
583600
if (command != null) {
584601
try {

0 commit comments

Comments
 (0)