trigger an action button passively or on certain instigator

Answered

i have a plugin that does an action through a button click event, if i want to trigger the same action through multiple instigators, is it possible?

for example if i want to trigger the action every 3 seconds or when the user writes a complete statement (a statement ending with semicolon), not only when the button is clicked, is it possible to trigger the same action if one of multiple events has occured?

if yes, how to trigger the action in both ways i mentioned above and where can i search for all the possible ways?

0
2 comments

Hi Amiri,

I assume that you implemented your action as AnAction.

You can use ActionManager.getInstance().getAction("actionId") to find an action by ID and invoke its actionPerformed() directly, or use ActionManager.tryToExecute(), especially if you want to trigger it periodically (see https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000784304-How-to-schedule-AnActionEvent-actions- for more details).

Regarding the events that trigger actions, it's up to your use case, there is no limited list of the contexts an action can be triggered.

0

Thank you!

it worked out, but this method creates a new thread everytime i press the action button which is inefficient..

so focus with me for a minute:

- I have this Action class which is pinned in EditorPopupMenu:

public class TestButton extends AnAction implements Toggleable

- I have overridden ActionPerformed and created a local field:

Runnable Task = new Runnable {// code here};

- I inserted the following statement right after initializing runnable:

EdtExecutorService.getScheduledExecutorInstance().scheduleWithFixedDelay(Task, 1, 5, TimeUnit.SECONDS);

apparently everytime i click the action button, EdtExecutorService creates a new thread, but what i want is no matter how many times i press the action button, only 1 thread executes.

- i tried to put use this method with a boolean flag that indicates that i have already pressed the button

EdtExecutorService.getScheduledExecutorInstance().shutdown();

but it does not kill the thread, it remains running.

so in a nut shell, i want to know how to kill a task that is executed using (EdtExecutorServices).

 

i also want to know other thing: my original goal was to execute an action everytime the editor gets i new Complete syntax sentance (a line of code ends with semicolon), what kind of listener should i use?

 

0

Please sign in to leave a comment.