78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2007 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 module org.eclipse.jface.internal.databinding.provisional.swt.MenuUpdater;
|
85
|
12 import org.eclipse.jface.internal.databinding.provisional.swt.SWTUtil;
|
78
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import org.eclipse.core.databinding.observable.ChangeEvent;
|
|
17 import org.eclipse.core.databinding.observable.IChangeListener;
|
|
18 import org.eclipse.core.databinding.observable.IObservable;
|
|
19 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
20 import org.eclipse.swt.events.DisposeEvent;
|
|
21 import org.eclipse.swt.events.DisposeListener;
|
|
22 import org.eclipse.swt.events.MenuEvent;
|
|
23 import org.eclipse.swt.events.MenuListener;
|
|
24 import org.eclipse.swt.widgets.Menu;
|
|
25
|
|
26 /**
|
|
27 * NON-API - A MenuUpdater updates an SWT menu in response to changes in the model. By
|
|
28 * wrapping a block of code in a MenuUpdater, clients can rely on the fact that
|
|
29 * the block of code will be re-executed whenever anything changes in the model
|
|
30 * that might affect its behavior.
|
|
31 *
|
|
32 * <p>
|
|
33 * MenuUpdaters only execute once their menus are shown. If something changes in
|
|
34 * the model, the updater is flagged as dirty and it stops listening to the
|
|
35 * model until the next time the menu is shown. If the menu is visible while the
|
|
36 * model changes, it will be updated right away.
|
|
37 * </p>
|
|
38 *
|
|
39 * <p>
|
|
40 * Clients should subclass this when copying information from the model to a
|
|
41 * menu. Typical usage:
|
|
42 * </p>
|
|
43 *
|
|
44 * <ul>
|
|
45 * <li>Override updateMenu. It should do whatever is necessary to display the
|
|
46 * contents of the model in the menu.</li>
|
|
47 * <li>In the constructor, attach listeners to the model. The listeners should
|
|
48 * call markDirty whenever anything changes in the model that affects
|
|
49 * updateMenu. Note: this step can be omitted when calling any method tagged
|
|
50 * with "@TrackedGetter" since MenuUpdater will automatically attach a listener
|
|
51 * to any object if a "@TrackedGetter" method is called in updateMenu.</li>
|
|
52 * <li>(optional)Extend dispose() to remove any listeners attached in the
|
|
53 * constructor</li>
|
|
54 * </ul>
|
|
55 *
|
|
56 * @since 1.1
|
|
57 */
|
|
58 public abstract class MenuUpdater {
|
|
59
|
|
60 private class PrivateInterface : MenuListener,
|
|
61 DisposeListener, Runnable, IChangeListener {
|
|
62
|
|
63 // DisposeListener implementation
|
|
64 public void widgetDisposed(DisposeEvent e) {
|
|
65 this.outer.dispose();
|
|
66 }
|
|
67
|
|
68 // Runnable implementation. This method runs at most once per repaint whenever the
|
|
69 // value gets marked as dirty.
|
|
70 public void run() {
|
|
71 if (theMenu !is null && !theMenu.isDisposed() && theMenu.isVisible()) {
|
|
72 updateIfNecessary();
|
|
73 }
|
|
74 }
|
|
75
|
|
76 // IChangeListener implementation (listening to the ComputedValue)
|
|
77 public void handleChange(ChangeEvent event) {
|
|
78 // Whenever this updator becomes dirty, schedule the run() method
|
|
79 makeDirty();
|
|
80 }
|
|
81
|
|
82 public void menuHidden(MenuEvent e) {
|
|
83 // do nothing
|
|
84 }
|
|
85
|
|
86 public void menuShown(MenuEvent e) {
|
|
87 updateIfNecessary();
|
|
88 }
|
|
89
|
|
90 }
|
|
91
|
85
|
92 private Runnable updateRunnable;
|
|
93 class UpdateRunnable : Runnable {
|
78
|
94 public void run() {
|
|
95 updateMenu();
|
|
96 }
|
|
97 };
|
|
98
|
85
|
99 private PrivateInterface privateInterface;
|
78
|
100 private Menu theMenu;
|
85
|
101 private IObservable[] dependencies;
|
78
|
102 private bool dirty = false;
|
|
103
|
|
104 /**
|
|
105 * Creates an updator for the given menu.
|
|
106 *
|
|
107 * @param toUpdate menu to update
|
|
108 */
|
|
109 public this(Menu toUpdate) {
|
85
|
110 updateRunnable = new UpdateRunnable();
|
|
111 privateInterface = new PrivateInterface();
|
78
|
112 theMenu = toUpdate;
|
|
113
|
|
114 theMenu.addDisposeListener(privateInterface);
|
|
115 theMenu.addMenuListener(privateInterface);
|
|
116 makeDirty();
|
|
117 }
|
|
118
|
|
119 private void updateIfNecessary() {
|
|
120 if (dirty) {
|
|
121 dependencies = ObservableTracker.runAndMonitor(updateRunnable, privateInterface, null);
|
|
122 dirty = false;
|
|
123 }
|
|
124 }
|
|
125
|
|
126 /**
|
|
127 * This is called automatically when the menu is disposed. It may also
|
|
128 * be called explicitly to remove this updator from the menu. Subclasses
|
|
129 * will normally extend this method to detach any listeners they attached
|
|
130 * in their constructor.
|
|
131 */
|
|
132 public void dispose() {
|
|
133 theMenu.removeDisposeListener(privateInterface);
|
|
134 theMenu.removeMenuListener(privateInterface);
|
|
135
|
|
136 stopListening();
|
|
137 }
|
|
138
|
|
139 private void stopListening() {
|
|
140 // Stop listening for dependency changes
|
|
141 for (int i = 0; i < dependencies.length; i++) {
|
|
142 IObservable observable = dependencies[i];
|
|
143
|
|
144 observable.removeChangeListener(privateInterface);
|
|
145 }
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * Updates the menu. This method will be invoked once after the
|
|
150 * updater is created, and once for any SWT.Show event if this
|
|
151 * updater is marked as dirty at that time.
|
|
152 *
|
|
153 * <p>
|
|
154 * Subclasses should overload this method to provide any code that
|
|
155 * udates the menu.
|
|
156 * </p>
|
|
157 */
|
|
158 protected abstract void updateMenu();
|
|
159
|
|
160 /**
|
|
161 * Marks this updator as dirty. Causes the updateControl method to
|
|
162 * be invoked before the next time the control is repainted.
|
|
163 */
|
|
164 protected final void makeDirty() {
|
|
165 if (!dirty) {
|
|
166 dirty = true;
|
|
167 stopListening();
|
|
168 SWTUtil.runOnce(theMenu.getDisplay(), privateInterface);
|
|
169 }
|
|
170 }
|
|
171
|
|
172 }
|