Reporting errors

A custom ErrorSource implementation would need to send the appropriate EditBus message when an error arrives, and then provide implementations of various "getter" methods that the ErrorList plugin would later call to obtain the error. Using DefaultErrorSource makes the task much easier. In fact, all that is involved is calling a single method on the appropriate DefaultErrorSource instance:

public void addError(DefaultErrorSource.DefaultError  error);

The constructor of the DefaultErrorSource.DefaultError class is as follows:

public DefaultError(int  type,
 String  path,
 int  line,
 int  start,
 int  end,
 String  message);

This method is fully thread safe; if your plugin generates errors from another thread, it can call this method without having to use any of the usual SwingUtilities.invokeLater() tricks required when calling non-thread safe methods.

Here are two examples of addError() calls. The first adds a warning, covering the entire line, the second adds an error that only applies to a subset of the specified line:

errorSource.addError(new ErrorSource.DefaultError(
    ErrorSource.WARNING,path,line,0,0,
    "Are you sure this is what you intended?"));
errorSource.addError(new ErrorSource.DefaultError(
    ErrorSource.WARNING,
    MiscUtilities.constructPath(directory,"input.src"),
    line,0,5,"First five characters of line are bad"));

Multiple-line messages can be created by calling the following method on the DefaultError instance before it is added to the error source (the last part is important, otherwise the extra messages will not be shown):

public void addExtraMessage(String extra);

That's basically all there is to it. There is one more method in the DefaultErrorSource class that needs to be mentioned:

public void clear(void);

This method removes all errors from the error source. It is typically called before each invocation of the operation that generates errors; for example, the Console plugin clears its error source before each new command is run.

public void removeFileErrors(void);

For more selective error removal, this method can be used to remove errors associated with one file only.