DefaultEdtScheduler.java :  » GWT » reactive4java » hu » akarnokd » reactiv4java » Java Open Source

Java Open Source » GWT » reactive4java 
reactive4java » hu » akarnokd » reactiv4java » DefaultEdtScheduler.java
/*
 * Copyright 2011 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.akarnokd.reactiv4java;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 * The default Event Dispatch Thread scheduler implementation.
 * Which ensures that all tasks submittet to it
 * will run on the EDT.
 * @author akarnokd, 2011.02.02.
 */
public class DefaultEdtScheduler implements Scheduler {
  /**
   * Helper class that has semantics for cancellation.
   * @author akarnokd, 2011.02.02.
   */
  static class EdtRunnable implements Runnable, Closeable {
    /** The wrapped runnable. */
    final Runnable run;
    /** Should the run() method still execute its body? */
    final AtomicBoolean alive = new AtomicBoolean(true);
    /**
     * Constructor.
     * @param run the runnable
     */
    public EdtRunnable(Runnable run) {
      if (run == null) {
        throw new IllegalArgumentException("run is null");
      }
      this.run = run;
    }
    @Override
    public void run() {
      if (alive.compareAndSet(true, false)) {
        run.run();
      }
    }
    @Override
    public void close() throws IOException {
      alive.set(false);
    }
  }
  @Override
  public Closeable schedule(Runnable run) {
    EdtRunnable t = new EdtRunnable(run);
    SwingUtilities.invokeLater(t);
    return t;
  }

  @Override
  public Closeable schedule(final Runnable run, long delay) {
    final Timer t = new Timer((int)(delay / 1000000), null);
    t.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        run.run();
        t.stop();
      }
    });
    t.start();
    return new Closeable() {
      @Override
      public void close() throws IOException {
        t.stop();
      }
    };
  }

  @Override
  public Closeable schedule(final Runnable run, long initialDelay, long betweenDelay) {
    final Timer t = new Timer((int)(initialDelay / 1000000), null);
    t.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        run.run();
      }
    });
    t.setDelay((int)(betweenDelay / 1000000));
    t.start();
    return new Closeable() {
      @Override
      public void close() throws IOException {
        t.stop();
      }
    };
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.