/src/ConfigurationDialog.java

https://github.com/efrolov/ProjectSimpleMail · Java · 115 lines · 74 code · 14 blank · 27 comment · 5 complexity · 671dff68b0901fc6180ccf2bbf9496e3 MD5 · raw file

  1. import java.awt.BorderLayout;
  2. import java.awt.Button;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.net.InetAddress;
  7. import java.net.UnknownHostException;
  8. import javax.swing.JDialog;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextField;
  12. import javax.swing.WindowConstants;
  13. /**
  14. * <p>
  15. * A child of {@code JDialog} that implements {@code ActionListener} to create a
  16. * dialog for editing objects of type {@code Configuration}.
  17. * </p>
  18. *
  19. * @see ActionListener, Configuration, JDialog
  20. */
  21. public class ConfigurationDialog extends JDialog implements ActionListener {
  22. private static final long serialVersionUID = 654591847880778195L;
  23. private Configuration myConf;
  24. private JTextField myEmail;
  25. private JTextField myIP;
  26. /**
  27. * <p>
  28. * Creates a new {@code ConfigurationDialog} initialized with the values
  29. * from the {@code Configuration} passed as a parameter. The parameter must be
  30. * non-null.
  31. * </p>
  32. *
  33. * @param c
  34. * a {@code Configuration} to be edited by this dialog
  35. */
  36. public ConfigurationDialog(Configuration c) {
  37. super();
  38. this.myConf = c;
  39. this.setTitle("Edit Configuration");
  40. GridLayout textGridLayout = new GridLayout(2, 2);
  41. JPanel contentPane = new JPanel(new BorderLayout());
  42. JPanel textPane = new JPanel();
  43. textPane.setLayout(textGridLayout);
  44. JPanel buttonPane = new JPanel();
  45. JLabel ip = new JLabel("SMTP Server IP:");
  46. this.myIP = new JTextField(this.myConf.getMySMTP().getHostName());
  47. JLabel em = new JLabel("Main Email Address:");
  48. this.myEmail = new JTextField(this.myConf.getMyEmail());
  49. textPane.add(ip);
  50. textPane.add(this.myIP);
  51. textPane.add(em);
  52. textPane.add(this.myEmail);
  53. textGridLayout.layoutContainer(textPane);
  54. contentPane.add(textPane, BorderLayout.PAGE_START);
  55. ActionListener aL = (this);
  56. Button cancel = new Button("Cancel");
  57. cancel.setSize(100, 50);
  58. cancel.setActionCommand("cancel");
  59. cancel.addActionListener(aL);
  60. Button save = new Button("Save");
  61. save.setSize(100, 50);
  62. save.setActionCommand("save");
  63. save.addActionListener(aL);
  64. buttonPane.add(cancel);
  65. buttonPane.add(save);
  66. contentPane.add(buttonPane, BorderLayout.SOUTH);
  67. this.setContentPane(contentPane);
  68. this.setAlwaysOnTop(true);
  69. this.setModal(true);
  70. this.setSize(250, 150);
  71. this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  72. this.setVisible(true);
  73. }
  74. /**
  75. * <p>
  76. * A method of ActionListener that catches {@code ActionEvent}s created by
  77. * the menu and buttons of this dialog and reacts accordingly.
  78. * </p>
  79. *
  80. * @param arg0
  81. * the {@code ActionEvent} for which a reaction is necessary
  82. */
  83. @Override
  84. public void actionPerformed(ActionEvent e) {
  85. String s = e.getActionCommand();
  86. if (s.compareTo("cancel") == 0) {
  87. this.dispose();
  88. } else if (s.compareTo("save") == 0) {
  89. DataStore d = DataStore.getInstance();
  90. this.myConf.setMyEmail(this.myEmail.getText());
  91. try {
  92. this.myConf
  93. .setMySMTP(InetAddress.getByName(this.myIP.getText()));
  94. } catch (UnknownHostException e1) {
  95. System.err.println("SMTP Host did not validate.");
  96. }
  97. d.setConfiguration(this.myConf);
  98. this.dispose();
  99. }
  100. }
  101. }