- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeeBookingGuidePage.java
More file actions
Latest commit
311 lines (262 loc) · 13 KB
/
Copy pathSeeBookingGuidePage.java
File metadata and controls
311 lines (262 loc) · 13 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
309
310
311
// SeeBookingGuidePage.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.ArrayList;
publicclassSeeBookingGuidePageextendsJPanel {
privateMainFramemainFrame;
privateStringguideUsername;
publicSeeBookingGuidePage(MainFramemainFrame, StringguideUsername) {
this.mainFrame = mainFrame;
this.guideUsername = guideUsername;
setLayout(newBorderLayout());
// Set the background image
ImageIconbackgroundImageIcon = newImageIcon("profile-bg.jpg");
ImagebackgroundImage = backgroundImageIcon.getImage();
BackgroundPanelbackgroundPanel = newBackgroundPanel(backgroundImage);
backgroundPanel.setLayout(newGridBagLayout()); // Center the content
// Create a panel to hold the booking list
JPanelbookingListPanel = newJPanel();
bookingListPanel.setLayout(newBoxLayout(bookingListPanel, BoxLayout.Y_AXIS));
bookingListPanel.setOpaque(false); // Make panel transparent to show background
// Fixed width for booking panels
intpanelWidth = 600; // Adjust as needed
// Title Label
JLabeltitleLabel = newJLabel("Bookings from Travelers", SwingConstants.CENTER);
titleLabel.setFont(newFont("Arial", Font.BOLD, 32));
titleLabel.setForeground(Color.BLACK);
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
bookingListPanel.add(titleLabel);
bookingListPanel.add(Box.createVerticalStrut(20)); // Add spacing
// Load available bookings for the guide
ArrayList<String[]> bookings = loadAvailableBookings();
// Iterate over bookings and display them
for (String[] booking : bookings) {
JPanelbookingPanel = newJPanel();
bookingPanel.setLayout(newBoxLayout(bookingPanel, BoxLayout.Y_AXIS));
bookingPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // Adds padding
bookingPanel.setBackground(newColor(255, 255, 224, 200)); // Light yellow with transparency
// Set fixed width for bookingPanel
bookingPanel.setMaximumSize(newDimension(panelWidth, Integer.MAX_VALUE));
bookingPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Get traveler username from booking
StringtravelerUsername = booking[1].trim();
// Load traveler info
String[] travelerInfo = loadTravelerInfo(travelerUsername);
StringtravelerName = travelerUsername; // Default to username if name not found
StringtravelerPhoneNumber = "N/A"; // Default if phone number not found
if (travelerInfo != null && travelerInfo.length > 1) {
travelerName = travelerInfo[0].trim(); // Assuming name is at index 0
travelerPhoneNumber = travelerInfo[1].trim(); // Assuming phone number is at index 1
}
// Display booking information with bold labels
JLabelbookingInfo = newJLabel("<html>"
+ "<b>Traveler Name: </b>" + travelerName + "<br>"
+ "<b>Phone Number: </b>" + travelerPhoneNumber + "<br>"
+ "<b>Booked Date: </b>" + booking[2].trim() + "<br>"
+ "<b>Transaction ID: </b>" + booking[3].trim() + "<br>"
+ "<b>Status: </b>" + booking[4].trim() + "</html>");
bookingInfo.setFont(newFont("Arial", Font.PLAIN, 16));
bookingInfo.setForeground(Color.BLACK);
bookingInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
bookingPanel.add(bookingInfo);
bookingPanel.add(Box.createVerticalStrut(10)); // Add spacing
// Buttons Panel
JPanelbuttonsPanel = newJPanel(newFlowLayout(FlowLayout.CENTER, 10, 0));
buttonsPanel.setOpaque(false);
// Confirm Button
JButtonconfirmButton = newJButton("Confirm");
styleButton(confirmButton);
confirmButton.setAlignmentX(Component.CENTER_ALIGNMENT);
if (!booking[4].trim().equals("Pending by Guide")) {
confirmButton.setEnabled(false);
}
// Reject Button
JButtonrejectButton = newJButton("Reject");
styleButton(rejectButton);
rejectButton.setAlignmentX(Component.CENTER_ALIGNMENT);
if (!booking[4].trim().equals("Pending by Guide")) {
rejectButton.setEnabled(false);
}
// Clone booking details to use in action listeners
finalString[] bookingDetails = booking.clone();
// Confirm action
confirmButton.addActionListener(e -> {
updateBookingStatus(bookingDetails, "Approved by Guide");
reloadPage();
});
// Reject action
rejectButton.addActionListener(e -> {
updateBookingStatus(bookingDetails, "Cancelled by Guide");
reloadPage();
});
// Add buttons to panel
buttonsPanel.add(confirmButton);
buttonsPanel.add(rejectButton);
bookingPanel.add(buttonsPanel);
// Add bookingPanel to bookingListPanel
bookingListPanel.add(bookingPanel);
// Add spacing between booking panels
bookingListPanel.add(Box.createRigidArea(newDimension(0, 10)));
}
// Create a scroll pane for the bookingListPanel
JScrollPanescrollPane = newJScrollPane(bookingListPanel);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.setBorder(null);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setUnitIncrement(16); // Adjust scrolling speed
scrollPane.setPreferredSize(newDimension(panelWidth + 50, 400)); // Adjust height as needed
// Create a wrapper panel to center the scrollPane
JPanelwrapperPanel = newJPanel();
wrapperPanel.setOpaque(false);
wrapperPanel.setLayout(newBoxLayout(wrapperPanel, BoxLayout.Y_AXIS));
wrapperPanel.add(scrollPane);
// Back button to return to the guide profile
JButtonbackButton = newJButton("Back to Profile");
styleButton(backButton);
backButton.setAlignmentX(Component.CENTER_ALIGNMENT);
backButton.addActionListener(e -> {
String[] guideInfo = loadGuideInfo(guideUsername);
if (guideInfo != null) {
mainFrame.showGuideProfilePage(guideInfo);
} else {
JOptionPane.showMessageDialog(this, "Error retrieving your profile information.");
}
});
// Create a panel for the back button
JPanelbackButtonPanel = newJPanel();
backButtonPanel.setOpaque(false);
backButtonPanel.add(backButton);
wrapperPanel.add(Box.createVerticalStrut(20)); // Add spacing
wrapperPanel.add(backButtonPanel);
// Center the wrapperPanel in the backgroundPanel using GridBagLayout
GridBagConstraintsgbcWrapper = newGridBagConstraints();
gbcWrapper.anchor = GridBagConstraints.CENTER;
backgroundPanel.add(wrapperPanel, gbcWrapper);
// Add backgroundPanel to the main panel
add(backgroundPanel, BorderLayout.CENTER);
}
// Load traveler info based on username
privateString[] loadTravelerInfo(Stringusername) {
try (BufferedReaderreader = newBufferedReader(newFileReader("TravelersData.txt"))) {
Stringline;
while ((line = reader.readLine()) != null) {
String[] userInfo = line.split(",");
if (userInfo.length > 3 && userInfo[3].trim().equalsIgnoreCase(username.trim())) { // Assuming username is at index 3
returnuserInfo;
}
}
} catch (IOExceptione) {
e.printStackTrace();
}
returnnull;
}
// Load available bookings for this guide
privateArrayList<String[]> loadAvailableBookings() {
ArrayList<String[]> bookings = newArrayList<>();
try (BufferedReaderreader = newBufferedReader(newFileReader("Bookings.txt"))) {
Stringline;
while ((line = reader.readLine()) != null) {
String[] bookingInfo = line.split(",");
if (bookingInfo.length > 4 && this.guideUsername.equalsIgnoreCase(bookingInfo[0].trim())) {
bookings.add(bookingInfo);
}
}
} catch (IOExceptione) {
e.printStackTrace();
}
returnbookings;
}
// Update the booking status based on booking details
privatevoidupdateBookingStatus(String[] targetBooking, StringnewStatus) {
FilebookingsFile = newFile("Bookings.txt");
FiletempFile = newFile("TempBookings.txt");
booleanbookingFound = false;
try (BufferedReaderreader = newBufferedReader(newFileReader(bookingsFile));
BufferedWriterwriter = newBufferedWriter(newFileWriter(tempFile))) {
Stringline;
while ((line = reader.readLine()) != null) {
String[] booking = line.split(",");
if (booking.length > 4 &&
booking[0].trim().equals(targetBooking[0].trim()) &&
booking[1].trim().equals(targetBooking[1].trim()) &&
booking[2].trim().equals(targetBooking[2].trim()) &&
booking[3].trim().equals(targetBooking[3].trim())) {
// Update the status
booking[4] = newStatus;
bookingFound = true;
}
StringbookingLine = String.join(",", booking);
writer.write(bookingLine);
writer.newLine();
}
if (!bookingFound) {
JOptionPane.showMessageDialog(this, "Booking not found.");
}
} catch (IOExceptione) {
e.printStackTrace();
}
// Replace the original file with the updated file
if (bookingsFile.delete()) {
if (!tempFile.renameTo(bookingsFile)) {
JOptionPane.showMessageDialog(this, "Error renaming temporary file.");
}
} else {
JOptionPane.showMessageDialog(this, "Error deleting original bookings file.");
}
}
// Reload the page to reflect changes
privatevoidreloadPage() {
mainFrame.showSeeBookingGuidePage(guideUsername);
}
// Load guide info based on username
privateString[] loadGuideInfo(Stringusername) {
try (BufferedReaderreader = newBufferedReader(newFileReader("GuidesData.txt"))) {
Stringline;
while ((line = reader.readLine()) != null) {
String[] guideInfo = line.split(",");
if (guideInfo.length > 3 && guideInfo[3].trim().equalsIgnoreCase(username.trim())) { // Assuming username is at index 3
returnguideInfo;
}
}
} catch (IOExceptione) {
e.printStackTrace();
}
returnnull;
}
privatevoidstyleButton(JButtonbutton) {
button.setFont(newFont("Arial", Font.BOLD, 16));
button.setForeground(Color.WHITE);
button.setBackground(newColor(64, 64, 64)); // Dark gray color
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
button.setCursor(newCursor(Cursor.HAND_CURSOR));
// Add mouse hover effect
button.addMouseListener(newjava.awt.event.MouseAdapter() {
publicvoidmouseEntered(java.awt.event.MouseEventevt) {
button.setBackground(newColor(255, 204, 0)); // Yellow color on hover
}
publicvoidmouseExited(java.awt.event.MouseEventevt) {
button.setBackground(newColor(64, 64, 64)); // Original color
}
});
}
// Custom JPanel to draw the background image
classBackgroundPanelextendsJPanel {
privateImagebackgroundImage;
publicBackgroundPanel(ImagebackgroundImage) {
this.backgroundImage = backgroundImage;
setLayout(newGridBagLayout()); // Center the content
}
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
// Draw the background image scaled to fit the panel
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
}