Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/ControlUpdater.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 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.ControlUpdater; | |
12 | |
13 import java.lang.all; | |
14 | |
15 import org.eclipse.core.databinding.observable.ChangeEvent; | |
16 import org.eclipse.core.databinding.observable.IChangeListener; | |
17 import org.eclipse.core.databinding.observable.IObservable; | |
18 import org.eclipse.core.databinding.observable.ObservableTracker; | |
19 import org.eclipse.swt.events.DisposeEvent; | |
20 import org.eclipse.swt.events.DisposeListener; | |
21 import org.eclipse.swt.events.PaintEvent; | |
22 import org.eclipse.swt.events.PaintListener; | |
23 import org.eclipse.swt.widgets.Control; | |
24 | |
25 /** | |
26 * NON-API - A ControlUpdater updates an SWT control in response to changes in the model. | |
27 * By wrapping a block of code in a ControlUpdater, clients can rely on the fact | |
28 * that the block of code will be re-executed whenever anything changes in the | |
29 * model that might affect its behavior. | |
30 * | |
31 * <p> | |
32 * ControlUpdaters only execute when their controls are visible. If something changes | |
33 * in the model while the control is invisible, the updator is flagged as dirty and | |
34 * the updator stops listening to the model until the next time the control repaints. | |
35 * This saves CPU cycles by deferring UI updates to widgets that are currently invisible. | |
36 * </p> | |
37 * | |
38 * <p> | |
39 * Clients should subclass this when copying information from the model to | |
40 * a control. Typical usage: | |
41 * </p> | |
42 * | |
43 * <ul> | |
44 * <li>Override updateControl. It should do whatever is necessary to display | |
45 * the contents of the model in the control.</li> | |
46 * <li>In the constructor, attach listeners to the model. The listeners should | |
47 * call markDirty whenever anything changes in the model that affects | |
48 * updateControl. Note: this step can be omitted when calling any method | |
49 * tagged with "@TrackedGetter" since ControlUpdater will automatically attach | |
50 * a listener to any object if a "@TrackedGetter" method is called in | |
51 * updateControl.</li> | |
52 * <li>(optional)Extend dispose() to remove any listeners attached in the constructor</li> | |
53 * </ul> | |
54 * | |
55 * <p> | |
56 * Example: | |
57 * </p> | |
58 * | |
59 * <code> | |
60 * // Displays an observable value in a label and keeps the label in synch with changes | |
61 * // in the value. | |
62 * IReadableValue someValue = ... | |
63 * final Label myLabel = new Label(parent, SWT.NONE); | |
64 * new ControlUpdater(myLabel) { | |
65 * protected void updateControl() { | |
66 * myLabel.setText(someValue.getValue().toString); | |
67 * } | |
68 * } | |
69 * // myLabel will display the value of someValue the next time it repaints, and will automatically | |
70 * // be updated whenever someValue changes and the label is visible | |
71 * </code> | |
72 * | |
73 * @since 1.1 | |
74 */ | |
75 public abstract class ControlUpdater { | |
76 | |
77 private class PrivateInterface : PaintListener, | |
78 DisposeListener, Runnable, IChangeListener { | |
79 | |
80 // PaintListener implementation | |
81 public void paintControl(PaintEvent e) { | |
82 updateIfNecessary(); | |
83 } | |
84 | |
85 // DisposeListener implementation | |
86 public void widgetDisposed(DisposeEvent e) { | |
87 this.outer.dispose(); | |
88 } | |
89 | |
90 // Runnable implementation. This method runs at most once per repaint whenever the | |
91 // value gets marked as dirty. | |
92 public void run() { | |
93 if (theControl !is null && !theControl.isDisposed() && theControl.isVisible()) { | |
94 updateIfNecessary(); | |
95 } | |
96 } | |
97 | |
98 // IChangeListener implementation (listening to the ComputedValue) | |
99 public void handleChange(ChangeEvent event) { | |
100 // Whenever this updator becomes dirty, schedule the run() method | |
101 makeDirty(); | |
102 } | |
103 | |
104 } | |
105 | |
106 private Runnable updateRunnable = new class() Runnable { | |
107 public void run() { | |
108 updateControl(); | |
109 } | |
110 }; | |
111 | |
112 private PrivateInterface privateInterface = new PrivateInterface(); | |
113 private Control theControl; | |
114 private IObservable[] dependencies = new IObservable[0]; | |
115 private bool dirty = false; | |
116 | |
117 /** | |
118 * Creates an updater for the given control. | |
119 * | |
120 * @param toUpdate control to update | |
121 */ | |
122 public this(Control toUpdate) { | |
123 theControl = toUpdate; | |
124 | |
125 theControl.addDisposeListener(privateInterface); | |
126 theControl.addPaintListener(privateInterface); | |
127 makeDirty(); | |
128 } | |
129 | |
130 private void updateIfNecessary() { | |
131 if (dirty) { | |
132 dependencies = ObservableTracker.runAndMonitor(updateRunnable, privateInterface, null); | |
133 dirty = false; | |
134 } | |
135 } | |
136 | |
137 /** | |
138 * This is called automatically when the control is disposed. It may also | |
139 * be called explicitly to remove this updator from the control. Subclasses | |
140 * will normally extend this method to detach any listeners they attached | |
141 * in their constructor. | |
142 */ | |
143 public void dispose() { | |
144 theControl.removeDisposeListener(privateInterface); | |
145 theControl.removePaintListener(privateInterface); | |
146 | |
147 stopListening(); | |
148 } | |
149 | |
150 private void stopListening() { | |
151 // Stop listening for dependency changes | |
152 for (int i = 0; i < dependencies.length; i++) { | |
153 IObservable observable = dependencies[i]; | |
154 | |
155 observable.removeChangeListener(privateInterface); | |
156 } | |
157 } | |
158 | |
159 /** | |
160 * Updates the control. This method will be invoked once after the | |
161 * updator is created, and once before any repaint during which the | |
162 * control is visible and dirty. | |
163 * | |
164 * <p> | |
165 * Subclasses should overload this method to provide any code that | |
166 * changes the appearance of the widget. | |
167 * </p> | |
168 */ | |
169 protected abstract void updateControl(); | |
170 | |
171 /** | |
172 * Marks this updator as dirty. Causes the updateControl method to | |
173 * be invoked before the next time the control is repainted. | |
174 */ | |
175 protected final void makeDirty() { | |
176 if (!dirty) { | |
177 dirty = true; | |
178 stopListening(); | |
179 SWTUtil.runOnce(theControl.getDisplay(), privateInterface); | |
180 } | |
181 } | |
182 | |
183 } |