KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > 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.internal.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 public class WorkQueue {
23     private boolean updateScheduled = false;
24
25     private boolean paintListenerAttached = false;
26
27     private LinkedList JavaDoc pendingWork = new LinkedList JavaDoc();
28
29     private Display d;
30
31     private Set JavaDoc pendingWorkSet = new HashSet JavaDoc();
32
33     private Runnable JavaDoc updateJob = new Runnable JavaDoc() {
34         public void run() {
35             doUpdate();
36             updateScheduled = false;
37         }
38     };
39
40     private Listener paintListener = new Listener() {
41         public void handleEvent(Event event) {
42             paintListenerAttached = false;
43             d.removeFilter(SWT.Paint, this);
44             doUpdate();
45         }
46     };
47
48     public WorkQueue(Display targetDisplay) {
49         d = targetDisplay;
50     }
51
52     private void doUpdate() {
53         for (;;) {
54             Runnable JavaDoc next;
55             synchronized (pendingWork) {
56                 if (pendingWork.isEmpty()) {
57                     break;
58                 }
59                 next = (Runnable JavaDoc) pendingWork.removeFirst();
60                 pendingWorkSet.remove(next);
61             }
62
63             next.run();
64         }
65         ;
66     }
67
68     /**
69      * Schedules some work to happen in the UI thread as soon as possible. If
70      * possible, the work will happen before the next control redraws. The given
71      * runnable will only be run once. Has no effect if this runnable has
72      * already been queued for execution.
73      *
74      * @param work
75      * runnable to execute
76      */

77     public void runOnce(Runnable JavaDoc work) {
78         synchronized (pendingWork) {
79             if (pendingWorkSet.contains(work)) {
80                 return;
81             }
82
83             pendingWorkSet.add(work);
84
85             asyncExec(work);
86         }
87     }
88
89     /**
90      * Schedules some work to happen in the UI thread as soon as possible. If
91      * possible, the work will happen before the next control redraws. Unlike
92      * runOnce, calling asyncExec twice with the same runnable will cause that
93      * runnable to run twice.
94      *
95      * @param work
96      * runnable to execute
97      */

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

124     public void cancelExec(Runnable JavaDoc toCancel) {
125         synchronized (pendingWork) {
126             pendingWork.remove(toCancel);
127             pendingWorkSet.remove(toCancel);
128         }
129     }
130
131     /**
132      * Cancels all pending work.
133      */

134     public void cancelAll() {
135         synchronized (pendingWork) {
136             pendingWork.clear();
137             pendingWorkSet.clear();
138         }
139     }
140 }
141
Popular Tags