-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortProductsGUI.java
More file actions
96 lines (87 loc) · 4.27 KB
/
Copy pathSortProductsGUI.java
File metadata and controls
96 lines (87 loc) · 4.27 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
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* Project 5 - SortProductsGUI.java
*
* The interface associated with a customer wanting to sort products in the application.
*
* @author Shafer Anthony Hofmann, Qihang Gan, Shreyas Viswanathan, Nathan Pasic
* Miller, Oliver Long
*
* @version December 6, 2023
*/
public class SortProductsGUI extends JComponent {
public SortProductsGUI(String[] initialProducts, CustomerClient customerClient) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame sortProductsFrame = new JFrame("Sort Products");
sortProductsFrame.setSize(350, 350);
sortProductsFrame.setLocationRelativeTo(null);
sortProductsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
JButton sortByPriceAscendingButton = new JButton("Sort By Price Ascending");
JButton sortByPriceDescendingButton = new JButton("Sort By Price Descending");
JButton sortByQuantityAscendingButton = new JButton("Sort By Quantity Ascending");
JButton sortByQuantityDescendingButton = new JButton("Sort By Quantity Descending");
buttonPanel.add(sortByPriceAscendingButton);
buttonPanel.add(sortByPriceDescendingButton);
buttonPanel.add(sortByQuantityAscendingButton);
buttonPanel.add(sortByQuantityDescendingButton);
DefaultListModel<String> listModel = new DefaultListModel<>();
JList<String> list = new JList<>(listModel);
for (String product : initialProducts) {
listModel.addElement(product);
}
sortByPriceAscendingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] sortProductsResult = customerClient.sortProducts("price", true);
sortAction(sortProductsResult, listModel);
}
});
sortByPriceDescendingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] sortProductsResult = customerClient.sortProducts("price", false);
sortAction(sortProductsResult, listModel);
}
});
sortByQuantityAscendingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] sortProductsResult = customerClient.sortProducts("quantity", true);
sortAction(sortProductsResult, listModel);
}
});
sortByQuantityDescendingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[] sortProductsResult = customerClient.sortProducts("quantity", false);
sortAction(sortProductsResult, listModel);
}
});
mainPanel.add(new JScrollPane(list), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.PAGE_END);
sortProductsFrame.add(mainPanel);
sortProductsFrame.setVisible(true);
}
});
}
public void sortAction(Object[] sortProductsResult, DefaultListModel<String> listModel) {
if (sortProductsResult[0].equals("ERROR")) {
new ErrorMessageGUI((String) sortProductsResult[1]);
return;
} else {
String[] originalProducts = ((String) sortProductsResult[1]).split("\n");
listModel.clear();
for (String product : originalProducts) {
listModel.addElement(product);
}
}
}
}