Skip to content

Using the style to format number in the swing UI. #52

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

Closed
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
16 changes: 16 additions & 0 deletions src/main/java/org/scijava/ui/swing/widget/SwingNumberWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ else if (model.isStyle(NumberWidget.SLIDER_STYLE)) {
final SpinnerNumberModel spinnerModel =
new SpinnerNumberModelFactory().createModel(value, min, max, stepSize);
spinner = new JSpinner(spinnerModel);
String format = getFormat(model);
if (format != null) {
spinner.setEditor(new JSpinner.NumberEditor(spinner, format));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have some sensible guessing on the format (based on min, max and/or default value) in case format is null here.

}

Dimension spinnerSize = spinner.getSize();
spinnerSize.width = 50;
spinner.setPreferredSize(spinnerSize);
Expand All @@ -132,6 +137,17 @@ else if (model.isStyle(NumberWidget.SLIDER_STYLE)) {
syncSliders();
}

private String getFormat(WidgetModel model) {
final String widgetStyle = model.getItem().getWidgetStyle();
String format = null;
for (final String s : widgetStyle.split(",")) {
if (s.startsWith("format:")) {
format = s.substring(s.indexOf(":") + 1).trim();
}
}
Comment on lines +143 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With scijava/scijava-common#384, I tried to centralize parsing of the style attribute into a WidgetStyle class (as proposed by @ctrueden) and to fix some related issues, such as inconsistent handling of spaces with style definitions.

To keep the code base as DRY as possible, I suggest relying on that WidgetStyle class instead of this getFormat implementation. The latter would be specific to scijava-ui-swing whereas the former would allow usage from any UI implementation.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imagejan , basically that does sound like a good idea, but that PR has been open since April. We would rather have this be merged and available sometime soon and when your widget style becomes available, start using it as of then.

We are however not sure what the release cycle is. Is there a chance we can get this code released earlier if we keep it contained to just ui-swing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged scijava/scijava-common#384 and released scijava-common 2.85.0. @sunsear @BoudewijnvanLangerak Could you please add <scijava-common.version>2.85.0</scijava-common.version> to the POM on this branch, and try to implement @imagejan's suggestions? I'll then try to get this PR merged quickly. I'm sorry I'm so slow merging PRs—I maintain too many things, and so the release cycle of everything suffers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With scijava/scijava-common#405, it should be possible to replace this getFormat() method by a call to:

WidgetStyle.getStyleModifier(model.getItem().getWidgetStyle(), "format")

return format;
}

// -- Typed methods --

@Override
Expand Down