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