-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShowAllCarsPanel.java
150 lines (137 loc) · 3.84 KB
/
ShowAllCarsPanel.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* A panel used for display information about all cars in the collection
* @
*
* PUBLIC FEATURES:
* // Constructors
* public ShowAllCarsPanel(CarSalesSystem carSys, JPanel dest)
*
* // Methods
* public void actionPerformed(ActionEvent ev)
* public void carsUpdated(CarUpdateEvent ev)
* public void stateChanged(ChangeEvent ev)
*
* COLLABORATORS:
* CarDetailComponents
*
* @version 1.0, 16 Oct 2004
* @author Adam Black
*/
public class ShowAllCarsPanel extends JPanel implements ActionListener, ChangeListener
{
private CarSalesSystem carSystem;
private Car[] carList;
private int currentIndex = 0;
private JLabel headingLabel = new JLabel("Show all makes and models");
private JButton previousButton = new JButton("Previous");
private JButton nextButton = new JButton("Next");
private JPanel buttonPanel = new JPanel();
private CarDetailsComponents carComponents = new CarDetailsComponents();
private boolean carsUpdated = false;
/**
* @param carSys links to a CarSalesSystem object
* @param dest where the panel will be displayed on the main frame
*/
public ShowAllCarsPanel(CarSalesSystem carSys)
{
carSystem = carSys;
carList = carSystem.getAllCars();
if (carList.length > 0)
carComponents.displayDetails(carList[0]);
carSys.addCarUpdateListener(this);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
previousButton.addActionListener(this);
nextButton.addActionListener(this);
headingLabel.setAlignmentX(0.5f);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
add(Box.createVerticalStrut(10));
add(headingLabel);
add(Box.createVerticalStrut(15));
carComponents.add(buttonPanel, "Center");
add(carComponents);
carList = carSystem.getAllCars();
}
/**
* check for button clicks
*
* @param ev ActionEvent object
*/
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource() == previousButton)
previousButtonClicked();
else if (ev.getSource() == nextButton)
nextButtonClicked();
}
/**
* this method is invoked when a car has been added to the system.
*
* @param ev CarUpdateEvent Object
*/
public void carsUpdated(CarUpdateEvent ev)
{
if (ev.getSource() == carSystem)
{
// remember the cars have been updated
carsUpdated = true;
}
}
/**
* when the tab on the main frame gets changed over to this one, we may have to update the
* car list with the latest version
*
* @param ev ChangeEvent object
*/
public void stateChanged(ChangeEvent ev)
{
// source is a JTabbedPane
if (ev.getSource() instanceof JTabbedPane)
{
JTabbedPane tab = (JTabbedPane)ev.getSource();
// if selected tab index is the ShowAllCarsPanel, which is this panel
if (tab.getSelectedIndex() == 2)
{
// if the cars have been updated since last time
if (carsUpdated)
{
carList = carSystem.getAllCars();
if (!(carList == null))
carComponents.displayDetails(carList[currentIndex]);
// next time don't bother updating the car list unless a car is added again
carsUpdated = false;
}
}
}
}
/**
* display next item in index, otherwise display no next item message dialog
*/
private void nextButtonClicked()
{
if (currentIndex < carList.length - 1)
{
currentIndex++;
carComponents.displayDetails(carList[currentIndex]);
}
else
JOptionPane.showMessageDialog(carSystem, "You can't navigate any further", "Alert", JOptionPane.ERROR_MESSAGE);
}
/**
* display previous item in index, otherwise display no next item message dialog
*/
private void previousButtonClicked()
{
if (currentIndex > 0)
{
currentIndex--;
carComponents.displayDetails(carList[currentIndex]);
}
else
JOptionPane.showMessageDialog(carSystem, "You can't navigate any further", "Alert", JOptionPane.ERROR_MESSAGE);
}
}