Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Hall2009-12-01 07:12:53 +0000
committerMatthew Hall2009-12-01 07:12:53 +0000
commitcb94eb4243ed8f425ba163d3a578ca3bd8bccccc (patch)
treef3aebd6b5da4ae6d32f3abbb3f8ebb0d064dc401
parentf6fb3522b94f5a296b8ca9814e82299718f10c0a (diff)
downloadorg.eclipse.e4.databinding-I20091207-1300.tar.gz
org.eclipse.e4.databinding-I20091207-1300.tar.xz
org.eclipse.e4.databinding-I20091207-1300.zip
https://bugs.eclipse.org/bugs/show_bug.cgi?id=294810
-rw-r--r--bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/WidgetValueProperty.java80
-rw-r--r--bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.java40
-rw-r--r--bundles/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/WidgetListener.java102
-rw-r--r--tests/org.eclipse.jface.tests.databinding/src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java36
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()) {
+ }
+ }
}

Back to the top