da70439027d18c5dc52e78b347c0fa990c48eb88
[RRRRHHHH_Code] / ruralHouses / src / gui / listOfRemovalRequestsGUI.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 listOfRemovalRequestsGUI 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 listOfRemovalRequestsGUI() {
36                 try {
37                         this.houses= am.getDeletionRequests();
38                         init();
39                 } catch (Exception e) {
40                         e.printStackTrace();
41                 }
42         }
43
44         private void init() throws Exception {
45                 setBounds(100, 100, 600, 450);
46                 contentPane = new JPanel();
47                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
48                 setContentPane(contentPane);
49                 contentPane.setLayout(null);
50
51                 JLabel lblNewLabel = new JLabel();
52                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
53                 lblNewLabel.setBounds(23, 41, 536, 33);
54                 contentPane.add(lblNewLabel);
55                 if (houses.isEmpty())
56                         lblNewLabel.setText("There are not houses matching your search");
57                 else
58                         lblNewLabel.setText("List of houses that match your search:");
59                 JScrollPane scrollPane = new JScrollPane();
60                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
61                 scrollPane.setBounds(23, 113, 536, 271);
62                 contentPane.add(scrollPane);
63
64                 table = new JTable() {
65                 private static final long serialVersionUID = 1L;
66
67                 public boolean isCellEditable(int row, int column) {                
68                         return false;               
69                 };
70             };
71                 scrollPane.setViewportView(table);
72                 tableModel = new DefaultTableModel(null, new String[] {
73                                 "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
74                                 "Livings" });
75                 
76                 //Maybe there is a better way to avoid interaction.
77                 //table.setEnabled(false);
78                 table.setModel(tableModel);
79                 Enumeration<RuralHouse> en = houses.elements();
80                 RuralHouse rh;
81                 JButton btnNewButton = new JButton("Confirm Deletion");
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.removeHouse(rh, rh.getOwner());
89                                         houses.remove(rh);
90                                         am.removeHouseDeletionRequests(rh);
91                                         Administrator.saveInstance();
92                                 }
93                         }
94                 });
95                 btnNewButton.setBounds(301, 394, 169, 25);
96                 contentPane.add(btnNewButton);
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 }