diff options
| author | 2020-04-23 00:15:07 +0200 | |
|---|---|---|
| committer | 2020-06-03 23:55:56 +0200 | |
| commit | 5fe6dbaacf5a11f26523889580219068c473b5ce (patch) | |
| tree | 3906a902fea3dada558a52b177a430921f35496a /enigma-swing/src/main/java | |
| parent | Improve CreateServerDialog (diff) | |
| download | enigma-fork-5fe6dbaacf5a11f26523889580219068c473b5ce.tar.gz enigma-fork-5fe6dbaacf5a11f26523889580219068c473b5ce.tar.xz enigma-fork-5fe6dbaacf5a11f26523889580219068c473b5ce.zip | |
Abstract out common code between ConnectToServerDialog and CreateServerDialog
Diffstat (limited to 'enigma-swing/src/main/java')
3 files changed, 142 insertions, 146 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/AbstractDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/AbstractDialog.java new file mode 100644 index 0000000..a4d8e1f --- /dev/null +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/AbstractDialog.java | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | package cuchaz.enigma.gui.dialog; | ||
| 2 | |||
| 3 | import java.awt.*; | ||
| 4 | import java.util.List; | ||
| 5 | |||
| 6 | import javax.swing.JButton; | ||
| 7 | import javax.swing.JDialog; | ||
| 8 | import javax.swing.JLabel; | ||
| 9 | import javax.swing.JPanel; | ||
| 10 | |||
| 11 | import cuchaz.enigma.utils.I18n; | ||
| 12 | import cuchaz.enigma.utils.Pair; | ||
| 13 | import cuchaz.enigma.utils.validation.ValidationContext; | ||
| 14 | |||
| 15 | public abstract class AbstractDialog extends JDialog { | ||
| 16 | |||
| 17 | protected final ValidationContext vc = new ValidationContext(); | ||
| 18 | |||
| 19 | private boolean actionConfirm = false; | ||
| 20 | |||
| 21 | public AbstractDialog(Frame owner, String title, String confirmAction, String cancelAction) { | ||
| 22 | super(owner, I18n.translate(title), true); | ||
| 23 | |||
| 24 | Container contentPane = getContentPane(); | ||
| 25 | contentPane.setLayout(new BorderLayout()); | ||
| 26 | Container inputContainer = new JPanel(new GridBagLayout()); | ||
| 27 | GridBagConstraints c = new GridBagConstraints(); | ||
| 28 | |||
| 29 | List<Pair<String, Component>> components = createComponents(); | ||
| 30 | |||
| 31 | for (int i = 0; i < components.size(); i += 1) { | ||
| 32 | Pair<String, Component> entry = components.get(i); | ||
| 33 | JLabel label = new JLabel(I18n.translate(entry.a)); | ||
| 34 | Component component = entry.b; | ||
| 35 | |||
| 36 | c.gridy = i; | ||
| 37 | c.insets = new Insets(4, 4, 4, 4); | ||
| 38 | |||
| 39 | c.gridx = 0; | ||
| 40 | c.weightx = 0.0; | ||
| 41 | c.anchor = GridBagConstraints.LINE_END; | ||
| 42 | c.fill = GridBagConstraints.NONE; | ||
| 43 | inputContainer.add(label, c); | ||
| 44 | |||
| 45 | c.gridx = 1; | ||
| 46 | c.weightx = 1.0; | ||
| 47 | c.anchor = GridBagConstraints.LINE_START; | ||
| 48 | c.fill = GridBagConstraints.HORIZONTAL; | ||
| 49 | inputContainer.add(component, c); | ||
| 50 | } | ||
| 51 | contentPane.add(inputContainer, BorderLayout.CENTER); | ||
| 52 | Container buttonContainer = new JPanel(new GridBagLayout()); | ||
| 53 | c = new GridBagConstraints(); | ||
| 54 | c.weightx = 1.0; | ||
| 55 | c.insets = new Insets(4, 4, 4, 4); | ||
| 56 | c.anchor = GridBagConstraints.LINE_END; | ||
| 57 | JButton connectButton = new JButton(I18n.translate(confirmAction)); | ||
| 58 | connectButton.addActionListener(event -> confirm()); | ||
| 59 | buttonContainer.add(connectButton, c); | ||
| 60 | c.weightx = 0.0; | ||
| 61 | c.anchor = GridBagConstraints.CENTER; | ||
| 62 | JButton abortButton = new JButton(I18n.translate(cancelAction)); | ||
| 63 | abortButton.addActionListener(event -> cancel()); | ||
| 64 | buttonContainer.add(abortButton, c); | ||
| 65 | contentPane.add(buttonContainer, BorderLayout.SOUTH); | ||
| 66 | |||
| 67 | pack(); | ||
| 68 | setLocationRelativeTo(owner); | ||
| 69 | } | ||
| 70 | |||
| 71 | protected abstract List<Pair<String, Component>> createComponents(); | ||
| 72 | |||
| 73 | protected void confirm() { | ||
| 74 | vc.reset(); | ||
| 75 | validateInputs(); | ||
| 76 | if (vc.canProceed()) { | ||
| 77 | actionConfirm = true; | ||
| 78 | setVisible(false); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | protected void cancel() { | ||
| 83 | actionConfirm = false; | ||
| 84 | setVisible(false); | ||
| 85 | } | ||
| 86 | |||
| 87 | public boolean isActionConfirm() { | ||
| 88 | return actionConfirm; | ||
| 89 | } | ||
| 90 | |||
| 91 | public void validateInputs() { | ||
| 92 | } | ||
| 93 | |||
| 94 | } | ||
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java index 697fc51..79b0c4e 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java | |||
| @@ -1,96 +1,49 @@ | |||
| 1 | package cuchaz.enigma.gui.dialog; | 1 | package cuchaz.enigma.gui.dialog; |
| 2 | 2 | ||
| 3 | import java.awt.*; | 3 | import java.awt.Component; |
| 4 | import java.awt.Dimension; | ||
| 5 | import java.awt.Frame; | ||
| 4 | import java.util.Arrays; | 6 | import java.util.Arrays; |
| 5 | import java.util.List; | 7 | import java.util.List; |
| 6 | import java.util.Objects; | 8 | import java.util.Objects; |
| 7 | import java.util.stream.Collectors; | ||
| 8 | import java.util.stream.Stream; | ||
| 9 | 9 | ||
| 10 | import javax.swing.*; | 10 | import javax.swing.JPasswordField; |
| 11 | import javax.swing.JTextField; | ||
| 11 | 12 | ||
| 12 | import cuchaz.enigma.gui.elements.ValidatableTextField; | 13 | import cuchaz.enigma.gui.elements.ValidatableTextField; |
| 13 | import cuchaz.enigma.network.EnigmaServer; | 14 | import cuchaz.enigma.network.EnigmaServer; |
| 14 | import cuchaz.enigma.utils.I18n; | 15 | import cuchaz.enigma.utils.Pair; |
| 15 | import cuchaz.enigma.utils.ServerAddress; | 16 | import cuchaz.enigma.utils.ServerAddress; |
| 16 | import cuchaz.enigma.utils.validation.Message; | 17 | import cuchaz.enigma.utils.validation.Message; |
| 17 | import cuchaz.enigma.utils.validation.ValidationContext; | ||
| 18 | 18 | ||
| 19 | public class ConnectToServerDialog extends JDialog { | 19 | public class ConnectToServerDialog extends AbstractDialog { |
| 20 | 20 | ||
| 21 | private final ValidationContext vc = new ValidationContext(); | 21 | private JTextField usernameField; |
| 22 | 22 | private ValidatableTextField ipField; | |
| 23 | private final JTextField usernameField; | 23 | private JPasswordField passwordField; |
| 24 | private final ValidatableTextField ipField; | ||
| 25 | private final JPasswordField passwordField; | ||
| 26 | private boolean actionConfirm = false; | ||
| 27 | 24 | ||
| 28 | public ConnectToServerDialog(Frame owner) { | 25 | public ConnectToServerDialog(Frame owner) { |
| 29 | super(owner, I18n.translate("prompt.connect.title"), true); | 26 | super(owner, "prompt.connect.title", "prompt.connect.confirm", "prompt.cancel"); |
| 27 | |||
| 28 | setSize(new Dimension(400, 185)); | ||
| 29 | setLocationRelativeTo(owner); | ||
| 30 | } | ||
| 30 | 31 | ||
| 31 | Container contentPane = getContentPane(); | 32 | @Override |
| 32 | contentPane.setLayout(new BorderLayout()); | 33 | protected List<Pair<String, Component>> createComponents() { |
| 33 | Container inputContainer = new JPanel(new GridBagLayout()); | ||
| 34 | GridBagConstraints c = new GridBagConstraints(); | ||
| 35 | usernameField = new JTextField(System.getProperty("user.name")); | 34 | usernameField = new JTextField(System.getProperty("user.name")); |
| 36 | ipField = new ValidatableTextField(); | 35 | ipField = new ValidatableTextField(); |
| 37 | passwordField = new JPasswordField(); | 36 | passwordField = new JPasswordField(); |
| 38 | 37 | ||
| 39 | List<JLabel> labels = Stream.of("prompt.connect.username", "prompt.connect.address", "prompt.password") | 38 | usernameField.addActionListener(event -> confirm()); |
| 40 | .map(I18n::translate) | 39 | ipField.addActionListener(event -> confirm()); |
| 41 | .map(JLabel::new) | 40 | passwordField.addActionListener(event -> confirm()); |
| 42 | .collect(Collectors.toList()); | ||
| 43 | List<JTextField> inputs = Arrays.asList(usernameField, ipField, passwordField); | ||
| 44 | |||
| 45 | for (int i = 0; i < inputs.size(); i += 1) { | ||
| 46 | c.gridy = i; | ||
| 47 | c.insets = new Insets(4, 4, 4, 4); | ||
| 48 | |||
| 49 | c.gridx = 0; | ||
| 50 | c.weightx = 0.0; | ||
| 51 | c.anchor = GridBagConstraints.LINE_END; | ||
| 52 | c.fill = GridBagConstraints.NONE; | ||
| 53 | inputContainer.add(labels.get(i), c); | ||
| 54 | |||
| 55 | c.gridx = 1; | ||
| 56 | c.weightx = 1.0; | ||
| 57 | c.anchor = GridBagConstraints.LINE_START; | ||
| 58 | c.fill = GridBagConstraints.HORIZONTAL; | ||
| 59 | inputs.get(i).addActionListener(event -> confirm()); | ||
| 60 | inputContainer.add(inputs.get(i), c); | ||
| 61 | } | ||
| 62 | contentPane.add(inputContainer, BorderLayout.CENTER); | ||
| 63 | Container buttonContainer = new JPanel(new GridBagLayout()); | ||
| 64 | c = new GridBagConstraints(); | ||
| 65 | c.weightx = 1.0; | ||
| 66 | c.insets = new Insets(4, 4, 4, 4); | ||
| 67 | c.anchor = GridBagConstraints.LINE_END; | ||
| 68 | JButton connectButton = new JButton(I18n.translate("prompt.connect.confirm")); | ||
| 69 | connectButton.addActionListener(event -> confirm()); | ||
| 70 | buttonContainer.add(connectButton, c); | ||
| 71 | c.weightx = 0.0; | ||
| 72 | c.anchor = GridBagConstraints.CENTER; | ||
| 73 | JButton abortButton = new JButton(I18n.translate("prompt.cancel")); | ||
| 74 | abortButton.addActionListener(event -> cancel()); | ||
| 75 | buttonContainer.add(abortButton, c); | ||
| 76 | contentPane.add(buttonContainer, BorderLayout.SOUTH); | ||
| 77 | |||
| 78 | setLocationRelativeTo(owner); | ||
| 79 | setSize(new Dimension(400, 185)); | ||
| 80 | } | ||
| 81 | |||
| 82 | private void confirm() { | ||
| 83 | vc.reset(); | ||
| 84 | validateInputs(); | ||
| 85 | if (vc.canProceed()) { | ||
| 86 | actionConfirm = true; | ||
| 87 | setVisible(false); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | 41 | ||
| 91 | private void cancel() { | 42 | return Arrays.asList( |
| 92 | actionConfirm = false; | 43 | new Pair<>("prompt.connect.username", usernameField), |
| 93 | setVisible(false); | 44 | new Pair<>("prompt.connect.address", ipField), |
| 45 | new Pair<>("prompt.password", passwordField) | ||
| 46 | ); | ||
| 94 | } | 47 | } |
| 95 | 48 | ||
| 96 | public void validateInputs() { | 49 | public void validateInputs() { |
| @@ -103,7 +56,7 @@ public class ConnectToServerDialog extends JDialog { | |||
| 103 | } | 56 | } |
| 104 | 57 | ||
| 105 | public Result getResult() { | 58 | public Result getResult() { |
| 106 | if (!actionConfirm) return null; | 59 | if (!isActionConfirm()) return null; |
| 107 | vc.reset(); | 60 | vc.reset(); |
| 108 | validateInputs(); | 61 | validateInputs(); |
| 109 | if (!vc.canProceed()) return null; | 62 | if (!vc.canProceed()) return null; |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java index bc4e9c9..f2bc7db 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java | |||
| @@ -1,96 +1,45 @@ | |||
| 1 | package cuchaz.enigma.gui.dialog; | 1 | package cuchaz.enigma.gui.dialog; |
| 2 | 2 | ||
| 3 | import java.awt.*; | 3 | import java.awt.Component; |
| 4 | import java.awt.Dimension; | ||
| 5 | import java.awt.Frame; | ||
| 4 | import java.util.Arrays; | 6 | import java.util.Arrays; |
| 5 | import java.util.List; | 7 | import java.util.List; |
| 6 | import java.util.stream.Collectors; | ||
| 7 | import java.util.stream.Stream; | ||
| 8 | |||
| 9 | import javax.swing.*; | ||
| 10 | 8 | ||
| 11 | import cuchaz.enigma.gui.elements.ValidatablePasswordField; | 9 | import cuchaz.enigma.gui.elements.ValidatablePasswordField; |
| 12 | import cuchaz.enigma.gui.elements.ValidatableTextField; | 10 | import cuchaz.enigma.gui.elements.ValidatableTextField; |
| 13 | import cuchaz.enigma.network.EnigmaServer; | 11 | import cuchaz.enigma.network.EnigmaServer; |
| 14 | import cuchaz.enigma.utils.I18n; | 12 | import cuchaz.enigma.utils.Pair; |
| 15 | import cuchaz.enigma.utils.validation.Message; | 13 | import cuchaz.enigma.utils.validation.Message; |
| 16 | import cuchaz.enigma.utils.validation.StandardValidation; | 14 | import cuchaz.enigma.utils.validation.StandardValidation; |
| 17 | import cuchaz.enigma.utils.validation.ValidationContext; | ||
| 18 | |||
| 19 | public class CreateServerDialog extends JDialog { | ||
| 20 | 15 | ||
| 21 | private final ValidationContext vc = new ValidationContext(); | 16 | public class CreateServerDialog extends AbstractDialog { |
| 22 | 17 | ||
| 23 | private final ValidatableTextField portField; | 18 | private ValidatableTextField portField; |
| 24 | private final ValidatablePasswordField passwordField; | 19 | private ValidatablePasswordField passwordField; |
| 25 | private boolean actionConfirm = false; | ||
| 26 | 20 | ||
| 27 | public CreateServerDialog(Frame owner) { | 21 | public CreateServerDialog(Frame owner) { |
| 28 | super(owner, I18n.translate("prompt.create_server.title"), true); | 22 | super(owner, "prompt.create_server.title", "prompt.create_server.confirm", "prompt.cancel"); |
| 29 | |||
| 30 | Container contentPane = getContentPane(); | ||
| 31 | contentPane.setLayout(new BorderLayout()); | ||
| 32 | Container inputContainer = new JPanel(new GridBagLayout()); | ||
| 33 | GridBagConstraints c = new GridBagConstraints(); | ||
| 34 | portField = new ValidatableTextField(Integer.toString(EnigmaServer.DEFAULT_PORT)); | ||
| 35 | passwordField = new ValidatablePasswordField(); | ||
| 36 | 23 | ||
| 37 | java.util.List<JLabel> labels = Stream.of("prompt.create_server.port", "prompt.password") | ||
| 38 | .map(I18n::translate) | ||
| 39 | .map(JLabel::new) | ||
| 40 | .collect(Collectors.toList()); | ||
| 41 | List<JTextField> inputs = Arrays.asList(portField, passwordField); | ||
| 42 | |||
| 43 | for (int i = 0; i < inputs.size(); i += 1) { | ||
| 44 | c.gridy = i; | ||
| 45 | c.insets = new Insets(4, 4, 4, 4); | ||
| 46 | |||
| 47 | c.gridx = 0; | ||
| 48 | c.weightx = 0.0; | ||
| 49 | c.anchor = GridBagConstraints.LINE_END; | ||
| 50 | c.fill = GridBagConstraints.NONE; | ||
| 51 | inputContainer.add(labels.get(i), c); | ||
| 52 | |||
| 53 | c.gridx = 1; | ||
| 54 | c.weightx = 1.0; | ||
| 55 | c.anchor = GridBagConstraints.LINE_START; | ||
| 56 | c.fill = GridBagConstraints.HORIZONTAL; | ||
| 57 | inputs.get(i).addActionListener(event -> confirm()); | ||
| 58 | inputContainer.add(inputs.get(i), c); | ||
| 59 | } | ||
| 60 | contentPane.add(inputContainer, BorderLayout.CENTER); | ||
| 61 | Container buttonContainer = new JPanel(new GridBagLayout()); | ||
| 62 | c = new GridBagConstraints(); | ||
| 63 | c.weightx = 1.0; | ||
| 64 | c.insets = new Insets(4, 4, 4, 4); | ||
| 65 | c.anchor = GridBagConstraints.LINE_END; | ||
| 66 | JButton connectButton = new JButton(I18n.translate("prompt.create_server.confirm")); | ||
| 67 | connectButton.addActionListener(event -> confirm()); | ||
| 68 | buttonContainer.add(connectButton, c); | ||
| 69 | c.weightx = 0.0; | ||
| 70 | c.anchor = GridBagConstraints.CENTER; | ||
| 71 | JButton abortButton = new JButton(I18n.translate("prompt.cancel")); | ||
| 72 | abortButton.addActionListener(event -> cancel()); | ||
| 73 | buttonContainer.add(abortButton, c); | ||
| 74 | contentPane.add(buttonContainer, BorderLayout.SOUTH); | ||
| 75 | |||
| 76 | setLocationRelativeTo(owner); | ||
| 77 | setSize(new Dimension(400, 150)); | 24 | setSize(new Dimension(400, 150)); |
| 25 | setLocationRelativeTo(owner); | ||
| 78 | } | 26 | } |
| 79 | 27 | ||
| 80 | private void confirm() { | 28 | @Override |
| 81 | vc.reset(); | 29 | protected List<Pair<String, Component>> createComponents() { |
| 82 | validateInputs(); | 30 | portField = new ValidatableTextField(Integer.toString(EnigmaServer.DEFAULT_PORT)); |
| 83 | if (vc.canProceed()) { | 31 | passwordField = new ValidatablePasswordField(); |
| 84 | actionConfirm = true; | ||
| 85 | setVisible(false); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | 32 | ||
| 89 | private void cancel() { | 33 | portField.addActionListener(event -> confirm()); |
| 90 | actionConfirm = false; | 34 | passwordField.addActionListener(event -> confirm()); |
| 91 | setVisible(false); | 35 | |
| 36 | return Arrays.asList( | ||
| 37 | new Pair<>("prompt.create_server.port", portField), | ||
| 38 | new Pair<>("prompt.password", passwordField) | ||
| 39 | ); | ||
| 92 | } | 40 | } |
| 93 | 41 | ||
| 42 | @Override | ||
| 94 | public void validateInputs() { | 43 | public void validateInputs() { |
| 95 | vc.setActiveElement(portField); | 44 | vc.setActiveElement(portField); |
| 96 | StandardValidation.isIntInRange(vc, portField.getText(), 0, 65535); | 45 | StandardValidation.isIntInRange(vc, portField.getText(), 0, 65535); |
| @@ -101,7 +50,7 @@ public class CreateServerDialog extends JDialog { | |||
| 101 | } | 50 | } |
| 102 | 51 | ||
| 103 | public Result getResult() { | 52 | public Result getResult() { |
| 104 | if (!actionConfirm) return null; | 53 | if (!isActionConfirm()) return null; |
| 105 | vc.reset(); | 54 | vc.reset(); |
| 106 | validateInputs(); | 55 | validateInputs(); |
| 107 | if (!vc.canProceed()) return null; | 56 | if (!vc.canProceed()) return null; |