Sunday October 14, 2007
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.
- Create a module named "DemoTaskList", with the code name base "org.netbeans.modules.demotasklist".
- Set dependencies on "File System API" and "Task List API".
- Create a new Java dource file, named DemoFileTaskScanner. Let it extend "FileTaskScanner".
- The helpful lightbulb will now prompt you to add import statements and two mandatory methods, scan and attach.
- 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"); } - 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>();
- 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 LinkedListtasks = 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) { } } - 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!
Oct 14 2007, 10:59:10 PM PDT Permalink


