-
Notifications
You must be signed in to change notification settings - Fork 0
/
OverDueTable.java
278 lines (246 loc) · 10.4 KB
/
OverDueTable.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package office;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import static javax.swing.JTable.AUTO_RESIZE_OFF;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
/**
*
* @author Chris
*/
public class OverDueTable extends JFrame implements ActionListener{
DBConnect dbCon = new DBConnect(); //Connects to the Database
private final int WIDTH = 1000, HEIGHT = 500;
JButton done, cancel, update;
JFrame dueFrame = new JFrame("Over Due Items");
JScrollPane jsp;
JTable table;
JComboBox comboBox = new JComboBox();
public OverDueTable() {
done = new JButton("Done");
cancel = new JButton("Cancel");
update = new JButton("Update");
done.setSize(75,30);
cancel.setSize(75,30);
update.setSize(75,30);
done.addActionListener(this);
cancel.addActionListener(this);
update.addActionListener(this);
dueFrame.setLayout(null);
table = loadTable();
table.setAutoResizeMode(AUTO_RESIZE_OFF);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
jsp = new JScrollPane(table,v ,h);
jsp.setSize(985,390);
done.setLocation(350, 400);
cancel.setLocation(576, 400);
update.setLocation(463,400);
jsp.setLocation(5,5);
dueFrame.add(jsp);
dueFrame.add(done);
dueFrame.add(cancel);
dueFrame.add(update);
dueFrame.setSize(WIDTH, HEIGHT);
dueFrame.setLocationRelativeTo(null);
}
public JTable loadTable(){
try{
Connection con = dbCon.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CHECKOUTS WHERE duedate < CURRENT_DATE AND returned = 'NO'");
// It creates and displays the table
table = new JTable(buildTableModel(rs)){
public boolean isCellEditable(int row, int column) {
if (row >= 0 && (column == 12 || column == 16 || column == 17)) {
return true;
} else {
return false;
}
}
};
// Closes the Connection
rs.close();
con.close();
} catch(Exception e){
System.out.print(e);
e.printStackTrace();
JOptionPane optionPane = new JOptionPane(e.toString());
JDialog dialog = optionPane.createDialog("Error!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
}
comboBox.addItem("YES");
comboBox.addItem("NO");
TableColumn column = null;
for (int i = 0; i < 18; i++) {
column = table.getColumnModel().getColumn(i);
if(i == 0){ //Size for Cust_id
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 1){ //Size for Check Out Date
column.setMinWidth(120);
column.setMaxWidth(120);
column.setPreferredWidth(120);
} else if(i == 2){ //Size for Staff
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 3 || i == 4 || i == 5){ //Size for FName,LName,Anum
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 6){ //Size for APT
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 7){ //Size for Phone
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 8){ //Size for Email
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 9){ //Size for Item
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 10){ //Size for Description
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 11){ //Size for Due Date
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 12){ //Size for Returned
column.setMinWidth(80);
column.setMaxWidth(80);
column.setPreferredWidth(80);
} else if(i == 13){ //Size for Checkin date
column.setMinWidth(100);
column.setMaxWidth(100);
column.setPreferredWidth(100);
} else if(i == 14){ //Size for Staff checked in
column.setMinWidth(60);
column.setMaxWidth(60);
column.setPreferredWidth(60);
} else if(i == 15){ //Size for Amount Owed
column.setMinWidth(90);
column.setMaxWidth(90);
column.setPreferredWidth(90);
} else if(i == 16){ //Size for 1st Follow up
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
} else if(i == 17){ //Size for 2nd Follow up
column.setMinWidth(200);
column.setMaxWidth(200);
column.setPreferredWidth(200);
}
if (i == 12) {
column.setCellEditor(new DefaultCellEditor(comboBox));
}
}
return table;
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == cancel){
dueFrame.dispose();
}
if(ae.getSource() == update){
if(table.getSelectedRow() == -1){
JFrame dummy = new JFrame();
dummy.setVisible(false);
dummy.setLocationRelativeTo(null);
dummy.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dummy, "Please select the row you wish to update!", "ATTENTION! ", JOptionPane.INFORMATION_MESSAGE);
dummy.dispose();
} else {
ExecuteCommand();
}
}
if(ae.getSource() == done){
if(table.getSelectedRow() == -1){
JFrame dummy = new JFrame();
dummy.setVisible(false);
dummy.setLocationRelativeTo(null);
dummy.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dummy, "Please select the row you wish to update!", "ATTENTION! ", JOptionPane.INFORMATION_MESSAGE);
dummy.dispose();
} else {
ExecuteCommand();
dueFrame.dispose();
}
}
}
public void ExecuteCommand(){
int selectedRowIndex = table.getSelectedRow();
int selectedColumnIndex = table.getSelectedColumn();
String returned = (String)table.getModel().getValueAt(selectedRowIndex, 12);
String followUp1 = (String)table.getModel().getValueAt(selectedRowIndex, 16);
String followUp2 = (String)table.getModel().getValueAt(selectedRowIndex, 17);
int custID = (int)table.getModel().getValueAt(selectedRowIndex, 0);
String query = "UPDATE CHECKOUTS SET RETURNED=?, FOLLOWUP1=?, FOLLOWUP2=? WHERE CUST_ID = ?";
try{
Connection con = dbCon.getConnection();
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, returned);
stmt.setString(2, followUp1);
stmt.setString(3, followUp2);
stmt.setInt(4, custID);
stmt.executeUpdate();
con.close();
} catch (Exception e){
System.out.print(e);
e.printStackTrace();
JOptionPane optionPane = new JOptionPane(e.toString());
JDialog dialog = optionPane.createDialog("Error!");
dialog.setAlwaysOnTop(this.isAlwaysOnTopSupported());
dialog.setVisible(true);
}
}
}