-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwn_Rentals.java
More file actions
463 lines (392 loc) · 19.4 KB
/
Copy pathOwn_Rentals.java
File metadata and controls
463 lines (392 loc) · 19.4 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package Rent_Rover;
/**
*
* @author PC MOD NEPAL
*/
import Rent_Rover.DataBases.DB;
import java.awt.Color;
import Rent_Rover.GradientPanel;
import Rent_Rover.GradientPanel.Direction;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
public class Own_Rentals extends javax.swing.JFrame {
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(Own_Rentals.class.getName());
private javax.swing.JPopupMenu suggestionPopup = new javax.swing.JPopupMenu();
/**
* Creates new form Primary_Screen
*/
private String username;
public Own_Rentals(String username) {
initComponents();
// Store username for later use
this.username = username;
// Fetch and set profile picture and name using the username
setProfilePicture(this.username);
// ✅ Set layout for scrollablepanel
scrollablepanel.setLayout(new BoxLayout(scrollablepanel, BoxLayout.Y_AXIS));
scrollablepanel.setBackground(Color.WHITE);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setViewportView(scrollablepanel);
// then call loadBookings() (you probably already do this)
loadBookings();
}
private void setProfilePicture(String username) {
try {
Connection con = DB.getConnection();
String query = "SELECT profile_picture FROM customer WHERE username = ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
byte[] imgBytes = rs.getBytes("profile_picture");
if (imgBytes != null) {
ImageIcon imageIcon = new ImageIcon(imgBytes);
Image image = imageIcon.getImage().getScaledInstance(pfp.getWidth(), pfp.getHeight(), Image.SCALE_SMOOTH);
pfp.setIcon(new ImageIcon(image));
CircularLabel.makeCircular(pfp);
}
}
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Own_Rentals() {
initComponents();
ImageScaler.setScaledImage(RR_logo, "C:\\Users\\97798\\Desktop\\ProjectImages\\RR logo.png");
scrollablepanel.setLayout(new BoxLayout(scrollablepanel, BoxLayout.Y_AXIS));
scrollablepanel.setBackground(Color.WHITE);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// Method to load all bookings for the logged-in user
// Updated createBookingPanel to include owner username and make panel clickable
// Updated createBookingPanel to include hover effect and full click functionality
private JPanel createBookingPanel(String vehicleName, byte[] vehicleImage,
String location, String createdAt,
String pricePerDay, String ownerUsername) {
final int CARD_HEIGHT = 150;
final int CARD_WIDTH = 1000; // fallback width; will be resized by BoxLayout/scrollpane
JPanel panel = new JPanel(null);
panel.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT));
panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, CARD_HEIGHT));
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
// Vehicle image
JLabel imageLabel = new JLabel();
imageLabel.setBounds(10, 10, 130, 130);
if (vehicleImage != null && vehicleImage.length > 0) {
ImageIcon icon = new ImageIcon(vehicleImage);
Image scaled = icon.getImage().getScaledInstance(imageLabel.getWidth(), imageLabel.getHeight(), Image.SCALE_SMOOTH);
imageLabel.setIcon(new ImageIcon(scaled));
} else {
imageLabel.setText("No Image");
imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
imageLabel.setVerticalAlignment(SwingConstants.CENTER);
imageLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
panel.add(imageLabel);
// Vehicle name
JLabel nameLabel = new JLabel(vehicleName);
nameLabel.setFont(new Font("SansSerif", Font.BOLD, 16));
nameLabel.setBounds(150, 10, 500, 26);
panel.add(nameLabel);
// Owner username
JLabel ownerLabel = new JLabel("Owner: " + (ownerUsername == null ? "unknown" : ownerUsername));
ownerLabel.setFont(new Font("SansSerif", Font.PLAIN, 13));
ownerLabel.setBounds(150, 40, 300, 20);
panel.add(ownerLabel);
// Location + price
JLabel locPrice = new JLabel("Location: " + location + " | Price/day: " + pricePerDay);
locPrice.setFont(new Font("SansSerif", Font.PLAIN, 13));
locPrice.setBounds(150, 65, 600, 20);
panel.add(locPrice);
// Created at
JLabel dateLabel = new JLabel("Listed On: " + createdAt);
dateLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
dateLabel.setForeground(Color.GRAY);
dateLabel.setBounds(150, 95, 400, 18);
panel.add(dateLabel);
// Make whole panel clickable + hover
Border defaultBorder = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
Border hoverBorder = BorderFactory.createLineBorder(new Color(0,120,215), 2);
panel.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("Hi! Panel clicked for vehicle: " + vehicleName);
}
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
panel.setBackground(new Color(245, 247, 250));
panel.setBorder(hoverBorder);
panel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
panel.setBackground(Color.WHITE);
panel.setBorder(defaultBorder);
panel.setCursor(Cursor.getDefaultCursor());
}
});
panel.setBorder(defaultBorder);
return panel;
}
// Updated loadBookings: no filtering by username, display all
public void loadBookings() {
scrollablepanel.removeAll();
final int CARD_HEIGHT = 150;
final int GAP = 10;
int count = 0;
try (Connection con = DB.getConnection()) {
String query = "SELECT username, brand, model, location, price_per_day, vehicle_image, created_at " +
"FROM bookings WHERE username = ? ORDER BY created_at DESC";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, username); // <-- logged-in user's name
ResultSet rs = ps.executeQuery();
while (rs.next()) {
count++;
String vehicleName = rs.getString("brand") + " " + rs.getString("model");
byte[] imageBytes = rs.getBytes("vehicle_image");
String location = rs.getString("location");
String createdAt = rs.getString("created_at");
String price = rs.getString("price_per_day");
String owner = rs.getString("username");
JPanel card = createBookingPanel(vehicleName, imageBytes, location, createdAt, price, owner);
scrollablepanel.add(card);
scrollablepanel.add(Box.createRigidArea(new Dimension(0, GAP)));
}
rs.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
int totalHeight = Math.max(1, count) * (CARD_HEIGHT + GAP) + 20;
int width = jScrollPane1.getViewport().getWidth() > 0 ? jScrollPane1.getViewport().getWidth() : 1000;
scrollablepanel.setPreferredSize(new Dimension(width, totalHeight));
scrollablepanel.revalidate();
scrollablepanel.repaint();
System.out.println("loadBookings -> cards: " + count);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
bottom = new javax.swing.JPanel();
whitepanel = new ImageScalerP("C:\\Users\\97798\\Desktop\\ProjectImages\\whitebg.jpg");
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
RR_logo = new javax.swing.JLabel();
homee = new javax.swing.JLabel();
renter = new javax.swing.JLabel();
pfp = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
scrollablepanel = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 255));
bottom.setBackground(new java.awt.Color(204, 204, 204));
jLabel2.setFont(new java.awt.Font("SansSerif", 1, 12)); // NOI18N
jLabel2.setText("Rent, Ride, Repeat");
jLabel1.setFont(new java.awt.Font("Perpetua", 3, 36)); // NOI18N
jLabel1.setText("RentRover");
jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel1MouseClicked(evt);
}
});
RR_logo.setMaximumSize(new java.awt.Dimension(76, 51));
homee.setFont(new java.awt.Font("SansSerif", 1, 18)); // NOI18N
homee.setText("Home");
homee.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
homee.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
homeeMouseClicked(evt);
}
});
renter.setFont(new java.awt.Font("SansSerif", 1, 18)); // NOI18N
renter.setText("Become a Renter");
renter.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
renter.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
renterMouseClicked(evt);
}
});
javax.swing.GroupLayout whitepanelLayout = new javax.swing.GroupLayout(whitepanel);
whitepanel.setLayout(whitepanelLayout);
whitepanelLayout.setHorizontalGroup(
whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(whitepanelLayout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(RR_logo, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(whitepanelLayout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(whitepanelLayout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(homee)
.addGap(306, 306, 306)))
.addComponent(renter)
.addGap(80, 80, 80)
.addComponent(pfp, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))
);
whitepanelLayout.setVerticalGroup(
whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, whitepanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RR_logo, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(pfp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, whitepanelLayout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(24, 24, 24))
.addGroup(whitepanelLayout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(whitepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(renter)
.addComponent(homee, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout scrollablepanelLayout = new javax.swing.GroupLayout(scrollablepanel);
scrollablepanel.setLayout(scrollablepanelLayout);
scrollablepanelLayout.setHorizontalGroup(
scrollablepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1359, Short.MAX_VALUE)
);
scrollablepanelLayout.setVerticalGroup(
scrollablepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 641, Short.MAX_VALUE)
);
jScrollPane1.setViewportView(scrollablepanel);
javax.swing.GroupLayout bottomLayout = new javax.swing.GroupLayout(bottom);
bottom.setLayout(bottomLayout);
bottomLayout.setHorizontalGroup(
bottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(whitepanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(bottomLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1)
.addContainerGap())
);
bottomLayout.setVerticalGroup(
bottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bottomLayout.createSequentialGroup()
.addGap(37, 37, 37)
.addComponent(whitepanel, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 643, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(bottom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bottom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void renterMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_renterMouseClicked
try {
// ✅ Pass booking details AND username to dashboard
RenterDashBoard renter = new RenterDashBoard(
this.username
);
this.dispose();
renter.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
StylishPopup.showError(this, "Error: " + ex.getMessage());
}
}//GEN-LAST:event_renterMouseClicked
private void homeeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_homeeMouseClicked
Primary_Screen_Logged primary = new Primary_Screen_Logged(this.username);
primary.setVisible(true);
this.dispose();
}//GEN-LAST:event_homeeMouseClicked
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
this.dispose();
new Own_Rentals().setVisible(true);
}//GEN-LAST:event_jLabel1MouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ReflectiveOperationException | javax.swing.UnsupportedLookAndFeelException ex) {
logger.log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> new Own_Rentals().setVisible(true));
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel RR_logo;
private javax.swing.JPanel bottom;
private javax.swing.JLabel homee;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel pfp;
private javax.swing.JLabel renter;
private javax.swing.JPanel scrollablepanel;
private javax.swing.JPanel whitepanel;
// End of variables declaration//GEN-END:variables
}