From 6f9c84969f1f82f9c25d76b4dc91c56ed79eb1bd Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 28 Nov 2019 16:20:32 +0100 Subject: [PATCH] fixes in indicator threshold validation --- .../sebserver/gbl/model/exam/Indicator.java | 11 +++++---- .../seb/sebserver/gui/content/ExamForm.java | 2 +- .../seb/sebserver/gui/form/FormBuilder.java | 5 +++- .../gui/form/ThresholdListBuilder.java | 23 +++++++++++++------ .../sebserver/gui/widget/ThresholdList.java | 16 ++++++++----- .../sebserver/gui/widget/WidgetFactory.java | 18 ++++++++++----- 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/Indicator.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/Indicator.java index 33fabfef..c145c73c 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/Indicator.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/exam/Indicator.java @@ -14,11 +14,12 @@ import java.util.List; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import org.apache.commons.lang3.StringUtils; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.EntityType; import ch.ethz.seb.sebserver.gbl.api.POSTMapper; import ch.ethz.seb.sebserver.gbl.model.Domain; @@ -163,11 +164,11 @@ public final class Indicator implements Entity { return new Indicator(null, exam.id, null, null, null, null); } - public static String getDisplayValue(final Indicator indicator, final Double value) { - if (value == null) { - return Constants.EMPTY_NOTE; + public static String getDisplayValue(final IndicatorType indicatorType, final Double value) { + if (value == null || indicatorType == null) { + return StringUtils.EMPTY; } - if (indicator.getType().integerValue) { + if (indicatorType.integerValue) { return String.valueOf(value.intValue()); } else { return String.valueOf(value.floatValue()); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java index 54949dc7..5c1cbfc4 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/ExamForm.java @@ -740,7 +740,7 @@ public class ExamForm implements TemplateComposer { ? "color: #4a4a4a; " : "color: #FFFFFF;") .append("'>") - .append(Indicator.getDisplayValue(indicator, threshold.value)) + .append(Indicator.getDisplayValue(indicator.type, threshold.value)) .append(" (") .append(threshold.color) .append(")") diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/form/FormBuilder.java b/src/main/java/ch/ethz/seb/sebserver/gui/form/FormBuilder.java index def47fc7..db14e2f8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/form/FormBuilder.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/form/FormBuilder.java @@ -260,7 +260,10 @@ public class FormBuilder { final LocTextKey label, final Indicator indicator) { - return new ThresholdListBuilder(name, label, indicator); + return new ThresholdListBuilder( + name, + label, + indicator.thresholds); } public static ImageUploadFieldBuilder imageUpload(final String name, final LocTextKey label, final String value) { diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/form/ThresholdListBuilder.java b/src/main/java/ch/ethz/seb/sebserver/gui/form/ThresholdListBuilder.java index f5b973a0..581705e9 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/form/ThresholdListBuilder.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/form/ThresholdListBuilder.java @@ -19,22 +19,22 @@ import org.eclipse.swt.widgets.Label; import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.model.Domain; -import ch.ethz.seb.sebserver.gbl.model.exam.Indicator; +import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.IndicatorType; import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.Threshold; import ch.ethz.seb.sebserver.gui.service.i18n.LocTextKey; import ch.ethz.seb.sebserver.gui.widget.ThresholdList; public class ThresholdListBuilder extends FieldBuilder> { - private final Indicator indicator; + private final Collection thresholds; protected ThresholdListBuilder( final String name, final LocTextKey label, - final Indicator indicator) { + final Collection thresholds) { - super(name, label, indicator.getThresholds()); - this.indicator = indicator; + super(name, label, thresholds); + this.thresholds = thresholds; } @Override @@ -46,15 +46,24 @@ public class ThresholdListBuilder extends FieldBuilder> { this.spanLabel); if (builder.readonly || this.readonly) { - // TODO do we need a read-only view for this? + // No read-only view needed for this so far? return; } else { final Composite fieldGrid = Form.createFieldGrid(builder.formParent, this.spanInput); + final ThresholdList thresholdList = builder.widgetFactory.thresholdList( fieldGrid, fieldGrid.getParent().getParent(), - this.indicator); + this.thresholds, + () -> { + try { + final String fieldValue = builder.form.getFieldValue(Domain.INDICATOR.ATTR_TYPE); + return IndicatorType.valueOf(fieldValue); + } catch (final Exception e) { + return null; + } + }); final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false); thresholdList.setLayoutData(gridData); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/widget/ThresholdList.java b/src/main/java/ch/ethz/seb/sebserver/gui/widget/ThresholdList.java index e3905bea..91c10933 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/widget/ThresholdList.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/widget/ThresholdList.java @@ -11,6 +11,7 @@ package ch.ethz.seb.sebserver.gui.widget; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -25,6 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.ethz.seb.sebserver.gbl.model.exam.Indicator; +import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.IndicatorType; import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.Threshold; import ch.ethz.seb.sebserver.gui.service.i18n.LocTextKey; import ch.ethz.seb.sebserver.gui.service.page.PageService; @@ -45,7 +47,7 @@ public final class ThresholdList extends Composite { private static final LocTextKey REMOVE_TEXT_KEY = new LocTextKey("sebserver.exam.indicator.thresholds.list.remove"); private final WidgetFactory widgetFactory; - private final Indicator indicator; + private final Supplier indicatorTypeSupplier; private final List thresholds = new ArrayList<>(); private final GridData valueCell; @@ -54,13 +56,13 @@ public final class ThresholdList extends Composite { private final Composite updateAnchor; ThresholdList( - final Indicator indicator, final Composite parent, final Composite updateAnchor, - final WidgetFactory widgetFactory) { + final WidgetFactory widgetFactory, + final Supplier indicatorTypeSupplier) { super(parent, SWT.NONE); - this.indicator = indicator; + this.indicatorTypeSupplier = indicatorTypeSupplier; this.updateAnchor = updateAnchor; this.widgetFactory = widgetFactory; super.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); @@ -137,7 +139,7 @@ public final class ThresholdList extends Composite { private void addThreshold(final Threshold threshold) { final Text valueInput = this.widgetFactory.numberInput( this, s -> { - if (this.indicator.getType().integerValue) { + if (this.indicatorTypeSupplier.get().integerValue) { Integer.parseInt(s); } else { Double.parseDouble(s); @@ -163,7 +165,9 @@ public final class ThresholdList extends Composite { if (threshold != null) { if (threshold.value != null) { - valueInput.setText(Indicator.getDisplayValue(this.indicator, threshold.value)); + valueInput.setText(Indicator.getDisplayValue( + this.indicatorTypeSupplier.get(), + threshold.value)); } if (threshold.color != null) { selector.select(threshold.color); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/widget/WidgetFactory.java b/src/main/java/ch/ethz/seb/sebserver/gui/widget/WidgetFactory.java index d86a0fbc..a5f52c1b 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/widget/WidgetFactory.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/widget/WidgetFactory.java @@ -40,7 +40,8 @@ import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; -import ch.ethz.seb.sebserver.gbl.model.exam.Indicator; +import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.IndicatorType; +import ch.ethz.seb.sebserver.gbl.model.exam.Indicator.Threshold; import ch.ethz.seb.sebserver.gbl.profile.GuiProfile; import ch.ethz.seb.sebserver.gbl.util.Tuple; import ch.ethz.seb.sebserver.gui.content.action.ActionDefinition; @@ -58,7 +59,7 @@ public class WidgetFactory { private static final Logger log = LoggerFactory.getLogger(WidgetFactory.class); - public static final int TEXT_AREA_INPUT_MIN_HEIGHT = 50; + public static final int TEXT_AREA_INPUT_MIN_HEIGHT = 100; public static final int TEXT_INPUT_MIN_HEIGHT = 24; public enum ImageIcon { @@ -605,11 +606,16 @@ public class WidgetFactory { public ThresholdList thresholdList( final Composite parent, final Composite updateAnchor, - final Indicator indicator) { + final Collection thresholds, + final Supplier indicatorTypeSupplier) { - final ThresholdList thresholdList = new ThresholdList(indicator, parent, updateAnchor, this); - if (indicator.thresholds != null) { - thresholdList.setThresholds(indicator.thresholds); + final ThresholdList thresholdList = new ThresholdList( + parent, + updateAnchor, + this, + indicatorTypeSupplier); + if (thresholds != null) { + thresholdList.setThresholds(thresholds); } return thresholdList; }