1 11 package org.eclipse.jface.internal.databinding.provisional.swt; 12 13 import org.eclipse.core.databinding.observable.ChangeEvent; 14 import org.eclipse.core.databinding.observable.IChangeListener; 15 import org.eclipse.core.databinding.observable.IObservable; 16 import org.eclipse.core.databinding.observable.ObservableTracker; 17 import org.eclipse.swt.events.DisposeEvent; 18 import org.eclipse.swt.events.DisposeListener; 19 import org.eclipse.swt.events.PaintEvent; 20 import org.eclipse.swt.events.PaintListener; 21 import org.eclipse.swt.widgets.Control; 22 23 73 public abstract class ControlUpdater { 74 75 private class PrivateInterface implements PaintListener, 76 DisposeListener, Runnable , IChangeListener { 77 78 public void paintControl(PaintEvent e) { 80 updateIfNecessary(); 81 } 82 83 public void widgetDisposed(DisposeEvent e) { 85 ControlUpdater.this.dispose(); 86 } 87 88 public void run() { 91 if (theControl != null && !theControl.isDisposed() && theControl.isVisible()) { 92 updateIfNecessary(); 93 } 94 } 95 96 public void handleChange(ChangeEvent event) { 98 makeDirty(); 100 } 101 102 } 103 104 private Runnable updateRunnable = new Runnable () { 105 public void run() { 106 updateControl(); 107 } 108 }; 109 110 private PrivateInterface privateInterface = new PrivateInterface(); 111 private Control theControl; 112 private IObservable[] dependencies = new IObservable[0]; 113 private boolean dirty = false; 114 115 120 public ControlUpdater(Control toUpdate) { 121 theControl = toUpdate; 122 123 theControl.addDisposeListener(privateInterface); 124 theControl.addPaintListener(privateInterface); 125 makeDirty(); 126 } 127 128 private void updateIfNecessary() { 129 if (dirty) { 130 dependencies = ObservableTracker.runAndMonitor(updateRunnable, privateInterface, null); 131 dirty = false; 132 } 133 } 134 135 141 public void dispose() { 142 theControl.removeDisposeListener(privateInterface); 143 theControl.removePaintListener(privateInterface); 144 145 stopListening(); 146 } 147 148 private void stopListening() { 149 for (int i = 0; i < dependencies.length; i++) { 151 IObservable observable = dependencies[i]; 152 153 observable.removeChangeListener(privateInterface); 154 } 155 } 156 157 167 protected abstract void updateControl(); 168 169 173 protected final void makeDirty() { 174 if (!dirty) { 175 dirty = true; 176 stopListening(); 177 SWTUtil.runOnce(theControl.getDisplay(), privateInterface); 178 } 179 } 180 181 } 182 | Popular Tags |