1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui;
14 import org.eclipse.swt.widgets.Display;
17 * An overridable Runnable class that can be implemented to execute the runnable
18 * with a specific delay. The delay is implemented using SWT's
19 * {@link Display#timerExec(int, Runnable)}.
22 * The nice thing is that this class allows the user to schedule runs repeatedly
23 * but guarantees that the runnable is still only ran once.
28 * DelayRunnable runnable = new DelayRunnable(display, delay) {
29 * public void perform() {
30 * // Do the things you want...
34 * After this you can do multiple schedulings of the runnable as follows,
35 * but the Runnable will only get ran once after the specified delay.
37 * runnable.scheduleRefresh();
41 * @author Tuukka Lehtonen
43 public abstract class DelayRunnable implements Runnable {
45 /** Default delay: 500 ms */
46 private static final int DEFAULT_REFRESH_DELAY_MS = 500;
48 private int refreshDelay;
50 private volatile boolean scheduled = false;
52 private Display display;
54 public DelayRunnable(Display display) {
55 this(display, DEFAULT_REFRESH_DELAY_MS);
58 public DelayRunnable(Display display, int refreshDelay) {
59 this.display = display;
60 this.refreshDelay = refreshDelay;
63 protected void release() {
67 public boolean isScheduled() {
71 public void scheduleRefresh() {
77 display.asyncExec(timerSync);
81 * The default implementation of run calls release first in order to allow
82 * more schedulings of this Runnable to happen. After releasing the runnable
83 * {@link #perform()} is called.
86 * If you need to override the release-behaviour of this DelayRunnable, you
87 * need to override this method instead of {@link #perform()}.
96 * The method to override for performing your own actions when this runnable
97 * gets ran. The default implementation is empty.
99 public void perform() {
103 * This exists because timerExec can only be ran from the SWT thread or the
104 * thread that created the tree.
106 private Runnable timerSync = new Runnable() {
108 display.timerExec(refreshDelay, DelayRunnable.this);