Browse Source

Fixes issue 3812: AMD groupname validation fails when editing a group

Change-Id: I86bf10717a12f341e9093f02cb97474eb771497d
Reviewed-on: http://gerrit.dmdirc.com/928
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 years ago
parent
commit
d47020719d

src/com/dmdirc/addons/ui_swing/dialogs/profiles/NoDuplicatesInListValidator.java → src/com/dmdirc/addons/ui_swing/components/validating/NoDuplicatesInListValidator.java View File

@@ -20,7 +20,7 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.addons.ui_swing.dialogs.profiles;
23
+package com.dmdirc.addons.ui_swing.components.validating;
24 24
 
25 25
 import com.dmdirc.config.prefs.validator.ValidationResponse;
26 26
 import com.dmdirc.config.prefs.validator.Validator;
@@ -34,11 +34,11 @@ import javax.swing.JList;
34 34
 public class NoDuplicatesInListValidator implements Validator<String> {
35 35
 
36 36
     /** List. */
37
-    private JList list;
37
+    protected JList list;
38 38
     /** List to validate. */
39
-    private DefaultListModel model;
39
+    protected DefaultListModel model;
40 40
     /** Case sensitive. */
41
-    private boolean caseSensitive;
41
+    protected boolean caseSensitive;
42 42
 
43 43
     /**
44 44
      * Creates a new validator.
@@ -69,11 +69,35 @@ public class NoDuplicatesInListValidator implements Validator<String> {
69 69
     @Override
70 70
     public ValidationResponse validate(final String object) {
71 71
         final String string = caseSensitive ? object : object.toLowerCase();
72
-        if (model.indexOf(string) != -1 && (list.getSelectedValue() == null ||
73
-                !list.getSelectedValue().equals(string))) {
72
+        if (indexOfString(string) != -1 && (list.getSelectedValue() == null ||
73
+                !listValueToString(list.getSelectedValue()).equals(string))) {
74 74
                 return new ValidationResponse("Value is a duplicate");
75 75
         } else {
76 76
             return new ValidationResponse();
77 77
         }
78 78
     }
79
+
80
+    /**
81
+     * Converts the list object to a string for validation. Defaults to the
82
+     * objects toString method.
83
+     *
84
+     * @param object Object to convert
85
+     *
86
+     * @return String representation
87
+     */
88
+    public String listValueToString(final Object object) {
89
+        return object.toString();
90
+    }
91
+
92
+    /**
93
+     * Returns the index of an object represented by a string.  Defaults to
94
+     * searching for the string.
95
+     *
96
+     * @param string String to look for
97
+     *
98
+     * @return Index of the string of -1 if not found
99
+     */
100
+    public int indexOfString(final String string) {
101
+        return model.indexOf(string);
102
+    }
79 103
 }

+ 63
- 12
src/com/dmdirc/addons/ui_swing/dialogs/actionsmanager/ActionsManagerDialog.java View File

@@ -32,7 +32,6 @@ import com.dmdirc.addons.ui_swing.Apple;
32 32
 import com.dmdirc.addons.ui_swing.MainFrame;
33 33
 import com.dmdirc.addons.ui_swing.SwingController;
34 34
 import com.dmdirc.config.IdentityManager;
35
-import com.dmdirc.config.prefs.validator.ActionGroupValidator;
36 35
 import com.dmdirc.config.prefs.validator.FileNameValidator;
37 36
 import com.dmdirc.config.prefs.validator.ValidatorChain;
38 37
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
@@ -41,6 +40,7 @@ import com.dmdirc.addons.ui_swing.components.SortedListModel;
41 40
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
42 41
 import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
43 42
 import com.dmdirc.addons.ui_swing.components.renderers.ActionGroupListCellRenderer;
43
+import com.dmdirc.addons.ui_swing.components.validating.NoDuplicatesInListValidator;
44 44
 import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
45 45
 
46 46
 import java.awt.Window;
@@ -100,16 +100,22 @@ public final class ActionsManagerDialog extends StandardDialog implements
100 100
     private JPanel groupPanel;
101 101
     /** Are we saving? */
102 102
     private final AtomicBoolean saving = new AtomicBoolean(false);
103
+    /** Duplicate action group validator. */
104
+    private ValidatorChain<String> validator;
103 105
 
104 106
     /** 
105 107
      * Creates a new instance of ActionsManagerDialog.
106 108
      */
109
+    @SuppressWarnings("unchecked")
107 110
     private ActionsManagerDialog(final Window parentWindow,
108 111
             final SwingController controller) {
109 112
         super(Apple.isAppleUI() ? new AppleJFrame((MainFrame) parentWindow,
110 113
                 controller) : null, ModalityType.MODELESS);
111 114
 
112 115
         initComponents();
116
+        validator = new ValidatorChain<String>(
117
+                new ActionGroupNoDuplicatesInListValidator(groups,
118
+                (DefaultListModel) groups.getModel()), new FileNameValidator());
113 119
         addListeners();
114 120
         layoutGroupPanel();
115 121
         layoutComponents();
@@ -335,13 +341,12 @@ public final class ActionsManagerDialog extends StandardDialog implements
335 341
     /**
336 342
      * Prompts then adds an action group.
337 343
      */
338
-    @SuppressWarnings("unchecked")
339 344
     private void addGroup() {
345
+        final int index = groups.getSelectedIndex();
346
+        groups.getSelectionModel().clearSelection();
340 347
         final StandardInputDialog inputDialog = new StandardInputDialog(this,
341 348
                 ModalityType.DOCUMENT_MODAL, "New action group",
342
-                "Please enter the name of the new action group",
343
-                new ValidatorChain<String>(new FileNameValidator(),
344
-                new ActionGroupValidator())) {
349
+                "Please enter the name of the new action group", validator) {
345 350
 
346 351
             /**
347 352
              * A version number for this class. It should be changed whenever the class
@@ -354,6 +359,7 @@ public final class ActionsManagerDialog extends StandardDialog implements
354 359
             @Override
355 360
             public boolean save() {
356 361
                 if (!saving.getAndSet(true)) {
362
+                    groups.setSelectedIndex(index);
357 363
                     if (getText() == null || getText().isEmpty() && !ActionManager.
358 364
                             getGroups().
359 365
                             containsKey(getText())) {
@@ -371,7 +377,7 @@ public final class ActionsManagerDialog extends StandardDialog implements
371 377
             /** {@inheritDoc} */
372 378
             @Override
373 379
             public void cancelled() {
374
-                //Ignore
380
+                groups.setSelectedIndex(index);
375 381
             }
376 382
         };
377 383
         inputDialog.display(this);
@@ -380,16 +386,12 @@ public final class ActionsManagerDialog extends StandardDialog implements
380 386
     /**
381 387
      * Prompts then edits an action group.
382 388
      */
383
-    @SuppressWarnings("unchecked")
384 389
     private void editGroup() {
385 390
         final String oldName =
386 391
                 ((ActionGroup) groups.getSelectedValue()).getName();
387 392
         final StandardInputDialog inputDialog = new StandardInputDialog(this,
388
-                ModalityType.DOCUMENT_MODAL,
389
-                "Edit action group",
390
-                "Please enter the new name of the action group",
391
-                new ValidatorChain<String>(new FileNameValidator(),
392
-                new ActionGroupValidator())) {
393
+                ModalityType.DOCUMENT_MODAL, "Edit action group",
394
+                "Please enter the new name of the action group", validator) {
393 395
 
394 396
             /**
395 397
              * A version number for this class. It should be changed whenever the class
@@ -521,3 +523,52 @@ public final class ActionsManagerDialog extends StandardDialog implements
521 523
         }
522 524
     }
523 525
 }
526
+
527
+/**
528
+ * No duplicates list validator, overriden to work with action groups.
529
+ */
530
+class ActionGroupNoDuplicatesInListValidator extends NoDuplicatesInListValidator {
531
+
532
+    /**
533
+     * Creates a new validator.
534
+     *
535
+     * @param list List
536
+     * @param model Model to validate
537
+     */
538
+    public ActionGroupNoDuplicatesInListValidator(final JList list,
539
+            final DefaultListModel model) {
540
+        super(true, list, model);
541
+    }
542
+
543
+    /**
544
+     * Creates a new validator.
545
+     *
546
+     * @param list List
547
+     * @param caseSensitive Case sensitive check?
548
+     * @param model Model to validate
549
+     */
550
+    public ActionGroupNoDuplicatesInListValidator(final boolean caseSensitive,
551
+            final JList list, final DefaultListModel model) {
552
+        super(caseSensitive, list, model);
553
+    }
554
+    
555
+    /** {@inheritDoc} */
556
+    @Override
557
+    public String listValueToString(final Object object) {
558
+        return ((ActionGroup) object).getName();
559
+    }
560
+
561
+    /** {@inheritDoc} */
562
+    @Override
563
+    public int indexOfString(final String string) {
564
+        final String value = caseSensitive ? string : string.toLowerCase();
565
+        int index = -1;
566
+        for (int i = 0; i < model.getSize(); i++) {
567
+            if (((ActionGroup) model.get(i)).getName().equals(value)) {
568
+                index = i;
569
+                break;
570
+            }
571
+        }
572
+        return index;
573
+    }
574
+}

+ 1
- 0
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileDetailPanel.java View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.dialogs.profiles;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.validating.NoDuplicatesInListValidator;
25 26
 import com.dmdirc.addons.ui_swing.MainFrame;
26 27
 import com.dmdirc.config.prefs.validator.FileNameValidator;
27 28
 import com.dmdirc.config.prefs.validator.IdentValidator;

Loading…
Cancel
Save