KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > swt > WorkQueue


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11 package org.eclipse.jface.internal.databinding.provisional.swt;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.LinkedList JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Event;
20 import org.eclipse.swt.widgets.Listener;
21
22 /**
23  * NON-API - Helper class to manage a queue of runnables to be posted to the UI thread in a way
24  * that they are only run once.
25  * @since 1.1
26  *
27  */

28 public class WorkQueue {
29     
30     private boolean updateScheduled = false;
31
32     private boolean paintListenerAttached = false;
33
34     private LinkedList JavaDoc pendingWork = new LinkedList JavaDoc();
35
36     private Display d;
37
38     private Set JavaDoc pendingWorkSet = new HashSet JavaDoc();
39
40     private Runnable JavaDoc updateJob = new Runnable JavaDoc() {
41         public void run() {
42             doUpdate();
43             updateScheduled = false;
44         }
45     };
46
47     private Listener paintListener = new Listener() {
48         public void handleEvent(Event event) {
49             paintListenerAttached = false;
50             d.removeFilter(SWT.Paint, this);
51             doUpdate();
52         }
53     };
54
55     /**
56      * @param targetDisplay
57      */

58     public WorkQueue(Display targetDisplay) {
59         d = targetDisplay;
60     }
61
62     private void doUpdate() {
63         for (;;) {
64             Runnable JavaDoc next;
65             synchronized (pendingWork) {
66                 if (pendingWork.isEmpty()) {
67                     break;
68                 }
69                 next = (Runnable JavaDoc) pendingWork.removeFirst();
70                 pendingWorkSet.remove(next);
71             }
72
73             next.run();
74         }
75     }
76
77     /**
78      * Schedules some work to happen in the UI thread as soon as possible. If
79      * possible, the work will happen before the next control redraws. The given
80      * runnable will only be run once. Has no effect if this runnable has
81      * already been queued for execution.
82      *
83      * @param work
84      * runnable to execute
85      */

86     public void runOnce(Runnable JavaDoc work) {
87         synchronized (pendingWork) {
88             if (pendingWorkSet.contains(work)) {
89                 return;
90             }
91
92             pendingWorkSet.add(work);
93
94             asyncExec(work);
95         }
96     }
97
98     /**
99      * Schedules some work to happen in the UI thread as soon as possible. If
100      * possible, the work will happen before the next control redraws. Unlike
101      * runOnce, calling asyncExec twice with the same runnable will cause that
102      * runnable to run twice.
103      *
104      * @param work
105      * runnable to execute
106      */

107     public void asyncExec(Runnable JavaDoc work) {
108         synchronized (pendingWork) {
109             pendingWork.add(work);
110             if (!updateScheduled) {
111                 updateScheduled = true;
112                 d.asyncExec(updateJob);
113             }
114
115             // If we're in the UI thread, add an event filter to ensure
116
// the work happens ASAP
117
if (Display.getCurrent() == d) {
118                 if (!paintListenerAttached) {
119                     paintListenerAttached = true;
120                     d.addFilter(SWT.Paint, paintListener);
121                 }
122             }
123         }
124     }
125
126     /**
127      * Cancels a previously-scheduled runnable. Has no effect if the given
128      * runnable was not previously scheduled or has already executed.
129      *
130      * @param toCancel
131      * runnable to cancel
132      */

133     public void cancelExec(Runnable JavaDoc toCancel) {
134         synchronized (pendingWork) {
135             pendingWork.remove(toCancel);
136             pendingWorkSet.remove(toCancel);
137         }
138     }
139
140     /**
141      * Cancels all pending work.
142      */

143     public void cancelAll() {
144         synchronized (pendingWork) {
145             pendingWork.clear();
146             pendingWorkSet.clear();
147         }
148     }
149 }
150
Popular Tags