5187 have number formatters restore empty field when invalid input

This commit is contained in:
William Schaefer 2019-06-25 14:43:54 -04:00
parent 008d8dc149
commit 003ab250a1
3 changed files with 67 additions and 11 deletions

View File

@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.sleuthkit.autopsy.logicalimager.configuration;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.text.NumberFormatter;
/**
* Number formatter which will reset to being a null value when an invalid value
* is entered
*/
final class DefaultToEmptyNumberFormatter extends NumberFormatter {
private static final long serialVersionUID = 1L;
/**
* Create a DefaultToEmptyNumberFormatter
*
* @param format the format for the numbers
*/
DefaultToEmptyNumberFormatter(NumberFormat format) {
super(format);
}
@Override
public Object stringToValue(String string)
throws ParseException {
Object returnValue = null;
try {
returnValue = super.stringToValue(string);
} catch (ParseException ignored) {
//reset value to being empty since invalid value was entered
}
return returnValue;
}
}

View File

@ -222,24 +222,24 @@
</Container>
<Component class="javax.swing.JFormattedTextField" name="minSizeTextField">
<Properties>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.editors.AbstractFormatterFactoryEditor">
<Format format="#,###; " subtype="-1" type="0"/>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat(&quot;#,###; &quot;)))" type="code"/>
</Property>
<Property name="enabled" type="boolean" value="false"/>
</Properties>
</Component>
<Component class="javax.swing.JFormattedTextField" name="maxSizeTextField">
<Properties>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.editors.AbstractFormatterFactoryEditor">
<Format format="#,###; " subtype="-1" type="0"/>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat(&quot;#,###; &quot;)))" type="code"/>
</Property>
<Property name="enabled" type="boolean" value="false"/>
</Properties>
</Component>
<Component class="javax.swing.JFormattedTextField" name="modifiedWithinTextField">
<Properties>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.editors.AbstractFormatterFactoryEditor">
<Format format="" subtype="-1" type="0"/>
<Property name="formatterFactory" type="javax.swing.JFormattedTextField$AbstractFormatterFactory" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat(&quot;#,###; &quot;)))" type="code"/>
</Property>
<Property name="enabled" type="boolean" value="false"/>
</Properties>

View File

@ -362,13 +362,13 @@ final class EditNonFullPathsRulePanel extends javax.swing.JPanel {
folderNamesScrollPane.setEnabled(false);
minSizeTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,###; "))));
minSizeTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat("#,###; "))));
minSizeTextField.setEnabled(false);
maxSizeTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,###; "))));
maxSizeTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat("#,###; "))));
maxSizeTextField.setEnabled(false);
modifiedWithinTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat(""))));
modifiedWithinTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new DefaultToEmptyNumberFormatter(new java.text.DecimalFormat("#,###; "))));
modifiedWithinTextField.setEnabled(false);
userFolderNote.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/info-icon-16.png"))); // NOI18N
@ -694,14 +694,30 @@ final class EditNonFullPathsRulePanel extends javax.swing.JPanel {
return (extensionsCheckbox.isSelected() && !StringUtils.isBlank(extensionsTextField.getText()) && !validateExtensions(extensionsTextField).isEmpty())
|| (fileNamesCheckbox.isSelected() && !StringUtils.isBlank(fileNamesTextArea.getText()))
|| (folderNamesCheckbox.isSelected() && !StringUtils.isBlank(folderNamesTextArea.getText()))
|| (minSizeCheckbox.isSelected() && !StringUtils.isBlank(minSizeTextField.getText()) && !(Long.parseLong(minSizeTextField.getText()) == 0))
|| (maxSizeCheckbox.isSelected() && !StringUtils.isBlank(maxSizeTextField.getText()) && !(Long.parseLong(maxSizeTextField.getText()) == 0))
|| (minSizeCheckbox.isSelected() && !StringUtils.isBlank(minSizeTextField.getText()) && isNonZeroLong(minSizeTextField.getText()))
|| (maxSizeCheckbox.isSelected() && !StringUtils.isBlank(maxSizeTextField.getText()) && isNonZeroLong(maxSizeTextField.getText()))
|| (modifiedWithinCheckbox.isSelected() && !StringUtils.isBlank(modifiedWithinTextField.getText()));
} catch (IOException ex) {
logger.log(Level.WARNING, "Invalid contents of extensionsTextField", ex);
return false;
}
}
/**
* Check that value could be a non zero long
*
* @param numberString the string to check
*
* @return true if the value is a non-zero long
*/
private boolean isNonZeroLong(String numberString) {
Long value = 0L;
try {
value = Long.parseLong(numberString);
} catch (NumberFormatException ignored) {
//The string was not a number, this method will return false becaue the value is still 0L
}
return (value != 0);
}
/**