1 11 package org.eclipse.jface.internal.databinding.internal.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 public class WorkQueue { 23 private boolean updateScheduled = false; 24 25 private boolean paintListenerAttached = false; 26 27 private LinkedList pendingWork = new LinkedList (); 28 29 private Display d; 30 31 private Set pendingWorkSet = new HashSet (); 32 33 private Runnable updateJob = new Runnable () { 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 next; 55 synchronized (pendingWork) { 56 if (pendingWork.isEmpty()) { 57 break; 58 } 59 next = (Runnable ) pendingWork.removeFirst(); 60 pendingWorkSet.remove(next); 61 } 62 63 next.run(); 64 } 65 ; 66 } 67 68 77 public void runOnce(Runnable 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 98 public void asyncExec(Runnable work) { 99 synchronized (pendingWork) { 100 pendingWork.add(work); 101 if (!updateScheduled) { 102 updateScheduled = true; 103 d.asyncExec(updateJob); 104 } 105 106 if (Display.getCurrent() == d) { 109 if (!paintListenerAttached) { 110 paintListenerAttached = true; 111 d.addFilter(SWT.Paint, paintListener); 112 } 113 } 114 } 115 } 116 117 124 public void cancelExec(Runnable toCancel) { 125 synchronized (pendingWork) { 126 pendingWork.remove(toCancel); 127 pendingWorkSet.remove(toCancel); 128 } 129 } 130 131 134 public void cancelAll() { 135 synchronized (pendingWork) { 136 pendingWork.clear(); 137 pendingWorkSet.clear(); 138 } 139 } 140 } 141 | Popular Tags |