-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationDialog.java
More file actions
242 lines (205 loc) · 7.71 KB
/
RegistrationDialog.java
File metadata and controls
242 lines (205 loc) · 7.71 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.Timer;
public class RegistrationDialog extends JDialog {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
private JButton registerButton;
private JButton cancelButton;
private JLabel statusLabel;
public RegistrationDialog(JFrame parent) {
super(parent, "Register New User", true);
initializeComponents();
setupLayout();
setupEventHandlers();
setSize(400, 280);
setLocationRelativeTo(parent);
setResizable(false);
}
private void initializeComponents() {
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
confirmPasswordField = new JPasswordField(20);
registerButton = new JButton("Register");
cancelButton = new JButton("Cancel");
statusLabel = new JLabel(" ");
// Style buttons
registerButton.setBackground(new Color(34, 139, 34));
registerButton.setForeground(Color.WHITE);
registerButton.setFocusPainted(false);
registerButton.setFont(new Font("Arial", Font.BOLD, 12));
cancelButton.setBackground(new Color(220, 20, 60));
cancelButton.setForeground(Color.WHITE);
cancelButton.setFocusPainted(false);
cancelButton.setFont(new Font("Arial", Font.BOLD, 12));
statusLabel.setForeground(Color.RED);
statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
private void setupLayout() {
setLayout(new BorderLayout());
// Header
JPanel headerPanel = new JPanel();
headerPanel.setBackground(new Color(70, 130, 180));
JLabel titleLabel = new JLabel("👤 Create New Account");
titleLabel.setForeground(Color.WHITE);
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
headerPanel.add(titleLabel);
// Main panel
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBackground(Color.WHITE);
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 5, 8, 5);
// Username
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
mainPanel.add(new JLabel("Username:"), gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(usernameField, gbc);
// Password
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(passwordField, gbc);
// Confirm Password
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
mainPanel.add(new JLabel("Confirm Password:"), gbc);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(confirmPasswordField, gbc);
// Buttons
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.setBackground(Color.WHITE);
buttonPanel.add(registerButton);
buttonPanel.add(cancelButton);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(buttonPanel, gbc);
// Status
gbc.gridy = 4;
mainPanel.add(statusLabel, gbc);
add(headerPanel, BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
}
private void setupEventHandlers() {
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
attemptRegistration();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// Enter key support
confirmPasswordField.addActionListener(e -> attemptRegistration());
}
private void attemptRegistration() {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
// Validation
if (username.isEmpty()) {
showStatus("Please enter a username");
return;
}
if (username.length() < 3) {
showStatus("Username must be at least 3 characters long");
return;
}
if (password.isEmpty()) {
showStatus("Please enter a password");
return;
}
if (password.length() < 6) {
showStatus("Password must be at least 6 characters long");
return;
}
if (!password.equals(confirmPassword)) {
showStatus("Passwords do not match");
confirmPasswordField.setText("");
return;
}
// Disable components during registration
setComponentsEnabled(false);
showStatus("Creating account...", Color.BLUE);
// Perform registration in background thread
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
return UserManager.register(username, password);
}
@Override
protected void done() {
try {
boolean success = get();
if (success) {
showStatus("Account created successfully!", Color.GREEN);
Timer timer = new Timer(1500, e -> dispose());
timer.setRepeats(false);
timer.start();
} else {
showStatus("Username already exists. Please choose another.");
usernameField.setText("");
passwordField.setText("");
confirmPasswordField.setText("");
}
} catch (Exception e) {
showStatus("Registration error: " + e.getMessage());
} finally {
setComponentsEnabled(true);
}
}
};
worker.execute();
}
private void showStatus(String message) {
showStatus(message, Color.RED);
}
private void showStatus(String message, Color color) {
statusLabel.setText(message);
statusLabel.setForeground(color);
}
private void setComponentsEnabled(boolean enabled) {
usernameField.setEnabled(enabled);
passwordField.setEnabled(enabled);
confirmPasswordField.setEnabled(enabled);
registerButton.setEnabled(enabled);
cancelButton.setEnabled(enabled);
}
}