-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieListPanel.java
More file actions
454 lines (371 loc) · 18.4 KB
/
MovieListPanel.java
File metadata and controls
454 lines (371 loc) · 18.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class MovieListPanel extends JPanel {
private ArrayList<Movie> movies;
private ArrayList<Location> locations;
private boolean showAll = true;
private boolean search = false;
private JComboBox<Location> locationComboBox;
private Movie lastSelectedMovie = null;
private JButton submitButton;
private String searchMovie;
private JTextField searchInput;
private JList<Movie> movieList;
private DefaultListModel<Movie> listModel;
private JButton showAllButton;
private JLabel movieDetailsLabel;
private JPanel detailsPanel;
private JPanel showtimesPanel;
private JPanel seatPanel;
private JButton currentSeatButton;
private JButton currentShowtimeButton;
private MovieTheatreController movieTC;
private MovieTheatreApp app;
private final Color Red = new Color(139, 0, 0);
private final Color Yellow = new Color(255, 248, 191);
private final Color Orange = new Color(244, 138, 104);
private final Color pastelGreen = new Color(152, 251, 152); // Light pastel green
public MovieListPanel(MovieTheatreApp app, MovieTheatreController movieTC) {
this.app = app;
this.movieTC = movieTC;
// Set layout for the main panel
this.setLayout(new BorderLayout());
// Back button at the bottom
JButton backButton = new JButton("Back");
backButton.setForeground(Red);
backButton.addActionListener(e -> {
seatPanel.removeAll();
seatPanel.setVisible(false);
if (currentShowtimeButton != null) {
updateButtonColor(currentShowtimeButton, true);
}
currentShowtimeButton = null;
app.setSelectedShowtime(null);
submitButton.setVisible(false);
app.switchToGuest(); // bc i got rid of registered panel so just one anel for both RU and OU
// if(app.getCurrentUser() == 0){
// System.out.println("Going to Guest view.");
// app.switchToGuest();
// } else if (app.getCurrentUser() == 1){
// System.out.println("Going to Registered view.");
// app.switchToRegistered();
// }
});
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
bottomPanel.add(backButton);
bottomPanel.setBackground(Yellow);
this.add(bottomPanel, BorderLayout.SOUTH);
// Search bar at the top
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
searchPanel.setBackground(Yellow);
JLabel searchLabel = new JLabel("Search a Movie:");
searchLabel.setForeground(Red);
searchInput = new JTextField(30);
JButton searchButton = new JButton("Search");
searchButton.setForeground(Red);
showAllButton = new JButton("Show All");
showAllButton.setForeground(Red);
showAllButton.setVisible(false);
searchButton.addActionListener(e -> {
submitButton.setVisible(false);
searchMovie = searchInput.getText();
search = true;
showAll = false;
updateMovieList();
showAllButton.setVisible(true);
});
showAllButton.addActionListener(e -> {
submitButton.setVisible(false);
search = false;
showAll = true;
searchInput.setText(""); // Clear the search bar
updateMovieList();
showAllButton.setVisible(false);
});
searchPanel.add(searchLabel);
searchPanel.add(searchInput);
searchPanel.add(searchButton);
searchPanel.add(showAllButton);
this.add(searchPanel, BorderLayout.NORTH);
// Movie list in the center
listModel = new DefaultListModel<>();
movieList = new JList<>(listModel);
movieList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane movieListScrollPane = new JScrollPane(movieList);
this.add(movieListScrollPane, BorderLayout.CENTER);
// Details panel on the right
detailsPanel = new JPanel();
detailsPanel.setLayout(new BoxLayout(detailsPanel, BoxLayout.Y_AXIS));
detailsPanel.setBackground(Yellow);
detailsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Movie details label
movieDetailsLabel = new JLabel("Movie Details");
movieDetailsLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
detailsPanel.add(movieDetailsLabel);
detailsPanel.add(Box.createVerticalStrut(10)); // Add spacing
// Location dropdown
locations = new ArrayList<>();
locationComboBox = new JComboBox<>(locations.toArray(new Location[0]));
locationComboBox.setPreferredSize(new Dimension(200, 30));
locationComboBox.setMaximumSize(locationComboBox.getPreferredSize());
locationComboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
locationComboBox.addActionListener(e -> {
submitButton.setVisible(false);
Location selectedLocation = (Location) locationComboBox.getSelectedItem();
app.setTheatre(selectedLocation);
System.out.println("Selected Location: " + selectedLocation);
showtimesPanel.removeAll();
seatPanel.removeAll();
seatPanel.setVisible(false);
detailsPanel.add(showtimesPanel);
displayShowtimes(selectedLocation, lastSelectedMovie);
});
detailsPanel.add(locationComboBox);
detailsPanel.add(Box.createVerticalStrut(10)); // Add spacing
// Showtimes panel
showtimesPanel = new JPanel();
showtimesPanel.setLayout(new BoxLayout(showtimesPanel, BoxLayout.Y_AXIS));
showtimesPanel.setBackground(Yellow);;
showtimesPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
detailsPanel.add(showtimesPanel);
detailsPanel.add(Box.createVerticalStrut(10)); // Add spacing
// Seat panel
seatPanel = new JPanel();
seatPanel.setLayout(new GridLayout(0, 5, 5, 5));
seatPanel.setBackground(Orange);
seatPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
seatPanel.setVisible(false);
seatPanel.setPreferredSize(new Dimension(300, 200)); // Adjust width and height as needed
seatPanel.setMaximumSize(seatPanel.getPreferredSize());
seatPanel.setMinimumSize(seatPanel.getPreferredSize());
detailsPanel.add(seatPanel);
detailsPanel.add(Box.createVerticalStrut(10)); // Add spacing
// Submit button
submitButton = new JButton("Book Now");
submitButton.setForeground(Red);
submitButton.setVisible(false);
submitButton.setAlignmentX(Component.LEFT_ALIGNMENT);
submitButton.addActionListener(e -> {
System.out.println("seat: " + app.getSelectedSeat());
System.out.println("showtime: " + app.getSelectedShowtime());
System.out.println("movie: " + app.getMovie());
System.out.println("loc: " + app.getTheatre());
ConfirmPanel confirm = app.getConfirmPanel();
confirm.getInfo();
app.switchToConfirm();
});
detailsPanel.add(submitButton);
// Add details panel to the main layout
this.add(detailsPanel, BorderLayout.EAST);
// Initialize the movie list
updateMovieList();
currentSeatButton = null;
currentShowtimeButton = null;
}
public void updateMovieList() {
listModel.clear(); // Clear existing list items
// Fetch movies based on search state
if (showAll) {
movies = movieTC.fetchMovies(-1);
} else if (search) {
movies = movieTC.fetchMovies(searchMovie);
}
if (movies == null) {
listModel.clear();
return;
}
System.out.println(app.getCurrentUser());
for (Movie movie : movies) {
if (movie != null) {
if (movieTC.isReleased(movie.getMovieID()) || app.getCurrentUser() == 1) {
listModel.addElement(movie); // Add the entire movie object to the list model
}
}
}
addMovieSelectionListener(movieTC);
}
private void addMovieSelectionListener(MovieTheatreController movieTC) {
movieList.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
seatPanel.setVisible(false);
Movie selectedMovie = movieList.getSelectedValue();
if (selectedMovie != null) {
if (lastSelectedMovie != null && lastSelectedMovie.equals(selectedMovie)) {
return;
}
seatPanel.setVisible(false);
seatPanel.removeAll();
showtimesPanel.removeAll();
locationComboBox.setVisible(true);
lastSelectedMovie = selectedMovie;
app.setMovie(lastSelectedMovie);
detailsPanel.setVisible(true);
String movieDetails = "<html><b>Title:</b> " + selectedMovie.getTitle() +
"<br><b>Genre:</b> " + selectedMovie.getGenre() + "</html>";
movieDetailsLabel.setText(movieDetails);
ArrayList<Location> loc = movieTC.getMovieLocations(selectedMovie);
locations.clear();
if (!loc.isEmpty()) {
locations.addAll(loc);
}
locationComboBox.setModel(new DefaultComboBoxModel<>(locations.toArray(new Location[0])));
if (!locations.isEmpty()) {
locationComboBox.setSelectedIndex(0);
}
} else {
detailsPanel.setVisible(false);
}
detailsPanel.revalidate();
detailsPanel.repaint();
}
});
}
public void displayShowtimes(Location location, Movie movie) {
showtimesPanel.removeAll(); // Clear old components
ArrayList<Showtime> showtimes = movieTC.fetchShowtimes(location, movie);
if (showtimes == null || showtimes.isEmpty()) {
JLabel noShowtimesLabel = new JLabel("No showtimes available for this movie at this location.");
noShowtimesLabel.setForeground(Red);
showtimesPanel.add(noShowtimesLabel);
} else {
Set<String> uniqueDates = new HashSet<>(); // Collect unique dates
for (Showtime showtime : showtimes) {
uniqueDates.add(showtime.getDate());
}
// Group showtimes by date
Map<String, ArrayList<Showtime>> showtimesGroupedByDate = new HashMap<>();
for (String date : uniqueDates) {
ArrayList<Showtime> dateShowtimes = new ArrayList<>();
for (Showtime showtime : showtimes) {
if (showtime.getDate().equals(date)) {
dateShowtimes.add(showtime);
}
}
showtimesGroupedByDate.put(date, dateShowtimes);
}
// Make sure the layout is set to BoxLayout to stack components vertically
showtimesPanel.setLayout(new BoxLayout(showtimesPanel, BoxLayout.Y_AXIS));
for (Map.Entry<String, ArrayList<Showtime>> entry : showtimesGroupedByDate.entrySet()) {
String date = entry.getKey();
ArrayList<Showtime> dateShowtimes = entry.getValue();
// Create a new date label and add it to the panel (this will automatically start a new row)
JLabel dateLabel = new JLabel("<html><b>Date:</b> " + date + "</html>");
dateLabel.setAlignmentX(Component.LEFT_ALIGNMENT); // Align it to the left (optional)
showtimesPanel.add(dateLabel);
// Add showtimes as JButton for each showtime entry
for (Showtime showtime : dateShowtimes) {
JButton showtimeButton = new JButton(showtime.toString());// Using the overridden toString method
updateButtonColor(showtimeButton, true);
showtimeButton.setAlignmentX(Component.LEFT_ALIGNMENT); // Align it to the left
showtimeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentShowtimeButton == showtimeButton){
if (currentShowtimeButton.getBackground().equals(pastelGreen)){
// means the button was prev Selected, now user wants to unclick
updateButtonColor(showtimeButton, true); // making the button default bg
currentShowtimeButton = null; // no selected showtime
System.out.println("Deselected showtime:" + app.getSelectedShowtime().getShowtimeID());
seatPanel.removeAll();
seatPanel.setVisible(false);
app.setSelectedShowtime(null);
submitButton.setVisible(false);
return;
}else {
// means the button wasn't prev selected, now user wants to select it
updateButtonColor(showtimeButton, false);
}
}
System.out.println("Showtime clicked: " + showtime);
updateButtonColor(showtimeButton, false);
if (currentShowtimeButton != null) {
updateButtonColor(currentShowtimeButton, true);
}
app.setSelectedShowtime(showtime);
submitButton.setVisible(false);
seatPanel.setVisible(false);
currentShowtimeButton = showtimeButton; // Update the current showtime button
// Debugging: Check selected showtime details
System.out.println("Showtime selected ID: " + app.getSelectedShowtime().getShowtimeID());
System.out.println("Showtime selected date/time: " + app.getSelectedShowtime().getDate() + " " + app.getSelectedShowtime().getTime());
displaySeatMap(showtime); // Show seat map for the selected showtime
seatPanel.setVisible(true); // Make seat panel visible
}
});
showtimesPanel.add(showtimeButton); // Add the button to the details panel
}
showtimesPanel.add(Box.createVerticalStrut(10)); // Add space between dates
}
}
// Refresh the details panel to reflect the updated showtimes
showtimesPanel.revalidate();
showtimesPanel.repaint();
}
public void displaySeatMap(Showtime showtime) {
seatPanel.removeAll();
ArrayList<Seat> seats = movieTC.fetchSeats(showtime.getShowtimeID());
if (seats == null || seats.isEmpty()) {
JLabel noSeats = new JLabel("No seats available for this showtime.");
noSeats.setForeground(Red);
seatPanel.add(noSeats);
} else {
seatPanel.setLayout(new GridLayout(0, 5, 5, 5));
for (Seat seat : seats) {
JButton seatButton = new JButton();
seatButton.setForeground(Red);
if (!seat.getAvailable()) {
seatButton.setEnabled(false);
}
seatButton.setText(seat.toString());
if (!seat.isAnRUSeat() && !movieTC.isReleased(lastSelectedMovie.getMovieID()) && app.getCurrentUser() == 1) {
seatButton.setEnabled(false);
}
seatButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentSeatButton == seatButton) {
if (currentSeatButton.getBackground().equals(pastelGreen)) {
updateButtonColor(seatButton, true); // making the button default bg
currentSeatButton = null;
System.out.println("Deselected seat:" + app.getSelectedSeat().getSeatID());
app.setSelectedSeat(null);
submitButton.setVisible(false);
return;
} else {
updateButtonColor(seatButton, false);
}
}
app.setSelectedSeat(seat);
submitButton.setVisible(true);
seatButton.setBackground(pastelGreen);
System.out.println("Seat selected ID: " + app.getSelectedSeat().getSeatID());
System.out.println("Seat selected: " + app.getSelectedSeat().getSeatRow() + app.getSelectedSeat().getSeatCol());
if (currentSeatButton != null) {
currentSeatButton.setBackground(UIManager.getColor("Button.background"));
}
currentSeatButton = seatButton;
}
});
seatPanel.add(seatButton);
seatPanel.revalidate();
seatPanel.repaint();
if (!detailsPanel.isAncestorOf(seatPanel)) {
detailsPanel.add(seatPanel, BorderLayout.SOUTH);
}
detailsPanel.revalidate();
detailsPanel.repaint();
}
}
}
public void updateButtonColor(JButton button, boolean unclicked){
if (unclicked){
button.setBackground(UIManager.getColor("Button.background"));
} else
{
button.setBackground(pastelGreen);
}
}
}