Debbugin continues...
[RRRRHHHH_Code] / ruralHouses / src / gui / listOfAdditionRequestsGUI.java
1 package gui;
2
3 import java.awt.Font;
4 import java.awt.Rectangle;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.Enumeration;
8 import java.util.Vector;
9
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JScrollPane;
15 import javax.swing.JTable;
16 import javax.swing.border.EmptyBorder;
17 import javax.swing.table.DefaultTableModel;
18
19 import businessLogic.AdminManager;
20 import businessLogic.HouseManager;
21 import businessLogic.HouseManagerInterface;
22 import domain.Administrator;
23 import domain.RuralHouse;
24
25 public class listOfAdditionRequestsGUI extends JFrame {
26
27         private JPanel contentPane;
28         private JTable table;
29         private DefaultTableModel tableModel;
30         private AdminManager am = new AdminManager();
31         private Vector<RuralHouse> houses;
32         /**
33          * Create the frame.
34          */
35         public listOfAdditionRequestsGUI() {
36                 setTitle("Adding requests");
37                 try {
38                         this.houses= am.getAdditionRequests();
39                         init();
40                 } catch (Exception e) {
41                         e.printStackTrace();
42                 }
43         }
44
45         private void init() throws Exception {
46                 setBounds(100, 100, 600, 450);
47                 contentPane = new JPanel();
48                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
49                 setContentPane(contentPane);
50                 contentPane.setLayout(null);
51
52                 JLabel lblNewLabel = new JLabel();
53                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
54                 lblNewLabel.setBounds(23, 41, 536, 33);
55                 contentPane.add(lblNewLabel);
56                 if (houses.isEmpty())
57                         lblNewLabel.setText("There are not houses to be added");
58                 else
59                         lblNewLabel.setText("List of houses to be added:");
60                 JScrollPane scrollPane = new JScrollPane();
61                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
62                 scrollPane.setBounds(23, 113, 536, 271);
63                 contentPane.add(scrollPane);
64
65                 table = new JTable() {
66                 private static final long serialVersionUID = 1L;
67
68                 public boolean isCellEditable(int row, int column) {                
69                         return false;               
70                 };
71             };
72                 scrollPane.setViewportView(table);
73                 tableModel = new DefaultTableModel(null, new String[] {
74                                 "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
75                                 "Livings" });
76                 
77                 //Maybe there is a better way to avoid interaction.
78                 //table.setEnabled(false);
79                 table.setModel(tableModel);
80                 
81                 JButton btnNewButton = new JButton("Confirm Addition");
82                 btnNewButton.addActionListener(new ActionListener() {
83                         public void actionPerformed(ActionEvent e) {
84                                 if (table.getRowCount()!=0) {
85                                         HouseManagerInterface hm = new HouseManager();
86                                         RuralHouse rh = houses.get(table.getSelectedRow());
87                                         //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that.
88                                         hm.registerNewHouse(rh);
89                                         houses.remove(rh);
90                                         am.removeHouseAdditionRequests(rh);
91                                         Administrator.saveInstance();
92                                         ((DefaultTableModel)table.getModel()).removeRow(houses.indexOf(rh));
93                                 }
94                         }
95                 });
96                 btnNewButton.setBounds(301, 394, 169, 25);
97                 contentPane.add(btnNewButton);
98                 Enumeration<RuralHouse> en = houses.elements();
99                 RuralHouse rh;
100                 
101                 while (en.hasMoreElements()) {
102                         rh = en.nextElement();
103                         Vector<Object> row = new Vector<Object>();
104                         row.add(rh.getHouseName());
105                         row.add(rh.getFeatures().getnRooms());
106                         row.add(rh.getFeatures().getnKitchens());
107                         row.add(rh.getFeatures().getnBaths());
108                         row.add(rh.getFeatures().getnParkings());
109                         row.add(rh.getFeatures().getnLivings());
110                         tableModel.addRow(row);
111                 }
112
113         }
114 }