30e5e6606b4156b55ed2a33df0f4e5a41d7ee4aa
[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.rmi.Naming;
8 import java.rmi.RemoteException;
9 import java.util.Enumeration;
10 import java.util.Vector;
11
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.JTable;
18 import javax.swing.border.EmptyBorder;
19 import javax.swing.table.DefaultTableModel;
20
21 import common.AdminInterface;
22 import common.HouseInterface;
23
24 import configuration.___IntNames;
25 import domain.RuralHouse;
26
27 public class listOfAdditionRequestsGUI extends JFrame {
28
29         /**
30          * 
31          */
32         private static final long serialVersionUID = 1L;
33         private JPanel contentPane;
34         private JTable table;
35         private DefaultTableModel tableModel;
36         private AdminInterface am = null;
37         private Vector<RuralHouse> houses;
38         /**
39          * Create the frame.
40          */
41         public listOfAdditionRequestsGUI() {
42                 
43                 try {
44                         am = (AdminInterface) Naming
45                                         .lookup(___IntNames.AdminManager);
46                 } catch (Exception e1) {
47                         System.out.println("Error accessing remote authentication: "
48                                         + e1.toString());
49                 }
50                 
51                 setTitle("Adding requests");
52                 try {
53                         this.houses= am.getAdditionRequests();
54                         init();
55                 } catch (Exception e) {
56                         e.printStackTrace();
57                 }
58         }
59
60         private void init() throws Exception {
61                 setBounds(100, 100, 600, 450);
62                 contentPane = new JPanel();
63                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
64                 setContentPane(contentPane);
65                 contentPane.setLayout(null);
66
67                 JLabel lblNewLabel = new JLabel();
68                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
69                 lblNewLabel.setBounds(23, 41, 536, 33);
70                 contentPane.add(lblNewLabel);
71                 if (houses.isEmpty())
72                         lblNewLabel.setText("There are not houses to be added");
73                 else
74                         lblNewLabel.setText("List of houses to be added:");
75                 JScrollPane scrollPane = new JScrollPane();
76                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
77                 scrollPane.setBounds(23, 113, 536, 271);
78                 contentPane.add(scrollPane);
79
80                 table = new JTable() {
81                 private static final long serialVersionUID = 1L;
82
83                 public boolean isCellEditable(int row, int column) {                
84                         return false;               
85                 };
86             };
87                 scrollPane.setViewportView(table);
88                 tableModel = new DefaultTableModel(null, new String[] {
89                                 "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
90                                 "Livings" });
91                 
92                 //Maybe there is a better way to avoid interaction.
93                 //table.setEnabled(false);
94                 table.setModel(tableModel);
95                 
96                 JButton btnNewButton = new JButton("Confirm Addition");
97                 btnNewButton.addActionListener(new ActionListener() {
98                         public void actionPerformed(ActionEvent e) {
99                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
100                                         HouseInterface hm= null;
101                                         try {
102                                                 hm = (HouseInterface) Naming
103                                                                 .lookup(___IntNames.HouseManager);
104                                         } catch (Exception e1) {
105                                                 System.out.println("Error accessing remote authentication: "
106                                                                 + e1.toString());
107                                         }
108                                         
109                                         RuralHouse rh = houses.get(table.getSelectedRow());
110                                         //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that.
111                                         try {
112                                                 hm.registerNewHouse(rh);
113                                                 am.removeHouseAdditionRequests(rh);
114                                                 am.saveInstance();
115                                         } catch (RemoteException e1) {
116                                                 // TODO Auto-generated catch block
117                                                 e1.printStackTrace();
118                                         }
119                                         
120                                         ((DefaultTableModel)table.getModel()).removeRow(houses.indexOf(rh));
121                                 }
122                         }
123                 });
124                 btnNewButton.setBounds(88, 396, 169, 25);
125                 contentPane.add(btnNewButton);
126                 
127                 JButton btnDenyAddition = new JButton("Deny Addition");
128                 btnDenyAddition.addActionListener(new ActionListener() {
129                         public void actionPerformed(ActionEvent arg0) {
130                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
131                                         RuralHouse rh = houses.get(table.getSelectedRow());
132                                         try {
133                                                 am.removeHouseAdditionRequests(rh);
134                                                 am.saveInstance();
135                                         } catch (RemoteException e) {
136                                                 // TODO Auto-generated catch block
137                                                 e.printStackTrace();
138                                         }
139                                         ((DefaultTableModel)table.getModel()).removeRow(houses.indexOf(rh));
140                                         houses.remove(rh);
141                                 }
142                         }
143                 });
144                 btnDenyAddition.setBounds(300, 396, 169, 25);
145                 contentPane.add(btnDenyAddition);
146                 Enumeration<RuralHouse> en = houses.elements();
147                 RuralHouse rh;
148                 
149                 while (en.hasMoreElements()) {
150                         rh = en.nextElement();
151                         Vector<Object> row = new Vector<Object>();
152                         row.add(rh.getHouseName());
153                         row.add(rh.getFeatures().getnRooms());
154                         row.add(rh.getFeatures().getnKitchens());
155                         row.add(rh.getFeatures().getnBaths());
156                         row.add(rh.getFeatures().getnParkings());
157                         row.add(rh.getFeatures().getnLivings());
158                         tableModel.addRow(row);
159                 }
160
161         }
162 }