Skip to content
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

Spreadsheet - Install listeners on context menu actions #6963

Open
astarno-dev opened this issue Dec 18, 2024 · 1 comment
Open

Spreadsheet - Install listeners on context menu actions #6963

astarno-dev opened this issue Dec 18, 2024 · 1 comment

Comments

@astarno-dev
Copy link

astarno-dev commented Dec 18, 2024

Describe your motivation

Currently it is possible to install listeners on several things, like values changing in cells or selection changing. However, a major missing option is to install listeners on any of the actions performed within the context menu. In particular: adding and removing rows/columns. There is also nothing mentioned about this in the documentation: https://vaadin.com/docs/latest/components/spreadsheet

image

Our current use-case is blocked because of this issue. We want users to be able to see a live overview of the modifications made to the sheet compared to an original one. Ideally, this is done by installing appropriate listeners and only doing the necessary comparisons when something changed. Currently, we cannot detect any changes in rows/columns being added/deleted, which is a huge gap in our modification detection.

I'm sure there is a multitude of other use-cases where this functionality would be useful.

Describe the solution you'd like

I would like to be able to install listeners in which I can catch actions such as users adding or removing rows/columns. Other context menu actions seems less essential, but would be nice to be able to install listeners on as well.

As a bonus, context menu actions are currently not mentioned in the documentation at all. Having some sentences about them and potential work-arounds until this functionality is implemented would be good.

Describe alternatives you've considered

We are currently experimenting with ActionHandlers. Unsure about the possibilities/outcome yet, but there would in our opinion still be a need for a more straight forward approach.

Additional context

No response

@astarno-dev
Copy link
Author

astarno-dev commented Dec 18, 2024

Current workaround:

  1. Define an interface for an ActionCallback function
@FunctionalInterface
public interface ActionCallback {
    void execute(Action action, Object sender, Object target);
}
  1. Extend the SpreadsheetDefaultActionHandler with your own ListeningSpreadsheetDefaultActionHandler
public class ListeningSpreadsheetDefaultActionHandler extends SpreadsheetDefaultActionHandler {

    private final List<ActionCallback> callbacks = new ArrayList<>();

    @Override
    public Action[] getActions(Object target, Object sender) {
        return super.getActions(target, sender);
    }

    @Override
    public void handleAction(Action action, Object sender, Object target) {
        super.handleAction(action, sender, target);

        for (ActionCallback callback : callbacks) {
            callback.execute(action, sender, target);
        }
    }

    public void addActionListener(ActionCallback fun) {
        callbacks.add(fun);
    }    
}
  1. Remove the SpreadsheetDefaultActionHandler from your spreadsheet and install your newly created ListeningSpreadsheetDefaultActionHandler instead
spreadsheet.removeDefaultActionHandler();
ListeningSpreadsheetDefaultActionHandler actionHandler = new ListeningSpreadsheetDefaultActionHandler();
actionHandler.addActionListener((action, sender, target) ->
        if (action instanceof DeleteRowAction) {
             System.out.println("Row deleted");
        } else {
             System.out.println("Action triggered with sender: " + sender + " and target: " + target)
        }       
);
requirementRulesSpreadsheet.addActionHandler(actionHandler);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants