Skip to content

Commit e5ee308

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 e5ee308

File tree

8 files changed

+90
-20
lines changed

8 files changed

+90
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
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,
Lines changed: 11 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 23 additions & 14 deletions
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;
@@ -54,6 +56,7 @@
5456
import org.eclipse.jface.bindings.TriggerSequence;
5557
import org.eclipse.jface.menus.IMenuStateIds;
5658
import org.eclipse.swt.SWT;
59+
import org.eclipse.swt.widgets.Display;
5760
import org.eclipse.swt.widgets.Event;
5861
import org.eclipse.swt.widgets.MenuItem;
5962
import org.eclipse.swt.widgets.ToolItem;
@@ -122,19 +125,15 @@ public void setModel(MItem item) {
122125
}
123126

124127
/**
125-
* This method seems to be necessary for calls via reflection when called
126-
* with MHandledItem parameter.
128+
* This method seems to be necessary for calls via reflection when called with
129+
* MHandledItem parameter.
127130
*
128-
* @param item
129-
* The model item
131+
* @param item The model item
130132
*/
131133
public void setModel(MHandledItem item) {
132134
setModel((MItem) item);
133135
}
134136

135-
/**
136-
*
137-
*/
138137
private void generateCommand() {
139138
if (getModel().getCommand() != null && getModel().getWbCommand() == null) {
140139
String cmdId = getModel().getCommand().getElementId();
@@ -153,10 +152,8 @@ private void generateCommand() {
153152
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
154153
}
155154
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$
155+
logger.error("Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
156+
+ parameters + " parameter(s). Model details: " + getModel());//$NON-NLS-1$
160157
return;
161158
}
162159

@@ -174,6 +171,8 @@ private void generateCommand() {
174171
} else if (radioState != null) {
175172
radioState.addListener(stateListener);
176173
}
174+
175+
parmCmd.getCommand().addCommandListener(event -> Display.getDefault().asyncExec(() -> update()));
177176
}
178177
}
179178

@@ -268,8 +267,7 @@ protected void updateMenuItem() {
268267
if (mnemonics != null && !mnemonics.isEmpty()) {
269268
int idx = text.indexOf(mnemonics);
270269
if (idx != -1) {
271-
text = text.substring(0, idx) + '&'
272-
+ text.substring(idx);
270+
text = text.substring(0, idx) + '&' + text.substring(idx);
273271
}
274272
}
275273
}
@@ -311,6 +309,7 @@ protected void updateToolItem() {
311309
item.setToolTipText(tooltip);
312310
item.setSelection(getModel().isSelected());
313311
item.setEnabled(getModel().isEnabled());
312+
314313
}
315314

316315
private String getToolTipText(boolean attachKeybinding) {
@@ -321,6 +320,8 @@ private String getToolTipText(boolean attachKeybinding) {
321320
parmCmd = getModel().getWbCommand();
322321
}
323322

323+
text = legacyActionLabelSupport(text, parmCmd);
324+
324325
if (parmCmd != null && text == null) {
325326
try {
326327
text = parmCmd.getName();
@@ -336,6 +337,14 @@ private String getToolTipText(boolean attachKeybinding) {
336337
return text;
337338
}
338339

340+
private String legacyActionLabelSupport(String text, ParameterizedCommand command) {
341+
342+
return java.util.Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
343+
.filter(INamedHandler.class::isInstance).map(INamedHandler.class::cast)
344+
.map(INamedHandler::getHandlerName).orElse(text);
345+
346+
}
347+
339348
@Override
340349
protected void handleWidgetDispose(Event event) {
341350
if (event.widget == widget) {
@@ -394,7 +403,7 @@ public void dispose() {
394403
@Override
395404
@SuppressWarnings("restriction")
396405
protected void handleHelpRequest() {
397-
if(helpService==null)
406+
if (helpService == null)
398407
return;
399408
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
400409
if (helpContextId != null) {

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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 18 additions & 1 deletion
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)