Skip to content

Commit 4637043

Browse files
N1k145mickaelistria
authored andcommitted
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 dfad87d commit 4637043

File tree

8 files changed

+99
-17
lines changed

8 files changed

+99
-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.300.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,20 @@
1+
package org.eclipse.core.commands;
2+
3+
/**
4+
* Extends the IHandler interface to provide a way for an handler to define a
5+
* Name. A use case is the Undo/Redo command where the handler specifies which
6+
* action will be undone.
7+
*
8+
* @since 3.12
9+
*
10+
*/
11+
public interface INamedHandler extends IHandler {
12+
13+
/**
14+
* Called by the framework to allow the handler to specify a name.
15+
*
16+
* @return name of the Handler
17+
*/
18+
String getHandlerName();
19+
20+
}

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

+23-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;
@@ -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,11 +125,10 @@ 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);
@@ -150,10 +152,8 @@ private void generateCommand() {
150152
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
151153
}
152154
if (parmCmd == null) {
153-
logger.error(
154-
"Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
155-
+ parameters
156-
+ " 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$
157157
return;
158158
}
159159

@@ -171,6 +171,8 @@ private void generateCommand() {
171171
} else if (radioState != null) {
172172
radioState.addListener(stateListener);
173173
}
174+
175+
parmCmd.getCommand().addCommandListener(event -> Display.getDefault().asyncExec(() -> update()));
174176
}
175177
}
176178

@@ -265,8 +267,7 @@ protected void updateMenuItem() {
265267
if (mnemonics != null && !mnemonics.isEmpty()) {
266268
int idx = text.indexOf(mnemonics);
267269
if (idx != -1) {
268-
text = text.substring(0, idx) + '&'
269-
+ text.substring(idx);
270+
text = text.substring(0, idx) + '&' + text.substring(idx);
270271
}
271272
}
272273
}
@@ -308,6 +309,7 @@ protected void updateToolItem() {
308309
item.setToolTipText(tooltip);
309310
item.setSelection(getModel().isSelected());
310311
item.setEnabled(getModel().isEnabled());
312+
311313
}
312314

313315
private String getToolTipText(boolean attachKeybinding) {
@@ -318,6 +320,8 @@ private String getToolTipText(boolean attachKeybinding) {
318320
parmCmd = getModel().getWbCommand();
319321
}
320322

323+
text = legacyActionLabelSupport(text, parmCmd);
324+
321325
if (parmCmd != null && text == null) {
322326
try {
323327
text = parmCmd.getName();
@@ -333,6 +337,14 @@ private String getToolTipText(boolean attachKeybinding) {
333337
return text;
334338
}
335339

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+
336348
@Override
337349
protected void handleWidgetDispose(Event event) {
338350
if (event.widget == widget) {
@@ -391,7 +403,7 @@ public void dispose() {
391403
@Override
392404
@SuppressWarnings("restriction")
393405
protected void handleHelpRequest() {
394-
if(helpService==null)
406+
if (helpService == null)
395407
return;
396408
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
397409
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;
@@ -24,7 +26,7 @@
2426
/**
2527
* @since 3.5
2628
*/
27-
public class WorkbenchHandlerServiceHandler extends HandlerServiceHandler implements IElementUpdater {
29+
public class WorkbenchHandlerServiceHandler extends HandlerServiceHandler implements IElementUpdater, INamedHandler {
2830

2931
public WorkbenchHandlerServiceHandler(String commandId) {
3032
super(commandId);
@@ -42,4 +44,15 @@ public void updateElement(UIElement element, Map parameters) {
4244
}
4345
}
4446

47+
@Override
48+
public String getHandlerName() {
49+
50+
IObjectWithState handler = currentStateHandler.get();
51+
52+
if (handler != null && handler instanceof INamedHandler namedHandler) {
53+
return namedHandler.getHandlerName();
54+
}
55+
56+
return null;
57+
}
4558
}

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;
@@ -51,7 +52,7 @@
5152
/**
5253
* @since 3.5
5354
*/
54-
public class E4HandlerProxy implements IHandler2, IHandlerListener, IElementUpdater, IObjectWithState {
55+
public class E4HandlerProxy implements IHandler2, IHandlerListener, IElementUpdater, IObjectWithState, INamedHandler {
5556
public HandlerActivation activation;
5657
private final Command command;
5758
private final IHandler handler;
@@ -226,4 +227,12 @@ public void removeState(String stateId) {
226227
}
227228
}
228229

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

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)