Custom Decimals

This commit is contained in:
Felix Homa 2022-03-28 15:33:18 +02:00
parent d6c60bc31e
commit b1133e0aee
Signed by: felix.homa
GPG Key ID: 43610F311720D3DA
8 changed files with 30 additions and 17 deletions

View File

@ -2,7 +2,8 @@
// :base gibt die Start-Punkte an und überschreibt die maximal Punkt, wenn ungleich 0 // :base gibt die Start-Punkte an und überschreibt die maximal Punkt, wenn ungleich 0
:base 0.0 :base 0.0
// Count of decimal places
:decimals 1
// Design für Punkte. Pts ist dabei für die Punkte, PtsText für den Text. die Punkte zählen als Teil des Textes (<li style=PtsText><span style=Pts>'Punkte'</span>Text</li>) // Design für Punkte. Pts ist dabei für die Punkte, PtsText für den Text. die Punkte zählen als Teil des Textes (<li style=PtsText><span style=Pts>'Punkte'</span>Text</li>)
// full -> best-Case // full -> best-Case
:fullPts color: #0f0; font-weight: bold; :fullPts color: #0f0; font-weight: bold;

View File

@ -16,6 +16,8 @@ public class HtmlContext
public String cbmsFail = ""; public String cbmsFail = "";
/** Style for checked Boxes in CheckboxMultiSelect */ /** Style for checked Boxes in CheckboxMultiSelect */
public String cbmsOk = ""; public String cbmsOk = "";
/** Number of Decimals on Points */
public int decimals = 1;
/** Style for the Points, when the best case Points have been achieved */ /** Style for the Points, when the best case Points have been achieved */
public String fullPtsStyle = ""; public String fullPtsStyle = "";
/** Style for the Text, when the best case Points have been achieved */ /** Style for the Text, when the best case Points have been achieved */
@ -24,6 +26,7 @@ public class HtmlContext
public MathContext mc = MathContext.DECIMAL32; public MathContext mc = MathContext.DECIMAL32;
/** Style for the Points, when the worst case Points have been achieved */ /** Style for the Points, when the worst case Points have been achieved */
public String noPtsStyle = ""; public String noPtsStyle = "";
/** Style for the Text, when the best case Points have been achieved */ /** Style for the Text, when the best case Points have been achieved */
public String noPtsTextStyle = ""; public String noPtsTextStyle = "";

View File

@ -200,6 +200,10 @@ public class KorrekturHelper
hc.basePoints = new BigDecimal(split[1]); hc.basePoints = new BigDecimal(split[1]);
break; break;
case ":decimals":
hc.decimals = Integer.parseInt(split[1]);
break;
case ":fullPts": case ":fullPts":
hc.fullPtsStyle = split.length > 1 ? split[1] : ""; hc.fullPtsStyle = split.length > 1 ? split[1] : "";
break; break;

View File

@ -11,12 +11,13 @@ public class Utils
/** /**
* formats Points for Users * formats Points for Users
* *
* @param bd the {@link BigDecimal} to format * @param bd the {@link BigDecimal} to format
* @param decimals decimals
* @return the formatted String (1 decimal place) * @return the formatted String (1 decimal place)
*/ */
public static String formatPoints(BigDecimal bd) public static String formatPoints(BigDecimal bd, int decimals)
{ {
return String.format("%.1f", bd.doubleValue()); return String.format("%." + decimals + "f", bd.doubleValue());
} }
/** /**

View File

@ -58,7 +58,7 @@ public class CheckboxMultiSelectNode extends AbstractNode
{ throw new IllegalArgumentException("Expected []"); } { throw new IllegalArgumentException("Expected []"); }
points[i - 1] = new BigDecimal(s[1]); points[i - 1] = new BigDecimal(s[1]);
text[i - 1] = s[2]; text[i - 1] = s[2];
rbs[i - 1] = new JCheckBox(Utils.formatPoints(points[i - 1]) + "P - " + text[i - 1]); rbs[i - 1] = new JCheckBox(Utils.formatPoints(points[i - 1], 2) + "P - " + text[i - 1]);
rbs[i - 1].addActionListener(cl); rbs[i - 1].addActionListener(cl);
content.add(rbs[i - 1]); content.add(rbs[i - 1]);
} }
@ -124,7 +124,8 @@ public class CheckboxMultiSelectNode extends AbstractNode
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.append("<li").append(hc.styleText(achievedPoints(hc.mc), maxPoints)).append(">"); sb.append("<li").append(hc.styleText(achievedPoints(hc.mc), maxPoints)).append(">");
sb.append("<span").append(hc.stylePts(achievedPoints(hc.mc), maxPoints)).append(">"); sb.append("<span").append(hc.stylePts(achievedPoints(hc.mc), maxPoints)).append(">");
sb.append(Utils.formatPoints(achievedPoints(hc.mc))).append(" / ").append(Utils.formatPoints(maxPoints)); sb.append(Utils.formatPoints(achievedPoints(hc.mc), hc.decimals)).append(" / ")
.append(Utils.formatPoints(maxPoints, hc.decimals));
sb.append("P</span> "); sb.append("P</span> ");
sb.append(baseMsg); sb.append(baseMsg);
sb.append("\n\t<ul style=\"list-style-type: none;margin-top: 0px;margin-bottom:0px\">\n"); sb.append("\n\t<ul style=\"list-style-type: none;margin-top: 0px;margin-bottom:0px\">\n");
@ -132,7 +133,7 @@ public class CheckboxMultiSelectNode extends AbstractNode
{ {
sb.append("\t\t<li").append(hc.cbx(rbs[i])).append(">"); sb.append("\t\t<li").append(hc.cbx(rbs[i])).append(">");
sb.append(rbs[i].isSelected() ? "&checkmark; " : "&cross; "); sb.append(rbs[i].isSelected() ? "&checkmark; " : "&cross; ");
sb.append("(").append(Utils.formatPoints(points[i])).append("P) "); sb.append("(").append(Utils.formatPoints(points[i], hc.decimals)).append("P) ");
sb.append(text[i]); sb.append(text[i]);
sb.append("</li>\n"); sb.append("</li>\n");
} }

View File

@ -33,7 +33,7 @@ public class CheckboxNode extends AbstractNode
{ throw new IllegalArgumentException("Not a [] Node"); } { throw new IllegalArgumentException("Not a [] Node"); }
points = new BigDecimal(spl[1]); points = new BigDecimal(spl[1]);
message = spl[2]; message = spl[2];
cbx = new JCheckBox(Utils.formatPoints(points) + "P " + message); cbx = new JCheckBox(Utils.formatPoints(points, 2) + "P " + message);
cbx.addActionListener(e -> onChange(this)); cbx.addActionListener(e -> onChange(this));
content.add(cbx); content.add(cbx);
} }
@ -70,7 +70,7 @@ public class CheckboxNode extends AbstractNode
public String toResultHtml(HtmlContext hc) public String toResultHtml(HtmlContext hc)
{ {
return "<li" + hc.styleText(points.signum()) + "><span" + hc.stylePts(points.signum()) + ">" return "<li" + hc.styleText(points.signum()) + "><span" + hc.stylePts(points.signum()) + ">"
+ Utils.formatPoints(achievedPoints(hc.mc)) + "P</span> " + message + "</li>"; + Utils.formatPoints(achievedPoints(hc.mc), hc.decimals) + "P</span> " + message + "</li>";
} }
} }

View File

@ -42,14 +42,15 @@ public class EitherNode extends AbstractNode
messageFail = spl.length == 3 ? null : spl.length == 4 ? spl[3].substring(1).trim() : spl[4]; messageFail = spl.length == 3 ? null : spl.length == 4 ? spl[3].substring(1).trim() : spl[4];
if (points.signum() > 0) if (points.signum() > 0)
{ {
btn_ok = new JRadioButton(Utils.formatPoints(points) + "P " + messageOK); btn_ok = new JRadioButton(Utils.formatPoints(points, 2) + "P " + messageOK);
btn_fail = new JRadioButton( btn_fail = new JRadioButton(
Utils.ptsToString(BigDecimal.ZERO) + "P " + (messageFail == null ? messageOK : messageFail)); Utils.ptsToString(BigDecimal.ZERO) + "P " + (messageFail == null ? messageOK : messageFail));
} }
else else
{ {
btn_ok = new JRadioButton(Utils.formatPoints(BigDecimal.ZERO) + "P " + messageOK); btn_ok = new JRadioButton(Utils.formatPoints(BigDecimal.ZERO, 2) + "P " + messageOK);
btn_fail = new JRadioButton(Utils.formatPoints(points) + "P " + (messageFail == null ? messageOK : messageFail)); btn_fail = new JRadioButton(
Utils.formatPoints(points, 2) + "P " + (messageFail == null ? messageOK : messageFail));
} }
btn_ok.addActionListener(e -> onChange(this)); btn_ok.addActionListener(e -> onChange(this));
btn_fail.addActionListener(e -> onChange(this)); btn_fail.addActionListener(e -> onChange(this));
@ -127,8 +128,8 @@ public class EitherNode extends AbstractNode
} }
return "<li" + hc.styleText(signum) + ">" + // return "<li" + hc.styleText(signum) + ">" + //
"<span" + hc.stylePts(signum) + ">" + // "<span" + hc.stylePts(signum) + ">" + //
Utils.formatPoints(achievedPoints(hc.mc)) + // Utils.formatPoints(achievedPoints(hc.mc), hc.decimals) + //
(points.signum() < 0 ? "" : " / " + Utils.formatPoints(maximumPoints())) + "P" + // (points.signum() < 0 ? "" : " / " + Utils.formatPoints(maximumPoints(), hc.decimals)) + "P" + //
"</span> " + // "</span> " + //
(points.signum() < 0 ? btn_fail.isSelected() ? messageOK : messageFail (points.signum() < 0 ? btn_fail.isSelected() ? messageOK : messageFail
: btn_fail.isSelected() ? messageFail : messageOK) : btn_fail.isSelected() ? messageFail : messageOK)

View File

@ -60,7 +60,7 @@ public class RadioMultiSelectNode extends AbstractNode
{ throw new IllegalArgumentException("Expected exactly 1 \\t"); } { throw new IllegalArgumentException("Expected exactly 1 \\t"); }
points[i - 1] = new BigDecimal(s[0]); points[i - 1] = new BigDecimal(s[0]);
text[i - 1] = s[1]; text[i - 1] = s[1];
rbs[i - 1] = new JRadioButton(Utils.formatPoints(points[i - 1]) + "P - " + text[i - 1]); rbs[i - 1] = new JRadioButton(Utils.formatPoints(points[i - 1], 2) + "P - " + text[i - 1]);
rbs[i - 1].addActionListener(cl); rbs[i - 1].addActionListener(cl);
content.add(rbs[i - 1]); content.add(rbs[i - 1]);
bg.add(rbs[i - 1]); bg.add(rbs[i - 1]);
@ -131,12 +131,14 @@ public class RadioMultiSelectNode extends AbstractNode
{ {
return "<li" + hc.styleText(BigDecimal.ZERO, maxPoints) + ">" + // return "<li" + hc.styleText(BigDecimal.ZERO, maxPoints) + ">" + //
"<span" + hc.stylePts(achievedPoints(hc.mc), maxPoints) + ">" + // "<span" + hc.stylePts(achievedPoints(hc.mc), maxPoints) + ">" + //
Utils.formatPoints(BigDecimal.ZERO) + " / " + Utils.formatPoints(maxPoints) + "P</span> " + // Utils.formatPoints(BigDecimal.ZERO, hc.decimals) + " / " + Utils.formatPoints(maxPoints, hc.decimals)
+ "P</span> " + //
(baseMsg == null ? "" : baseMsg) + "</li>"; (baseMsg == null ? "" : baseMsg) + "</li>";
} }
return "<li" + hc.styleText(achievedPoints(hc.mc), maxPoints) + ">" + // return "<li" + hc.styleText(achievedPoints(hc.mc), maxPoints) + ">" + //
"<span" + hc.stylePts(achievedPoints(hc.mc), maxPoints) + "> " + // "<span" + hc.stylePts(achievedPoints(hc.mc), maxPoints) + "> " + //
Utils.formatPoints(achievedPoints(hc.mc)) + " / " + Utils.formatPoints(maxPoints) + "P</span> " + // Utils.formatPoints(achievedPoints(hc.mc), hc.decimals) + " / " + Utils.formatPoints(maxPoints, hc.decimals)
+ "P</span> " + //
(baseMsg == null ? text[getSelected()] : baseMsg + " (" + text[getSelected()] + ")") + "</li>"; (baseMsg == null ? text[getSelected()] : baseMsg + " (" + text[getSelected()] + ")") + "</li>";
} }