-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkingSpotView.java
More file actions
76 lines (66 loc) · 2.69 KB
/
Copy pathParkingSpotView.java
File metadata and controls
76 lines (66 loc) · 2.69 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
import javax.swing.*;
import java.awt.*;
import java.time.format.DateTimeFormatter;
/**
* Project 2: ParkingSpotView class that extends JButton.
* Stores reference to controller CarparkSystem and model of parkingSpot.
*
* @author Abdurrahman Faqih 104675143
* @version 0.1 9 May 2024
*/
public class ParkingSpotView extends JButton {
private CarparkSystem controller; // Store reference to controller
// Initialize view components
private ParkingSpot parkingSpot;
private JLabel parkingSpotId = new JLabel();
private JLabel parkedCarDetails = new JLabel();
public ParkingSpotView(CarparkSystem controller, ParkingSpot parkingSpot) {
this.controller = controller;
this.parkingSpot = parkingSpot;
// Set view settings
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setBackground(new Color(137, 232, 138));
setBorder(BorderFactory.createLineBorder(new Color(79, 79, 82), 3, true));
setLayout(new BorderLayout());
setOpaque(true);
// Add components
parkingSpotId.setText(parkingSpot.getId());
parkingSpotId.setHorizontalAlignment(SwingConstants.CENTER);
add(parkingSpotId, BorderLayout.NORTH);
// Add action listener to parking spot
this.addActionListener(e -> this.clickActionListener());
}
/**
* Gets parking spot model and returns it
* @return parking spot
*/
public ParkingSpot getParkingSpot() {
return this.parkingSpot;
}
/**
* Update parking spot view with changes in parking spot model
*/
public void updateParkingSpotView() {
if (parkingSpot.isOccupied()) {
// Add car details to parking spot view
Car parkedCar = parkingSpot.getParkedCar(); // Get parked car
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
parkedCarDetails.setText("<html>Registration: " + parkedCar.getRegNo() + "<br />"
+ parkedCar.getMake() + " " + parkedCar.getModel() + " " + parkedCar.getYear()
+ "<br />Car parked at: " + parkedCar.getTimeStart().format(formatter) + "</html>");
parkedCarDetails.setHorizontalAlignment(SwingConstants.CENTER);
add(parkedCarDetails, BorderLayout.CENTER);
setBackground(new Color(242, 90, 90));
} else {
// Remove car details from parking spot view
remove(parkedCarDetails);
setBackground(new Color(137, 232, 138));
}
}
/**
* Action listener for button clicks on parking spot view
*/
public void clickActionListener() {
this.controller.openParkingSpotViewActionDialog(this);
}
}