Преглед на файлове

Tidy up new server dialog linker.

pull/147/head
Greg Holmes преди 9 години
родител
ревизия
42bc7958d5

+ 1
- 0
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/newserver/NewServerDialog.java Целия файл

@@ -71,6 +71,7 @@ public class NewServerDialog extends StandardDialog {
71 71
                 = new NoBorderJCheckBox("Save these settings as the defaults?");
72 72
 
73 73
         model.loadModel();
74
+        linker.init(edit, getOkButton(), hostname);
74 75
         linker.bindHostname(hostname);
75 76
         linker.bindPort(port);
76 77
         linker.bindPassword(password);

+ 55
- 102
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/newserver/NewServerLinker.java Целия файл

@@ -22,131 +22,80 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.dialogs.newserver;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.ConsumerDocumentListener;
25 26
 import com.dmdirc.addons.ui_swing.components.renderers.PropertyListCellRenderer;
26 27
 import com.dmdirc.addons.ui_swing.components.vetoable.VetoableComboBoxModel;
27 28
 import com.dmdirc.addons.ui_swing.dialogs.profile.ProfileManagerDialog;
28 29
 import com.dmdirc.addons.ui_swing.injection.DialogProvider;
29 30
 import com.dmdirc.config.profiles.Profile;
30 31
 import com.dmdirc.interfaces.ui.NewServerDialogModel;
31
-import com.dmdirc.ui.core.newserver.NewServerDialogModelAdapter;
32
+import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
32 33
 
33 34
 import java.awt.event.ItemEvent;
34 35
 import java.beans.PropertyVetoException;
36
+import java.util.List;
35 37
 import java.util.Optional;
36 38
 
37 39
 import javax.swing.JButton;
38 40
 import javax.swing.JCheckBox;
39 41
 import javax.swing.JComboBox;
40 42
 import javax.swing.JTextField;
41
-import javax.swing.event.DocumentEvent;
42
-import javax.swing.event.DocumentListener;
43 43
 
44 44
 /**
45 45
  * Links the New Server Dialog with its model.
46 46
  */
47
-public class NewServerLinker {
47
+public class NewServerLinker implements NewServerDialogModelListener {
48 48
 
49 49
     private final NewServerDialogModel model;
50 50
     private final NewServerDialog dialog;
51
+    private JButton okButton;
52
+    private JButton edit;
53
+    private JTextField hostnameField;
54
+    private VetoableComboBoxModel<Profile> comboBoxModel;
51 55
 
52 56
     public NewServerLinker(final NewServerDialogModel model, final NewServerDialog dialog) {
53 57
         this.model = model;
54 58
         this.dialog = dialog;
55 59
     }
56 60
 
57
-    public void bindHostname(final JTextField hostnameField) {
58
-        hostnameField.getDocument().addDocumentListener(new DocumentListener() {
59
-
60
-            private void update() {
61
-                model.setHostname(Optional.ofNullable(hostnameField.getText()));
62
-            }
63
-
64
-            @Override
65
-            public void insertUpdate(final DocumentEvent e) {
66
-                update();
67
-            }
68
-
69
-            @Override
70
-            public void removeUpdate(final DocumentEvent e) {
71
-                update();
72
-            }
61
+    public void init(final JButton edit, final JButton okButton,
62
+            final JTextField hostnameField) {
63
+        this.edit = edit;
64
+        this.hostnameField = hostnameField;
65
+        this.okButton = okButton;
66
+        comboBoxModel = new VetoableComboBoxModel<>();
67
+        model.addListener(this);
68
+    }
73 69
 
74
-            @Override
75
-            public void changedUpdate(final DocumentEvent e) {
76
-                update();
77
-            }
78
-        });
79
-        model.addListener(new NewServerDialogModelAdapter() {
80
-
81
-            @Override
82
-            public void serverDetailsChanged(final Optional<String> hostname,
83
-                    final Optional<Integer> port, final Optional<String> password,
84
-                    final boolean ssl, final boolean saveAsDefault) {
85
-                if (!hostname.equals(Optional.ofNullable(hostnameField.getText()))) {
86
-                    hostnameField.setText(hostname.isPresent() ? hostname.get() : "");
87
-                }
88
-            }
89
-        });
70
+    public void bindHostname(final JTextField hostnameField) {
71
+        hostnameField.getDocument().addDocumentListener(new ConsumerDocumentListener(
72
+                s -> model.setHostname(Optional.ofNullable(s))
73
+        ));
90 74
         hostnameField.setText(model.getHostname().isPresent() ? model.getHostname().get() : "");
91 75
     }
92 76
 
93 77
     public void bindPort(final JTextField portField) {
94
-        portField.getDocument().addDocumentListener(new DocumentListener() {
95
-
96
-            private void update() {
97
-                try {
98
-                    model.setPort(Optional.ofNullable(Integer.valueOf(portField.getText())));
99
-                } catch (NumberFormatException ex) {
100
-                    //Do nothing, it'll have to be corrected and its handled by the validator.
78
+        portField.getDocument().addDocumentListener(new ConsumerDocumentListener(
79
+                s -> {
80
+                    try {
81
+                        model.setPort(Optional.ofNullable(Integer.valueOf(s)));
82
+                    } catch (NumberFormatException ex) {
83
+                        //Do nothing, it'll have to be corrected and its handled by the validator.
84
+                    }
101 85
                 }
102
-            }
103
-
104
-            @Override
105
-            public void insertUpdate(final DocumentEvent e) {
106
-                update();
107
-            }
108
-
109
-            @Override
110
-            public void removeUpdate(final DocumentEvent e) {
111
-                update();
112
-            }
113
-
114
-            @Override
115
-            public void changedUpdate(final DocumentEvent e) {
116
-                update();
117
-            }
118
-        });
119
-        portField.setText(model.getPort().isPresent() ? Integer.toString(model.getPort().get())
120
-                : "");
86
+        ));
87
+        portField.setText(
88
+                model.getPort().isPresent() ? Integer.toString(model.getPort().get()) : "");
121 89
     }
122 90
 
123 91
     public void bindPassword(final JTextField passwordField) {
124
-        passwordField.getDocument().addDocumentListener(new DocumentListener() {
125
-
126
-            private void update() {
127
-                model.setPassword(Optional.ofNullable(passwordField.getText()));
128
-            }
129
-
130
-            @Override
131
-            public void insertUpdate(final DocumentEvent e) {
132
-                update();
133
-            }
134
-
135
-            @Override
136
-            public void removeUpdate(final DocumentEvent e) {
137
-                update();
138
-            }
139
-
140
-            @Override
141
-            public void changedUpdate(final DocumentEvent e) {
142
-                update();
143
-            }
144
-        });
92
+        passwordField.getDocument().addDocumentListener(new ConsumerDocumentListener(
93
+                s -> model.setPassword(Optional.ofNullable(s))
94
+        ));
145 95
         passwordField.setText(model.getPassword().isPresent() ? model.getPassword().get() : "");
146 96
     }
147 97
 
148 98
     public void bindProfiles(final JComboBox<Profile> profilesCombobox) {
149
-        final VetoableComboBoxModel<Profile> comboBoxModel = new VetoableComboBoxModel<>();
150 99
         profilesCombobox.setModel(comboBoxModel);
151 100
         if (model.getSelectedProfile().isPresent()) {
152 101
             comboBoxModel.setSelectedItem(model.getSelectedProfile().get());
@@ -171,15 +120,6 @@ public class NewServerLinker {
171 120
     public void bindEditProfiles(final JButton edit,
172 121
             final DialogProvider<ProfileManagerDialog> profileManagerDialog) {
173 122
         edit.addActionListener(e -> profileManagerDialog.displayOrRequestFocus());
174
-        model.addListener(new NewServerDialogModelAdapter() {
175
-
176
-            @Override
177
-            public void selectedProfileChanged(final Optional<Profile> oldProfile,
178
-                    final Optional<Profile> newProfile) {
179
-                edit.setEnabled(model.isProfileListValid()
180
-                        && model.getSelectedProfile().isPresent());
181
-            }
182
-        });
183 123
     }
184 124
 
185 125
     public void bindSSL(final JCheckBox sslCheckbox) {
@@ -198,15 +138,6 @@ public class NewServerLinker {
198 138
             model.save();
199 139
             dialog.dispose();
200 140
         });
201
-        model.addListener(new NewServerDialogModelAdapter() {
202
-
203
-            @Override
204
-            public void serverDetailsChanged(final Optional<String> hostname,
205
-                    final Optional<Integer> port, final Optional<String> password,
206
-                    final boolean ssl, final boolean saveAsDefault) {
207
-                okButton.setEnabled(model.isSaveAllowed());
208
-            }
209
-        });
210 141
         okButton.setEnabled(model.isSaveAllowed());
211 142
     }
212 143
 
@@ -214,4 +145,26 @@ public class NewServerLinker {
214 145
         cancelButton.addActionListener(e -> dialog.dispose());
215 146
     }
216 147
 
148
+    @Override
149
+    public void selectedProfileChanged(final Optional<Profile> oldProfile,
150
+            final Optional<Profile> newProfile) {
151
+        edit.setEnabled(model.isProfileListValid() && model.getSelectedProfile().isPresent());
152
+    }
153
+
154
+    @Override
155
+    public void profileListChanged(final List<Profile> profiles) {
156
+        comboBoxModel.removeAllElements();
157
+        profiles.forEach(comboBoxModel::addElement);
158
+        edit.setEnabled(model.isProfileListValid() && model.getSelectedProfile().isPresent());
159
+        okButton.setEnabled(model.isSaveAllowed());
160
+    }
161
+
162
+    @Override
163
+    public void serverDetailsChanged(final Optional<String> hostname, final Optional<Integer> port,
164
+            final Optional<String> password, final boolean ssl, final boolean saveAsDefault) {
165
+        if (!hostname.equals(Optional.ofNullable(hostnameField.getText()))) {
166
+            hostnameField.setText(hostname.isPresent() ? hostname.get() : "");
167
+        }
168
+        okButton.setEnabled(model.isSaveAllowed());
169
+    }
217 170
 }

Loading…
Отказ
Запис