EitherNode

This commit is contained in:
Felix Homa 2022-03-27 23:18:20 +02:00
parent 7eb519f9b5
commit 75dcb2c2d4
Signed by: felix.homa
GPG Key ID: 43610F311720D3DA
5 changed files with 158 additions and 9 deletions

View File

@ -1,9 +1,11 @@
package de.tuDortmund.cs.rvs.pingger.korrekturHelper; package de.tuDortmund.cs.rvs.pingger.korrekturHelper;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
@ -28,11 +30,12 @@ public abstract class AbstractNode implements Node
{ {
main = new JPanel(true); main = new JPanel(true);
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createLineBorder(Color.GRAY));
content = new JPanel(new BorderLayout(), true); content = new JPanel(new BorderLayout(), true);
sub = new JPanel(true); sub = new JPanel(true);
sub.setLayout(new BoxLayout(sub, BoxLayout.Y_AXIS)); sub.setLayout(new BoxLayout(sub, BoxLayout.Y_AXIS));
sub.setBorder(new EmptyBorder(0, 8, 8, 0)); sub.setBorder(new EmptyBorder(0, 16, 4, 0));
main.add(content); main.add(content);
main.add(sub); main.add(sub);

View File

@ -10,11 +10,6 @@ import javax.swing.JCheckBox;
*/ */
public class CheckboxNode extends AbstractNode public class CheckboxNode extends AbstractNode
{ {
private static String ptsToString(BigDecimal p)
{
return (p.signum() > 0 ? "+" : "") + p.toPlainString();
}
private final JCheckBox cbx; private final JCheckBox cbx;
private final String message; private final String message;
@ -35,7 +30,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(ptsToString(points) + "P " + message); cbx = new JCheckBox(Utils.ptsToString(points) + "P " + message);
content.add(cbx); content.add(cbx);
} }
@ -64,14 +59,14 @@ public class CheckboxNode extends AbstractNode
@Override @Override
public String toConfigString() public String toConfigString()
{ {
return "[]\t" + ptsToString(points) + "\t" + message; return "[]\t" + Utils.ptsToString(points) + "\t" + message;
} }
@Override @Override
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()) + ">"
+ ptsToString(achievedPoints(hc.mc)) + "P</span>" + message + "</li>"; + Utils.ptsToString(achievedPoints(hc.mc)) + "P</span>" + message + "</li>";
} }
} }

View File

@ -0,0 +1,130 @@
package de.tuDortmund.cs.rvs.pingger.korrekturHelper;
import java.math.BigDecimal;
import java.math.MathContext;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
/**
* @author Pingger
*/
public class EitherNode extends AbstractNode
{
private final ButtonGroup bg;
private final JRadioButton btn_ok, btn_fail;
private final String messageFail;
private final String messageOK;
private final BigDecimal points;
/**
* @param config the config String
*/
public EitherNode(String config)
{
if (config.contains("\n"))
{ throw new IllegalArgumentException("Bad config! Found line feed!"); }
var spl = config.split("\t");
if (spl.length != 4 && spl.length != 5)
{ throw new IllegalArgumentException("Expected 2, 3 or 4 \\t!"); }
if (!"\\".equals(spl[0]))
{ throw new IllegalArgumentException("Expected \\"); }
if (!"|".equals(spl[3]))
{ throw new IllegalArgumentException("Expected | after 3rd \\t"); }
this.points = new BigDecimal(spl[1]);
messageOK = spl[2];
messageFail = spl.length == 3 ? null : spl.length == 4 ? spl[3].substring(1).trim() : spl[4];
if (points.signum() > 0)
{
btn_ok = new JRadioButton(Utils.ptsToString(points) + "P " + messageOK);
btn_fail = new JRadioButton(
Utils.ptsToString(BigDecimal.ZERO) + "P " + (messageFail == null ? messageOK : messageFail));
}
else
{
btn_ok = new JRadioButton(Utils.ptsToString(BigDecimal.ZERO) + "P " + messageOK);
btn_fail = new JRadioButton(Utils.ptsToString(points) + "P " + (messageFail == null ? messageOK : messageFail));
}
bg = new ButtonGroup();
bg.add(btn_ok);
bg.add(btn_fail);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(btn_ok);
content.add(btn_fail);
}
@Override
protected void _reset()
{
bg.clearSelection();
}
@Override
public BigDecimal achievedPoints(MathContext mc)
{
if (btn_ok.isSelected())
{
return switch (points.signum())
{
case -1 -> BigDecimal.ZERO;
case 1 -> points;
case 0 -> BigDecimal.ZERO;
default -> throw new IllegalArgumentException("Unexpected value: " + points.signum());
};
}
if (btn_fail.isSelected())
{
return switch (points.signum())
{
case 1 -> BigDecimal.ZERO;
case -1 -> points;
case 0 -> BigDecimal.ZERO;
default -> throw new IllegalArgumentException("Unexpected value: " + points.signum());
};
}
return BigDecimal.ZERO;
}
@Override
public boolean isVisibleInResultHtml()
{ return true; }
@Override
public BigDecimal maximumPoints()
{
return points.signum() < 0 ? BigDecimal.ZERO : points;
}
@Override
public String toConfigString()
{
return "\\\t" + points.toPlainString() + "\t" + messageOK + (messageFail == null ? "" : ("\t|\t" + messageFail));
}
@Override
public String toResultHtml(HtmlContext hc)
{
var signum = 0;
if (points.signum() < 0)
{
signum = achievedPoints(hc.mc).equals(BigDecimal.ZERO) ? 1 : -1;
}
else
{
signum = achievedPoints(hc.mc).equals(BigDecimal.ZERO) ? -1 : 1;
}
return "<li" + hc.styleText(signum) + ">" + //
"<span" + hc.stylePts(signum) + ">" + //
Utils.ptsToString(achievedPoints(hc.mc)) + //
(points.signum() < 0 ? "" : " / " + maximumPoints()) + "P" + //
"</span>" + //
(btn_fail.isSelected() ? btn_fail.getText() : btn_ok.getText()) + //
"</li>";
}
}

View File

@ -17,6 +17,7 @@ public class KorrekturHelper
var in = KorrekturHelper.class.getClassLoader() var in = KorrekturHelper.class.getClassLoader()
.getResourceAsStream("de/tuDortmund/cs/rvs/pingger/korrekturHelper/Test.schema"); .getResourceAsStream("de/tuDortmund/cs/rvs/pingger/korrekturHelper/Test.schema");
in.transferTo(System.out); in.transferTo(System.out);
in.close();
} }
} }

View File

@ -0,0 +1,20 @@
package de.tuDortmund.cs.rvs.pingger.korrekturHelper;
import java.math.BigDecimal;
/**
* @author Pingger
*/
public class Utils
{
/**
* @param p the {@link BigDecimal} to format
* @return a String for the given {@link BigDecimal}
*/
public static String ptsToString(BigDecimal p)
{
return (p.signum() > 0 ? "+" : "") + p.toPlainString();
}
}