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 committed Mar 24, 2024
1 parent 8622402 commit 81bba97
Showing 1 changed file with 26 additions and 41 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 @@ -179,30 +179,26 @@ private void drawTriangle() {
}

private void addMouseDownListener() {
super.addListener(SWT.MouseDown, new Listener() {

@Override
public void handleEvent(final Event event) {
if (shouldShowMenu(event.x, event.y)) {
final Button button = (Button) event.widget;
final Rectangle rect = button.getBounds();
final Point p = button.toDisplay(rect.x, rect.y + rect.height);
getMenu().setLocation(p.x - rect.x, p.y - rect.y);
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;
listener.handleEvent(evt);
}
super.addListener(SWT.MouseDown, event -> {
boolean shouldShowMenu = event.x >= splitButtonAreaLeft && event.y >= splitButtonAreaTop
&& event.x <= splitButtonAreaRight && event.y <= splitButtonAreaBottom;
if (shouldShowMenu) {
final Button button = (Button) event.widget;
final Rectangle rect = button.getBounds();
final Point p = button.toDisplay(rect.x, rect.y + rect.height);
getMenu().setLocation(p.x - rect.x, p.y - rect.y);
getMenu().setVisible(true);
} else {
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));
}
}

private boolean shouldShowMenu(final int x, final int y) {
return x >= splitButtonAreaLeft && y >= splitButtonAreaTop && x <= splitButtonAreaRight && y <= splitButtonAreaBottom;
}
});
}
Expand Down Expand Up @@ -240,8 +236,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 +266,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 +302,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 +339,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 81bba97

Please sign in to comment.