Browse Source

Adds vetoable selection listeners and made NSD use them to prevent de-selection.

Fixes issue 4126

Change-Id: I07dbbe0ea7afc5907be20c157b6753bc7e32b122
Reviewed-on: http://gerrit.dmdirc.com/1231
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.4
Greboid 14 years ago
parent
commit
e384f363cd

+ 60
- 0
src/com/dmdirc/addons/ui_swing/components/vetoable/VetoableChangeEvent.java View File

@@ -0,0 +1,60 @@
1
+/*
2
+ * Copyright (c) 2006-2010 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.addons.ui_swing.components.vetoable;
24
+
25
+import javax.swing.event.ChangeEvent;
26
+
27
+/**
28
+ * Vetoable change event.
29
+ */
30
+public class VetoableChangeEvent extends ChangeEvent {
31
+    /**
32
+     * A version number for this class. It should be changed whenever the class
33
+     * structure is changed (or anything else that would prevent serialized
34
+     * objects being unserialized with the new class).
35
+     */
36
+    private static final long serialVersionUID = 1;
37
+    /** New value. */
38
+    private Object newValue;
39
+
40
+    /**
41
+     * Creates a new vetoable change event.
42
+     *
43
+     * @param source Event source
44
+     * @param newValue New value
45
+     */
46
+    public VetoableChangeEvent(final Object source, final Object newValue ) {
47
+        super(source);
48
+        this.newValue = newValue;
49
+    }
50
+
51
+    /**
52
+     * Returns the new value for the event,
53
+     *
54
+     * @return New value
55
+     */
56
+    public Object getNewValue() {
57
+        return newValue;
58
+    }
59
+
60
+}

+ 107
- 0
src/com/dmdirc/addons/ui_swing/components/vetoable/VetoableComboBoxModel.java View File

@@ -0,0 +1,107 @@
1
+/*
2
+ * Copyright (c) 2006-2010 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.addons.ui_swing.components.vetoable;
24
+
25
+import com.dmdirc.util.ListenerList;
26
+
27
+import javax.swing.DefaultComboBoxModel;
28
+
29
+/**
30
+ * Vetoable combo box model.
31
+ */
32
+public class VetoableComboBoxModel extends DefaultComboBoxModel {
33
+
34
+    /**
35
+     * A version number for this class. It should be changed whenever the class
36
+     * structure is changed (or anything else that would prevent serialized
37
+     * objects being unserialized with the new class).
38
+     */
39
+    private static final long serialVersionUID = 1;
40
+    /** Listener list. */
41
+    private ListenerList listeners = new ListenerList();
42
+
43
+    /**
44
+     * Constructs an empty DefaultComboBoxModel object.
45
+     */
46
+    public VetoableComboBoxModel() {
47
+        super();
48
+    }
49
+
50
+    /**
51
+     * Constructs a DefaultComboBoxModel object initialized with
52
+     * an array of objects.
53
+     *
54
+     * @param items  an array of Object objects
55
+     */
56
+    public VetoableComboBoxModel(final Object[] items) {
57
+        super(items);
58
+    }
59
+
60
+    /**
61
+     * Adds a vetaoble selection listener to this model.
62
+     *
63
+     * @param l Listener to add
64
+     */
65
+    public void addVetoableSelectionListener(
66
+            final VetoableComboBoxSelectionListener l) {
67
+        listeners.add(VetoableComboBoxSelectionListener.class, l);
68
+    }
69
+
70
+    /**
71
+     * Removes a vetoable selection listener from this model.
72
+     *
73
+     * @param l Listener to remove
74
+     */
75
+    public void removeVetoableSelectionListener(
76
+            final VetoableComboBoxSelectionListener l) {
77
+        listeners.remove(VetoableComboBoxSelectionListener.class, l);
78
+    }
79
+
80
+    /**
81
+     * Fires a vetoable selection change event.
82
+     *
83
+     * @param newValue New value
84
+     *
85
+     * @return true iif the event is to be vetoed
86
+     */
87
+    protected boolean fireVetoableSelectionChange(final Object newValue) {
88
+        boolean result = true;
89
+        VetoableChangeEvent event = new VetoableChangeEvent(this, newValue);
90
+        for (VetoableComboBoxSelectionListener listener :
91
+            listeners.get(VetoableComboBoxSelectionListener.class)) {
92
+                result &= listener.selectionChanged(event);
93
+        }
94
+        return result;
95
+    }
96
+
97
+    /** {@inheritDoc} */
98
+    @Override
99
+    public void setSelectedItem(final Object anItem) {
100
+        final Object oldItem = getSelectedItem();
101
+
102
+        super.setSelectedItem(anItem);
103
+        if (!fireVetoableSelectionChange(anItem)) {
104
+            super.setSelectedItem(oldItem);
105
+        }
106
+    }
107
+}

+ 40
- 0
src/com/dmdirc/addons/ui_swing/components/vetoable/VetoableComboBoxSelectionListener.java View File

@@ -0,0 +1,40 @@
1
+/*
2
+ * Copyright (c) 2006-2010 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.addons.ui_swing.components.vetoable;
24
+
25
+import java.util.EventListener;
26
+
27
+/**
28
+ * Vetoable change listener.
29
+ */
30
+public interface VetoableComboBoxSelectionListener extends EventListener {
31
+
32
+    /**
33
+     * A vetoable selection event has occurred.
34
+     *
35
+     * @param e Change event
36
+     *
37
+     * @return true iif the selection is allowed to happen
38
+     */
39
+    boolean selectionChanged(VetoableChangeEvent e);
40
+}

+ 21
- 6
src/com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.java View File

@@ -25,12 +25,15 @@ package com.dmdirc.addons.ui_swing.dialogs;
25 25
 import com.dmdirc.Server;
26 26
 import com.dmdirc.ServerManager;
27 27
 import com.dmdirc.addons.ui_swing.MainFrame;
28
+import com.dmdirc.addons.ui_swing.components.vetoable.VetoableChangeEvent;
28 29
 import com.dmdirc.config.Identity;
29 30
 import com.dmdirc.config.IdentityManager;
30 31
 import com.dmdirc.config.prefs.validator.PortValidator;
31 32
 import com.dmdirc.config.prefs.validator.RegexStringValidator;
32 33
 import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
33 34
 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
35
+import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
36
+import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxSelectionListener;
34 37
 import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
35 38
 
36 39
 import com.dmdirc.logger.ErrorLevel;
@@ -58,7 +61,8 @@ import net.miginfocom.swing.MigLayout;
58 61
 /**
59 62
  * Dialog that allows the user to enter details of a new server to connect to.
60 63
  */
61
-public final class NewServerDialog extends StandardDialog implements ActionListener {
64
+public final class NewServerDialog extends StandardDialog implements 
65
+        ActionListener, VetoableComboBoxSelectionListener {
62 66
 
63 67
     /**
64 68
      * A version number for this class. It should be changed whenever the class
@@ -174,6 +178,8 @@ public final class NewServerDialog extends StandardDialog implements ActionListe
174 178
         getCancelButton().addActionListener(this);
175 179
         getOkButton().addActionListener(this);
176 180
         editProfileButton.addActionListener(this);
181
+        ((VetoableComboBoxModel) identityField.getModel()).
182
+                addVetoableSelectionListener(this);
177 183
     }
178 184
 
179 185
     /**
@@ -187,7 +193,7 @@ public final class NewServerDialog extends StandardDialog implements ActionListe
187 193
         newServerWindowCheck = new JCheckBox();
188 194
         newServerWindowCheck.setSelected(true);
189 195
         sslCheck = new JCheckBox();
190
-        identityField = new JComboBox();
196
+        identityField = new JComboBox(new VetoableComboBoxModel());
191 197
         editProfileButton = new JButton();
192 198
 
193 199
         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
@@ -266,11 +272,11 @@ public final class NewServerDialog extends StandardDialog implements ActionListe
266 272
         dispose();
267 273
         openingServer = true;
268 274
 
269
-        final Identity profile =
270
-                (Identity) identityField.getSelectedItem();
275
+        final Identity profile = (Identity) identityField.getSelectedItem();
271 276
 
272 277
         try {
273
-            final URI address = new URI("irc" + (sslCheck.isSelected() ? "s" : ""), pass, host, port, null, null, null);
278
+            final URI address = new URI("irc" + (sslCheck.isSelected() ? "s" :
279
+                ""), pass, host, port, null, null, null);
274 280
 
275 281
             // Open in a new window?
276 282
             if (newServerWindowCheck.isSelected() || ServerManager.getServerManager()
@@ -317,7 +323,7 @@ public final class NewServerDialog extends StandardDialog implements ActionListe
317 323
         if (e.getSource() == getOkButton()) {
318 324
             save();
319 325
         } else if (e.getSource() == editProfileButton) {
320
-            ProfileManagerDialog.showProfileManagerDialog(mainFrame );
326
+            ProfileManagerDialog.showProfileManagerDialog(mainFrame);
321 327
         } else if (e.getSource() == getCancelButton()) {
322 328
             dispose();
323 329
         }
@@ -341,4 +347,13 @@ public final class NewServerDialog extends StandardDialog implements ActionListe
341 347
             me = null;
342 348
         }
343 349
     }
350
+
351
+    /** {@inheritDoc} */
352
+    @Override
353
+    public boolean selectionChanged(final VetoableChangeEvent e) {
354
+        if (e.getNewValue() == null) {
355
+            return false;
356
+        }
357
+        return true;
358
+    }
344 359
 }

Loading…
Cancel
Save