Skip to content

Commit

Permalink
Rework SplitButton to not use SWT's internal TypedListener
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell authored and merks committed Apr 10, 2024
1 parent 44150cc commit 59eb85c
Showing 1 changed file with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*******************************************************************************/
package org.eclipse.nebula.widgets.splitbutton;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
Expand All @@ -33,7 +33,6 @@
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.TypedListener;
import org.eclipse.swt.widgets.Widget;

/**
Expand All @@ -57,7 +56,8 @@ public class SplitButton extends Button {

private static final int EXTRA_WIDTH = 60;

private final List<Listener> listeners = new LinkedList<>();
private final List<Listener> selectionListeners = new LinkedList<>();
private final List<SelectionListener> typedSelectionListeners = new LinkedList<>();

private int splitButtonAreaLeft = -1;
private int splitButtonAreaTop = -1;
Expand Down Expand Up @@ -191,13 +191,16 @@ public void handleEvent(final Event event) {
getMenu().setVisible(true);

} else {
for (final Listener listener : listeners) {
final Event evt = new Event();
evt.widget = SplitButton.this;
evt.display = getDisplay();
evt.type = SWT.Selection;
Event evt = new Event();
evt.widget = SplitButton.this;
evt.display = getDisplay();
evt.type = SWT.Selection;
for (Listener listener : selectionListeners) {
listener.handleEvent(evt);
}
for (SelectionListener listener : typedSelectionListeners) {
listener.widgetSelected(new SelectionEvent(evt));
}
}
}

Expand Down Expand Up @@ -240,8 +243,7 @@ public void addSelectionListener(SelectionListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
final TypedListener typedListener = new TypedListener(listener);
listeners.add(typedListener);
typedSelectionListeners.add(listener);
}

/**
Expand Down Expand Up @@ -271,17 +273,7 @@ public void removeSelectionListener(SelectionListener listener) {
if (listener == null) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}

final Iterator<Listener> it = listeners.iterator();
while (it.hasNext()) {
final Listener current = it.next();
if (current instanceof TypedListener) {
final TypedListener tl = (TypedListener) current;
if (tl.getEventListener() != null && tl.getEventListener().equals(listener)) {
it.remove();
}
}
}
typedSelectionListeners.removeIf(listener::equals);
}

/**
Expand Down Expand Up @@ -317,7 +309,7 @@ public void addListener(int eventType, Listener listener) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (eventType == SWT.Selection) {
listeners.add(listener);
selectionListeners.add(listener);
return;
}
super.addListener(eventType, listener);
Expand Down Expand Up @@ -354,7 +346,7 @@ public void removeListener(int eventType, Listener listener) {
SWT.error(SWT.ERROR_NULL_ARGUMENT);
}
if (eventType == SWT.Selection) {
listeners.add(listener);
selectionListeners.remove(listener);
return;
}
super.removeListener(eventType, listener);
Expand Down

0 comments on commit 59eb85c

Please sign in to comment.