AccumulativeRunnable.java revision 0
1008N/A * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. 1008N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1008N/A * This code is free software; you can redistribute it and/or modify it 1008N/A * under the terms of the GNU General Public License version 2 only, as 1008N/A * published by the Free Software Foundation. Sun designates this 1008N/A * particular file as subject to the "Classpath" exception as provided 1008N/A * by Sun in the LICENSE file that accompanied this code. 1008N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1008N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1008N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1008N/A * version 2 for more details (a copy is included in the LICENSE file that 1008N/A * You should have received a copy of the GNU General Public License version 1008N/A * 2 along with this work; if not, write to the Free Software Foundation, 1008N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1008N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1008N/A * CA 95054 USA or visit www.sun.com if you need additional information or 1008N/A * An abstract class to be used in the cases where we need {@code Runnable} 1008N/A * to perform some actions on an appendable set of data. 1008N/A * The set of data might be appended after the {@code Runnable} is 1008N/A * sent for the execution. Usually such {@code Runnables} are sent to 1008N/A * Say we want to implement JLabel.setText(String text) which sends 1008N/A * {@code text} string to the JLabel.setTextImpl(String text) on the EDT. 1008N/A * In the event JLabel.setText is called rapidly many times off the EDT 1008N/A * we will get many updates on the EDT but only the last one is important. 1008N/A * (Every next updates overrides the previous one.) 1008N/A * We might want to implement this {@code setText} in a way that only 1008N/A * the last update is delivered. 1008N/A * Here is how one can do this using {@code AccumulativeRunnable}: 1008N/A * AccumulativeRunnable<String> doSetTextImpl = 1008N/A * new AccumulativeRunnable<String>() { 1008N/A * protected void run(List<String> args) { 1008N/A * //set to the last string being passed 1008N/A * setTextImpl(args.get(args.size() - 1)); 1008N/A * void setText(String text) { 1008N/A * //add text and send for the execution if needed. 1008N/A * Say we want want to implement addDirtyRegion(Rectangle rect) 1008N/A * which sends this region to the 1008N/A * handleDirtyRegions(List<Rect> regiouns) on the EDT. 1008N/A * addDirtyRegions better be accumulated before handling on the EDT. 1008N/A * Here is how it can be implemented using AccumulativeRunnable: 1008N/A * AccumulativeRunnable<Rectangle> doHandleDirtyRegions = 1008N/A * new AccumulativeRunnable<Rectangle>() { 1008N/A * protected void run(List<Rectangle> args) { 1008N/A * handleDirtyRegions(args); 1008N/A * void addDirtyRegion(Rectangle rect) { 1008N/A * doHandleDirtyRegions.add(rect); 1008N/A * @param <T> the type this {@code Runnable} accumulates 1008N/A * Equivalent to {@code Runnable.run} method with the 1008N/A * accumulated arguments to process. 1389N/A * @param args accumulated argumets to process. 1389N/A * This implementation calls {@code run(List<T> args)} mehtod 1389N/A * with the list of accumulated arguments. 1389N/A * appends arguments and sends this {@cod Runnable} for the 1389N/A * This implementation uses {@see #submit} to send this 1008N/A * {@code Runnable} for execution. 1389N/A * @param args the arguments to accumulate 1389N/A * Sends this {@code Runnable} for the execution 1389N/A * This method is to be executed only from {@code add} method. 1389N/A * This implementation uses {@code SwingWorker.invokeLater}. 1389N/A * Returns accumulated arguments and flashes the arguments storage. 1389N/A * @return accumulated arguments