Skip to content

Commit 7ce399f

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 04a9884 commit 7ce399f

File tree

8 files changed

+94
-13
lines changed

8 files changed

+94
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<component id="org.eclipse.core.commands" version="2">
3+
<resource path="src/org/eclipse/core/commands/IHandler.java" type="org.eclipse.core.commands.IHandler">
4+
<filter comment="Add Basic Support for Named Command Handlers #1317" id="404000815">
5+
<message_arguments>
6+
<message_argument value="org.eclipse.core.commands.IHandler"/>
7+
<message_argument value="getHandlerLabel()"/>
8+
</message_arguments>
9+
</filter>
10+
</resource>
11+
</component>

bundles/org.eclipse.core.commands/src/org/eclipse/core/commands/IHandler.java

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
13+
* Niklas Kellner - Named Handler API
1314
*******************************************************************************/
1415
package org.eclipse.core.commands;
1516

@@ -90,4 +91,18 @@ public interface IHandler {
9091
* performed.
9192
*/
9293
void removeHandlerListener(IHandlerListener handlerListener);
94+
95+
/**
96+
* Return the label for this handler. The handler can implement this method to
97+
* provide a more dynamic label according to the actual action it performend.
98+
* When returning null, callers may instead use the invoking command label or a
99+
* generic label.
100+
*
101+
* @return name of the Handler, can be null
102+
*
103+
* @since 3.11
104+
*/
105+
default String getHandlerLabel() {
106+
return null;
107+
}
93108
}

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-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import java.util.HashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Objects;
28+
import org.eclipse.core.commands.Command;
2729
import org.eclipse.core.commands.ExecutionException;
30+
import org.eclipse.core.commands.IHandler;
2831
import org.eclipse.core.commands.IStateListener;
2932
import org.eclipse.core.commands.ParameterizedCommand;
3033
import org.eclipse.core.commands.State;
@@ -54,6 +57,7 @@
5457
import org.eclipse.jface.bindings.TriggerSequence;
5558
import org.eclipse.jface.menus.IMenuStateIds;
5659
import org.eclipse.swt.SWT;
60+
import org.eclipse.swt.widgets.Display;
5761
import org.eclipse.swt.widgets.Event;
5862
import org.eclipse.swt.widgets.MenuItem;
5963
import org.eclipse.swt.widgets.ToolItem;
@@ -120,11 +124,10 @@ public void setModel(MItem item) {
120124
}
121125

122126
/**
123-
* This method seems to be necessary for calls via reflection when called
124-
* with MHandledItem parameter.
127+
* This method seems to be necessary for calls via reflection when called with
128+
* MHandledItem parameter.
125129
*
126-
* @param item
127-
* The model item
130+
* @param item The model item
128131
*/
129132
public void setModel(MHandledItem item) {
130133
setModel((MItem) item);
@@ -148,10 +151,8 @@ private void generateCommand() {
148151
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
149152
}
150153
if (parmCmd == null) {
151-
logger.error(
152-
"Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
153-
+ parameters
154-
+ " 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$
155156
return;
156157
}
157158

@@ -169,6 +170,8 @@ private void generateCommand() {
169170
} else if (radioState != null) {
170171
radioState.addListener(stateListener);
171172
}
173+
174+
parmCmd.getCommand().addCommandListener(event -> Display.getDefault().asyncExec(() -> update()));
172175
}
173176
}
174177

@@ -264,8 +267,7 @@ protected void updateMenuItem() {
264267
if (mnemonics != null && !mnemonics.isEmpty()) {
265268
int idx = text.indexOf(mnemonics);
266269
if (idx != -1) {
267-
text = text.substring(0, idx) + '&'
268-
+ text.substring(idx);
270+
text = text.substring(0, idx) + '&' + text.substring(idx);
269271
}
270272
}
271273
}
@@ -307,6 +309,7 @@ protected void updateToolItem() {
307309
item.setToolTipText(tooltip);
308310
item.setSelection(getModel().isSelected());
309311
item.setEnabled(getModel().isEnabled());
312+
310313
}
311314

312315
private String getToolTipText(boolean attachKeybinding) {
@@ -317,6 +320,8 @@ private String getToolTipText(boolean attachKeybinding) {
317320
parmCmd = getModel().getWbCommand();
318321
}
319322

323+
text = legacyActionLabelSupport(text, parmCmd);
324+
320325
if (parmCmd != null && text == null) {
321326
try {
322327
text = parmCmd.getName();
@@ -332,6 +337,12 @@ private String getToolTipText(boolean attachKeybinding) {
332337
return text;
333338
}
334339

340+
private String legacyActionLabelSupport(String text, ParameterizedCommand command) {
341+
342+
return java.util.Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
343+
.map(IHandler::getHandlerLabel).filter(Objects::nonNull).orElse(text);
344+
}
345+
335346
@Override
336347
protected void handleWidgetDispose(Event event) {
337348
if (event.widget == widget) {
@@ -389,7 +400,7 @@ public void dispose() {
389400

390401
@Override
391402
protected void handleHelpRequest() {
392-
if(helpService==null)
403+
if (helpService == null)
393404
return;
394405
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
395406
if (helpContextId != null) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
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;
2224
import org.eclipse.jface.action.IAction;
25+
import org.eclipse.jface.action.LegacyActionTools;
2326
import org.eclipse.jface.util.IPropertyChangeListener;
2427
import org.eclipse.swt.widgets.Event;
2528

@@ -168,4 +171,9 @@ public final String toString() {
168171

169172
return buffer.toString();
170173
}
174+
175+
@Override
176+
public String getHandlerLabel() {
177+
return Optional.of(action).map(IAction::getText).map(LegacyActionTools::removeAcceleratorText).orElse(null);
178+
}
171179
}

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

+13
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.IHandler;
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;
@@ -42,4 +44,15 @@ public void updateElement(UIElement element, Map parameters) {
4244
}
4345
}
4446

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

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

+5
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,9 @@ public void removeState(String stateId) {
226226
}
227227
}
228228

229+
@Override
230+
public String getHandlerLabel() {
231+
return handler.getHandlerLabel();
232+
}
233+
229234
}

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

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

1717
import java.util.Map;
18+
import java.util.Objects;
19+
import java.util.Optional;
1820
import org.eclipse.core.commands.Command;
1921
import org.eclipse.core.commands.CommandEvent;
2022
import org.eclipse.core.commands.ExecutionException;
@@ -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,20 @@ private void updateMenuItem() {
535540
}
536541
}
537542

543+
private String legacyActionLabelSupport(String text) {
544+
return Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
545+
.map(IHandler::getHandlerLabel).filter(Objects::nonNull)
546+
.orElse(text);
547+
}
548+
538549
private void updateToolItem() {
539550
ToolItem item = (ToolItem) widget;
540551

541552
String text = label;
542-
String tooltip = label;
553+
554+
text = legacyActionLabelSupport(text);
555+
556+
String tooltip = text;
543557

544558
if (text == null) {
545559
if (command != null) {
@@ -579,6 +593,8 @@ private void updateButton() {
579593
Button item = (Button) widget;
580594

581595
String text = label;
596+
text = legacyActionLabelSupport(text);
597+
582598
if (text == null) {
583599
if (command != null) {
584600
try {

0 commit comments

Comments
 (0)