Home » openjdk-7 » sun » swing » [javadoc | source]
abstract public class: AccumulativeRunnable [javadoc | source]

All Implemented Interfaces:

An abstract class to be used in the cases where we need {@code Runnable} to perform some actions on an appendable set of data. The set of data might be appended after the {@code Runnable} is sent for the execution. Usually such {@code Runnables} are sent to the EDT.

Usage example:

Say we want to implement JLabel.setText(String text) which sends {@code text} string to the JLabel.setTextImpl(String text) on the EDT. In the event JLabel.setText is called rapidly many times off the EDT we will get many updates on the EDT but only the last one is important. (Every next updates overrides the previous one.) We might want to implement this {@code setText} in a way that only the last update is delivered.

Here is how one can do this using {@code AccumulativeRunnable}:

AccumulativeRunnable doSetTextImpl =
new  AccumulativeRunnable() {
    - the type this {@code Runnable} accumulates
    Override: protected - void run(List<String> args) { //set to the last string being passed setTextImpl(args.get(args.size() - 1)); } } void setText(String text) { //add text and send for the execution if needed. doSetTextImpl.add(text); }

Say we want want to implement addDirtyRegion(Rectangle rect) which sends this region to the handleDirtyRegions(List regiouns) on the EDT. addDirtyRegions better be accumulated before handling on the EDT.

Here is how it can be implemented using AccumulativeRunnable:

AccumulativeRunnable doHandleDirtyRegions =
    new AccumulativeRunnable() {
Override: protected - void run(List<Rectangle> args) { handleDirtyRegions(args); } }; void addDirtyRegion(Rectangle rect) { doHandleDirtyRegions.add(rect); }

author: Igor - Kushnirskiy
since: 1.6 -
Method from sun.swing.AccumulativeRunnable Summary:
add,   run,   run,   submit
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from sun.swing.AccumulativeRunnable Detail:
 public final synchronized  void add(T args) 
    appends arguments and sends this {@cod Runnable} for the execution if needed.

    This implementation uses {@see #submit} to send this {@code Runnable} for execution.

 public final  void run() 

    This implementation calls {@code run(List args)} mehtod with the list of accumulated arguments.

 abstract protected  void run(List<T> args)
    Equivalent to {@code Runnable.run} method with the accumulated arguments to process.
 protected  void submit() 
    Sends this {@code Runnable} for the execution

    This method is to be executed only from {@code add} method.

    This implementation uses {@code SwingWorker.invokeLater}.