1 11 package org.eclipse.jface.internal.databinding.provisional.swt; 12 13 import java.util.HashSet ; 14 import java.util.LinkedList ; 15 import java.util.Set ; 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 28 public class WorkQueue { 29 30 private boolean updateScheduled = false; 31 32 private boolean paintListenerAttached = false; 33 34 private LinkedList pendingWork = new LinkedList (); 35 36 private Display d; 37 38 private Set pendingWorkSet = new HashSet (); 39 40 private Runnable updateJob = new Runnable () { 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 58 public WorkQueue(Display targetDisplay) { 59 d = targetDisplay; 60 } 61 62 private void doUpdate() { 63 for (;;) { 64 Runnable next; 65 synchronized (pendingWork) { 66 if (pendingWork.isEmpty()) { 67 break; 68 } 69 next = (Runnable ) pendingWork.removeFirst(); 70 pendingWorkSet.remove(next); 71 } 72 73 next.run(); 74 } 75 } 76 77 86 public void runOnce(Runnable 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 107 public void asyncExec(Runnable work) { 108 synchronized (pendingWork) { 109 pendingWork.add(work); 110 if (!updateScheduled) { 111 updateScheduled = true; 112 d.asyncExec(updateJob); 113 } 114 115 if (Display.getCurrent() == d) { 118 if (!paintListenerAttached) { 119 paintListenerAttached = true; 120 d.addFilter(SWT.Paint, paintListener); 121 } 122 } 123 } 124 } 125 126 133 public void cancelExec(Runnable toCancel) { 134 synchronized (pendingWork) { 135 pendingWork.remove(toCancel); 136 pendingWorkSet.remove(toCancel); 137 } 138 } 139 140 143 public void cancelAll() { 144 synchronized (pendingWork) { 145 pendingWork.clear(); 146 pendingWorkSet.clear(); 147 } 148 } 149 } 150 | Popular Tags |