Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3607 added general purpose validating floating point number input text field

ghidragon1317d35fa185
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/FloatingPointTextField.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/FloatingPointTextField.java
new file mode 100644
index 0000000000..2d317a50bd
--- /dev/null
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/textfield/FloatingPointTextField.java
@@ -0,0 +1,173 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package docking.widgets.textfield;
+
+import javax.swing.JTextField;
+import javax.swing.text.*;
+
+/**
+ * A simple text field for inputing floating point numbers. The field is continuously validated so
+ * that only valid characters and values can be entered. If the text is blank or contains only "-",
+ * ".", or "-.", the value is considered to be 0. You can optionally set a min and max value. In
+ * order for the continuous validation to work, the max must be a non-negative number and the min
+ * must be a non-positive number.
+ */
+public class FloatingPointTextField extends JTextField {
+ private Double minValue = Double.NEGATIVE_INFINITY;
+ private Double maxValue = Double.POSITIVE_INFINITY;
+
+ /**
+ * Constructs a new empty FloatingPointTextField.
+ * @param columns the number of columns for determining the preferred width
+ */
+ public FloatingPointTextField(int columns) {
+ super(columns);
+ AbstractDocument doc = (AbstractDocument) getDocument();
+ doc.setDocumentFilter(new FloatingPointDocumentFilter());
+ }
+
+ /**
+ * Constructs a new FloatingPointTextField initialized with the given value.
+ * @param columns the number of columns for determining the preferred width
+ */
+ public FloatingPointTextField(int columns, double initialValue) {
+ this(columns);
+ setValue(initialValue);
+ }
+
+ /**
+ * Returns the value represented by the text in the field. If the field only contains "-",".",
+ * or "-.", the value returned will be 0.
+ * @return the value represented by the text in the field
+ */
+ public double getValue() {
+ return parseDouble(getText());
+ }
+
+ /**
+ * Sets the text in the field to the given value.
+ * @param value the value to display in the text field
+ */
+ public void setValue(double value) {
+ setText(Double.toString(value));
+ }
+
+ /**
+ * Sets the maximum allowed value. The max must be 0 or positive so that continuous validation
+ * can work.
+ * @param max the maximum allowed value.
+ */
+ public void setMaxValue(double max) {
+ if (max < 0.0) {
+ throw new IllegalArgumentException("Max value can not be negative!");
+ }
+ maxValue = max;
+ }
+
+ /**
+ * Sets the minimum allowed value. The min must be 0 or negative so that continuous validation
+ * can work.
+ * @param min the minimum allowed value.
+ */
+ public void setMinValue(double min) {
+ if (min > 0.0) {
+ throw new IllegalArgumentException("Min value can not be positive!");
+ }
+ minValue = min;
+ }
+
+ private double parseDouble(String text) {
+ if (text.equals("")) {
+ text = "0";
+ }
+ else if (text.equals("-")) {
+ text = "-0"; // will parse a negative 0, which compares less than 0. Prevents
+ // allowing user to type in a "-" char when the minimum is set to 0
+ }
+ else if (text.equals(".")) {
+ text = "0";
+ }
+ else if (text.equals("-.")) {
+ text = "-0";
+ }
+ // because the field only allows valid values, we don't have to check for an exception
+ return Double.parseDouble(text);
+
+ }
+
+ private class FloatingPointDocumentFilter extends DocumentFilter {
+ // optional single "-", followed by 0 or more digits, followed by an optional single ".",
+ // followed by 0 or more digits
+ private static final String FLOATING_POINT_REGEX = "-?[0-9]*\\.?[0-9]*";
+
+ @Override
+ public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr)
+ throws BadLocationException {
+ text = text.trim();
+ StringBuilder builder = getText(fb);
+ builder.insert(offset, text);
+ if (isValid(builder)) {
+ super.insertString(fb, offset, text, attr);
+ }
+ }
+
+ @Override
+ public void replace(FilterBypass fb, int offset, int length, String text,
+ AttributeSet attrs) throws BadLocationException {
+ text = text.trim();
+ StringBuilder builder = getText(fb);
+ builder.replace(offset, offset + length, text);
+ if (isValid(builder)) {
+ super.replace(fb, offset, length, text, attrs);
+ }
+ }
+
+ @Override
+ public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
+ StringBuilder builder = getText(fb);
+ builder.delete(offset, offset + length);
+ if (isValid(builder)) {
+ super.remove(fb, offset, length);
+ }
+ }
+
+ private boolean isValid(StringBuilder builder) {
+ if (builder.isEmpty()) {
+ return true;
+ }
+ String value = builder.toString();
+ if (!value.matches(FLOATING_POINT_REGEX)) {
+ return false;
+ }
+
+ double d = parseDouble(value);
+ if (minValue.compareTo(d) > 0) {
+ return false;
+ }
+ if (maxValue.compareTo(d) < 0) {
+ return false;
+ }
+ return true;
+ }
+
+ private StringBuilder getText(FilterBypass fb) throws BadLocationException {
+ StringBuilder builder = new StringBuilder();
+ Document document = fb.getDocument();
+ builder.append(document.getText(0, document.getLength()));
+ return builder;
+ }
+ }
+}
diff --git a/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/FloatingPointTextFieldTest.java b/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/FloatingPointTextFieldTest.java
new file mode 100644
index 0000000000..59bbba3c38
--- /dev/null
+++ b/Ghidra/Framework/Docking/src/test.slow/java/docking/widgets/textfield/FloatingPointTextFieldTest.java
@@ -0,0 +1,164 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package docking.widgets.textfield;
+
+import static org.junit.Assert.*;
+
+import javax.swing.JFrame;
+import javax.swing.UIManager;
+
+import org.junit.*;
+
+import docking.test.AbstractDockingTest;
+
+public class FloatingPointTextFieldTest extends AbstractDockingTest {
+
+ private JFrame frame;
+ private FloatingPointTextField field;
+
+ @Before
+ public void setUp() throws Exception {
+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+ field = new FloatingPointTextField(10);
+ frame = new JFrame("Test");
+ frame.getContentPane().add(field);
+ frame.pack();
+ frame.setVisible(true);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ frame.setVisible(false);
+ }
+
+ @Test
+ public void testDefaultState() {
+ assertEquals("", field.getText());
+ assertEquals(0.0d, field.getValue(), 0.00001d);
+ }
+
+ @Test
+ public void testTypeValidPositiveFloatingPointNumber() {
+ triggerText(field, "123.456");
+ assertEquals(123.456d, field.getValue(), 0.0001d);
+ }
+
+ @Test
+ public void testTypeValidNegativeFloatingPointNumber() {
+ triggerText(field, "-342.312");
+ assertEquals(-342.312d, field.getValue(), 0.0001d);
+ }
+
+ @Test
+ public void testTypeValidHexNumber() {
+ triggerText(field, "1.2abcdef");
+ assertEquals("1.2", field.getText());
+ }
+
+ @Test
+ public void testPartial1() {
+ triggerText(field, "-");
+ assertEquals("-", field.getText());
+ assertEquals(0.0d, field.getValue(), 0.0001d);
+ }
+
+ @Test
+ public void testPartial2() {
+ triggerText(field, ".");
+ assertEquals(".", field.getText());
+ assertEquals(0.0d, field.getValue(), 0.0001d);
+ }
+
+ @Test
+ public void testPartial3() {
+ triggerText(field, "-.");
+ assertEquals("-.", field.getText());
+ assertEquals(0.0d, field.getValue(), 0.0001d);
+ }
+
+ @Test
+ public void testMinValue0() {
+ field.setMinValue(0.0d);
+ triggerText(field, "-");
+ assertEquals("", field.getText());
+ }
+
+ @Test
+ public void testMinValueNegative() {
+ field.setMinValue(-1.0);
+ triggerText(field, "-3.1");
+ assertEquals("-.1", field.getText());
+ field.setText("");
+ triggerText(field, "-.999");
+ assertEquals("-.999", field.getText());
+ }
+
+ @Test
+ public void testMinValuePositive() {
+ try {
+ field.setMinValue(1.0d);
+ fail("Expected exception trying to set min value to positive number");
+ }
+ catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testMaxValue0() {
+ field.setMaxValue(0.0d);
+ triggerText(field, "12");
+ assertEquals("", field.getText());
+ triggerText(field, "-5.3");
+ assertEquals("-5.3", field.getText());
+ }
+
+ @Test
+ public void testMaxValuePositive() {
+ field.setMaxValue(10.0d);
+ triggerText(field, "12.123");
+ assertEquals("1.123", field.getText());
+ field.setText("");
+ triggerText(field, "-50.3");
+ assertEquals("-50.3", field.getText());
+ field.setText("");
+ triggerText(field, "50.3");
+ assertEquals("5.3", field.getText());
+ }
+
+ @Test
+ public void testMaxValueNegative() {
+ try {
+ field.setMaxValue(-1.0d);
+ fail("Expected exception trying to set max value to negative number");
+ }
+ catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testLeadingTrailingSpaces() {
+ field.setText(" 3.14 ");
+ assertEquals("3.14", field.getText());
+ }
+
+ @Test
+ public void testSetValue() {
+ field.setValue(123.456d);
+ assertEquals("123.456", field.getText());
+ }
+}