Bugs when deleting houses and offers fixed and GUI's adapated for empty cases
[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.RuralHouse;
23
24 public class listOfAdditionRequestsGUI extends JFrame {
25
26         private JPanel contentPane;
27         private JTable table;
28         private DefaultTableModel tableModel;
29         private AdminManager am = new AdminManager();
30         private Vector<RuralHouse> houses;
31         /**
32          * Create the frame.
33          */
34         public listOfAdditionRequestsGUI() {
35                 try {
36                         this.houses= am.getAdditionRequests();
37                         init();
38                 } catch (Exception e) {
39                         e.printStackTrace();
40                 }
41         }
42
43         private void init() throws Exception {
44                 setBounds(100, 100, 600, 450);
45                 contentPane = new JPanel();
46                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
47                 setContentPane(contentPane);
48                 contentPane.setLayout(null);
49
50                 JLabel lblNewLabel = new JLabel();
51                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
52                 lblNewLabel.setBounds(23, 41, 536, 33);
53                 contentPane.add(lblNewLabel);
54                 if (houses.isEmpty())
55                         lblNewLabel.setText("There are not houses matching your search");
56                 else
57                         lblNewLabel.setText("List of houses that match your search:");
58                 JScrollPane scrollPane = new JScrollPane();
59                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
60                 scrollPane.setBounds(23, 113, 536, 271);
61                 contentPane.add(scrollPane);
62
63                 table = new JTable() {
64                 private static final long serialVersionUID = 1L;
65
66                 public boolean isCellEditable(int row, int column) {                
67                         return false;               
68                 };
69             };
70                 scrollPane.setViewportView(table);
71                 tableModel = new DefaultTableModel(null, new String[] {
72                                 "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
73                                 "Livings" });
74                 
75                 //Maybe there is a better way to avoid interaction.
76                 //table.setEnabled(false);
77                 table.setModel(tableModel);
78                 
79                 JButton btnNewButton = new JButton("Confirm Addition");
80                 btnNewButton.addActionListener(new ActionListener() {
81                         public void actionPerformed(ActionEvent e) {
82                                 if (table.getRowCount()!=0) {
83                                         HouseManagerInterface hm = new HouseManager();
84                                         RuralHouse rh = houses.get(table.getSelectedRow());
85                                         //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that.
86                                         hm.registerNewHouse(rh);
87                                         houses.remove(rh);
88                                         am.removeHouseAdditionRequests(rh);
89                                 }
90                         }
91                 });
92                 btnNewButton.setBounds(301, 394, 169, 25);
93                 contentPane.add(btnNewButton);
94                 Enumeration<RuralHouse> en = houses.elements();
95                 RuralHouse rh;
96                 
97                 while (en.hasMoreElements()) {
98                         rh = en.nextElement();
99                         Vector<Object> row = new Vector<Object>();
100                         row.add(rh.getHouseName());
101                         row.add(rh.getFeatures().getnRooms());
102                         row.add(rh.getFeatures().getnKitchens());
103                         row.add(rh.getFeatures().getnBaths());
104                         row.add(rh.getFeatures().getnParkings());
105                         row.add(rh.getFeatures().getnLivings());
106                         tableModel.addRow(row);
107                 }
108
109         }
110 }