-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuideRegistrationPage.java
More file actions
308 lines (259 loc) · 12.9 KB
/
Copy pathGuideRegistrationPage.java
File metadata and controls
308 lines (259 loc) · 12.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.Scanner;
public class GuideRegistrationPage extends JPanel {
private JTextField nameField, numberField, emailField, usernameField, addressField, experienceField, costField, detailsField, areaField, languageField, bankField, bankNameField;
private JPasswordField passwordField;
private MainFrame mainFrame;
public GuideRegistrationPage(MainFrame mainFrame) {
this.mainFrame = mainFrame;
setLayout(new BorderLayout());
// Set the background image
ImageIcon backgroundImageIcon = new ImageIcon("reg-guide.jpg"); // Replace with your image file
Image backgroundImage = backgroundImageIcon.getImage();
BackgroundPanel backgroundPanel = new BackgroundPanel(backgroundImage);
backgroundPanel.setLayout(new GridBagLayout()); // To center the form panel
// Create form panel
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setPreferredSize(new Dimension(600, 800)); // Adjust size as needed
formPanel.setBackground(new Color(255, 255, 224, 200)); // Light yellow with some transparency
formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // Padding around the form
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 15, 5, 15); // Spacing between components
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
// Title Label
JLabel titleLabel = new JLabel("Guide Registration", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 32));
titleLabel.setForeground(new Color(64, 64, 64)); // Dark gray color
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
formPanel.add(titleLabel, gbc);
gbc.gridwidth = 1; // Reset gridwidth after title
// Name Label and Field
gbc.gridy = 1;
addLabelAndField(formPanel, gbc, "Full Name:", nameField = createStyledTextField());
// Phone Number Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Phone Number:", numberField = createStyledTextField());
// Email Address Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Email Address:", emailField = createStyledTextField());
// Username Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Username:", usernameField = createStyledTextField());
// Password Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Password:", passwordField = createStyledPasswordField());
// Address Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Address:", addressField = createStyledTextField());
// Experience Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Experience in Year:", experienceField = createStyledTextField());
// Per Hour Cost Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Per Day Cost in BDT:", costField = createStyledTextField());
// Details Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Details:", detailsField = createStyledTextField());
// Covered Area Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Covered Area:", areaField = createStyledTextField());
// Preferred Language Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Preferred Language:", languageField = createStyledTextField());
// Bank A/C No. Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Bank A/C No.:", bankField = createStyledTextField());
// Your Bank Name Label and Field (Extra Field)
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Your Bank Name:", bankNameField = createStyledTextField());
// Register and Back Buttons
JButton registerButton = new JButton("Register");
styleButton(registerButton);
registerButton.addActionListener(e -> registerGuide());
JButton backButton = new JButton("Back");
styleButton(backButton);
backButton.addActionListener(e -> mainFrame.showPage("SelectionPage"));
// Button Panel to hold the buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
buttonPanel.setOpaque(false); // Transparent background
buttonPanel.add(registerButton);
buttonPanel.add(backButton);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
formPanel.add(buttonPanel, gbc);
// Add formPanel to the backgroundPanel
backgroundPanel.add(formPanel);
// Add backgroundPanel to the main panel
add(backgroundPanel, BorderLayout.CENTER);
}
private void addLabelAndField(JPanel panel, GridBagConstraints gbc, String labelText, JComponent field) {
JLabel label = new JLabel(labelText);
label.setFont(new Font("Arial", Font.PLAIN, 16));
label.setForeground(new Color(64, 64, 64)); // Dark gray color
gbc.gridx = 0;
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(field, gbc);
}
private JTextField createStyledTextField() {
JTextField textField = new JTextField();
styleTextField(textField);
// Increase input field height
textField.setPreferredSize(new Dimension(250, 35)); // Adjust height as needed
return textField;
}
private JPasswordField createStyledPasswordField() {
JPasswordField passwordField = new JPasswordField();
styleTextField(passwordField);
// Increase input field height
passwordField.setPreferredSize(new Dimension(250, 35)); // Adjust height as needed
return passwordField;
}
private void styleTextField(JTextComponent textField) {
textField.setFont(new Font("Arial", Font.PLAIN, 16));
textField.setForeground(Color.BLACK);
textField.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1));
textField.setBackground(new Color(255, 255, 240)); // Light yellow background
textField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
textField.setBorder(BorderFactory.createLineBorder(new Color(64, 64, 64), 1));
}
public void focusLost(FocusEvent e) {
textField.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1));
}
});
// Ensure the text field is empty
textField.setText("");
}
private void styleButton(JButton button) {
button.setFont(new Font("Arial", Font.BOLD, 18));
button.setForeground(Color.WHITE);
button.setBackground(new Color(64, 64, 64)); // Dark gray color
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setMargin(new Insets(10, 20, 10, 20));
// Add mouse hover effect
button.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
button.setBackground(new Color(255, 204, 0)); // Yellow color on hover
}
public void mouseExited(MouseEvent e) {
button.setBackground(new Color(64, 64, 64)); // Original color
}
});
}
// Method to handle guide registration logic
private void registerGuide() {
String name = nameField.getText().trim();
String number = numberField.getText().trim();
String email = emailField.getText().trim();
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword()).trim();
String address = addressField.getText().trim();
String experience = experienceField.getText().trim();
String cost = costField.getText().trim();
String details = detailsField.getText().trim();
String area = areaField.getText().trim();
String language = languageField.getText().trim();
String bankAccount = bankField.getText().trim();
String bankName = bankNameField.getText().trim(); // New field
// Check if any of the fields are empty
StringBuilder emptyFields = new StringBuilder();
if (name.isEmpty()) emptyFields.append(" Full Name\n");
if (number.isEmpty()) emptyFields.append(" Phone Number\n");
if (email.isEmpty()) emptyFields.append(" Email Address\n");
if (username.isEmpty()) emptyFields.append(" Username\n");
if (password.isEmpty()) emptyFields.append(" Password\n");
if (address.isEmpty()) emptyFields.append(" Address\n");
if (experience.isEmpty()) emptyFields.append(" Experience\n");
if (cost.isEmpty()) emptyFields.append(" Per Hour Cost\n");
if (details.isEmpty()) emptyFields.append(" Details\n");
if (area.isEmpty()) emptyFields.append(" Covered Area\n");
if (language.isEmpty()) emptyFields.append(" Preferred Language\n");
if (bankAccount.isEmpty()) emptyFields.append(" Bank A/C No.\n");
if (bankName.isEmpty()) emptyFields.append(" Your Bank Name\n");
if (emptyFields.length() > 0) {
JOptionPane.showMessageDialog(this, "Please fill the following fields:\n" + emptyFields.toString());
return;
}
// Check for duplicate username in TravelersData.txt and GuidesData.txt
if (isUsernameTaken(username, "TravelersData.txt", "GuidesData.txt")) {
JOptionPane.showMessageDialog(this, "Username already exists. Please choose another username.");
return;
}
// Save guide information to GuidesData.txt
try (BufferedWriter writer = new BufferedWriter(new FileWriter("GuidesData.txt", true))) {
writer.write(String.join(",", name, number, email, username, password, address, experience, cost, details, area, language, bankAccount, bankName));
writer.newLine();
JOptionPane.showMessageDialog(this, "Registration Successful!");
// Clear all fields after successful registration
clearFields();
// Optionally, navigate to the login page
mainFrame.showPage("LoginPage");
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to clear all input fields
private void clearFields() {
nameField.setText("");
numberField.setText("");
emailField.setText("");
usernameField.setText("");
passwordField.setText("");
addressField.setText("");
experienceField.setText("");
costField.setText("");
detailsField.setText("");
areaField.setText("");
languageField.setText("");
bankField.setText("");
bankNameField.setText("");
}
// Method to check if a username already exists in the files
private boolean isUsernameTaken(String username, String filePath1, String filePath2) {
return isUsernameInFile(username, filePath1) || isUsernameInFile(username, filePath2);
}
// Helper method to check if username exists in a specific file
private boolean isUsernameInFile(String username, String filePath) {
try (Scanner scanner = new Scanner(new File(filePath))) {
while (scanner.hasNextLine()) {
String[] userInfo = scanner.nextLine().split(",");
if (userInfo.length > 3 && userInfo[3].equals(username)) { // Assuming username is the 4th field (index 3)
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
// Custom JPanel to draw the background image
class BackgroundPanel extends JPanel {
private Image backgroundImage;
public BackgroundPanel(Image backgroundImage) {
this.backgroundImage = backgroundImage;
setPreferredSize(new Dimension(backgroundImage.getWidth(null), backgroundImage.getHeight(null)));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the background image scaled to fit the panel
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
}