-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added simple expression parsing in number fields.
SelectDouble, SelectInteger now use DelayedDocumentValidator which uses SimpleParser which uses exp4j
- Loading branch information
1 parent
755da53
commit 2281244
Showing
7 changed files
with
137 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/marginallyclever/donatello/select/DelayedDocumentValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.marginallyclever.donatello.select; | ||
|
||
import com.marginallyclever.donatello.simpleparser.SimpleParser; | ||
|
||
import javax.swing.*; | ||
import javax.swing.event.DocumentEvent; | ||
import javax.swing.event.DocumentListener; | ||
import javax.swing.text.JTextComponent; | ||
import java.awt.*; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* {@link DelayedDocumentValidator} validates the text in a number field using a {@link SimpleParser} so it can | ||
* handle simple math expressions. | ||
*/ | ||
public class DelayedDocumentValidator implements DocumentListener { | ||
private final JTextComponent field; | ||
private Timer timer=null; | ||
private final Consumer<Double> consumer; | ||
|
||
public DelayedDocumentValidator(JTextComponent field, Consumer<Double> consumer) { | ||
super(); | ||
this.field = field; | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public void changedUpdate(DocumentEvent arg0) { | ||
if(arg0.getLength()==0) return; | ||
validate(); | ||
} | ||
|
||
@Override | ||
public void insertUpdate(DocumentEvent arg0) { | ||
if(arg0.getLength()==0) return; | ||
validate(); | ||
} | ||
|
||
@Override | ||
public void removeUpdate(DocumentEvent arg0) { | ||
if(arg0.getLength()==0) return; | ||
validate(); | ||
} | ||
|
||
public void validate() { | ||
try { | ||
double newValue = SimpleParser.evaluate(field.getText()); | ||
field.setForeground(UIManager.getColor("Textfield.foreground")); | ||
field.setToolTipText(null); | ||
|
||
if(timer!=null) timer.cancel(); | ||
timer = new Timer("Delayed response"); | ||
timer.schedule(new TimerTask() { | ||
public void run() { | ||
consumer.accept(newValue); | ||
} | ||
}, 100L); // brief delay in case someone is typing fast | ||
} catch (IllegalArgumentException e) { | ||
field.setForeground(Color.RED); | ||
field.setToolTipText(e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/marginallyclever/donatello/simpleparser/SimpleParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.marginallyclever.donatello.simpleparser; | ||
|
||
import net.objecthunter.exp4j.Expression; | ||
import net.objecthunter.exp4j.ExpressionBuilder; | ||
import net.objecthunter.exp4j.function.Function; | ||
|
||
/** | ||
* A simple parser that evaluates a mathematical expression. Supports the use of the constant PI and the hypot function. | ||
*/ | ||
public class SimpleParser { | ||
private static final Function hypot = new Function("hypot",2) { | ||
@Override | ||
public double apply(double... doubles) { | ||
return Math.hypot(doubles[0], doubles[1]); | ||
} | ||
}; | ||
|
||
public static double evaluate(String expression) { | ||
Expression e = new ExpressionBuilder(expression) | ||
.variables("PI") // Declare the variable | ||
.function(hypot) | ||
.build() | ||
.setVariable("PI", Math.PI); // Assign value to variable | ||
return e.evaluate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/test/java/com/marginallyclever/donatello/simplerparser/SimpleParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.marginallyclever.donatello.simplerparser; | ||
|
||
import com.marginallyclever.donatello.simpleparser.SimpleParser; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SimpleParserTest { | ||
@Test | ||
public void test() { | ||
Assertions.assertEquals(-12,SimpleParser.evaluate("3 + 5 * (2 - 8) / 2.0")); | ||
Assertions.assertEquals(1,SimpleParser.evaluate("sin(PI / 2)")); // 1.0 | ||
Assertions.assertEquals(1,SimpleParser.evaluate("cos(0)")); // 1.0 | ||
Assertions.assertEquals(0.999999,SimpleParser.evaluate("tan(PI / 4)"),1e-6); | ||
Assertions.assertEquals(0.7615941559557649,SimpleParser.evaluate("tanh(1)"),1e-6); // Hyperbolic tangent | ||
Assertions.assertEquals(5,SimpleParser.evaluate("hypot(3, 4)")); // Pythagorean theorem | ||
Assertions.assertEquals(5,SimpleParser.evaluate("sqrt(25)")); | ||
Assertions.assertEquals(-2,SimpleParser.evaluate("-5 + 3")); | ||
Assertions.assertEquals(1,SimpleParser.evaluate("10 % 3")); | ||
Assertions.assertEquals(57.29577951308232,SimpleParser.evaluate("(180 / PI)"),1e-6); // Convert to degrees | ||
Assertions.assertEquals(0.017453292519943295,SimpleParser.evaluate("(PI / 180)"),1e-6); // Convert to radians | ||
Assertions.assertEquals(3,SimpleParser.evaluate("floor(PI)")); | ||
Assertions.assertEquals(4,SimpleParser.evaluate("ceil(PI)")); | ||
Assertions.assertThrows(IllegalArgumentException.class,()->SimpleParser.evaluate("random")); | ||
} | ||
} |