Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2927e3d4132aaef5ebd43a2fef2f29adf3cb6040 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boris Bokowski, IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.internal.databinding.provisional.swt;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.ObservableTracker;
import org.eclipse.core.databinding.observable.list.IListChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

/**
 * NON-API - This class can be used to update a composite with automatic dependency tracking.
 * @since 1.1
 * 
 */
public abstract class CompositeUpdater {

	private class UpdateRunnable implements Runnable, IChangeListener {
		private Widget widget;
		Object element;

		private boolean dirty = true;

		private IObservable[] dependencies = new IObservable[0];

		UpdateRunnable(Widget widget, Object element) {
			this.widget = widget;
			this.element = element;
		}

		// Runnable implementation. This method runs at most once per repaint
		// whenever the
		// value gets marked as dirty.
		public void run() {
			if (theComposite != null && !theComposite.isDisposed()
					&& widget != null && !widget.isDisposed()) {
				updateIfNecessary();
			}
		}

		private void updateIfNecessary() {
			if (dirty) {
				dependencies = ObservableTracker.runAndMonitor(new Runnable() {
					public void run() {
						updateWidget(widget, element);
					}
				}, this, null);
				dirty = false;
			}
		}

		// IChangeListener implementation (listening to any dependency)
		public void handleChange(ChangeEvent event) {
			// Whenever this updator becomes dirty, schedule the run() method
			makeDirty();
		}

		protected final void makeDirty() {
			if (!dirty) {
				dirty = true;
				stopListening();
				if (!theComposite.isDisposed()) {
					SWTUtil.runOnce(theComposite.getDisplay(), this);
				}
			}
		}

		private void stopListening() {
			// Stop listening for dependency changes
			for (int i = 0; i < dependencies.length; i++) {
				IObservable observable = dependencies[i];

				observable.removeChangeListener(this);
			}
		}
	}
	
	private class LayoutRunnable implements Runnable {
		private boolean posted = false;
		private Set controlsToLayout = new HashSet();
		void add(Control toLayout) {
			controlsToLayout.add(toLayout);
			if (!posted) {
				posted = true;
				theComposite.getDisplay().asyncExec(this);
			}
		}
		public void run() {
			posted = false;
			theComposite.getShell().layout((Control[])controlsToLayout.toArray(new Control[controlsToLayout.size()]));
			controlsToLayout.clear();
		}
	}
	
	private LayoutRunnable layoutRunnable = new LayoutRunnable();
	
	/**
	 * To be called from {@link #updateWidget(Widget, Object)} or {@link #createWidget(int)}
	 * if this updater's composite's layout may need to be updated. 
	 * @param control
	 * @since 1.2
	 */
	protected void requestLayout(Control control) {
		layoutRunnable.add(control);
	}

	private class PrivateInterface implements DisposeListener,
			IListChangeListener {

		// DisposeListener implementation
		public void widgetDisposed(DisposeEvent e) {
			CompositeUpdater.this.dispose();
		}

		public void handleListChange(ListChangeEvent event) {
			ListDiffEntry[] diffs = event.diff.getDifferences();
			for (int i = 0; i < diffs.length; i++) {
				ListDiffEntry listDiffEntry = diffs[i];
				if (listDiffEntry.isAddition()) {
					createChild(listDiffEntry.getElement(), listDiffEntry.getPosition());
				} else {
					disposeWidget(listDiffEntry.getPosition());
				}
			}
			theComposite.layout();
		}

	}

	private PrivateInterface privateInterface = new PrivateInterface();

	private Composite theComposite;

	private IObservableList model;

	/**
	 * Creates an updater for the given control and list. For each element of
	 * the list, a child widget of the composite will be created using
	 * {@link #createWidget(int)}.
	 * 
	 * @param toUpdate
	 *            composite to update
	 * @param model
	 *            an observable list to track
	 */
	public CompositeUpdater(Composite toUpdate, IObservableList model) {
		this.theComposite = toUpdate;
		this.model = model;

		model.addListChangeListener(privateInterface);
		theComposite.addDisposeListener(privateInterface);
		ObservableTracker.runAndIgnore(new Runnable(){
			public void run() {
				int index = 0;
				for (Iterator it = CompositeUpdater.this.model.iterator(); it.hasNext();) {
					Object element = it.next();
					createChild(element, index++);
				}
			}
		});
	}

	/**
	 * @param position
	 * @since 1.2
	 */
	protected void disposeWidget(int position) {
		theComposite.getChildren()[position].dispose();
	}

	/**
	 * This is called automatically when the control is disposed. It may also be
	 * called explicitly to remove this updator from the control. Subclasses
	 * will normally extend this method to detach any listeners they attached in
	 * their constructor.
	 */
	public void dispose() {
		theComposite.removeDisposeListener(privateInterface);
		model.removeListChangeListener(privateInterface);
	}

	/**
	 * Creates a new child widget for the target composite at the given index.
	 * 
	 * <p>
	 * Subclasses should implement this method to provide the code that creates
	 * a child widget at a specific index. Note that
	 * {@link #updateWidget(Widget, Object)} will be called after this method
	 * returns. Only those properties of the widget that don't change over time
	 * should be set in this method.
	 * </p>
	 * 
	 * @param index
	 *            the at which to create the widget
	 * @return the widget
	 */
	protected abstract Widget createWidget(int index);

	/**
	 * Updates the given widget based on the element found in the model list.
	 * This method will be invoked once after the widget is created, and once
	 * before any repaint during which the control is visible and dirty.
	 * 
	 * <p>
	 * Subclasses should implement this method to provide any code that changes
	 * the appearance of the widget.
	 * </p>
	 * 
	 * @param widget
	 *            the widget to update
	 * @param element
	 *            the element associated with the widget
	 */
	protected abstract void updateWidget(Widget widget, Object element);

	void createChild(Object element, int index) {
		Widget newChild = createWidget(index);
		final UpdateRunnable updateRunnable = new UpdateRunnable(newChild, element);
		newChild.setData(updateRunnable);
		updateRunnable.updateIfNecessary();
	}

}

Back to the top