Simple Implementation of Task List API

The simplest implementation of the Task List API is even semi-useful. It finds all the files in a project, either the main project or all opened projects, and puts them in the Task List, as shown here:

You then get all the Task List features for free, such as that when you click on a row in the Task List, the related file opens in the Source Editor. Other features, such as sorting, and other organizing functionality, is also available. All for adding a handful of lines of code. Read on for instructions on how to do this yourself.

  1. Create a module named "DemoTaskList", with the code name base "org.netbeans.modules.demotasklist".

  2. Set dependencies on "File System API" and "Task List API".

  3. Create a new Java dource file, named DemoFileTaskScanner. Let it extend "FileTaskScanner".

  4. The helpful lightbulb will now prompt you to add import statements and two mandatory methods, scan and attach.

  5. Create a constructor and a create method with the following content:
    DemoFileTaskScanner(String displayName, String description) {
        super(displayName, description, "Advanced");
    }
    
    public static DemoFileTaskScanner create() {
        return new DemoFileTaskScanner("demotask", "demohint");
    }

  6. Now we will create a list of tasks that we will return in the scan method. Begin by declaring and creating a java.util.LinkedList:
    private LinkedList<Task> tasks = new LinkedList<Task>();

  7. Now we will use it in the scan method:
    @Override
    public List<? extends Task> scan(FileObject resource) {
        if (!resource.isFolder()) {
            tasks.add(Task.create(resource, "", resource.getNameExt(), 1));
        }
        return tasks;
    }

    Take note of the line in bold above. Here you're using org.netbeans.spi.tasklist.Task. The first argument is the file you're dealing with, the second the name of the group (which in this simple case is undefined), the third the description that is shown in the Task List's Description column, and finally the line number. Normally you would calculate the line number somehow, but since we're just creating a very simple implementation, we'll return 1, because every file must have at least a first line.

    You now have a class with exactly this content:

    public class DemoFileTaskScanner extends FileTaskScanner {
    
        private LinkedList tasks = new LinkedList<Task>();
    
        DemoFileTaskScanner(String displayName, String description) {
            super(displayName, description, "Advanced");
        }
    
        public static DemoFileTaskScanner create() {
            return new DemoFileTaskScanner("demotask", "demohint");
        }
    
        @Override
        public List<? extends Task> scan(FileObject resource) {
            if (!resource.isFolder()) {
                tasks.add(Task.create(resource, "", resource.getNameExt(), 1));
            }
            return tasks;
        }
    
        @Override
        public void attach(Callback arg0) {
        }
        
    }

  8. Register the class in the layer.xml file in the TaskList/Scanners folder:
    <folder name="TaskList">
        <folder name="Scanners">
            <file name="DemoScanner.instance">
                <attr name="instanceOf" 
                    stringvalue="org.netbeans.spi.tasklist.FileTaskScanner"/>
                <attr name="instanceCreate" 
                    methodvalue="org.netbeans.modules.demotasklist.DemoFileTaskScanner.create"/>
            </file>
        </folder>
    </folder>

Install and enjoy your new plugin! You will find that you can use the Task List to search for all files in all projects, either the main project or the open projects, or all of them together.

In other news. Check out Carsten Zerbst's cool new STEP Editor!

Comments:

Jaroslav Tulach and Stan Aubrecht have told me that the above is not completely correct. One needs to either clear the "tasks" in FileTaskScanner.finish() or create a new instance of the list in FileTaskScanner.prepare(). Otherwise, as is currently the case, new "tasks" keep on being created, without them being cleaned up.

Posted by Geertjan on October 22, 2007 at 01:17 AM PDT #

Hi geertjan,

This is followup from your latest post about action items list. How can we use PushTaskScanner instead of FileTaskScanner. I've managed to create tasks using
setScope of PushTaskScanner but it only shows tasks when user change scope on the task list window.
The needed use case: I've implemented new language for NB. The compiler for this language put compile result on the output window. I'm parsing this window and when there is compilation error i put error annotation on the given text file and i would like to register error action item (task). What is a model use of the PushTaskScanner in that case. The nbforums and javadoc does not provide all of the answers. More popular is FileTaskScanner with a more straight api.

thanks !

Posted by slawekmikula on October 07, 2013 at 10:27 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today