IntelliJ Plugin Development- TASK

I have just developed a plugin for intelliJ. IntelliJ idea is a stable and rich environment. It was joy to find all my client favorites libraries in the environment, such as MigLayout ,  Google Guava (with it’s event bus) , JGoodies and more.

Idea got a wiki site which describes how to set up a development environment. The project is open source, and the code can be retrieved from GIT repository.

 

In this blog I will describe the Task class of Intellij IDEA Task components. We are all familiar with SwingWorker, Task class is following this principle.

 

Task class enable plugin developer to execute tasks in thread other than the EDT, and bring back feedback to the user. A task can be modal or execute in the background. IntelliJ user are familiar with tasks since the IDE uses them widely.

 

The Task run function is executed outside of the EDT, the onSuccess and onCancel are executed in The EDT. Below there is a sample code of Modal Task which can be canceled:

public class ModalTaskImpl extends Task.Modal {
    public ModalTaskImpl(@Nullable Project project) {
        super(project, "Modal Task", true);
    }
 
    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
       for (int i = 11 ; i < 10000; i++){
           progressIndicator.setText("Modal Counter = "+i);
           progressIndicator.setFraction(i/10000);
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           progressIndicator.checkCanceled();
       }
    }
 
    @Override
    public void onSuccess() {
        super.onSuccess();
        Messages.showMessageDialog(getProject(),"Success","Success",Messages.getInformationIcon());
    }
 
    @Override
    public void onCancel() {
        super.onCancel();
        Messages.showMessageDialog(getProject(),"Cancel","Nodal Canceled",Messages.getQuestionIcon());
   }
}

Below there is a sample code of Background Task which cannot be canceled.

public class BackgroundTaskImpl extends Task.Backgroundable {
 
    public BackgroundTaskImpl(@Nullable Project project) {
        super(project, "Background Task",false);
    }
 
 
    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
        for (int i = 11 ; i < 10000; i++){
            progressIndicator.setText("Modal Counter = "+i);
            progressIndicator.setFraction(i/10000);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    @Override
    public void onSuccess() {
        super.onSuccess();
        Messages.showMessageDialog(getProject(), "Success", "Success", Messages.getInformationIcon());
    }
 
}

 

To download the sample plugin click here.



 

Developer