Skip to content

Commit

Permalink
Bug Fix: Formula columns not working with infinity decimals like 1.33…
Browse files Browse the repository at this point in the history
…3... AGAIN! (Issue #268)
  • Loading branch information
GoldenGnu committed Sep 24, 2021
1 parent 0207516 commit d82b413
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
Expand Up @@ -60,6 +60,8 @@

public class JFormulaDialog<T extends Enum<T> & EnumTableColumn<Q>, Q> extends JDialogCentered {

public static MathContext FORMULA_PRECISION = MathContext.DECIMAL64; //64bit == Double size

private enum FormulaDialogAction {
OK, CANCEL
}
Expand All @@ -78,16 +80,16 @@ public JFormulaDialog(Program program, ColumnManager<T, Q> columnManager) {
super(program, GuiShared.get().formulaTitle(), Images.MISC_FORMULA.getImage());

this.columnManager = columnManager;

ListenerClass listener = new ListenerClass();

JLabel jNameLabel = new JLabel(GuiShared.get().formulaName());

jName = new JTextField();
jName.addCaretListener(listener);

JLabel jFormulaLabel = new JLabel(GuiShared.get().formulaString());

jFormula = new JTextField();
jFormula.addCaretListener(listener);

Expand All @@ -113,7 +115,7 @@ public void actionPerformed(ActionEvent e) {
}
JDropDownButton jFunctions = new JDropDownButton(GuiShared.get().formulaFunctions());

ExpressionValues expression = new ExpressionValues("");
ExpressionValues expression = new ExpressionValues();
for (LazyFunction function : expression.getFunctions()) {
StringBuilder builder = new StringBuilder();
for (int i = 1; i < function.getNumParams(); i++) {
Expand Down Expand Up @@ -198,7 +200,6 @@ public void actionPerformed(ActionEvent e) {
)
)
)

);
layout.setVerticalGroup(
layout.createSequentialGroup()
Expand Down Expand Up @@ -248,6 +249,7 @@ public Formula edit(Formula formula) {
setVisible(true);
return returnValue;
}

public Formula add() {
reset("", "");
setVisible(true);
Expand Down Expand Up @@ -306,7 +308,7 @@ public static String replaceAll(EnumTableColumn<?> enumColumn, String text) {
}

private Expression getExpression() {
return new Expression(getExpressionString(), MathContext.DECIMAL64); //64bit == Double size
return new Expression(getExpressionString(), FORMULA_PRECISION);
}

private boolean isFomulaValid() {
Expand Down Expand Up @@ -356,25 +358,26 @@ private void validate() {
}

private class ListenerClass implements ActionListener, CaretListener {

@Override
public void actionPerformed(final ActionEvent e) {
if (FormulaDialogAction.OK.name().equals(e.getActionCommand())) {
save();
} else if (FormulaDialogAction.CANCEL.name().equals(e.getActionCommand())) {
setVisible(false);
}
}
}

@Override
public void caretUpdate(CaretEvent e) {
validate();
}
}

public static class ExpressionValues extends Expression {
public ExpressionValues(String expression) {
super(expression);
private static class ExpressionValues extends Expression {

public ExpressionValues() {
super("", FORMULA_PRECISION);
}

public Collection<LazyOperator> getOperators() {
Expand All @@ -396,7 +399,7 @@ public static class Formula {
private Integer index;

public Formula(String columnName, String expressionString, Integer index) {
this.expression = new Expression(expressionString, MathContext.UNLIMITED);
this.expression = new Expression(expressionString, FORMULA_PRECISION);
this.columnName = columnName;
this.index = index;
this.usedVariables = expression.getUsedVariables();
Expand Down Expand Up @@ -463,6 +466,5 @@ public boolean equals(Object obj) {
}
return true;
}

}
}
Expand Up @@ -21,7 +21,6 @@
package net.nikr.eve.jeveasset.gui.shared.menu;

import com.udojava.evalex.Expression;
import java.math.MathContext;
import java.util.HashSet;
import net.nikr.eve.jeveasset.TestUtil;
import net.nikr.eve.jeveasset.gui.shared.table.EnumTableFormatAdaptor;
Expand Down Expand Up @@ -57,12 +56,12 @@ public void testExpressionException() {

private void eval(String eval) {
//Re-use expression
Expression expression = new Expression(eval, MathContext.UNLIMITED);
Expression expression = new Expression(eval, JFormulaDialog.FORMULA_PRECISION);
JFormulaDialog.safeEval(new HashSet<>(), expression);
EnumTableFormatAdaptor.safeEval(expression);
//One time use
JFormulaDialog.safeEval(new HashSet<>(), new Expression(eval, MathContext.UNLIMITED));
EnumTableFormatAdaptor.safeEval(new Expression(eval, MathContext.UNLIMITED));
JFormulaDialog.safeEval(new HashSet<>(), new Expression(eval, JFormulaDialog.FORMULA_PRECISION));
EnumTableFormatAdaptor.safeEval(new Expression(eval, JFormulaDialog.FORMULA_PRECISION));
}

}

0 comments on commit d82b413

Please sign in to comment.