Skip to content

Commit 9534ce8

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 9534ce8

File tree

8 files changed

+89
-20
lines changed

8 files changed

+89
-20
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, false, false));
268270
}
269271

270272
}

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

+22-14
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,19 +124,15 @@ 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);
133134
}
134135

135-
/**
136-
*
137-
*/
138136
private void generateCommand() {
139137
if (getModel().getCommand() != null && getModel().getWbCommand() == null) {
140138
String cmdId = getModel().getCommand().getElementId();
@@ -153,10 +151,8 @@ private void generateCommand() {
153151
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
154152
}
155153
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$
154+
logger.error("Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
155+
+ parameters + " parameter(s). Model details: " + getModel());//$NON-NLS-1$
160156
return;
161157
}
162158

@@ -174,6 +170,8 @@ private void generateCommand() {
174170
} else if (radioState != null) {
175171
radioState.addListener(stateListener);
176172
}
173+
174+
parmCmd.getCommand().addCommandListener(event -> update());
177175
}
178176
}
179177

@@ -268,8 +266,7 @@ protected void updateMenuItem() {
268266
if (mnemonics != null && !mnemonics.isEmpty()) {
269267
int idx = text.indexOf(mnemonics);
270268
if (idx != -1) {
271-
text = text.substring(0, idx) + '&'
272-
+ text.substring(idx);
269+
text = text.substring(0, idx) + '&' + text.substring(idx);
273270
}
274271
}
275272
}
@@ -311,6 +308,7 @@ protected void updateToolItem() {
311308
item.setToolTipText(tooltip);
312309
item.setSelection(getModel().isSelected());
313310
item.setEnabled(getModel().isEnabled());
311+
314312
}
315313

316314
private String getToolTipText(boolean attachKeybinding) {
@@ -321,6 +319,8 @@ private String getToolTipText(boolean attachKeybinding) {
321319
parmCmd = getModel().getWbCommand();
322320
}
323321

322+
text = legacyActionLabelSupport(text, parmCmd);
323+
324324
if (parmCmd != null && text == null) {
325325
try {
326326
text = parmCmd.getName();
@@ -336,6 +336,14 @@ private String getToolTipText(boolean attachKeybinding) {
336336
return text;
337337
}
338338

339+
private String legacyActionLabelSupport(String text, ParameterizedCommand command) {
340+
341+
return java.util.Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
342+
.filter(INamedHandler.class::isInstance).map(INamedHandler.class::cast)
343+
.map(INamedHandler::getHandlerName).orElse(text);
344+
345+
}
346+
339347
@Override
340348
protected void handleWidgetDispose(Event event) {
341349
if (event.widget == widget) {
@@ -394,7 +402,7 @@ public void dispose() {
394402
@Override
395403
@SuppressWarnings("restriction")
396404
protected void handleHelpRequest() {
397-
if(helpService==null)
405+
if (helpService == null)
398406
return;
399407
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
400408
if (helpContextId != null) {

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
*******************************************************************************/
1515
package org.eclipse.jface.commands;
1616

17+
import java.util.Optional;
18+
1719
import org.eclipse.core.commands.AbstractHandler;
1820
import org.eclipse.core.commands.ExecutionEvent;
1921
import org.eclipse.core.commands.ExecutionException;
2022
import org.eclipse.core.commands.HandlerEvent;
2123
import org.eclipse.core.commands.IHandlerListener;
24+
import org.eclipse.core.commands.INamedHandler;
2225
import org.eclipse.jface.action.IAction;
26+
import org.eclipse.jface.action.LegacyActionTools;
2327
import org.eclipse.jface.util.IPropertyChangeListener;
2428
import org.eclipse.swt.widgets.Event;
2529

@@ -30,7 +34,7 @@
3034
*
3135
* @since 3.1
3236
*/
33-
public final class ActionHandler extends AbstractHandler {
37+
public final class ActionHandler extends AbstractHandler implements INamedHandler {
3438

3539
/**
3640
* The wrapped action. This value is never <code>null</code>.
@@ -168,4 +172,9 @@ public final String toString() {
168172

169173
return buffer.toString();
170174
}
175+
176+
@Override
177+
public String getHandlerName() {
178+
return Optional.of(action).map(IAction::getText).map(LegacyActionTools::removeAcceleratorText).orElse(null);
179+
}
171180
}

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)