Repository navigation
Expand file tree
/
Copy pathOrderDisplay.java
More file actions
148 lines (135 loc) · 3.43 KB
/
Copy pathOrderDisplay.java
File metadata and controls
148 lines (135 loc) · 3.43 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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class OrderDisplay extends JPanel implements ActionListener {
/**
* This class displays a list of the orders for a certain book
*/
private static final long serialVersionUID = 1L;
private JList ordList;
private DefaultListModel ordMod;
private JScrollPane scroll;
private JPanel buts;
private JButton rm;
private JButton num;
private JButton teach;
private ArrayList<BookOrder> arr;
private Book bk;
public static final String ordFile="orders.txt";
public OrderDisplay() {
// TODO Auto-generated constructor stub
super();
bk=new Book("",null,0,0,null,Book.g.AP,0);
init();
}
/**
* Initialize the display
*/
private void init(){
arr=new ArrayList<BookOrder>();
buts=new JPanel(new FlowLayout());
((FlowLayout)buts.getLayout()).setAlignment(FlowLayout.LEFT);
rm=new JButton("Room");
rm.setMargin(new Insets(0,0,0,0));
rm.addActionListener(this);
num=new JButton("Num");
num.setMargin(new Insets(0,0,0,0));
num.addActionListener(this);
teach=new JButton("Teach");
teach.setMargin(new Insets(0,30,0,30));
teach.addActionListener(this);
buts.add(rm);
buts.add(num);
buts.add(teach);
this.setLayout(new BorderLayout());
this.add(buts,BorderLayout.NORTH);
ordMod=new DefaultListModel();
ordList=new JList(ordMod);
scroll=new JScrollPane(ordList);
this.add(scroll);
this.setMinimumSize(new Dimension(200,500));
this.setVisible(true);
}
/**
* Set the list to display this book's orders
* @param k
*/
public void setList(Book k){
bk=k;
ArrayList<BookOrder> a=new ArrayList<BookOrder>();
for(OrderList ol:BookData.ordersSubmit){
if(ol.getBook().equals(k.getTitle())){
a.add(ol.getOrder());
}
}
BookOrder[] b=(a.toArray(new BookOrder[a.size()]));
if(b.length<=ordMod.getSize()){
int i=0;
for(i=0;i<b.length;i++){
ordMod.set(i, b[i]);
arr.set(i,b[i]);
}
while(ordMod.getSize()>b.length){
ordMod.remove(ordMod.getSize()-1);
arr.remove(ordMod.getSize());
}
}else{
int i=0;
for(i=0;i<ordMod.getSize();i++){
ordMod.set(i, b[i]);
arr.set(i,b[i]);
}
for(;i<b.length;i++){
ordMod.add(i, b[i]);
arr.add(i,b[i]);
}
}
this.repaint();
for(BookOrder ao:arr){
BookData.saveOrder(ordFile,new OrderList(ao,k.getTitle()));
}
}
public BookOrder getSelection(){
return (BookOrder)ordMod.getElementAt(ordList.getLeadSelectionIndex());
}
public int getSelectionInd(){
return ordList.getLeadSelectionIndex();
}
/**
* This removes an order
*/
public void removeOrd(){
BookOrder ind=getSelection();
ordMod.removeElement(ind);
arr.remove(ind);
BookData.ordersSubmit.remove(ind);
setList(bk);
}
/**
* This responds to buttons by sorting the orders
*/
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==rm){
bk.setOrders(Sort.sortRoomBO(bk.getOrders()));
setList(bk);
}else if(ae.getSource()==num){
bk.setOrders(Sort.sortNumBO(bk.getOrders()));
setList(bk);
}else{
bk.setOrders(Sort.sortTeacher(bk.getOrders()));
setList(bk);
}
}
}