Browse Source

Ask user for profile name on creation.

Change-Id: Id8f99a90564e09d64d12e0a9edfdccb5a5a25524
Fixes-Issue: CLIENT-434
Reviewed-on: http://gerrit.dmdirc.com/3149
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 years ago
parent
commit
2e7fbace7d

+ 19
- 18
src/com/dmdirc/addons/ui_swing/dialogs/profiles/Profile.java View File

@@ -25,11 +25,14 @@ package com.dmdirc.addons.ui_swing.dialogs.profiles;
25 25
 import com.dmdirc.interfaces.config.ConfigProvider;
26 26
 import com.dmdirc.interfaces.config.IdentityFactory;
27 27
 
28
+import com.google.common.base.Preconditions;
29
+
28 30
 import java.util.ArrayList;
31
+import java.util.Arrays;
29 32
 import java.util.List;
30 33
 import java.util.Objects;
31 34
 
32
-import javax.annotation.Nullable;
35
+import javax.annotation.Nonnull;
33 36
 
34 37
 /**
35 38
  * Profile wrapper class.
@@ -54,35 +57,33 @@ public class Profile {
54 57
     /**
55 58
      * Creates a new profile.
56 59
      *
60
+     * @param name            Profile name
57 61
      * @param identityFactory The factory to use to create the profile's config file when saving.
58 62
      */
59
-    public Profile(final IdentityFactory identityFactory) {
60
-        this(identityFactory, null);
63
+    public Profile(final String name, final IdentityFactory identityFactory) {
64
+        this.identityFactory = identityFactory;
65
+        this.configProvider = null;
66
+        this.name = name;
67
+        this.nicknames = new ArrayList<>(Arrays.asList(name));
68
+        this.realname = name;
69
+        this.ident = "";
61 70
     }
62 71
 
63 72
     /**
64 73
      * Creates a new profile based off the specified Identity.
65 74
      *
66 75
      * @param identityFactory The factory to use to create the profile's config file when saving.
67
-     * @param configProvider  Provider to read existing profile from. If null, a blank profile is
68
-     *                        created.
76
+     * @param configProvider  Provider to read existing profile from.
69 77
      */
70 78
     public Profile(final IdentityFactory identityFactory,
71
-            @Nullable final ConfigProvider configProvider) {
79
+            @Nonnull final ConfigProvider configProvider) {
80
+        Preconditions.checkNotNull(configProvider);
72 81
         this.identityFactory = identityFactory;
73 82
         this.configProvider = configProvider;
74
-
75
-        if (configProvider == null) {
76
-            name = "New Profile";
77
-            nicknames = new ArrayList<>();
78
-            realname = "";
79
-            ident = "";
80
-        } else {
81
-            name = configProvider.getOption("identity", "name");
82
-            nicknames = configProvider.getOptionList("profile", "nicknames");
83
-            realname = configProvider.getOption("profile", "realname");
84
-            ident = configProvider.getOption("profile", "ident");
85
-        }
83
+        this.name = configProvider.getOption("identity", "name");
84
+        this.nicknames = configProvider.getOptionList("profile", "nicknames");
85
+        this.realname = configProvider.getOption("profile", "realname");
86
+        this.ident = configProvider.getOption("profile", "ident");
86 87
     }
87 88
 
88 89
     public String getName() {

+ 7
- 3
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerController.java View File

@@ -43,9 +43,13 @@ public class ProfileManagerController {
43 43
         this.identityFactory = identityFactory;
44 44
     }
45 45
 
46
-    /** Adds a new profile. */
47
-    public void addProfile() {
48
-        model.addProfile(new Profile(identityFactory));
46
+    /**
47
+     * Adds a new profile.
48
+     *
49
+     * @param name Name of the profile
50
+     */
51
+    public void addProfile(final String name) {
52
+        model.addProfile(new Profile(name, identityFactory));
49 53
     }
50 54
 
51 55
     /** Deletes the active profile. */

+ 66
- 45
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialogLinker.java View File

@@ -163,17 +163,18 @@ public class ProfileManagerDialogLinker {
163 163
             public void actionPerformed(final ActionEvent e) {
164 164
                 new StandardInputDialog(dialog, ModalityType.DOCUMENT_MODAL, iconManager,
165 165
                         "Add nickname", "Enter nickname to add:", new AddNicknameValidator(model)) {
166
-                    private static final long serialVersionUID = 1L;
167
-                    @Override
168
-                    public boolean save() {
169
-                        controller.addNickname(getText());
170
-                        return true;
171
-                    }
172
-
173
-                    @Override
174
-                    public void cancelled() {
175
-                    }
176
-                }.display();
166
+                            private static final long serialVersionUID = 1L;
167
+
168
+                            @Override
169
+                            public boolean save() {
170
+                                controller.addNickname(getText());
171
+                                return true;
172
+                            }
173
+
174
+                            @Override
175
+                            public void cancelled() {
176
+                            }
177
+                        }.display();
177 178
             }
178 179
         });
179 180
         model.addPropertyChangeListener("selectedprofile", new PropertyChangeListener() {
@@ -204,17 +205,18 @@ public class ProfileManagerDialogLinker {
204 205
                         ModalityType.DOCUMENT_MODAL, iconManager,
205 206
                         "Add nickname", "Enter edited nickname:",
206 207
                         new EditNicknameValidator(model)) {
207
-                    private static final long serialVersionUID = 1L;
208
-                    @Override
209
-                    public boolean save() {
210
-                        controller.editNickname(getText());
211
-                        return true;
212
-                    }
213
-
214
-                    @Override
215
-                    public void cancelled() {
216
-                    }
217
-                };
208
+                            private static final long serialVersionUID = 1L;
209
+
210
+                            @Override
211
+                            public boolean save() {
212
+                                controller.editNickname(getText());
213
+                                return true;
214
+                            }
215
+
216
+                            @Override
217
+                            public void cancelled() {
218
+                            }
219
+                        };
218 220
                 inputDialog.setText((String) model.getSelectedNickname());
219 221
                 inputDialog.display();
220 222
             }
@@ -239,17 +241,18 @@ public class ProfileManagerDialogLinker {
239 241
             public void actionPerformed(final ActionEvent e) {
240 242
                 new StandardQuestionDialog(dialog, ModalityType.DOCUMENT_MODAL,
241 243
                         "Delete nickname?", "Are you sure you want to delete this nickname?") {
242
-                    private static final long serialVersionUID = 1L;
243
-                    @Override
244
-                    public boolean save() {
245
-                        controller.deleteNickname();
246
-                        return true;
247
-                    }
248
-
249
-                    @Override
250
-                    public void cancelled() {
251
-                    }
252
-                }.display();
244
+                            private static final long serialVersionUID = 1L;
245
+
246
+                            @Override
247
+                            public boolean save() {
248
+                                controller.deleteNickname();
249
+                                return true;
250
+                            }
251
+
252
+                            @Override
253
+                            public void cancelled() {
254
+                            }
255
+                        }.display();
253 256
             }
254 257
         });
255 258
         model.addPropertyChangeListener("selectednickname", new PropertyChangeListener() {
@@ -395,7 +398,24 @@ public class ProfileManagerDialogLinker {
395 398
         addProfile.addActionListener(new ActionListener() {
396 399
             @Override
397 400
             public void actionPerformed(final ActionEvent e) {
398
-                controller.addProfile();
401
+                final StandardInputDialog inputDialog = new StandardInputDialog(dialog,
402
+                        ModalityType.DOCUMENT_MODAL, iconManager,
403
+                        "Add profile", "New profile name:",
404
+                        new ProfileNameValidator(model.getProfiles())) {
405
+                            private static final long serialVersionUID = 1L;
406
+
407
+                            @Override
408
+                            public boolean save() {
409
+                                controller.addProfile(getText());
410
+                                return true;
411
+                            }
412
+
413
+                            @Override
414
+                            public void cancelled() {
415
+                            }
416
+                        };
417
+                inputDialog.setText((String) model.getSelectedNickname());
418
+                inputDialog.display();
399 419
             }
400 420
         });
401 421
     }
@@ -411,17 +431,18 @@ public class ProfileManagerDialogLinker {
411 431
             public void actionPerformed(final ActionEvent e) {
412 432
                 new StandardQuestionDialog(dialog, ModalityType.DOCUMENT_MODAL,
413 433
                         "Delete profile?", "Are you sure you want to delete this profile?") {
414
-                    private static final long serialVersionUID = 1L;
415
-                    @Override
416
-                    public boolean save() {
417
-                        controller.deleteProfile();
418
-                        return true;
419
-                    }
420
-
421
-                    @Override
422
-                    public void cancelled() {
423
-                    }
424
-                }.display();
434
+                            private static final long serialVersionUID = 1L;
435
+
436
+                            @Override
437
+                            public boolean save() {
438
+                                controller.deleteProfile();
439
+                                return true;
440
+                            }
441
+
442
+                            @Override
443
+                            public void cancelled() {
444
+                            }
445
+                        }.display();
425 446
             }
426 447
         });
427 448
         model.addPropertyChangeListener("profiles", new PropertyChangeListener() {

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

@@ -443,7 +443,7 @@ public class ProfileManagerModel {
443 443
         if (filenameValidation.isFailure()) {
444 444
             return filenameValidation;
445 445
         }
446
-        return new ProfileNameValidator(profiles, selectedProfile).validate(
446
+        return new ProfileRenameValidator(profiles, selectedProfile).validate(
447 447
                 selectedProfile.getName());
448 448
     }
449 449
 

+ 4
- 8
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileNameValidator.java View File

@@ -28,25 +28,21 @@ import com.dmdirc.util.validators.Validator;
28 28
 import java.util.List;
29 29
 
30 30
 /**
31
- * Ensures profile names are unique.
31
+ * Validates a profile name ensuring its uniqueness and validity as a filename.
32 32
  */
33
-class ProfileNameValidator implements Validator<String> {
33
+public class ProfileNameValidator implements Validator<String> {
34 34
 
35 35
     /** List of profiles to validate. */
36 36
     private final List<Profile> profiles;
37
-    /** Currently selected profile. */
38
-    private final Profile selectedProfile;
39 37
 
40
-    public ProfileNameValidator(final List<Profile> profiles, final Profile selectedProfile) {
38
+    public ProfileNameValidator(final List<Profile> profiles) {
41 39
         this.profiles = profiles;
42
-        this.selectedProfile = selectedProfile;
43 40
     }
44 41
 
45 42
     @Override
46 43
     public ValidationResponse validate(final String object) {
47 44
         for (Profile targetprofile : profiles) {
48
-            if (targetprofile != selectedProfile
49
-                    && targetprofile.getName().equalsIgnoreCase(object)) {
45
+            if (targetprofile.getName().equalsIgnoreCase(object)) {
50 46
                 return new ValidationResponse("Profile names must be unique");
51 47
             }
52 48
         }

+ 56
- 0
src/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileRenameValidator.java View File

@@ -0,0 +1,56 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.dialogs.profiles;
24
+
25
+import com.dmdirc.util.validators.ValidationResponse;
26
+import com.dmdirc.util.validators.Validator;
27
+
28
+import java.util.List;
29
+
30
+/**
31
+ * Ensures profile names are unique.
32
+ */
33
+class ProfileRenameValidator implements Validator<String> {
34
+
35
+    /** List of profiles to validate. */
36
+    private final List<Profile> profiles;
37
+    /** Currently selected profile. */
38
+    private final Profile selectedProfile;
39
+
40
+    public ProfileRenameValidator(final List<Profile> profiles, final Profile selectedProfile) {
41
+        this.profiles = profiles;
42
+        this.selectedProfile = selectedProfile;
43
+    }
44
+
45
+    @Override
46
+    public ValidationResponse validate(final String object) {
47
+        for (Profile targetprofile : profiles) {
48
+            if (targetprofile != selectedProfile
49
+                    && targetprofile.getName().equalsIgnoreCase(object)) {
50
+                return new ValidationResponse("Profile names must be unique");
51
+            }
52
+        }
53
+        return new ValidationResponse();
54
+    }
55
+
56
+}

+ 5
- 3
test/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerControllerTest.java View File

@@ -29,7 +29,9 @@ import org.junit.runner.RunWith;
29 29
 import org.mockito.Mock;
30 30
 import org.mockito.runners.MockitoJUnitRunner;
31 31
 
32
-import static org.mockito.Mockito.*;
32
+import static org.mockito.Mockito.mock;
33
+import static org.mockito.Mockito.verify;
34
+import static org.mockito.Mockito.when;
33 35
 
34 36
 /**
35 37
  * Tests for ProfileManagerController.
@@ -53,8 +55,8 @@ public class ProfileManagerControllerTest {
53 55
      */
54 56
     @Test
55 57
     public void testAddProfile() {
56
-        instance.addProfile();
57
-        verify(model).addProfile(new Profile(identityFactory));
58
+        instance.addProfile("New Profile");
59
+        verify(model).addProfile(new Profile("New Profile", identityFactory));
58 60
     }
59 61
 
60 62
     /**

+ 1
- 1
test/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerModelTest.java View File

@@ -509,7 +509,7 @@ public class ProfileManagerModelTest {
509 509
      */
510 510
     @Test
511 511
     public void testIsOKAllowed() {
512
-        final Profile profile = new Profile(null);
512
+        final Profile profile = new Profile("New Profile", null);
513 513
         profile.setName("*");
514 514
         profile.setRealname("");
515 515
         profile.setIdent("*");

+ 3
- 3
test/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileNameValidatorTest.java View File

@@ -46,7 +46,7 @@ public class ProfileNameValidatorTest {
46 46
         final List<Profile> profiles = new ArrayList<>();
47 47
         profiles.add(selected);
48 48
         profiles.add(other);
49
-        ProfileNameValidator instance = new ProfileNameValidator(profiles, selected);
49
+        ProfileRenameValidator instance = new ProfileRenameValidator(profiles, selected);
50 50
         assertFalse(instance.validate("Random").isFailure());
51 51
     }
52 52
 
@@ -62,7 +62,7 @@ public class ProfileNameValidatorTest {
62 62
         final List<Profile> profiles = new ArrayList<>();
63 63
         profiles.add(selected);
64 64
         profiles.add(other);
65
-        ProfileNameValidator instance = new ProfileNameValidator(profiles, selected);
65
+        ProfileRenameValidator instance = new ProfileRenameValidator(profiles, selected);
66 66
         assertTrue(instance.validate("other").isFailure());
67 67
     }
68 68
 
@@ -78,7 +78,7 @@ public class ProfileNameValidatorTest {
78 78
         final List<Profile> profiles = new ArrayList<>();
79 79
         profiles.add(selected);
80 80
         profiles.add(other);
81
-        ProfileNameValidator instance = new ProfileNameValidator(profiles, selected);
81
+        ProfileRenameValidator instance = new ProfileRenameValidator(profiles, selected);
82 82
         assertFalse(instance.validate("selected").isFailure());
83 83
     }
84 84
 }

+ 5
- 5
test/com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileTest.java View File

@@ -66,11 +66,11 @@ public class ProfileTest {
66 66
      */
67 67
     @Test
68 68
     public void testEmptyConstructor() {
69
-        Profile instance = new Profile(identityFactory);
69
+        Profile instance = new Profile("New Profile", identityFactory);
70 70
         assertEquals("", instance.getIdent());
71 71
         assertEquals("New Profile", instance.getName());
72
-        assertEquals(new ArrayList<String>(), instance.getNicknames());
73
-        assertEquals("", instance.getRealname());
72
+        assertEquals(new ArrayList<>(Arrays.asList("New Profile")), instance.getNicknames());
73
+        assertEquals("New Profile", instance.getRealname());
74 74
     }
75 75
 
76 76
     /**
@@ -170,7 +170,7 @@ public class ProfileTest {
170 170
 
171 171
         when(identityFactory.createProfileConfig("New Profile")).thenReturn(configProvider);
172 172
 
173
-        Profile instance = new Profile(identityFactory, null);
173
+        Profile instance = new Profile("New Profile", identityFactory);
174 174
         instance.save();
175 175
         verify(identityFactory).createProfileConfig("New Profile");
176 176
     }
@@ -244,7 +244,7 @@ public class ProfileTest {
244 244
      */
245 245
     @Test
246 246
     public void testDeleteNullIdentity() {
247
-        Profile instance = new Profile(null);
247
+        Profile instance = new Profile("New Profile", null);
248 248
         instance.delete();
249 249
     }
250 250
 }

Loading…
Cancel
Save