Skip to content

Add Basic Support for Named Command Handlers #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bundles/org.eclipse.core.commands/.settings/.api_filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.core.commands" version="2">
<resource path="src/org/eclipse/core/commands/IHandler.java" type="org.eclipse.core.commands.IHandler">
<filter comment="Add Basic Support for Named Command Handlers #1317" id="404000815">
<message_arguments>
<message_argument value="org.eclipse.core.commands.IHandler"/>
<message_argument value="getHandlerLabel()"/>
</message_arguments>
</filter>
</resource>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
* Niklas Kellner - Named Handler API
*******************************************************************************/
package org.eclipse.core.commands;

Expand Down Expand Up @@ -90,4 +91,18 @@ public interface IHandler {
* performed.
*/
void removeHandlerListener(IHandlerListener handlerListener);

/**
* Return the label for this handler. The handler can implement this method to
* provide a more dynamic label according to the actual action that is
* performed. When returning null, callers may instead use the invoking command
* label or a generic label.
*
* @return name of the Handler, can be null
*
* @since 3.11
*/
default String getHandlerLabel() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class HandlerServiceHandler extends AbstractHandlerWithState {

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

public HandlerServiceHandler(String commandId) {
this.commandId = commandId;
Expand Down Expand Up @@ -265,6 +265,8 @@ private void switchHandler(Object handler) {
typed.addState(id, getState(id));
}
}

fireHandlerChanged(new HandlerEvent(this, false, false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IStateListener;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.State;
Expand Down Expand Up @@ -54,6 +57,7 @@
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.menus.IMenuStateIds;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolItem;
Expand Down Expand Up @@ -120,11 +124,10 @@ public void setModel(MItem item) {
}

/**
* This method seems to be necessary for calls via reflection when called
* with MHandledItem parameter.
* This method seems to be necessary for calls via reflection when called with
* MHandledItem parameter.
*
* @param item
* The model item
* @param item The model item
*/
public void setModel(MHandledItem item) {
setModel((MItem) item);
Expand All @@ -148,10 +151,8 @@ private void generateCommand() {
WorkbenchSWTActivator.trace(Policy.DEBUG_MENUS_FLAG, "command: " + parmCmd, null); //$NON-NLS-1$
}
if (parmCmd == null) {
logger.error(
"Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ parameters
+ " parameter(s). Model details: " + getModel());//$NON-NLS-1$
logger.error("Unable to generate the parameterized " + "command with the id \"" + cmdId + "\" with the " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ parameters + " parameter(s). Model details: " + getModel());//$NON-NLS-1$
return;
}

Expand All @@ -169,6 +170,8 @@ private void generateCommand() {
} else if (radioState != null) {
radioState.addListener(stateListener);
}

parmCmd.getCommand().addCommandListener(event -> Display.getDefault().asyncExec(() -> update()));
}
}

Expand Down Expand Up @@ -264,8 +267,7 @@ protected void updateMenuItem() {
if (mnemonics != null && !mnemonics.isEmpty()) {
int idx = text.indexOf(mnemonics);
if (idx != -1) {
text = text.substring(0, idx) + '&'
+ text.substring(idx);
text = text.substring(0, idx) + '&' + text.substring(idx);
}
}
}
Expand Down Expand Up @@ -307,6 +309,7 @@ protected void updateToolItem() {
item.setToolTipText(tooltip);
item.setSelection(getModel().isSelected());
item.setEnabled(getModel().isEnabled());

}

private String getToolTipText(boolean attachKeybinding) {
Expand All @@ -317,6 +320,8 @@ private String getToolTipText(boolean attachKeybinding) {
parmCmd = getModel().getWbCommand();
}

text = legacyActionLabelSupport(text, parmCmd);

if (parmCmd != null && text == null) {
try {
text = parmCmd.getName();
Expand All @@ -332,6 +337,12 @@ private String getToolTipText(boolean attachKeybinding) {
return text;
}

private String legacyActionLabelSupport(String text, ParameterizedCommand command) {

return java.util.Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
.map(IHandler::getHandlerLabel).filter(Objects::nonNull).orElse(text);
}

@Override
protected void handleWidgetDispose(Event event) {
if (event.widget == widget) {
Expand Down Expand Up @@ -389,7 +400,7 @@ public void dispose() {

@Override
protected void handleHelpRequest() {
if(helpService==null)
if (helpService == null)
return;
String helpContextId = getModel().getPersistedState().get(EHelpService.HELP_CONTEXT_ID);
if (helpContextId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
*******************************************************************************/
package org.eclipse.jface.commands;

import java.util.Optional;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.LegacyActionTools;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.widgets.Event;

Expand Down Expand Up @@ -168,4 +171,9 @@ public final String toString() {

return buffer.toString();
}

@Override
public String getHandlerLabel() {
return Optional.of(action).map(IAction::getText).map(LegacyActionTools::removeAcceleratorText).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package org.eclipse.ui.internal;

import java.util.Map;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IObjectWithState;
import org.eclipse.e4.core.commands.internal.HandlerServiceHandler;
import org.eclipse.e4.core.commands.internal.HandlerServiceImpl;
import org.eclipse.e4.core.contexts.IEclipseContext;
Expand Down Expand Up @@ -42,4 +44,15 @@ public void updateElement(UIElement element, Map parameters) {
}
}

@Override
public String getHandlerLabel() {

IObjectWithState handler = currentStateHandler.get();

if (handler instanceof IHandler namedHandler) {
return namedHandler.getHandlerLabel();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,9 @@ public void removeState(String stateId) {
}
}

@Override
public String getHandlerLabel() {
return handler.getHandlerLabel();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package org.eclipse.ui.menus;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.CommandEvent;
import org.eclipse.core.commands.ExecutionException;
Expand Down Expand Up @@ -494,6 +496,9 @@ private void updateMenuItem() {
MenuItem item = (MenuItem) widget;

String text = label;

text = legacyActionLabelSupport(text);

if (text == null) {
if (command != null) {
try {
Expand Down Expand Up @@ -535,11 +540,20 @@ private void updateMenuItem() {
}
}

private String legacyActionLabelSupport(String text) {
return Optional.of(command).map(ParameterizedCommand::getCommand).map(Command::getHandler)
.map(IHandler::getHandlerLabel).filter(Objects::nonNull)
.orElse(text);
}

private void updateToolItem() {
ToolItem item = (ToolItem) widget;

String text = label;
String tooltip = label;

text = legacyActionLabelSupport(text);

String tooltip = text;

if (text == null) {
if (command != null) {
Expand Down Expand Up @@ -579,6 +593,8 @@ private void updateButton() {
Button item = (Button) widget;

String text = label;
text = legacyActionLabelSupport(text);

if (text == null) {
if (command != null) {
try {
Expand Down