KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > swt > MenuUpdater


1 /*******************************************************************************
2  * Copyright (c) 2006 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 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 /**
24  * NON-API - A MenuUpdater updates an SWT menu in response to changes in the model. By
25  * wrapping a block of code in a MenuUpdater, clients can rely on the fact that
26  * the block of code will be re-executed whenever anything changes in the model
27  * that might affect its behavior.
28  *
29  * <p>
30  * MenuUpdaters only execute once their menus are shown. If something changes in
31  * the model, the updater is flagged as dirty and it stops listening to the
32  * model until the next time the menu is shown. If the menu is visible while the
33  * model changes, it will be updated right away.
34  * </p>
35  *
36  * <p>
37  * Clients should subclass this when copying information from the model to a
38  * menu. Typical usage:
39  * </p>
40  *
41  * <ul>
42  * <li>Override updateMenu. It should do whatever is necessary to display the
43  * contents of the model in the menu.</li>
44  * <li>In the constructor, attach listeners to the model. The listeners should
45  * call markDirty whenever anything changes in the model that affects
46  * updateMenu. Note: this step can be omitted when calling any method tagged
47  * with "@TrackedGetter" since MenuUpdater will automatically attach a listener
48  * to any object if a "@TrackedGetter" method is called in updateMenu.</li>
49  * <li>(optional)Extend dispose() to remove any listeners attached in the
50  * constructor</li>
51  * </ul>
52  *
53  * @since 1.1
54  */

55 public abstract class MenuUpdater {
56     
57     private class PrivateInterface implements MenuListener,
58         DisposeListener, Runnable JavaDoc, IChangeListener {
59
60         // DisposeListener implementation
61
public void widgetDisposed(DisposeEvent e) {
62             MenuUpdater.this.dispose();
63         }
64         
65         // Runnable implementation. This method runs at most once per repaint whenever the
66
// value gets marked as dirty.
67
public void run() {
68             if (theMenu != null && !theMenu.isDisposed() && theMenu.isVisible()) {
69                 updateIfNecessary();
70             }
71         }
72         
73         // IChangeListener implementation (listening to the ComputedValue)
74
public void handleChange(ChangeEvent event) {
75             // Whenever this updator becomes dirty, schedule the run() method
76
makeDirty();
77         }
78
79         public void menuHidden(MenuEvent e) {
80             // do nothing
81
}
82
83         public void menuShown(MenuEvent e) {
84             updateIfNecessary();
85         }
86         
87     }
88     
89     private Runnable JavaDoc updateRunnable = new Runnable JavaDoc() {
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     /**
101      * Creates an updator for the given menu.
102      *
103      * @param toUpdate menu to update
104      */

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     /**
121      * This is called automatically when the menu is disposed. It may also
122      * be called explicitly to remove this updator from the menu. Subclasses
123      * will normally extend this method to detach any listeners they attached
124      * in their constructor.
125      */

126     public void dispose() {
127         theMenu.removeDisposeListener(privateInterface);
128         theMenu.removeMenuListener(privateInterface);
129
130         stopListening();
131     }
132
133     private void stopListening() {
134         // Stop listening for dependency changes
135
for (int i = 0; i < dependencies.length; i++) {
136             IObservable observable = dependencies[i];
137                 
138             observable.removeChangeListener(privateInterface);
139         }
140     }
141
142     /**
143      * Updates the menu. This method will be invoked once after the
144      * updater is created, and once for any SWT.Show event if this
145      * updater is marked as dirty at that time.
146      *
147      * <p>
148      * Subclasses should overload this method to provide any code that
149      * udates the menu.
150      * </p>
151      */

152     protected abstract void updateMenu();
153     
154     /**
155      * Marks this updator as dirty. Causes the updateControl method to
156      * be invoked before the next time the control is repainted.
157      */

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