How to enable/disable action in runtime

Hi all,

I'm trying to add an action to the toolbar and popup menu. The action starts a task running in backround thread. The action button should be disabled when task is running; and then it should be enabled when task is finished.

Currently, my action overrides the AnAction.update(...) method:

public class MyAction extends AnAction {

   public void update (AnActionEvent e) {
       e.getPresentation().setEnabled( ! MyTaskManager.isTaskRunning());
   }

   public void actionPerformed(...) {
       MyTaskManager.startTaskInBackground();
   }

}


This approach works for most of the time; the action icon on toolbar is enabled/disabled eventually. However, the icon state is NOT changed immediately when task is finished; it is changed later when IDEA processes some other events. For example, if I start my action, and then be idle (do not move the mouse, do not type any text, etc.) - the toolbar icon stays disabled for minutes.

Obviously, there is a missed piece: when task is finished, my task manager should notify idea UI, and it should trigger MyAction.update() in time.

The question is: how to notify idea UI?

3 comments

Hi Sergey,

The ide updates action button states when its parent component(s) receive an event (mouse/key/whatsoever).

Alternatively you can make your action class implement com.intellij.openapi.actionSystem.AnAction.TransparentUpdate - it will make it visible to the scheduled updater.

Denis

0

Please sign in to leave a comment.