0N/A/*
3909N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.swing;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.lang.reflect.Array;
0N/Aimport javax.swing.SwingUtilities;
0N/A
0N/A/**
0N/A * An abstract class to be used in the cases where we need {@code Runnable}
0N/A * to perform some actions on an appendable set of data.
0N/A * The set of data might be appended after the {@code Runnable} is
0N/A * sent for the execution. Usually such {@code Runnables} are sent to
0N/A * the EDT.
0N/A *
0N/A * <p>
0N/A * Usage example:
0N/A *
0N/A * <p>
0N/A * Say we want to implement JLabel.setText(String text) which sends
0N/A * {@code text} string to the JLabel.setTextImpl(String text) on the EDT.
0N/A * In the event JLabel.setText is called rapidly many times off the EDT
0N/A * we will get many updates on the EDT but only the last one is important.
0N/A * (Every next updates overrides the previous one.)
0N/A * We might want to implement this {@code setText} in a way that only
0N/A * the last update is delivered.
0N/A * <p>
0N/A * Here is how one can do this using {@code AccumulativeRunnable}:
0N/A * <pre>
0N/A * AccumulativeRunnable<String> doSetTextImpl =
0N/A * new AccumulativeRunnable<String>() {
0N/A * @Override
0N/A * protected void run(List&lt;String&gt; args) {
0N/A * //set to the last string being passed
0N/A * setTextImpl(args.get(args.size() - 1));
0N/A * }
0N/A * }
0N/A * void setText(String text) {
0N/A * //add text and send for the execution if needed.
0N/A * doSetTextImpl.add(text);
0N/A * }
0N/A * </pre>
0N/A *
0N/A * <p>
0N/A * Say we want want to implement addDirtyRegion(Rectangle rect)
0N/A * which sends this region to the
0N/A * handleDirtyRegions(List<Rect> regiouns) on the EDT.
0N/A * addDirtyRegions better be accumulated before handling on the EDT.
0N/A *
0N/A * <p>
0N/A * Here is how it can be implemented using AccumulativeRunnable:
0N/A * <pre>
0N/A * AccumulativeRunnable<Rectangle> doHandleDirtyRegions =
0N/A * new AccumulativeRunnable<Rectangle>() {
0N/A * @Override
0N/A * protected void run(List&lt;Rectangle&gt; args) {
0N/A * handleDirtyRegions(args);
0N/A * }
0N/A * };
0N/A * void addDirtyRegion(Rectangle rect) {
0N/A * doHandleDirtyRegions.add(rect);
0N/A * }
0N/A * </pre>
0N/A *
0N/A * @author Igor Kushnirskiy
0N/A *
0N/A * @param <T> the type this {@code Runnable} accumulates
0N/A *
0N/A * @since 1.6
0N/A */
0N/Apublic abstract class AccumulativeRunnable<T> implements Runnable {
0N/A private List<T> arguments = null;
0N/A
0N/A /**
0N/A * Equivalent to {@code Runnable.run} method with the
0N/A * accumulated arguments to process.
0N/A *
0N/A * @param args accumulated argumets to process.
0N/A */
0N/A protected abstract void run(List<T> args);
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * <p>
0N/A * This implementation calls {@code run(List<T> args)} mehtod
0N/A * with the list of accumulated arguments.
0N/A */
0N/A public final void run() {
0N/A run(flush());
0N/A }
0N/A
0N/A /**
0N/A * appends arguments and sends this {@cod Runnable} for the
0N/A * execution if needed.
0N/A * <p>
0N/A * This implementation uses {@see #submit} to send this
0N/A * {@code Runnable} for execution.
0N/A * @param args the arguments to accumulate
0N/A */
3464N/A @SafeVarargs
0N/A public final synchronized void add(T... args) {
0N/A boolean isSubmitted = true;
0N/A if (arguments == null) {
0N/A isSubmitted = false;
0N/A arguments = new ArrayList<T>();
0N/A }
0N/A Collections.addAll(arguments, args);
0N/A if (!isSubmitted) {
0N/A submit();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sends this {@code Runnable} for the execution
0N/A *
0N/A * <p>
0N/A * This method is to be executed only from {@code add} method.
0N/A *
0N/A * <p>
0N/A * This implementation uses {@code SwingWorker.invokeLater}.
0N/A */
0N/A protected void submit() {
0N/A SwingUtilities.invokeLater(this);
0N/A }
0N/A
0N/A /**
0N/A * Returns accumulated arguments and flashes the arguments storage.
0N/A *
0N/A * @return accumulated arguments
0N/A */
0N/A private final synchronized List<T> flush() {
0N/A List<T> list = arguments;
0N/A arguments = null;
0N/A return list;
0N/A }
0N/A}