Fix codacy errors

This commit is contained in:
Joe Ho 2019-06-12 18:56:54 -04:00
parent 8100e5e635
commit 75f8b44f8b
13 changed files with 63 additions and 78 deletions

View File

@ -40,7 +40,7 @@ import org.openide.util.NbBundle;
/**
* Configuration Visual Panel 1
*/
public final class ConfigVisualPanel1 extends JPanel implements DocumentListener {
public final class ConfigVisualPanel1 extends JPanel {
private LogicalImagerConfig config;
private String configFilename;
@ -144,7 +144,7 @@ public final class ConfigVisualPanel1 extends JPanel implements DocumentListener
configFilename = path;
configFileTextField.setText(path);
newFile = false;
} catch (Exception ex) {
} catch (JsonIOException | JsonSyntaxException | IOException ex) {
JOptionPane.showMessageDialog(this,
Bundle.ConfigVisualPanel1_invalidConfigJson() + ex.getMessage() ,
Bundle.ConfigVisualPanel1_configurationError(),
@ -203,22 +203,13 @@ public final class ConfigVisualPanel1 extends JPanel implements DocumentListener
configFileTextField.setText(filename);
}
@Override
public void insertUpdate(DocumentEvent e) {
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
public boolean isPanelValid() {
return (newFile || !configFileTextField.getText().isEmpty());
}
/**
* Document Listener for textfield
*/
private static class MyDocumentListener implements DocumentListener {
private final ConfigVisualPanel1 panel;

View File

@ -18,6 +18,7 @@
*/
package org.sleuthkit.autopsy.configurelogicalimager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -555,7 +556,7 @@ public final class ConfigVisualPanel2 extends JPanel {
ImmutablePair<String, LogicalImagerRule> ruleMap = panel.toRule();
appendRow(ruleMap);
break;
} catch (Exception ex) {
} catch (IOException | NumberFormatException ex) {
JOptionPane.showMessageDialog(this,
ex.getMessage(),
"New rule error",
@ -696,7 +697,7 @@ public final class ConfigVisualPanel2 extends JPanel {
for (LogicalImagerRule rule : ruleSet.getRules()) {
rulesTableModel.setValueAt(rule.getName(), row, 0);
if (rowSelectionkey != null && rule.getName().equals(rowSelectionkey)) {
if (rowSelectionkey != null && rowSelectionkey.equals(rule.getName())) {
selectThisRow = row;
}
rulesTableModel.setValueAt(rule.getDescription(), row, 1);
@ -839,6 +840,9 @@ public final class ConfigVisualPanel2 extends JPanel {
}
}
/**
* RulesTableModel for rules table
*/
private class RulesTableModel extends AbstractTableModel {
private final List<String> ruleName = new ArrayList<>();
@ -925,6 +929,9 @@ public final class ConfigVisualPanel2 extends JPanel {
}
}
/**
* Table model for single column list table.
*/
private class SingleColumnTableModel extends AbstractTableModel {
private final List<String> list = new ArrayList<>();
@ -947,12 +954,10 @@ public final class ConfigVisualPanel2 extends JPanel {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object ret = null;
switch (columnIndex) {
case 0:
ret = list.get(rowIndex);
break;
default:
throw new UnsupportedOperationException("Invalid table column index: " + columnIndex); //NON-NLS
if (columnIndex == 0) {
ret = list.get(rowIndex);
} else {
throw new UnsupportedOperationException("Invalid table column index: " + columnIndex); //NON-NLS
}
return ret;
}
@ -964,12 +969,10 @@ public final class ConfigVisualPanel2 extends JPanel {
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
list.add((String) aValue);
break;
default:
throw new UnsupportedOperationException("Invalid table column index: " + columnIndex); //NON-NLS
if (columnIndex == 0) {
list.add((String) aValue);
} else {
throw new UnsupportedOperationException("Invalid table column index: " + columnIndex); //NON-NLS
}
}
}

View File

@ -39,10 +39,7 @@ public class ConfigWizardPanel1 implements WizardDescriptor.ValidatingPanel<Wiza
* component from this class, just use getComponent().
*/
private ConfigVisualPanel1 component;
private String configFilename = null;
private LogicalImagerConfig config = null;
boolean isValid = false;
private boolean newFile = true;
boolean valid = false;
// Get the visual component for the panel. In this template, the component
// is kept separate. This can be more efficient: if the wizard is created
@ -56,7 +53,7 @@ public class ConfigWizardPanel1 implements WizardDescriptor.ValidatingPanel<Wiza
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("UPDATE_UI")) { // NON-NLS
isValid = component.isPanelValid();
valid = component.isPanelValid();
fireChangeEvent();
}
}
@ -76,7 +73,7 @@ public class ConfigWizardPanel1 implements WizardDescriptor.ValidatingPanel<Wiza
@Override
public boolean isValid() {
return isValid;
return valid;
// If it depends on some condition (form filled out...) and
// this condition changes (last form field filled in...) then
// use ChangeSupport to implement add/removeChangeListener below.
@ -133,17 +130,14 @@ public class ConfigWizardPanel1 implements WizardDescriptor.ValidatingPanel<Wiza
@Override
public void storeSettings(WizardDescriptor wiz) {
// use wiz.putProperty to remember current panel state
configFilename = component.getConfigFilename();
config = component.getConfig();
newFile = component.isNewFile();
wiz.putProperty("configFilename", configFilename); // NON-NLS
wiz.putProperty("config", config); // NON-NLS
wiz.putProperty("newFile", newFile); // NON-NLS
wiz.putProperty("configFilename", component.getConfigFilename()); // NON-NLS
wiz.putProperty("config", component.getConfig()); // NON-NLS
wiz.putProperty("newFile", component.isNewFile()); // NON-NLS
}
@Override
public void validate() throws WizardValidationException {
isValid = component.isPanelValid();
valid = component.isPanelValid();
}
}

View File

@ -51,8 +51,7 @@ public class ConfigWizardPanel2 implements WizardDescriptor.Panel<WizardDescript
private ConfigVisualPanel2 component;
private String configFilename;
private LogicalImagerConfig config;
private boolean newFile;
boolean isValid = false;
boolean valid = false;
// Get the visual component for the panel. In this template, the component
// is kept separate. This can be more efficient: if the wizard is created
@ -89,8 +88,7 @@ public class ConfigWizardPanel2 implements WizardDescriptor.Panel<WizardDescript
// use wiz.getProperty to retrieve previous panel state
configFilename = (String) wiz.getProperty("configFilename"); // NON-NLS
config = (LogicalImagerConfig) wiz.getProperty("config"); // NON-NLS
newFile = (boolean) wiz.getProperty("newFile"); // NON-NLS
component.setConfiguration(configFilename, config, newFile);
component.setConfiguration(configFilename, config, (boolean) wiz.getProperty("newFile"));
}
@Override
@ -98,14 +96,6 @@ public class ConfigWizardPanel2 implements WizardDescriptor.Panel<WizardDescript
// use wiz.putProperty to remember current panel state
}
@Override
public void addChangeListener(ChangeListener cl) {
}
@Override
public void removeChangeListener(ChangeListener cl) {
}
public String getConfigFilename() {
return configFilename;
}
@ -155,4 +145,14 @@ public class ConfigWizardPanel2 implements WizardDescriptor.Panel<WizardDescript
}
}
@Override
public void addChangeListener(ChangeListener cl) {
// Not used
}
@Override
public void removeChangeListener(ChangeListener cl) {
// Not used
}
}

View File

@ -74,12 +74,10 @@ public final class ConfigureLogicalImager implements ActionListener {
// {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
wiz.setTitleFormat(new MessageFormat("{0}")); // NON-NLS
wiz.setTitle(Bundle.ConfigureLogicalImager_title());
if (DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) {
// do something
if (panels.get(1) instanceof ConfigWizardPanel2) {
if ((DialogDisplayer.getDefault().notify(wiz) == WizardDescriptor.FINISH_OPTION) &&
(panels.get(1) instanceof ConfigWizardPanel2)) {
ConfigWizardPanel2 panel = (ConfigWizardPanel2) panels.get(1);
panel.saveConfigFile();
}
}
}
}

View File

@ -41,9 +41,7 @@ public class EditFullPathsRulePanel extends javax.swing.JPanel {
private JButton cancelButton;
List<String> newFullPaths = new ArrayList<>();
private final JTextArea fullPathsTextArea;
private boolean editing = false;
/**
* Creates new form EditFullPathsRulePanel
*/
@ -53,7 +51,6 @@ public class EditFullPathsRulePanel extends javax.swing.JPanel {
public EditFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) {
initComponents();
this.editing = editing;
if (editing) {
ruleNameTextField.setEnabled(!editing);
}

View File

@ -46,7 +46,6 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
private JButton cancelButton;
private final javax.swing.JTextArea filenamesTextArea;
private final javax.swing.JTextArea folderNamesTextArea;
private boolean editing = true;
/**
* Creates new form EditRulePanel
@ -58,7 +57,6 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
EditNonFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) {
initComponents();
this.editing = editing;
if (editing) {
ruleNameTextField.setEnabled(!editing);
}
@ -472,7 +470,7 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
}
builder.minDays(minDays);
} catch (NumberFormatException | ParseException ex) {
throw new IOException(Bundle.EditNonFullPathsRulePanel_modifiedDaysMustBeNumberException(ex.getMessage()));
throw new IOException(Bundle.EditNonFullPathsRulePanel_modifiedDaysMustBeNumberException(ex.getMessage()), ex);
}
}
@ -485,7 +483,7 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
throw new IOException(Bundle.EditNonFullPathsRulePanel_minFileSizeNotPositiveException());
}
} catch (NumberFormatException | ParseException ex) {
throw new IOException(Bundle.EditNonFullPathsRulePanel_minFileSizeMustBeNumberException(ex.getMessage()));
throw new IOException(Bundle.EditNonFullPathsRulePanel_minFileSizeMustBeNumberException(ex.getMessage()), ex);
}
}
@ -498,7 +496,7 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
throw new IOException(Bundle.EditNonFullPathsRulePanel_maxFileSizeNotPositiveException());
}
} catch (NumberFormatException | ParseException ex) {
throw new IOException(Bundle.EditNonFullPathsRulePanel_maxFileSizeMustBeNumberException(ex.getMessage()));
throw new IOException(Bundle.EditNonFullPathsRulePanel_maxFileSizeMustBeNumberException(ex.getMessage()), ex);
}
}
@ -520,10 +518,10 @@ public class EditNonFullPathsRulePanel extends javax.swing.JPanel {
"EditNonFullPathsRulePanel.emptyExtensionException=Extensions cannot have an empty entry",
})
private List<String> validateExtensions(JTextField textField) throws IOException {
List<String> extensions = new ArrayList<>();
if (isBlank(textField.getText())) {
return null;
}
List<String> extensions = new ArrayList<>();
for (String extension : textField.getText().split(",")) {
extension = strip(extension);
if (extension.isEmpty()) {

View File

@ -103,10 +103,10 @@ public class EditRulePanel extends JPanel {
"EditRulePanel.blankLineException={0} cannot have a blank line",
})
static public List<String> validateTextList(JTextArea textArea, String fieldName) throws IOException {
List<String> list = new ArrayList<>();
if (isBlank(textArea.getText())) {
return null;
}
List<String> list = new ArrayList<>();
for (String line : textArea.getText().split("\\n")) { // NON-NLS
line = strip(line);
if (line.isEmpty()) {

View File

@ -35,6 +35,8 @@ public final class EncryptionProgramsRule {
private static final String ENCRYPTION_PROGRAMS_RULE_DESCRIPTION = Bundle.EncryptionProgramsRule_encryptionProgramsRuleDescription();
private static final List<String> FILENAMES = new ArrayList<>();
private EncryptionProgramsRule() {}
// TODO: Add more files here
static {
FILENAMES.add("truecrypt.exe"); //NON-NLS

View File

@ -45,7 +45,6 @@ public class LogicalImagerConfigDeserializer implements JsonDeserializer<Logical
@Override
public LogicalImagerConfig deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
boolean finalizeImageWriter = false;
List<LogicalImagerRuleSet> ruleSets = new ArrayList<>();
final JsonObject jsonObject = je.getAsJsonObject();
final JsonElement jsonFinalizeImageWriter = jsonObject.get("finalize-image-writer"); // NON-NLS
@ -57,6 +56,8 @@ public class LogicalImagerConfigDeserializer implements JsonDeserializer<Logical
if (asJsonArray == null) {
throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_missingRuleSetException());
}
List<LogicalImagerRuleSet> ruleSets = new ArrayList<>();
for (JsonElement element: asJsonArray) {
String setName = null;
List<LogicalImagerRule> rules = null;
@ -149,7 +150,7 @@ public class LogicalImagerConfigDeserializer implements JsonDeserializer<Logical
default:
throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(sizeKey));
}
};
}
break;
case "date-range": // NON-NLS
JsonObject dateRangeObject = entry1.getValue().getAsJsonObject();
@ -169,7 +170,7 @@ public class LogicalImagerConfigDeserializer implements JsonDeserializer<Logical
default:
throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(dateKey));
}
};
}
break;
default:
throw new JsonParseException(Bundle.LogicalImagerConfigDeserializer_unsupportedKeyException(key1));

View File

@ -52,10 +52,10 @@ public class LogicalImagerRule {
private List<String> fullPaths = new ArrayList<>();
@SerializedName("size-range")
@Expose(serialize = true)
private Map<String, Integer> sizeRange = new HashMap<>();
final private Map<String, Integer> sizeRange = new HashMap<>();
@SerializedName("date-range")
@Expose(serialize = true)
private Map<String, Integer> dateRange = new HashMap<>();
final private Map<String, Integer> dateRange = new HashMap<>();
// The following fields should not be serialized, internal use only
@Expose(serialize = false)
@ -160,6 +160,9 @@ public class LogicalImagerRule {
return maxDate;
}
/**
* Builder class
*/
public static class Builder {
private Boolean shouldAlert = null;
private Boolean shouldSave = null;
@ -174,8 +177,6 @@ public class LogicalImagerRule {
private Integer minDays = null;
private Integer minDate = null;
private Integer maxDate = null;
public Builder() {}
public Builder shouldAlert(boolean shouldAlert) {
this.shouldAlert = shouldAlert;

View File

@ -29,11 +29,11 @@ public class LogicalImagerRuleSet {
@SerializedName("set-name")
@Expose(serialize = true)
private String setName;
final private String setName;
@SerializedName("rules")
@Expose(serialize = true)
private List<LogicalImagerRule> rules;
private final List<LogicalImagerRule> rules;
public LogicalImagerRuleSet(String setName, List<LogicalImagerRule> rules) {
this.setName = setName;

View File

@ -48,8 +48,8 @@ public class NewRuleSetPanel extends javax.swing.JPanel {
editFullPathsRulePanel = new EditFullPathsRulePanel(okButton, cancelButton, "", new LogicalImagerRule(), false);
fullPathsPanel.add(editFullPathsRulePanel, BorderLayout.NORTH);
sharedLayeredPane.add(nonFullPathsJPanel, new Integer(0));
sharedLayeredPane.add(fullPathsPanel, new Integer(1));
sharedLayeredPane.add(nonFullPathsJPanel, Integer.valueOf(0));
sharedLayeredPane.add(fullPathsPanel, Integer.valueOf(1));
nonFullPathsJPanel.setVisible(true);
fullPathsPanel.setVisible(false);
}