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

Add support to the slider to format the value #574

Merged
merged 1 commit into from
Mar 15, 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 @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.nebula.widgets.opal.nebulaslider;

import java.util.function.IntFunction;

import org.eclipse.nebula.widgets.opal.commons.SelectionListenerUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
Expand Down Expand Up @@ -47,6 +49,8 @@ public class NebulaSlider extends Canvas {

private boolean moving = false;

private IntFunction<String> format;

/**
* Constructs a new instance of this class given its parent and a style value
* describing its behavior and appearance.
Expand Down Expand Up @@ -181,7 +185,7 @@ private void drawSelector(final GC gc) {
// And the value
gc.setForeground(renderer.getSelectorTextColor());
gc.setFont(renderer.getTextFont());
final String valueAsString = String.valueOf(value);
final String valueAsString = stringValueOf(value);
final Point textSize = gc.textExtent(valueAsString);

final int xText = hMargin + xPosition + selectorWidth / 2;
Expand All @@ -190,6 +194,29 @@ private void drawSelector(final GC gc) {
gc.drawText(valueAsString, xText - textSize.x / 2, yText - textSize.y / 2, true);
}

private String stringValueOf(int value) {
if(format != null) {
return format.apply(value);
}
return String.valueOf(value);
}

/**
* Set the format that should be used to format the given number to a string.
*
* @param format
* the format or <code>null</code> to use the default format.
*/
public void setLabelFormatProvider(IntFunction<String> format) {
checkWidget();
this.format = format;
}

public IntFunction<String> getLabelFormatProvider() {
checkWidget();
return format;
}

private void addMouseListeners() {

addListener(SWT.MouseDown, e -> {
Expand Down
Loading