Browse Source

ValidatingTextLabels now have reasons on in tooltips for the errors

Profile Manager now uses ValidatingJTextFields and StandardInputDialogs

git-svn-id: http://svn.dmdirc.com/trunk@2865 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Gregory Holmes 16 years ago
parent
commit
34e4e3bb43

+ 16
- 1
src/com/dmdirc/ui/swing/components/StandardInputDialog.java View File

@@ -67,9 +67,15 @@ public abstract class StandardInputDialog extends StandardDialog {
67 67
 
68 68
             /** {@inheritDoc} */
69 69
             @Override
70
-            public boolean validate(String object) {
70
+            public boolean validate(final String object) {
71 71
                 return true;
72 72
             }
73
+
74
+            /** {@inheritDoc} */
75
+            @Override
76
+            public String getFailureReason() {
77
+                return "";
78
+            }
73 79
         });
74 80
     }
75 81
 
@@ -210,4 +216,13 @@ public abstract class StandardInputDialog extends StandardDialog {
210 216
     public final String getText() {
211 217
         return textField.getText();
212 218
     }
219
+    
220
+    /**
221
+     * Sets the dialogs text to the specified text.
222
+     * 
223
+     * @param text New test
224
+     */
225
+    public final void setText(final String text) {
226
+        textField.setText(text);
227
+    }
213 228
 }

+ 44
- 0
src/com/dmdirc/ui/swing/components/validating/NotEmptyValidator.java View File

@@ -0,0 +1,44 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.ui.swing.components.validating;
24
+
25
+/**
26
+ * Validator checking the string is not empty.
27
+ */
28
+public class NotEmptyValidator implements Validator<String> {
29
+
30
+    /**
31
+     * A version number for this class. It should be changed whenever the class
32
+     * structure is changed (or anything else that would prevent serialized
33
+     * objects being unserialized with the new class).
34
+     */
35
+    private static final long serialVersionUID = 1;
36
+
37
+    public boolean validate(String object) {
38
+        return !object.isEmpty();
39
+    }
40
+
41
+    public String getFailureReason() {
42
+        return "Cannot be an empty string.";
43
+    }
44
+}

+ 11
- 1
src/com/dmdirc/ui/swing/components/validating/RegexValidator.java View File

@@ -29,14 +29,18 @@ public class RegexValidator implements Validator<String> {
29 29
 
30 30
     /** Regex. */
31 31
     private final String regex;
32
+    /** Failure reason. */
33
+    private final String failedReason;
32 34
 
33 35
     /**
34 36
      * Instantiates a new regex validator.
35 37
      * 
36 38
      * @param regex Regex to validate text against
39
+     * @param failedReason Reason for validation failure
37 40
      */
38
-    public RegexValidator(final String regex) {
41
+    public RegexValidator(final String regex, final String failedReason) {
39 42
         this.regex = regex;
43
+        this.failedReason = failedReason;
40 44
     }
41 45
 
42 46
     /** {@inheritDoc} */
@@ -44,4 +48,10 @@ public class RegexValidator implements Validator<String> {
44 48
     public boolean validate(final String object) {
45 49
         return object.matches(regex);
46 50
     }
51
+
52
+    /** {@inheritDoc} */
53
+    @Override
54
+    public String getFailureReason() {
55
+        return failedReason;
56
+    }
47 57
 }

+ 15
- 2
src/com/dmdirc/ui/swing/components/validating/ValidatingJTextField.java View File

@@ -24,6 +24,8 @@ package com.dmdirc.ui.swing.components.validating;
24 24
 
25 25
 import com.dmdirc.IconManager;
26 26
 
27
+import java.awt.Font;
28
+
27 29
 import javax.swing.BorderFactory;
28 30
 import javax.swing.JComponent;
29 31
 import javax.swing.JLabel;
@@ -31,9 +33,9 @@ import javax.swing.JTextField;
31 33
 import javax.swing.UIManager;
32 34
 import javax.swing.event.DocumentEvent;
33 35
 import javax.swing.event.DocumentListener;
34
-
35 36
 import javax.swing.text.BadLocationException;
36 37
 import javax.swing.text.Document;
38
+
37 39
 import net.miginfocom.swing.MigLayout;
38 40
 
39 41
 /**
@@ -75,6 +77,7 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
75 77
         this.validator = validator;
76 78
         errorIcon =
77 79
                 new JLabel(IconManager.getIconManager().getIcon("input-error"));
80
+        errorIcon.setToolTipText(validator.getFailureReason());
78 81
 
79 82
         if (!"javax.swing.plaf.synth.SynthLookAndFeel".equals(UIManager.get("TextFieldUI"))) {
80 83
             setBorder(textField.getBorder());
@@ -86,7 +89,7 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
86 89
         add(textField, "grow, pushx");
87 90
         add(errorIcon);
88 91
 
89
-        errorIcon.setVisible(false);
92
+        checkError();
90 93
 
91 94
         textField.getDocument().addDocumentListener(this);
92 95
     }
@@ -333,4 +336,14 @@ public class ValidatingJTextField extends JComponent implements DocumentListener
333 336
     public void copy() {
334 337
         textField.copy();
335 338
     }
339
+
340
+    /**
341
+     * Returns the font for the textfield.
342
+     * 
343
+     * @see javax.swing.JTextField#copy()
344
+     */
345
+    @Override
346
+    public Font getFont() {
347
+        return textField.getFont();
348
+    }
336 349
 }

+ 7
- 0
src/com/dmdirc/ui/swing/components/validating/Validator.java View File

@@ -37,4 +37,11 @@ public interface Validator<V> {
37 37
      * @return true iif the object is correct
38 38
      */
39 39
     boolean validate(final V object);
40
+    
41
+    /**
42
+     * Returns the reason the validation failed.
43
+     * 
44
+     * @return Reason for validation failure
45
+     */
46
+    String getFailureReason();
40 47
 }

+ 1
- 1
src/com/dmdirc/ui/swing/dialogs/NewServerDialog.java View File

@@ -237,7 +237,7 @@ public final class NewServerDialog extends StandardDialog {
237 237
         final JButton button2 = new JButton();
238 238
         
239 239
         serverLabel = new JLabel();
240
-        serverField = new ValidatingJTextField(new RegexValidator("^[^\\s]+$+"));
240
+        serverField = new ValidatingJTextField(new RegexValidator("^[^\\s]+$+", "Cannot contain spaces."));
241 241
         portLabel = new JLabel();
242 242
         portField = new JSpinner(new SpinnerNumberModel());
243 243
         passwordLabel = new JLabel();

+ 6
- 0
src/com/dmdirc/ui/swing/dialogs/prefs/URLConfigPanel.java View File

@@ -303,6 +303,12 @@ public class URLConfigPanel extends JPanel implements ListSelectionListener,
303 303
                 return false;
304 304
             }
305 305
         }
306
+
307
+        /** {@inheritDoc} */
308
+        @Override
309
+        public String getFailureReason() {
310
+            return "Cannot be an empty string, and must not already exist";
311
+        }
306 312
     }
307 313
 
308 314
 }

+ 105
- 315
src/com/dmdirc/ui/swing/dialogs/profiles/ProfileDetailPanel.java View File

@@ -23,21 +23,20 @@
23 23
 package com.dmdirc.ui.swing.dialogs.profiles;
24 24
 
25 25
 import com.dmdirc.IconManager;
26
+import com.dmdirc.Main;
27
+import com.dmdirc.ui.swing.MainFrame;
26 28
 import com.dmdirc.ui.swing.components.ImageButton;
27
-import static com.dmdirc.ui.swing.UIUtilities.layoutGrid;
28
-import static com.dmdirc.ui.swing.UIUtilities.SMALL_BORDER;
29
+import com.dmdirc.ui.swing.components.StandardInputDialog;
30
+import com.dmdirc.ui.swing.components.validating.NotEmptyValidator;
31
+import com.dmdirc.ui.swing.components.validating.RegexValidator;
32
+import com.dmdirc.ui.swing.components.validating.ValidatingJTextField;
29 33
 
30
-import java.awt.Component;
31 34
 import java.awt.Dimension;
32
-import java.awt.GridLayout;
33 35
 import java.awt.event.ActionEvent;
34 36
 import java.awt.event.ActionListener;
35 37
 import java.util.ArrayList;
36 38
 import java.util.Enumeration;
37
-import java.util.HashMap;
38
-import java.util.Map;
39 39
 
40
-import javax.swing.Box;
41 40
 import javax.swing.DefaultListModel;
42 41
 import javax.swing.Icon;
43 42
 import javax.swing.JButton;
@@ -46,76 +45,52 @@ import javax.swing.JList;
46 45
 import javax.swing.JOptionPane;
47 46
 import javax.swing.JPanel;
48 47
 import javax.swing.JScrollPane;
49
-import javax.swing.JTextField;
50
-import javax.swing.SpringLayout;
51
-import javax.swing.event.DocumentEvent;
52
-import javax.swing.event.DocumentListener;
53 48
 import javax.swing.event.ListDataEvent;
54 49
 import javax.swing.event.ListDataListener;
55 50
 import javax.swing.event.ListSelectionEvent;
56 51
 import javax.swing.event.ListSelectionListener;
57
-import javax.swing.text.Document;
52
+
53
+import net.miginfocom.swing.MigLayout;
58 54
 
59 55
 /** Profile detail panel. */
60 56
 public class ProfileDetailPanel extends JPanel implements ActionListener,
61
-        ListSelectionListener, DocumentListener,
62
-        ListDataListener {
57
+        ListSelectionListener, ListDataListener {
63 58
 
64 59
     /**
65 60
      * A version number for this class. It should be changed whenever the class
66 61
      * structure is changed (or anything else that would prevent serialized
67 62
      * objects being unserialized with the new class).
68 63
      */
69
-    private static final long serialVersionUID = 1;
70
-    /** Invalid filename characters. */
71
-    private static final String FILENAME_REGEX = ".*[^\\w ].*";
72
-    /** Invalid nickname characters. */
73
-    private static final String NICKNAME_REGEX = ".*[\\a\\v\\s].*";
74
-    /** Invalid ident characters. */
75
-    private static final String IDENT_REGEX = ".*[\\a\\v\\s].*";
64
+    private static final long serialVersionUID = 2;
65
+    /** Nickname regex. */
66
+    private static final String NICKNAME_REGEX =
67
+            "[A-Za-z0-9\\[\\]{|}\\-\\^\\\\]+";
68
+    /** Filename regex. */
69
+    private static final String FILENAME_REGEX = "[A-Za-z0-9]+";
76 70
     /** Displayed profile. */
77 71
     private Profile profile;
78 72
     /** Name text field. */
79
-    private JTextField name;
73
+    private ValidatingJTextField name;
80 74
     /** Nick name text field. */
81
-    private JTextField nickname;
75
+    private ValidatingJTextField nickname;
82 76
     /** Realname text field. */
83
-    private JTextField realname;
77
+    private ValidatingJTextField realname;
84 78
     /** Ident text field. */
85
-    private JTextField ident;
79
+    private ValidatingJTextField ident;
86 80
     /** Alternate nicknames list. */
87 81
     private JList altNicknames;
88
-    /** Buttons panel. */
89
-    private JPanel buttonsPanel;
90 82
     /** Add button. */
91 83
     private JButton addButton;
92 84
     /** Delete button. */
93 85
     private JButton delButton;
94 86
     /** Edit button. */
95 87
     private JButton editButton;
96
-    /** name error. */
97
-    private ImageButton nameError;
98
-    /** nick error. */
99
-    private ImageButton nicknameError;
100
-    /** real name error. */
101
-    private ImageButton realnameError;
102
-    /** ident error. */
103
-    private ImageButton identError;
104 88
     /** Alt nicknames error. */
105 89
     private ImageButton altNicknamesError;
106
-    /** Document -> Component map. */
107
-    private Map<Document, Component> map;
108
-    /** Error icon. */
109
-    private Icon errorIcon;
110
-    /** Warning icon. */
111
-    private Icon warningIcon;
112
-    /** Transparent icon. */
113
-    private Icon normalIcon;
114 90
 
115 91
     /** Creates a new profile detail panel. */
116 92
     public ProfileDetailPanel() {
117 93
         initMainComponents();
118
-        initButtonsPanel();
119 94
         layoutComponents();
120 95
 
121 96
         clearProfile();
@@ -123,39 +98,19 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
123 98
 
124 99
     /** Initialises the components in the main panel. */
125 100
     private void initMainComponents() {
126
-        map =   new HashMap<Document, Component>();
127
-
128
-        errorIcon =
129
-                IconManager.getIconManager().getIcon("input-error");
130
-        warningIcon = IconManager.getIconManager().getIcon("warning");
131
-        normalIcon = IconManager.getIconManager().getIcon("nothing");
132
-
133
-        name = new JTextField();
134
-        nickname = new JTextField();
135
-        realname = new JTextField();
136
-        ident = new JTextField();
101
+        name = new ValidatingJTextField(new RegexValidator(FILENAME_REGEX,
102
+                "Name must only contain letters and numbers."));
103
+        nickname =
104
+                new ValidatingJTextField(new RegexValidator(NICKNAME_REGEX,
105
+                "Nickname must only contain letters, numbers and []{}|-^\\."));
106
+        realname = new ValidatingJTextField(new NotEmptyValidator());
107
+        ident = new ValidatingJTextField(new RegexValidator(NICKNAME_REGEX,
108
+                "Ident must only contain letters, numbers and []{}|-^\\."));
137 109
         altNicknames = new JList(new DefaultListModel());
138 110
 
139
-        nameError =
140
-                new ImageButton("nicknameError", normalIcon);
141
-        nicknameError =
142
-                new ImageButton("nicknameError", normalIcon);
143
-        realnameError =
144
-                new ImageButton("realnameError", normalIcon);
145
-        identError = new ImageButton("identError", normalIcon);
146
-        altNicknamesError =
147
-                new ImageButton("altNicknamesError", normalIcon);
148
-
149
-        nameError.setFocusable(false);
150
-        nicknameError.setFocusable(false);
151
-        realnameError.setFocusable(false);
152
-        identError.setFocusable(false);
153
-        altNicknamesError.setFocusable(false);
154
-
155
-        map.put(name.getDocument(), nameError);
156
-        map.put(nickname.getDocument(), nicknameError);
157
-        map.put(realname.getDocument(), realnameError);
158
-        map.put(ident.getDocument(), identError);
111
+        addButton = new JButton("Add");
112
+        delButton = new JButton("Delete");
113
+        editButton = new JButton("Edit");
159 114
 
160 115
         name.setPreferredSize(new Dimension(0, name.getFont().getSize()));
161 116
         nickname.setPreferredSize(new Dimension(0, nickname.getFont().getSize()));
@@ -163,21 +118,8 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
163 118
         ident.setPreferredSize(new Dimension(0, nickname.getFont().getSize()));
164 119
         altNicknames.setVisibleRowCount(3);
165 120
 
166
-        name.getDocument().addDocumentListener(this);
167
-        nickname.getDocument().addDocumentListener(this);
168
-        realname.getDocument().addDocumentListener(this);
169
-        ident.getDocument().addDocumentListener(this);
170 121
         altNicknames.addListSelectionListener(this);
171 122
         altNicknames.getModel().addListDataListener(this);
172
-    }
173
-
174
-    /** Initialiases the components in the buttons panel. */
175
-    private void initButtonsPanel() {
176
-        buttonsPanel =
177
-                new JPanel(new GridLayout(1, 3, SMALL_BORDER, SMALL_BORDER));
178
-        addButton = new JButton("Add");
179
-        delButton = new JButton("Delete");
180
-        editButton = new JButton("Edit");
181 123
 
182 124
         addButton.addActionListener(this);
183 125
         delButton.addActionListener(this);
@@ -186,38 +128,26 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
186 128
 
187 129
     /** Lays out the components in the panel. */
188 130
     private void layoutComponents() {
189
-        buttonsPanel.add(addButton);
190
-        buttonsPanel.add(editButton);
191
-        buttonsPanel.add(delButton);
192
-
193
-        setLayout(new SpringLayout());
131
+        setLayout(new MigLayout("fill"));
194 132
 
195 133
         add(new JLabel("Name:"));
196
-        add(nameError);
197
-        add(name);
134
+        add(name, "growx, pushx, wrap");
198 135
 
199 136
         add(new JLabel("Nickname:"));
200
-        add(nicknameError);
201
-        add(nickname);
137
+        add(nickname, "growx, pushx, wrap");
202 138
 
203 139
         add(new JLabel("Realname:"));
204
-        add(realnameError);
205
-        add(realname);
140
+        add(realname, "growx, pushx, wrap");
206 141
 
207 142
         add(new JLabel("Ident:"));
208
-        add(identError);
209
-        add(ident);
143
+        add(ident, "growx, pushx, wrap");
210 144
 
211 145
         add(new JLabel("Alternate nicknames:"));
212
-        add(altNicknamesError);
213
-        add(new JScrollPane(altNicknames));
214
-
215
-        add(Box.createHorizontalGlue());
216
-        add(Box.createHorizontalGlue());
217
-        add(buttonsPanel);
146
+        add(new JScrollPane(altNicknames), "growx, pushx, wrap");
218 147
 
219
-        layoutGrid(this, 6, 3, SMALL_BORDER, SMALL_BORDER, SMALL_BORDER,
220
-                SMALL_BORDER);
148
+        add(addButton, "skip 1, split 3, sg button, growx");
149
+        add(editButton, "sg button, growx");
150
+        add(delButton, "sg button, growx");
221 151
     }
222 152
 
223 153
     /**
@@ -247,7 +177,7 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
247 177
         for (String altNick : profile.getAltNicknames()) {
248 178
             ((DefaultListModel) altNicknames.getModel()).addElement(altNick);
249 179
         }
250
-        
180
+
251 181
         name.setEnabled(true);
252 182
         nickname.setEnabled(true);
253 183
         realname.setEnabled(true);
@@ -272,8 +202,6 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
272 202
         addButton.setEnabled(false);
273 203
         delButton.setEnabled(false);
274 204
         editButton.setEnabled(false);
275
-        
276
-        clearErrors();
277 205
     }
278 206
 
279 207
     /** Saves the detail panels details to the profile. */
@@ -293,205 +221,85 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
293 221
             profile.addAltNickname((String) enumeration.nextElement());
294 222
         }
295 223
     }
296
-    
297
-    /** Clears error states. */
298
-    private void clearErrors() {
299
-        nameError.setToolTipText("");
300
-        nicknameError.setToolTipText("");
301
-        realnameError.setToolTipText("");
302
-        identError.setToolTipText("");
303
-        altNicknamesError.setToolTipText("");
304
-        
305
-        nameError.setIcons(normalIcon);
306
-        nicknameError.setIcons(normalIcon);
307
-        realnameError.setIcons(normalIcon);
308
-        identError.setIcons(normalIcon);
309
-        altNicknamesError.setIcons(normalIcon);
310
-    }
311 224
 
312 225
     /**
313 226
      * Validates the current details.
314 227
      *
315 228
      * @return Validation Result
316 229
      */
317
-    public ValidationResult validateDetails() {
318
-        clearErrors();
319
-
320
-        final ValidationResult checkAltNicknames = checkAltNicknames();
321
-        final ValidationResult checkIdent = checkIdent();
322
-        final ValidationResult checkRealname = checkRealname();
323
-        final ValidationResult checkNickname = checkNickname();
324
-        final ValidationResult checkName = checkName();
325
-
326
-        if (checkAltNicknames.equals(ValidationResult.FAIL) ||
327
-                checkIdent.equals(ValidationResult.FAIL) ||
328
-                checkRealname.equals(ValidationResult.FAIL) ||
329
-                checkNickname.equals(ValidationResult.FAIL) || 
330
-                checkName.equals(ValidationResult.FAIL)) {
331
-            return ValidationResult.FAIL;
332
-        } else if (checkAltNicknames.equals(ValidationResult.WARNING) ||
333
-                checkIdent.equals(ValidationResult.WARNING) ||
334
-                checkRealname.equals(ValidationResult.WARNING) ||
335
-                checkNickname.equals(ValidationResult.WARNING) ||
336
-                checkName.equals(ValidationResult.WARNING)) {
337
-            return ValidationResult.WARNING;
338
-        } else {
339
-            return ValidationResult.PASS;
340
-        }
341
-    }
342
-
343
-    /**
344
-     * Validates the alternate nicknames.
345
-     *
346
-     * @return Validation result
347
-     */
348
-    private ValidationResult checkAltNicknames() {
349
-        if (!altNicknames.isEnabled() || altNicknames.getModel().getSize() == 0) {
350
-            return ValidationResult.PASS;
351
-        }
352
-
353
-        ValidationResult returnValue =
354
-                ValidationResult.PASS;
355
-        final Enumeration<?> enumeration =
356
-                ((DefaultListModel) altNicknames.getModel()).elements();
357
-        while (enumeration.hasMoreElements()) {
358
-            final String altNickname =
359
-                    (String) enumeration.nextElement();
360
-            if (altNickname.isEmpty() || altNickname.matches(NICKNAME_REGEX)) {
361
-                altNicknamesError.setIcons(errorIcon);
362
-                altNicknamesError.setToolTipText("Your nickname cannot be blank.");
363
-                return ValidationResult.FAIL;
364
-            } else if (altNickname.length() <= 1 || altNickname.length() > 9) {
365
-                altNicknamesError.setIcons(warningIcon);
366
-                returnValue =
367
-                        ValidationResult.WARNING;
368
-                altNicknamesError.setToolTipText("Some servers may not allow nicknames this "
369
-                    + (altNickname.length() <= 1 ? "short" : "long") + ".");
370
-            }
371
-        }
372
-
373
-        return returnValue;
374
-    }
375
-
376
-    /**
377
-     * Validates the ident.
378
-     *
379
-     * @return Validation result
380
-     */
381
-    private ValidationResult checkIdent() {
382
-        ValidationResult returnValue =
383
-                ValidationResult.PASS;
384
-        final String identText = ident.getText();
385
-
386
-        if (!ident.isEnabled()) {
387
-            returnValue = ValidationResult.PASS;
388
-        } else if (identText.matches(IDENT_REGEX)) {
389
-            identError.setIcons(errorIcon);
390
-            ident.requestFocus();
391
-            returnValue =
392
-                    ValidationResult.FAIL;
393
-            identError.setToolTipText("Your ident cannot be blank.");
394
-        }
395
-
396
-        return returnValue;
397
-    }
398
-
399
-    /**
400
-     * Validates the realname.
401
-     *
402
-     * @return Validation result
403
-     */
404
-    private ValidationResult checkRealname() {
405
-        ValidationResult returnValue =
406
-                ValidationResult.PASS;
407
-        final String realnameText = realname.getText();
408
-
409
-        if (!realname.isEnabled()) {
410
-            returnValue = ValidationResult.PASS;
411
-        } else if (realnameText.isEmpty()) {
412
-            realnameError.setIcons(errorIcon);
413
-            realname.requestFocus();
414
-            returnValue =
415
-                    ValidationResult.FAIL;
416
-            realnameError.setToolTipText("Your realname cannot be blank.");
230
+    public boolean validateDetails() {
231
+        if (!ident.validateText() || !realname.validateText() ||
232
+                !nickname.validateText() || !name.validateText()) {
233
+            return false;
417 234
         }
418
-
419
-        return returnValue;
235
+        return true;
420 236
     }
421 237
 
422
-    /**
423
-     * Validates the nickname.
424
-     *
425
-     * @return Validation result
238
+    /** 
239
+     * {@inheritDoc}
240
+     * 
241
+     * @param e Action event
426 242
      */
427
-    private ValidationResult checkNickname() {
428
-        ValidationResult returnValue =
429
-                ValidationResult.PASS;
430
-        final String nicknameText = nickname.getText();
431
-
432
-        if (!nickname.isEnabled()) {
433
-            returnValue = ValidationResult.PASS;
434
-        } else if (nicknameText.isEmpty() || nicknameText.matches(NICKNAME_REGEX)) {
435
-            nicknameError.setIcons(errorIcon);
436
-            nickname.requestFocus();
437
-            returnValue =
438
-                    ValidationResult.FAIL;
439
-            nicknameError.setToolTipText("Your nickname cannot be blank.");
440
-        } else if (nicknameText.length() <= 1 || nicknameText.length() > 9) {
441
-            nicknameError.setIcons(warningIcon);
442
-            returnValue =
443
-                    ValidationResult.WARNING;
444
-            nicknameError.setToolTipText("Some servers may not allow nicknames this "
445
-                    + (nicknameText.length() <= 1 ? "short" : "long") + ".");
446
-        }
447
-
448
-        return returnValue;
449
-    }
450
-
451
-    /**
452
-     * Validates the name.
453
-     *
454
-     * @return Validation result
455
-     */
456
-    private ValidationResult checkName() {
457
-        ValidationResult returnValue =
458
-                ValidationResult.PASS;
459
-        final String nameText = name.getText();
460
-
461
-        if (!name.isEnabled()) {
462
-            returnValue = ValidationResult.PASS;
463
-        } else if (nameText.isEmpty() || nameText.matches(FILENAME_REGEX)) {
464
-            nameError.setIcons(errorIcon);
465
-            name.requestFocus();
466
-            returnValue = ValidationResult.FAIL;
467
-            nameError.setToolTipText("The profile name "
468
-                    + (nameText.isEmpty() ? "must not be blank." :
469
-                        "may only contain letters, numbers, underscores and spaces."));
470
-        }
471
-
472
-        return returnValue;
473
-    }
474
-
475
-    /** {@inheritDoc} */
476 243
     @Override
477 244
     public void actionPerformed(final ActionEvent e) {
478 245
         if (e.getSource() == addButton) {
479
-            final String newName =
480
-                    JOptionPane.showInputDialog(this,
246
+            new StandardInputDialog((MainFrame) Main.getUI().getMainWindow(),
247
+                    false, "New Alternate Nickname",
481 248
                     "Please enter the name for alternate nickname",
482
-                    "New Alternate Nickname");
483
-            if (newName != null && !newName.isEmpty()) {
484
-                ((DefaultListModel) altNicknames.getModel()).addElement(newName);
485
-            }
249
+                    new RegexValidator(NICKNAME_REGEX,
250
+                    "Ident must only contain letters, numbers and []{}|-^\\.")) {
251
+
252
+                /**
253
+                 * A version number for this class. It should be changed whenever the class
254
+                 * structure is changed (or anything else that would prevent serialized
255
+                 * objects being unserialized with the new class).
256
+                 */
257
+                private static final long serialVersionUID = 2;
258
+
259
+                /** {@inheritDoc} */
260
+                @Override
261
+                public boolean save() {
262
+                    ((DefaultListModel) altNicknames.getModel()).addElement(getText());
263
+                    return true;
264
+                }
265
+
266
+                /** {@inheritDoc} */
267
+                @Override
268
+                public void cancelled() {
269
+                //Ignore
270
+                }
271
+            }.display();
486 272
         } else if (e.getSource() == editButton) {
487
-            final String newName =
488
-                    JOptionPane.showInputDialog(this,
489
-                    "Please enter the new nickname for the alternate nickname",
490
-                    altNicknames.getSelectedValue());
491
-            if (newName != null && !newName.isEmpty()) {
492
-                ((DefaultListModel) altNicknames.getModel()).setElementAt(newName,
493
-                        altNicknames.getSelectedIndex());
494
-            }
273
+            final StandardInputDialog dialog = new StandardInputDialog(
274
+                    (MainFrame) Main.getUI().getMainWindow(),
275
+                    false, "New Alternate Nickname",
276
+                    "Please enter the name for alternate nickname",
277
+                    new RegexValidator(NICKNAME_REGEX,
278
+                    "Ident must only contain letters, numbers and []{}|-^\\.")) {
279
+
280
+                /**
281
+                 * A version number for this class. It should be changed whenever the class
282
+                 * structure is changed (or anything else that would prevent serialized
283
+                 * objects being unserialized with the new class).
284
+                 */
285
+                private static final long serialVersionUID = 2;
286
+
287
+                /** {@inheritDoc} */
288
+                @Override
289
+                public boolean save() {
290
+                    ((DefaultListModel) altNicknames.getModel()).setElementAt(
291
+                            getText(), altNicknames.getSelectedIndex());
292
+                    return true;
293
+                }
294
+
295
+                /** {@inheritDoc} */
296
+                @Override
297
+                public void cancelled() {
298
+                //Ignore
299
+                }
300
+            };
301
+            dialog.setText((String) altNicknames.getSelectedValue());
302
+            dialog.display();
495 303
         } else if (e.getSource() == delButton) {
496 304
             if (JOptionPane.showConfirmDialog(this,
497 305
                     "Are you sure you want to delete this nickname?",
@@ -514,24 +322,6 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
514 322
         }
515 323
     }
516 324
 
517
-    /** {@inheritDoc} */
518
-    @Override
519
-    public void insertUpdate(final DocumentEvent e) {
520
-        validateDetails();
521
-    }
522
-
523
-    /** {@inheritDoc} */
524
-    @Override
525
-    public void removeUpdate(final DocumentEvent e) {
526
-        validateDetails();
527
-    }
528
-
529
-    /** {@inheritDoc} */
530
-    @Override
531
-    public void changedUpdate(final DocumentEvent e) {
532
-        //Ignore
533
-    }
534
-
535 325
     /** {@inheritDoc} */
536 326
     @Override
537 327
     public void intervalAdded(final ListDataEvent e) {
@@ -549,4 +339,4 @@ public class ProfileDetailPanel extends JPanel implements ActionListener,
549 339
     public void contentsChanged(final ListDataEvent e) {
550 340
         validateDetails();
551 341
     }
552
-}
342
+}

+ 13
- 8
src/com/dmdirc/ui/swing/dialogs/profiles/ProfileManagerDialog.java View File

@@ -25,9 +25,9 @@ package com.dmdirc.ui.swing.dialogs.profiles;
25 25
 import com.dmdirc.Main;
26 26
 import com.dmdirc.config.Identity;
27 27
 import com.dmdirc.config.IdentityManager;
28
+import com.dmdirc.ui.swing.JWrappingLabel;
28 29
 import com.dmdirc.ui.swing.MainFrame;
29 30
 import com.dmdirc.ui.swing.components.StandardDialog;
30
-import com.dmdirc.ui.swing.components.TextLabel;
31 31
 
32 32
 import java.awt.Dimension;
33 33
 import java.awt.event.ActionEvent;
@@ -42,6 +42,7 @@ import javax.swing.JScrollPane;
42 42
 import javax.swing.WindowConstants;
43 43
 import javax.swing.event.ListSelectionEvent;
44 44
 import javax.swing.event.ListSelectionListener;
45
+
45 46
 import net.miginfocom.swing.MigLayout;
46 47
 
47 48
 /** Profile editing dialog. */
@@ -65,7 +66,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
65 66
     /** Profile detail panel. */
66 67
     private ProfileDetailPanel details;
67 68
     /** Info label. */
68
-    private TextLabel infoLabel;
69
+    private JWrappingLabel infoLabel;
69 70
     /** Add button. */
70 71
     private JButton addButton;
71 72
     /** Delete button. */
@@ -94,6 +95,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
94 95
     public static synchronized void showProfileManagerDialog() {
95 96
         me = getProfileManagerDialog();
96 97
 
98
+        me.pack();
97 99
         me.setLocationRelativeTo((MainFrame) Main.getUI().getMainWindow());
98 100
         me.setVisible(true);
99 101
         me.requestFocus();
@@ -115,8 +117,6 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
115 117
     /** Initialises the components. */
116 118
     private void initComponents() {
117 119
         setTitle("Profile Editor");
118
-        setMinimumSize(new Dimension(600, 400));
119
-        //setPreferredSize(new Dimension(600, 400));
120 120
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
121 121
         setResizable(false);
122 122
         
@@ -128,7 +128,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
128 128
         addButton = new JButton("Add");
129 129
         deleteButton = new JButton("Delete");
130 130
         infoLabel =
131
-                new TextLabel("Profiles describe information needed to " +
131
+                new JWrappingLabel("Profiles describe information needed to " +
132 132
                 "connect to a server.  You can use a different profile for " +
133 133
                 "each connection. Profiles are automatically saved when you " +
134 134
                 "select another or click OK");
@@ -142,6 +142,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
142 142
     private void layoutComponents() {
143 143
         getContentPane().setLayout(new MigLayout("fill"));
144 144
 
145
+        infoLabel.setMaximumSize(new Dimension(400, 0));
145 146
         getContentPane().add(new JScrollPane(profileList), "spany 2, grow, " +
146 147
                 "wmax 200, wmin 200");
147 148
         getContentPane().add(infoLabel, "wrap, growx, spanx 2");
@@ -182,7 +183,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
182 183
 
183 184
     /** Saves the profile list. */
184 185
     private void save() {
185
-        if (!details.validateDetails().equals(ValidationResult.FAIL)) {
186
+        if (details.validateDetails()) {
186 187
             details.save();
187 188
             final Iterator<Profile> it = model.iterator();
188 189
 
@@ -194,7 +195,11 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
194 195
         }
195 196
     }
196 197
 
197
-    /** {@inheritDoc} */
198
+    /** 
199
+     * {@inheritDoc}
200
+     * 
201
+     * @param e Action event
202
+     */
198 203
     @Override
199 204
     public void actionPerformed(final ActionEvent e) {
200 205
         if (e.getSource().equals(getOkButton())) {
@@ -219,7 +224,7 @@ public final class ProfileManagerDialog extends StandardDialog implements Action
219 224
     @Override
220 225
     public void valueChanged(final ListSelectionEvent e) {
221 226
         if (e.getValueIsAdjusting()) {
222
-            if (details.validateDetails().equals(ValidationResult.FAIL)) {
227
+            if (!details.validateDetails()) {
223 228
                 profileList.setSelectedIndex(selectedIndex);
224 229
             }
225 230
         }

Loading…
Cancel
Save