Skip to content

Commit ab331a6

Browse files
committed
Add support for the default selection to the slider
Currently the slider fires an update event on each change, but this can be not desired e.g because changing the value might be an expensive operation, or even the user chooses the same value in the end. This now captures the current value on mouse down and if it has changed on mouseup fires a default selection event. This way a user of the slider can always decide what approach works best or even combine the different approaches. For example one can use the fast changing value to update ui-elements and the default selection to persist the value to disk/database/preferences/...
1 parent ee2548d commit ab331a6

File tree

2 files changed

+24
-2
lines changed
  • widgets
    • nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider
    • opal/commons/org.eclipse.nebula.widgets.opal.commons/src/org/eclipse/nebula/widgets/opal/commons

2 files changed

+24
-2
lines changed

widgets/nebulaslider/org.eclipse.nebula.widgets.nebulaslider/src/org/eclipse/nebula/widgets/opal/nebulaslider/NebulaSlider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class NebulaSlider extends Canvas {
5151

5252
private IntFunction<String> format;
5353

54+
private int movingValue;
55+
5456
/**
5557
* Constructs a new instance of this class given its parent and a style value
5658
* describing its behavior and appearance.
@@ -229,12 +231,16 @@ private void addMouseListeners() {
229231
return;
230232
}
231233
moving = true;
234+
movingValue = value;
232235
mouseDeltaX = xPosition - e.x;
233236
});
234237

235238
addListener(SWT.MouseUp, e -> {
236239
moving = false;
237240
mouseDeltaX = 0;
241+
if(movingValue != value) {
242+
SelectionListenerUtil.fireDefaultSelectionListeners(this, e);
243+
}
238244
redraw();
239245
});
240246

widgets/opal/commons/org.eclipse.nebula.widgets.opal.commons/src/org/eclipse/nebula/widgets/opal/commons/SelectionListenerUtil.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,23 @@ public static void removeSelectionListener(final Control control, final Selectio
6565
* @return true if the selection could be changed, false otherwise
6666
*/
6767
public static boolean fireSelectionListeners(final Control control, final Event sourceEvent) {
68-
for (final Listener listener : control.getListeners(SWT.Selection)) {
68+
return fireSelectionListenersEvent(control, sourceEvent, SWT.Selection);
69+
}
70+
71+
/**
72+
* Fire the default selection listeners of a given control
73+
*
74+
* @param control the control that fires the event
75+
* @param sourceEvent mouse event
76+
* @return true if the selection could be changed, false otherwise
77+
*/
78+
public static boolean fireDefaultSelectionListeners(final Control control, final Event sourceEvent) {
79+
return fireSelectionListenersEvent(control, sourceEvent, SWT.DefaultSelection);
80+
}
81+
82+
private static boolean fireSelectionListenersEvent(final Control control, final Event sourceEvent, int type) {
83+
Listener[] listeners = control.getListeners(SWT.Selection);
84+
for(final Listener listener : listeners) {
6985
final Event event = new Event();
7086

7187
event.button = sourceEvent==null?1:sourceEvent.button;
@@ -76,7 +92,7 @@ public static boolean fireSelectionListeners(final Control control, final Event
7692
event.time = sourceEvent == null ? 0 : sourceEvent.time;
7793
event.x = sourceEvent == null ? 0 : sourceEvent.x;
7894
event.y = sourceEvent == null ? 0 : sourceEvent.y;
79-
event.type = SWT.Selection;
95+
event.type = type;
8096

8197
listener.handleEvent(event);
8298
if (!event.doit) {

0 commit comments

Comments
 (0)