Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework SplitButton to not use SWT's internal TypedListener #582

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading