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.MenuEvent; 20 import org.eclipse.swt.events.MenuListener; 21 import org.eclipse.swt.widgets.Menu; 22 23 55 public abstract class MenuUpdater { 56 57 private class PrivateInterface implements MenuListener, 58 DisposeListener, Runnable , IChangeListener { 59 60 public void widgetDisposed(DisposeEvent e) { 62 MenuUpdater.this.dispose(); 63 } 64 65 public void run() { 68 if (theMenu != null && !theMenu.isDisposed() && theMenu.isVisible()) { 69 updateIfNecessary(); 70 } 71 } 72 73 public void handleChange(ChangeEvent event) { 75 makeDirty(); 77 } 78 79 public void menuHidden(MenuEvent e) { 80 } 82 83 public void menuShown(MenuEvent e) { 84 updateIfNecessary(); 85 } 86 87 } 88 89 private Runnable updateRunnable = new Runnable () { 90 public void run() { 91 updateMenu(); 92 } 93 }; 94 95 private PrivateInterface privateInterface = new PrivateInterface(); 96 private Menu theMenu; 97 private IObservable[] dependencies = new IObservable[0]; 98 private boolean dirty = false; 99 100 105 public MenuUpdater(Menu toUpdate) { 106 theMenu = toUpdate; 107 108 theMenu.addDisposeListener(privateInterface); 109 theMenu.addMenuListener(privateInterface); 110 makeDirty(); 111 } 112 113 private void updateIfNecessary() { 114 if (dirty) { 115 dependencies = ObservableTracker.runAndMonitor(updateRunnable, privateInterface, null); 116 dirty = false; 117 } 118 } 119 120 126 public void dispose() { 127 theMenu.removeDisposeListener(privateInterface); 128 theMenu.removeMenuListener(privateInterface); 129 130 stopListening(); 131 } 132 133 private void stopListening() { 134 for (int i = 0; i < dependencies.length; i++) { 136 IObservable observable = dependencies[i]; 137 138 observable.removeChangeListener(privateInterface); 139 } 140 } 141 142 152 protected abstract void updateMenu(); 153 154 158 protected final void makeDirty() { 159 if (!dirty) { 160 dirty = true; 161 stopListening(); 162 SWTUtil.runOnce(theMenu.getDisplay(), privateInterface); 163 } 164 } 165 166 } 167 | Popular Tags |