diff options
author | Matthew Hall | 2009-12-01 07:12:53 +0000 |
---|---|---|
committer | Matthew Hall | 2009-12-01 07:12:53 +0000 |
commit | cb94eb4243ed8f425ba163d3a578ca3bd8bccccc (patch) | |
tree | f3aebd6b5da4ae6d32f3abbb3f8ebb0d064dc401 | |
parent | f6fb3522b94f5a296b8ca9814e82299718f10c0a (diff) | |
download | org.eclipse.e4.databinding-cb94eb4243ed8f425ba163d3a578ca3bd8bccccc.tar.gz org.eclipse.e4.databinding-cb94eb4243ed8f425ba163d3a578ca3bd8bccccc.tar.xz org.eclipse.e4.databinding-cb94eb4243ed8f425ba163d3a578ca3bd8bccccc.zip |
FIXED - bug 294810: [DataBinding] WidgetProperties.focused() does not update properlyv20091203I20100105-1530I20091210-1200I20091209-1800bI20091209-1800aI20091209-1800I20091207-1800I20091207-1300I20091206-0800I20091204-0800
https://bugs.eclipse.org/bugs/show_bug.cgi?id=294810
4 files changed, 181 insertions, 77 deletions
diff --git a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/WidgetValueProperty.java b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/WidgetValueProperty.java index 38ba16f9..00cffa7b 100644 --- a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/WidgetValueProperty.java +++ b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/WidgetValueProperty.java @@ -15,14 +15,10 @@ package org.eclipse.jface.databinding.swt; import org.eclipse.core.databinding.observable.Realm; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.property.INativePropertyListener; -import org.eclipse.core.databinding.property.IProperty; import org.eclipse.core.databinding.property.ISimplePropertyListener; -import org.eclipse.core.databinding.property.NativePropertyListener; import org.eclipse.core.databinding.property.value.SimpleValueProperty; import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator; -import org.eclipse.jface.internal.databinding.swt.WidgetListenerUtil; -import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Event; +import org.eclipse.jface.internal.databinding.swt.WidgetListener; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Widget; @@ -95,77 +91,9 @@ public abstract class WidgetValueProperty extends SimpleValueProperty implements public INativePropertyListener adaptListener( ISimplePropertyListener listener) { - return changeEvents == null && staleEvents == null ? null - : new WidgetListener(this, listener); - } - - private class WidgetListener extends NativePropertyListener implements - Listener { - protected WidgetListener(IProperty property, - ISimplePropertyListener listener) { - super(property, listener); - } - - public void handleEvent(Event event) { - if (staleEvents != null) - for (int i = 0; i < staleEvents.length; i++) - if (event.type == staleEvents[i]) { - fireStale(event.widget); - break; - } - - if (changeEvents != null) - for (int i = 0; i < changeEvents.length; i++) - if (event.type == changeEvents[i]) { - fireChange(event.widget, null); - break; - } - } - - protected void doAddTo(Object source) { - Widget widget = (Widget) source; - if (changeEvents != null) { - for (int i = 0; i < changeEvents.length; i++) { - int event = changeEvents[i]; - if (event != SWT.None) { - WidgetListenerUtil - .asyncAddListener(widget, event, this); - } - } - } - if (staleEvents != null) { - for (int i = 0; i < staleEvents.length; i++) { - int event = staleEvents[i]; - if (event != SWT.None) { - WidgetListenerUtil - .asyncAddListener(widget, event, this); - } - } - } - } - - protected void doRemoveFrom(Object source) { - Widget widget = (Widget) source; - if (!widget.isDisposed()) { - if (changeEvents != null) { - for (int i = 0; i < changeEvents.length; i++) { - int event = changeEvents[i]; - if (event != SWT.None) - WidgetListenerUtil.asyncRemoveListener(widget, - event, this); - } - } - if (staleEvents != null) { - for (int i = 0; i < staleEvents.length; i++) { - int event = staleEvents[i]; - if (event != SWT.None) { - WidgetListenerUtil.asyncRemoveListener(widget, - event, this); - } - } - } - } - } + if (changeEvents == null && staleEvents == null) + return null; + return new WidgetListener(this, listener, changeEvents, staleEvents); } public IObservableValue observe(Object source) { diff --git a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.java b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.java index 5498f7fa..caa5f588 100644 --- a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.java +++ b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.java @@ -12,8 +12,13 @@ package org.eclipse.jface.internal.databinding.swt; +import org.eclipse.core.databinding.observable.Diffs; +import org.eclipse.core.databinding.property.INativePropertyListener; +import org.eclipse.core.databinding.property.IProperty; +import org.eclipse.core.databinding.property.ISimplePropertyListener; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; /** * @since 3.3 @@ -24,7 +29,7 @@ public class ControlFocusedProperty extends WidgetBooleanValueProperty { * */ public ControlFocusedProperty() { - super(new int[] { SWT.FocusIn, SWT.FocusOut }); + super(); } public boolean doGetBooleanValue(Object source) { @@ -36,6 +41,39 @@ public class ControlFocusedProperty extends WidgetBooleanValueProperty { ((Control) source).setFocus(); } + public INativePropertyListener adaptListener( + ISimplePropertyListener listener) { + int[] events = { SWT.FocusIn, SWT.FocusOut }; + return new ControlFocusListener(this, listener, events, null); + } + + private class ControlFocusListener extends WidgetListener { + /** + * @param property + * @param listener + * @param changeEvents + * @param staleEvents + */ + private ControlFocusListener(IProperty property, + ISimplePropertyListener listener, int[] changeEvents, + int[] staleEvents) { + super(property, listener, changeEvents, staleEvents); + } + + public void handleEvent(Event event) { + switch (event.type) { + case SWT.FocusIn: + fireChange(event.widget, Diffs.createValueDiff(Boolean.FALSE, + Boolean.TRUE)); + break; + case SWT.FocusOut: + fireChange(event.widget, Diffs.createValueDiff(Boolean.TRUE, + Boolean.FALSE)); + break; + } + } + } + public String toString() { return "Control.focus <boolean>"; //$NON-NLS-1$ } diff --git a/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/WidgetListener.java b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/WidgetListener.java new file mode 100644 index 00000000..6fb036ac --- /dev/null +++ b/bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/WidgetListener.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2008-2009 Matthew Hall 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: + * Matthew Hall - initial API and implementation + * (from WidgetValueProperty.java) + * Matthew Hall - bug 294810 + ******************************************************************************/ + +package org.eclipse.jface.internal.databinding.swt; + +import org.eclipse.core.databinding.property.IProperty; +import org.eclipse.core.databinding.property.ISimplePropertyListener; +import org.eclipse.core.databinding.property.NativePropertyListener; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Widget; + +/** + * @since 3.3 + */ +public class WidgetListener extends NativePropertyListener implements Listener { + private final int[] changeEvents; + private final int[] staleEvents; + + /** + * @param property + * @param listener + * @param changeEvents + * @param staleEvents + */ + public WidgetListener(IProperty property, ISimplePropertyListener listener, + int[] changeEvents, int[] staleEvents) { + super(property, listener); + this.changeEvents = changeEvents; + this.staleEvents = staleEvents; + } + + public void handleEvent(Event event) { + if (staleEvents != null) + for (int i = 0; i < staleEvents.length; i++) + if (event.type == staleEvents[i]) { + fireStale(event.widget); + break; + } + + if (changeEvents != null) + for (int i = 0; i < changeEvents.length; i++) + if (event.type == changeEvents[i]) { + fireChange(event.widget, null); + break; + } + } + + protected void doAddTo(Object source) { + Widget widget = (Widget) source; + if (changeEvents != null) { + for (int i = 0; i < changeEvents.length; i++) { + int event = changeEvents[i]; + if (event != SWT.None) { + WidgetListenerUtil.asyncAddListener(widget, event, this); + } + } + } + if (staleEvents != null) { + for (int i = 0; i < staleEvents.length; i++) { + int event = staleEvents[i]; + if (event != SWT.None) { + WidgetListenerUtil.asyncAddListener(widget, event, this); + } + } + } + } + + protected void doRemoveFrom(Object source) { + Widget widget = (Widget) source; + if (!widget.isDisposed()) { + if (changeEvents != null) { + for (int i = 0; i < changeEvents.length; i++) { + int event = changeEvents[i]; + if (event != SWT.None) + WidgetListenerUtil.asyncRemoveListener(widget, event, + this); + } + } + if (staleEvents != null) { + for (int i = 0; i < staleEvents.length; i++) { + int event = staleEvents[i]; + if (event != SWT.None) { + WidgetListenerUtil.asyncRemoveListener(widget, event, + this); + } + } + } + } + } +}
\ No newline at end of file diff --git a/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java index 06dd5d7e..79366a04 100644 --- a/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java +++ b/tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java @@ -13,14 +13,21 @@ package org.eclipse.jface.tests.internal.databinding.swt; +import org.eclipse.core.databinding.observable.value.IObservableValue; +import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker; import org.eclipse.jface.databinding.swt.ISWTObservableValue; import org.eclipse.jface.databinding.swt.SWTObservables; +import org.eclipse.jface.databinding.swt.WidgetProperties; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; /** * @since 3.2 @@ -172,4 +179,33 @@ public class ControlObservableValueTest extends AbstractDefaultRealmTestCase { ISWTObservableValue value = SWTObservables.observeTooltipText(shell); assertEquals(String.class, value.getValueType()); } + + public void testObserveFocus() { + shell.setLayout(new FillLayout()); + Control c1 = new Text(shell, SWT.NONE); + Control c2 = new Text(shell, SWT.NONE); + shell.pack(); + shell.setVisible(true); + + assertTrue(c1.setFocus()); + + IObservableValue value = WidgetProperties.focused().observe(c2); + ValueChangeEventTracker tracker = ValueChangeEventTracker + .observe(value); + + assertTrue(c2.setFocus()); + + processDisplayQueue(); + + assertEquals(Boolean.TRUE, value.getValue()); + + assertEquals(1, tracker.count); + assertEquals(Boolean.FALSE, tracker.event.diff.getOldValue()); + assertEquals(Boolean.TRUE, tracker.event.diff.getNewValue()); + } + + private void processDisplayQueue() { + while (Display.getCurrent().readAndDispatch()) { + } + } } |