-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsDialog.java
128 lines (104 loc) · 3.49 KB
/
SettingsDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* Displays a setting form that allows configuring SMTP settings.
* @author www.codejava.net
*
*/
public class SettingsDialog extends JDialog {
private ConfigUtility configUtil;
private JLabel labelHost = new JLabel("Host name: ");
private JLabel labelPort = new JLabel("Port number: ");
private JLabel labelUser = new JLabel("Username: ");
private JLabel labelPass = new JLabel("Password: ");
private JTextField textHost = new JTextField(20);
private JTextField textPort = new JTextField(20);
private JTextField textUser = new JTextField(20);
private JTextField textPass = new JTextField(20);
private JButton buttonSave = new JButton("Save");
public SettingsDialog(JFrame parent, ConfigUtility configUtil) {
super(parent, "SMTP Settings", true);
this.configUtil = configUtil;
setupForm();
loadSettings();
pack();
setLocationRelativeTo(null);
}
private void setupForm() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
add(labelHost, constraints);
constraints.gridx = 1;
add(textHost, constraints);
constraints.gridy = 1;
constraints.gridx = 0;
add(labelPort, constraints);
constraints.gridx = 1;
add(textPort, constraints);
constraints.gridy = 2;
constraints.gridx = 0;
add(labelUser, constraints);
constraints.gridx = 1;
add(textUser, constraints);
constraints.gridy = 3;
constraints.gridx = 0;
add(labelPass, constraints);
constraints.gridx = 1;
add(textPass, constraints);
constraints.gridy = 4;
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
buttonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
buttonSaveActionPerformed(event);
}
});
}
private void loadSettings() {
Properties configProps = null;
try {
configProps = configUtil.loadProperties();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error reading settings: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
textHost.setText(configProps.getProperty("mail.smtp.host"));
textPort.setText(configProps.getProperty("mail.smtp.port"));
textUser.setText(configProps.getProperty("mail.user"));
textPass.setText(configProps.getProperty("mail.password"));
}
private void buttonSaveActionPerformed(ActionEvent event) {
try {
configUtil.saveProperties(textHost.getText(),
textPort.getText(),
textUser.getText(),
textPass.getText());
JOptionPane.showMessageDialog(SettingsDialog.this,
"Properties were saved successfully!");
dispose();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error saving properties file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}