summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Drossel2008-06-19 09:54:55 (EDT)
committerCarsten Drossel2008-06-19 09:54:55 (EDT)
commit187f4904d91d84835071db1ce1962c53284c46aa (patch)
tree57ccd0934383002b29d489098a0bf4ad36e1c024
parentee4bb178f31c27bd2bacda60db4f2e49dae0c6bd (diff)
downloadorg.eclipse.riena-187f4904d91d84835071db1ce1962c53284c46aa.zip
org.eclipse.riena-187f4904d91d84835071db1ce1962c53284c46aa.tar.gz
org.eclipse.riena-187f4904d91d84835071db1ce1962c53284c46aa.tar.bz2
SWT-ridgets are now notifying IFocusListeners
-rw-r--r--org.eclipse.riena.tests/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidgetTest.java73
-rw-r--r--org.eclipse.riena.ui.ridgets.swt/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidget.java101
2 files changed, 128 insertions, 46 deletions
diff --git a/org.eclipse.riena.tests/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidgetTest.java b/org.eclipse.riena.tests/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidgetTest.java
index 39450a0..75f3adc 100644
--- a/org.eclipse.riena.tests/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidgetTest.java
+++ b/org.eclipse.riena.tests/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidgetTest.java
@@ -12,12 +12,16 @@ package org.eclipse.riena.internal.ui.ridgets.swt;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
+import java.util.List;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import org.eclipse.riena.tests.UITestHelper;
import org.eclipse.riena.ui.ridgets.IRidget;
+import org.eclipse.riena.ui.ridgets.listener.FocusEvent;
+import org.eclipse.riena.ui.ridgets.listener.IFocusListener;
import org.eclipse.riena.ui.tests.base.PropertyChangeEventEquals;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
@@ -35,6 +39,7 @@ public abstract class AbstractSWTRidgetTest extends TestCase {
private Shell shell;
private Control control;
private IRidget ridget;
+ private Text otherControl;
private PropertyChangeListener propertyChangeListenerMock;
@Override
@@ -51,7 +56,10 @@ public abstract class AbstractSWTRidgetTest extends TestCase {
propertyChangeListenerMock = EasyMock.createMock(PropertyChangeListener.class);
ridget.addPropertyChangeListener(propertyChangeListenerMock);
- shell.setSize(100, 100);
+ otherControl = new Text(shell, SWT.SINGLE);
+ otherControl.setText("other focusable widget");
+
+ shell.setSize(130, 100);
shell.setLocation(0, 0);
shell.open();
}
@@ -61,6 +69,8 @@ public abstract class AbstractSWTRidgetTest extends TestCase {
ridget = null;
control.dispose();
control = null;
+ otherControl.dispose();
+ otherControl = null;
shell.dispose();
shell = null;
realm.dispose();
@@ -156,28 +166,77 @@ public abstract class AbstractSWTRidgetTest extends TestCase {
public void testSetFocusable() {
IRidget ridget = getRidget();
Control control = getUIControl();
- Control firstControl = new Text(getShell(), SWT.SINGLE);
- firstControl.moveAbove(control);
+ otherControl.moveAbove(control);
control.setFocus();
if (control.isFocusControl()) { // skip if control cannot receive focus
ridget.setFocusable(false);
- firstControl.setFocus();
+ otherControl.setFocus();
- assertTrue(firstControl.isFocusControl());
+ assertTrue(otherControl.isFocusControl());
- UITestHelper.sendString(control.getDisplay(), "\t");
+ UITestHelper.sendString(otherControl.getDisplay(), "\t");
assertFalse(control.isFocusControl());
ridget.setFocusable(true);
- UITestHelper.sendString(control.getDisplay(), "\t");
+
+ otherControl.setFocus();
+ UITestHelper.sendString(otherControl.getDisplay(), "\t");
assertTrue(control.isFocusControl());
}
}
+ public void testRequestFocus() throws Exception {
+
+ control.setFocus();
+ if (control.isFocusControl()) { // skip if control cannot receive focus
+ assertTrue(otherControl.setFocus());
+
+ assertFalse(control.isFocusControl());
+ assertFalse(ridget.hasFocus());
+
+ final List<FocusEvent> focusGainedEvents = new ArrayList<FocusEvent>();
+ final List<FocusEvent> focusLostEvents = new ArrayList<FocusEvent>();
+ IFocusListener focusListener = new IFocusListener() {
+ public void focusGained(FocusEvent event) {
+ focusGainedEvents.add(event);
+ }
+
+ public void focusLost(FocusEvent event) {
+ focusLostEvents.add(event);
+ }
+ };
+ ridget.addFocusListener(focusListener);
+
+ ridget.requestFocus();
+
+ assertTrue(control.isFocusControl());
+ assertTrue(ridget.hasFocus());
+ assertEquals(1, focusGainedEvents.size());
+ assertEquals(ridget, focusGainedEvents.get(0).getNewFocusOwner());
+ assertEquals(0, focusLostEvents.size());
+
+ assertTrue(otherControl.setFocus());
+
+ assertFalse(control.isFocusControl());
+ assertFalse(ridget.hasFocus());
+ assertEquals(1, focusGainedEvents.size());
+ assertEquals(1, focusLostEvents.size());
+ assertEquals(ridget, focusLostEvents.get(0).getOldFocusOwner());
+
+ ridget.removeFocusListener(focusListener);
+
+ ridget.requestFocus();
+ assertTrue(otherControl.setFocus());
+
+ assertEquals(1, focusGainedEvents.size());
+ assertEquals(1, focusLostEvents.size());
+ }
+ }
+
// helping methods
// ////////////////
diff --git a/org.eclipse.riena.ui.ridgets.swt/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidget.java b/org.eclipse.riena.ui.ridgets.swt/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidget.java
index d28b156..77f52c2 100644
--- a/org.eclipse.riena.ui.ridgets.swt/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidget.java
+++ b/org.eclipse.riena.ui.ridgets.swt/src/org/eclipse/riena/internal/ui/ridgets/swt/AbstractSWTRidget.java
@@ -24,8 +24,7 @@ import org.eclipse.swt.widgets.Widget;
*/
public abstract class AbstractSWTRidget extends AbstractRidget {
- private static final FocusListener FOCUS_MANAGER = new FocusManager();
-
+ private FocusListener focusManager = new FocusManager();
private Control uiControl;
private boolean visible;
private boolean focusable;
@@ -36,11 +35,11 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
* Checks that the given uiControl is assignable to the the given type.
*
* @param uiControl
- * a uiControl, may be null
+ * a uiControl, may be null
* @param type
- * a class instance (non-null)
+ * a class instance (non-null)
* @throws BindingException
- * if the uiControl is not of the given type
+ * if the uiControl is not of the given type
*/
public static void assertType(Object uiControl, Class<?> type) {
if ((uiControl != null) && !(type.isAssignableFrom(uiControl.getClass()))) {
@@ -58,12 +57,13 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
public void setUIControl(Object uiControl) {
checkUIControl(uiControl);
+ uninstallListeners();
unbindUIControl();
this.uiControl = (Control) uiControl;
updateVisible();
updateToolTip();
- updateFocusable();
bindUIControl();
+ installListeners();
}
public Control getUIControl() {
@@ -94,7 +94,6 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
public final void setFocusable(boolean focusable) {
if (this.focusable != focusable) {
this.focusable = focusable;
- updateFocusable();
}
}
@@ -131,14 +130,14 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
* Performs checks on the control about to be bound by this ridget.
* </p>
* <p>
- * Implementors must make sure the given <tt>uiControl</tt> has the expected
- * type.
+ * Implementors must make sure the given <tt>uiControl</tt> has the
+ * expected type.
* </p>
*
* @param uiControl
- * a {@link Widget} instance or null
+ * a {@link Widget} instance or null
* @throws BindingException
- * if the <tt>uiControl</tt> fails the check
+ * if the <tt>uiControl</tt> fails the check
*/
abstract protected void checkUIControl(Object uiControl);
@@ -165,6 +164,26 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
*/
abstract protected void unbindUIControl();
+ /**
+ * Adds listeners to the <tt>uiControl</tt> after it was bound to the
+ * ridget.
+ */
+ protected void installListeners() {
+ if (uiControl != null) {
+ uiControl.addFocusListener(focusManager);
+ }
+ }
+
+ /**
+ * Removes listeners from the <tt>uiControl</tt> when it is about to be
+ * unbound from the ridget.
+ */
+ protected void uninstallListeners() {
+ if (uiControl != null) {
+ uiControl.removeFocusListener(focusManager);
+ }
+ }
+
// helping methods
// ////////////////
@@ -180,45 +199,49 @@ public abstract class AbstractSWTRidget extends AbstractRidget {
}
}
- private void updateFocusable() {
- if (uiControl != null) {
- // ensure the listener is only added once
- uiControl.removeFocusListener(FOCUS_MANAGER);
- if (!isFocusable()) {
- uiControl.addFocusListener(FOCUS_MANAGER);
- }
- }
- }
-
/**
- * Prevents the widget corresponding to this ridget from getting the UI
- * focus.
+ * Focus listener that also prevents the widget corresponding to this ridget
+ * from getting the UI focus when the ridget is not focusable.
*
* @see AbstractSWTRidget#setFocusable(boolean).
*/
- private final static class FocusManager extends FocusAdapter {
+ private final class FocusManager extends FocusAdapter {
public void focusGained(FocusEvent e) {
- Control control = (Control) e.widget;
- Composite parent = control.getParent();
- Control[] tabList = parent.getTabList();
- int i = findNextElement(control, tabList);
- if (i != -1) {
- Control nextFocusControl = tabList[i];
- nextFocusControl.setFocus();
- } else { // no suitable control found, try one level up
- Composite pParent = parent.getParent();
- if (pParent != null) {
- tabList = pParent.getTabList();
- i = findNextElement(parent, tabList);
- if (i != -1) {
- Control nextFocusControl = tabList[i];
- nextFocusControl.setFocus();
+ if (focusable) {
+ fireFocusGained(new org.eclipse.riena.ui.ridgets.listener.FocusEvent(null, AbstractSWTRidget.this));
+ } else {
+ Control control = (Control) e.widget;
+ Composite parent = control.getParent();
+ Control[] tabList = parent.getTabList();
+ int i = findNextElement(control, tabList);
+ if (i != -1) {
+ Control nextFocusControl = tabList[i];
+ nextFocusControl.setFocus();
+ } else { // no suitable control found, try one level up
+ Composite pParent = parent.getParent();
+ if (pParent != null) {
+ tabList = pParent.getTabList();
+ i = findNextElement(parent, tabList);
+ if (i != -1) {
+ Control nextFocusControl = tabList[i];
+ nextFocusControl.setFocus();
+ }
}
}
}
}
+ /**
+ * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
+ */
+ @Override
+ public void focusLost(FocusEvent e) {
+ if (focusable) {
+ fireFocusLost(new org.eclipse.riena.ui.ridgets.listener.FocusEvent(AbstractSWTRidget.this, null));
+ }
+ }
+
private int findNextElement(Control control, Control[] controls) {
int myIndex = -1;
// find index for control