· 8 years ago · Mar 06, 2018, 07:56 PM
1package employeeApp;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.GridLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.io.BufferedInputStream;
12import java.io.BufferedOutputStream;
13import java.io.BufferedWriter;
14import java.io.DataInputStream;
15import java.io.EOFException;
16import java.io.File;
17import java.io.FileInputStream;
18import java.io.FileNotFoundException;
19import java.io.FileOutputStream;
20import java.io.FileWriter;
21import java.io.IOException;
22import java.io.ObjectInputStream;
23import java.io.ObjectOutputStream;
24import java.text.SimpleDateFormat;
25import java.util.Date;
26import java.util.Hashtable;
27import java.util.Vector;
28
29import javax.swing.BorderFactory;
30import javax.swing.DefaultCellEditor;
31import javax.swing.JButton;
32import javax.swing.JComboBox;
33import javax.swing.JFileChooser;
34import javax.swing.JFrame;
35import javax.swing.JLabel;
36import javax.swing.JMenu;
37import javax.swing.JMenuBar;
38import javax.swing.JMenuItem;
39import javax.swing.JOptionPane;
40import javax.swing.JPanel;
41import javax.swing.JScrollPane;
42import javax.swing.JTable;
43import javax.swing.JTextField;
44import javax.swing.ListSelectionModel;
45import javax.swing.border.Border;
46import javax.swing.event.ListSelectionEvent;
47import javax.swing.event.ListSelectionListener;
48import javax.swing.event.TableModelEvent;
49import javax.swing.event.TableModelListener;
50import javax.swing.filechooser.FileNameExtensionFilter;
51
52import employeeApp.MyTableModel;
53
54public class EmployeeApplication extends JFrame {
55 JTable myTable;
56
57 // Menu structure
58 JMenuBar myBar;
59 JMenu fileMenu, employeeMenu, exitMenu;
60 JMenuItem fileLoad, fileSave, fileSaveAs, filePrintReport, addEmployee, removeEmployee, exitProgram;
61
62 // Array of data types to be used in combo box when defining new structure
63 String[] dataTypeNames;
64
65 JPanel p;
66 MyTableModel tm;
67 JScrollPane myPane;
68
69 // Subdialog used when defining new structure
70 //DepositFundsDialog depositDialog;
71 JLabel lblPPSNo, lblName, lblGender, lblDepartment, lblPosition, lblSalary;
72 JTextField txtPPSNo, txtName, txtDepartment, txtPosition, txtSalary;
73 JComboBox cbGender, comboBox;
74 String[] genderArr = { "Male", "Female" };
75 JButton btnNew, btnUpdate, btnDelete, btnPrintAll, btnClose;
76
77 // Used to indicate whether data is already in a file
78 File currentFile;
79
80
81 public EmployeeApplication(){
82 // Create menu bar and table to panel
83 JMenuBar myBar = createMenuBar();
84 this.setJMenuBar(myBar);
85
86 p = new JPanel();
87 tm = new MyTableModel();
88 myTable = new JTable(tm);
89 myPane = new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
90
91 myTable.setSelectionForeground(Color.white);
92 myTable.setSelectionBackground(Color.red);
93 myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
94
95
96 p.setLayout(new BorderLayout());
97 myPane.setPreferredSize(new Dimension(450, 110));
98 p.add(myPane, BorderLayout.NORTH);
99
100 JPanel myCenterPanel = createCentrePanel();
101 Border line = BorderFactory.createLineBorder(Color.black);
102 myCenterPanel.setBorder(BorderFactory.createTitledBorder(line, "Employee Records Editor"));
103
104
105
106 p.add(myCenterPanel, BorderLayout.CENTER);
107
108 JPanel myBottomPanel = createBottomPanel();
109 p.add(myBottomPanel, BorderLayout.SOUTH);
110
111 // Associating event listeners with menu items
112 //TODO: on row selection populate all the textfields and combobox with the values from the selected row
113
114
115
116
117
118 //TODO: Add Employee record - both the add button and the add employee menu item should use
119 //the same event handler/listener to add a record and insert a row
120
121
122 //TODO: Remove Employee record - both the delete button and the remove employee menu item should use
123 //the same event handler/listener to delete a record
124
125 /*btnDelete.addActionListener(new ActionListener(){
126 tm.deleteRow();
127 });*/
128
129 //TODO:Update button
130
131
132
133 // TODO load the contents of the employee.dat file into the table
134 fileLoad.addActionListener(new ActionListener() {
135 public void actionPerformed(ActionEvent e) {
136 JFileChooser fc = new JFileChooser();
137 int returnVal = fc.showOpenDialog(EmployeeApplication.this);
138 if (returnVal == JFileChooser.APPROVE_OPTION) {
139 File file = fc.getSelectedFile();
140 // Check to see if there is already a file with this name
141 if (file.exists()) {
142 int result = JOptionPane.showConfirmDialog(EmployeeApplication.this, "This will overwrite the existing file.\n Are you sure you want to do this?");
143 if (result == 0) {
144 try {
145 // We put the implementation of writing into a separate method
146 readDataFile(file);
147 }
148 catch (ClassNotFoundException e1) {
149 // TODO Auto-generated catch block
150 e1.printStackTrace();
151 }
152 catch (IOException ex) {
153 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
154 }
155 }
156 }
157 }
158 }
159 });
160
161
162 // Saving the file - usually in a different location
163 fileSaveAs.addActionListener(new ActionListener() {
164 public void actionPerformed(ActionEvent e) {
165 JFileChooser fc = new JFileChooser();
166 int returnVal = fc.showSaveDialog(EmployeeApplication.this);
167 if (returnVal == JFileChooser.APPROVE_OPTION) {
168 currentFile = fc.getSelectedFile();
169 // Check to see if there is already a file with this name
170 if (file.exists()) {
171 int result = JOptionPane.showConfirmDialog(EmployeeApplication.this, "This will overwrite the existing file.\n Are you sure you want to do this?");
172 if (result == 0) {
173 try {
174 // We put the implementation of writing into a separate method
175 writeDataFile(file);
176 }
177 catch (IOException ex) {
178 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
179 }
180 }
181 }
182 // This is a new file name
183 else {
184 try {
185 writeDataFile(file);
186 }
187 catch (IOException ex) {
188 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
189 }
190 }
191 }
192 }
193 });
194
195 // Saving the file - usually in the same location
196 fileSave.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
198 // Data may not yet have been saved to a file
199 // currentFile not being null means that the file object has been created and we then check that it exists in the file system
200 if (currentFile != null && currentFile.exists()) {
201 int result = JOptionPane.showConfirmDialog(EmployeeApplication.this, "This will overwrite the existing file.\n Are you sure you want to do this?");
202 if (result == 0) {
203 try {
204 writeDataFile(currentFile);
205 }
206 catch (IOException ex) {
207 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
208 }
209 }
210 }
211 // Otherwise we need to specify the name to use for this file
212 else {
213 JOptionPane.showMessageDialog(EmployeeApplication.this, "File doesn't exist.\n", "Error message", JOptionPane.ERROR_MESSAGE);
214 // Ask user to specify file name (remember user can type in new file name in file chooser)
215 JFileChooser fc = new JFileChooser();
216 int returnVal = fc.showOpenDialog(EmployeeApplication.this);
217 if (returnVal == JFileChooser.APPROVE_OPTION) {
218 File file = fc.getSelectedFile();
219 if (currentFile.exists()) {
220 int result = JOptionPane.showConfirmDialog(EmployeeApplication.this, "This will overwrite the existing file.\n Are you sure you want to do this?");
221 if (result == 0) {
222 try {
223 writeDataFile(currentFile);
224 }
225 catch (IOException ex) {
226 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
227 }
228 }
229 }
230 else {
231 try {
232 writeDataFile(currentFile);
233 }
234 catch (IOException ex) {
235 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
236 }
237 }
238 }
239 }
240 }
241 });
242
243
244 // Prints report as external HTML file with name BankReport.html
245
246 filePrintReport.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent e) {
248
249 /* Insert code here */
250
251 }
252 });
253
254
255
256 // Both of these listeners behave in the same way so we extract their code to a separate method
257
258 // exits program from menu
259 exitProgram.addActionListener(new ActionListener() {
260 public void actionPerformed(ActionEvent e) {
261 closeDown();
262 }
263 });
264
265 btnClose.addActionListener(new ActionListener() {
266 public void actionPerformed(ActionEvent e) {
267 closeDown();
268 }
269 });
270
271 // exits program by closing window
272 this.addWindowListener(new WindowAdapter(){
273 public void windowClosing(WindowEvent e){
274 closeDown();
275 }
276 }); // end windowlistener
277
278 this.setTitle("Employee Application");
279 this.setContentPane(p);
280 this.setVisible(true);
281 this.pack();
282 } // constructor
283
284 public void readDataFile(File f) throws IOException, ClassNotFoundException {
285 ObjectInputStream in = null;
286
287 try {
288 FileInputStream fi = new FileInputStream("employee.dat");
289
290 in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
291
292 Vector<String> columns;
293 Vector<Object> rows;
294
295 columns = (Vector<String>) in.readObject();
296 rows = (Vector<Object>) in.readObject();
297
298 in.readObject();
299
300 tm.setColumnNames(columns);
301 tm.setTableData(rows);
302
303 in.readObject();
304 tm.modified = false;
305
306
307 } catch (FileNotFoundException ex) {
308 JOptionPane.showMessageDialog(EmployeeApplication.this, "File Not Found Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
309 }
310 catch (IOException ex) {
311 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
312 }
313 catch (Exception ex) {
314 JOptionPane.showMessageDialog(EmployeeApplication.this, "Exception\n" + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
315 }
316 // Make sure to close stream
317 finally {
318 in.close();
319 }
320 }
321
322
323
324 public void writeDataFile(File f) throws IOException, FileNotFoundException {
325 ObjectOutputStream out = null;
326 try {
327 out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
328 // Write column headers as one object
329 out.writeObject(tm.getColumnNames());
330 // Write table data as second object
331 out.writeObject(tm.getTableData());
332 // This indicates that there is no unsaved data for the moment
333 tm.modified = false;
334 }
335 catch (FileNotFoundException ex) {
336 JOptionPane.showMessageDialog(EmployeeApplication.this, "File Not Found Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
337 }
338 catch (IOException ex) {
339 JOptionPane.showMessageDialog(EmployeeApplication.this, "I/O Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
340 }
341 catch (Exception ex) {
342 JOptionPane.showMessageDialog(EmployeeApplication.this, "Exception\n " + ex.toString(), "Error Message", JOptionPane.ERROR_MESSAGE);
343 }
344 // Make sure to close stream
345 finally {
346 out.close();
347 }
348 }
349// This behaviour will be used whether we close the application by clicking on the X button in the top-right corner or by selecting
350// an option from a menu, so it makes sense to have it as an independent method that can be reused.
351 public void closeDown() {
352 // Ask user to confirm decision
353 int result = JOptionPane.showConfirmDialog(EmployeeApplication.this, "This will close the application.\n Are you sure you want to do this?");
354 if (result == 0) {
355 // Give user second chance if there is unsaved data
356 if (tm.modified) {
357 int result2 = JOptionPane.showConfirmDialog(EmployeeApplication.this, "You have unsaved data that will be lost.\n Are you sure you want to do this?");
358 if (result2 == 0) {
359 System.exit(0);
360 }
361 }
362 else {
363 System.exit(0);
364 }
365 }
366 }
367
368 public JMenuBar createMenuBar(){
369 // Setting up menu
370 fileLoad = new JMenuItem("Load/Open");
371 fileSave = new JMenuItem("Save");
372 fileSaveAs = new JMenuItem("Save As");
373 filePrintReport = new JMenuItem("Print Report");
374
375 fileMenu = new JMenu("File");
376 fileMenu.add(fileLoad);
377 fileMenu.add(fileSave);
378 fileMenu.add(fileSaveAs);
379 fileMenu.add(filePrintReport);
380
381 addEmployee = new JMenuItem("Add");
382 removeEmployee = new JMenuItem("Remove");
383
384 employeeMenu = new JMenu("Employees");
385 employeeMenu.add(addEmployee);
386 employeeMenu.add(removeEmployee);
387
388 exitProgram = new JMenuItem("Exit Program");
389 exitMenu = new JMenu("Exit");
390 exitMenu.add(exitProgram);
391
392 myBar = new JMenuBar();
393 myBar.add(fileMenu);
394 myBar.add(employeeMenu);
395 myBar.add(exitMenu);
396
397 return myBar;
398
399 }
400 public JPanel createCentrePanel()
401 {
402 JPanel centerPanel = new JPanel(new GridLayout(6, 2));
403
404 lblPPSNo = new JLabel("PPS Number", JLabel.LEFT);
405 lblName = new JLabel("FullName", JLabel.LEFT);
406 lblGender= new JLabel("Gender", JLabel.LEFT);
407 lblDepartment= new JLabel("Department", JLabel.LEFT);
408 lblPosition= new JLabel("Position", JLabel.LEFT);
409 lblSalary= new JLabel("Salary", JLabel.LEFT);
410
411
412 txtPPSNo= new JTextField(20);
413 txtName= new JTextField(20);
414 txtDepartment= new JTextField(20);
415 txtPosition= new JTextField(20);
416 txtSalary= new JTextField(20);
417 txtSalary.setText("0.0");
418 String[] genderArr = { "Male", "Female" };
419 cbGender= new JComboBox(genderArr);
420
421 centerPanel.add(lblPPSNo);
422 centerPanel.add(txtPPSNo);
423 centerPanel.add(lblName);
424 centerPanel.add(txtName);
425 centerPanel.add(lblGender);
426 centerPanel.add(cbGender);
427 centerPanel.add(lblDepartment);
428 centerPanel.add(txtDepartment);
429 centerPanel.add(lblPosition);
430 centerPanel.add(txtPosition);
431 centerPanel.add(lblSalary);
432 centerPanel.add(txtSalary);
433
434 return centerPanel;
435
436 }
437
438 public JPanel createBottomPanel()
439 {
440 JPanel BottomPanel = new JPanel();
441 btnNew = new JButton("Add New");
442 btnUpdate = new JButton("Update");
443 btnDelete = new JButton("Delete");;
444 btnPrintAll = new JButton("Print All");
445 btnClose = new JButton("Close");
446 BottomPanel.add(btnNew);
447 BottomPanel.add(btnUpdate);
448 BottomPanel.add(btnDelete);
449 BottomPanel.add(btnPrintAll);
450 BottomPanel.add(btnClose);
451
452 return BottomPanel;
453
454 }
455
456
457 public static void main (String args[]){
458 new EmployeeApplication();
459 } // main
460
461}