Merge pull request #4987 from wschaeferB/5239-PreventDupeRules

5239 prevent duplicate named rules being created
This commit is contained in:
esaunders 2019-07-03 17:28:06 -04:00 committed by GitHub
commit 08b35ea257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 10 deletions

View File

@ -37,6 +37,9 @@ ConfigVisualPanel2.editConfiguration=Configure imager
ConfigVisualPanel2.editRuleError=Edit rule error ConfigVisualPanel2.editRuleError=Edit rule error
ConfigVisualPanel2.editRuleSet=Edit Rule ConfigVisualPanel2.editRuleSet=Edit Rule
ConfigVisualPanel2.newRule.name=New Rule ConfigVisualPanel2.newRule.name=New Rule
# {0} - ruleName
ConfigVisualPanel2.newRuleError.duplicateName=A rule with name "{0}" already exists. Please enter a different rule name
ConfigVisualPanel2.newRuleError.title=New rule error
ConfigVisualPanel2.ok=OK ConfigVisualPanel2.ok=OK
ConfigVisualPanel2.rulesTable.columnModel.title0=Rule Name ConfigVisualPanel2.rulesTable.columnModel.title0=Rule Name
ConfigVisualPanel2.rulesTable.columnModel.title1=Description ConfigVisualPanel2.rulesTable.columnModel.title1=Description

View File

@ -426,6 +426,13 @@ final class ConfigVisualPanel2 extends JPanel {
if (option == JOptionPane.OK_OPTION) { if (option == JOptionPane.OK_OPTION) {
try { try {
ImmutablePair<String, LogicalImagerRule> ruleMap = editPanel.toRule(); ImmutablePair<String, LogicalImagerRule> ruleMap = editPanel.toRule();
if (!ruleName.equals(ruleMap.getKey()) && ruleExists(ruleMap)) {
JOptionPane.showMessageDialog(this,
Bundle.ConfigVisualPanel2_newRuleError_duplicateName(ruleMap.getKey()),
Bundle.ConfigVisualPanel2_editRuleError(),
JOptionPane.ERROR_MESSAGE);
continue;
}
updateRow(row, ruleMap); updateRow(row, ruleMap);
break; break;
} catch (IOException | NumberFormatException ex) { } catch (IOException | NumberFormatException ex) {
@ -442,7 +449,10 @@ final class ConfigVisualPanel2 extends JPanel {
} }
}//GEN-LAST:event_editRuleButtonActionPerformed }//GEN-LAST:event_editRuleButtonActionPerformed
@Messages({"ConfigVisualPanel2.newRule.name=New Rule"}) @Messages({"ConfigVisualPanel2.newRule.name=New Rule",
"ConfigVisualPanel2.newRuleError.title=New rule error",
"# {0} - ruleName",
"ConfigVisualPanel2.newRuleError.duplicateName=A rule with name \"{0}\" already exists. Please enter a different rule name"})
private void newRuleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newRuleButtonActionPerformed private void newRuleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newRuleButtonActionPerformed
NewRulePanel panel; NewRulePanel panel;
panel = new NewRulePanel(okButton, cancelButton); panel = new NewRulePanel(okButton, cancelButton);
@ -455,14 +465,22 @@ final class ConfigVisualPanel2 extends JPanel {
null, new Object[]{okButton, cancelButton}, okButton); null, new Object[]{okButton, cancelButton}, okButton);
if (option == JOptionPane.OK_OPTION) { if (option == JOptionPane.OK_OPTION) {
try { try {
// Save the new rule
ImmutablePair<String, LogicalImagerRule> ruleMap = panel.toRule(); ImmutablePair<String, LogicalImagerRule> ruleMap = panel.toRule();
if (ruleExists(ruleMap)) {
JOptionPane.showMessageDialog(this,
Bundle.ConfigVisualPanel2_newRuleError_duplicateName(ruleMap.getKey()),
Bundle.ConfigVisualPanel2_newRuleError_title(),
JOptionPane.ERROR_MESSAGE);
continue;
}
// Save the new rule
appendRow(ruleMap); appendRow(ruleMap);
break; break;
} catch (IOException | NumberFormatException ex) { } catch (IOException | NumberFormatException ex) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
ex.getMessage(), ex.getMessage(),
"New rule error", Bundle.ConfigVisualPanel2_newRuleError_title(),
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
// let user fix the error // let user fix the error
} }
@ -767,6 +785,22 @@ final class ConfigVisualPanel2 extends JPanel {
updatePanel(configFilename, config, ruleMap.getKey()); updatePanel(configFilename, config, ruleMap.getKey());
} }
/**
* Check if a rule with the same name as this rule already exists
*
* @param ruleMap the rule to check the name of
*
* @return true if it exists, false otherwise
*/
private boolean ruleExists(ImmutablePair<String, LogicalImagerRule> ruleMap) {
for (LogicalImagerRule rule : getRuleSetFromCurrentConfig().getRules()) {
if (rule.getName().equals(ruleMap.getKey())) {
return true;
}
}
return false;
}
private void appendRow(ImmutablePair<String, LogicalImagerRule> ruleMap) { private void appendRow(ImmutablePair<String, LogicalImagerRule> ruleMap) {
getRuleSetFromCurrentConfig().getRules().add(ruleMap.getValue()); getRuleSetFromCurrentConfig().getRules().add(ruleMap.getValue());
updatePanel(configFilename, config, ruleMap.getKey()); updatePanel(configFilename, config, ruleMap.getKey());

View File

@ -54,10 +54,6 @@ final class EditFullPathsRulePanel extends javax.swing.JPanel {
EditFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) { EditFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) {
initComponents(); initComponents();
if (editing) {
ruleNameTextField.setEnabled(!editing);
}
this.setRule(ruleName, rule); this.setRule(ruleName, rule);
this.setButtons(okButton, cancelButton); this.setButtons(okButton, cancelButton);

View File

@ -71,9 +71,6 @@ final class EditNonFullPathsRulePanel extends javax.swing.JPanel {
}) })
EditNonFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) { EditNonFullPathsRulePanel(JButton okButton, JButton cancelButton, String ruleName, LogicalImagerRule rule, boolean editing) {
initComponents(); initComponents();
if (editing) {
ruleNameTextField.setEnabled(!editing);
}
this.setRule(ruleName, rule); this.setRule(ruleName, rule);
this.setButtons(okButton, cancelButton); this.setButtons(okButton, cancelButton);