bug 419415 various small changes to improve usability of UpdatingComposite
diff --git a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/ControlCreator.java b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/ControlCreator.java index 6175dc0..b39c60f 100644 --- a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/ControlCreator.java +++ b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/ControlCreator.java
@@ -17,7 +17,7 @@ */ public abstract class ControlCreator<T extends Control> { - UpdatingComposite parent; + protected UpdatingComposite parent; Set<T> controls = new HashSet<T>();
diff --git a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/UpdatingComposite.java b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/UpdatingComposite.java index 5cc07d4..57a316d 100644 --- a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/UpdatingComposite.java +++ b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/UpdatingComposite.java
@@ -10,6 +10,8 @@ import org.eclipse.core.databinding.observable.IObservable; import org.eclipse.core.databinding.observable.ObservableTracker; import org.eclipse.core.databinding.observable.Realm; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -29,9 +31,10 @@ * child controls must not create the controls directly. Instead a control * creator is instantiated for each 'type' of control. If a control creator is * called to create a control then it will first search the existing unused - * controls for a control that was previously created by that control creator. A - * new control is only created if none exist. If the re-used control needs to be - * moved in the child control order then that is handled by this class. + * controls for a control that was previously created by that control creator + * (or a control creator that 'equals' it). A new control is only created if + * none exist. If the re-used control needs to be moved in the child control + * order then that is handled by this class. * <P> * Any change to one of the observable dependencies causes the child controls to * be re-built. @@ -43,8 +46,9 @@ * * <pre> * class MyComposite extends UpdatableComposite { - * LabelCreator labelCreator = new LabelCreator(this); - * LabelCreator labelCreator = new LabelCreator(this); + * ControlCreator labelCreator = new LabelCreator(this); + * ControlCreator companyNameControlCreator = new CompanyNameControlCreator(this); + * ControlCreator genderComboCreator = new GenderComboCreator(this); * * @Override * protected void createControls() { @@ -60,9 +64,9 @@ * companyNameControlCreator.create(); * } else { * Label label3 = labelCreator.create(); - * label3.setText("Sex:"); + * label3.setText("Gender:"); * - * sexComboCreator.create(); + * genderComboCreator.create(); * } * } * </pre> @@ -215,6 +219,12 @@ trackControls(); // } // }); + + addDisposeListener(new DisposeListener() { + public void widgetDisposed(DisposeEvent e) { + stopListening(); + } + }); } protected final void makeDirty() { @@ -229,6 +239,16 @@ */ Display.getCurrent().asyncExec(new Runnable() { public void run() { + /* + * Although we stop listening to the dependencies when this + * composite is disposed, there is a small window in which + * the composite may be disposed before we get back onto the + * UI thread here. + */ + if (isDisposed()) { + return; + } + trackControls(); computeSizeObservable.fireChange(); } @@ -289,7 +309,7 @@ T matchingControl = null; for (Control control : remainder) { - if (control.getData(key) == controlCreator) { + if (controlCreator.equals(control.getData(key))) { matchingControl = controlCreator.typeControl(control); break; }