Merge branch 'master' of ssh://xp-dev.com/RRRRHHHH_Code
[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.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 listOfRemovalRequestsGUI 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         /**
40          * Create the frame.
41          */
42         public listOfRemovalRequestsGUI() {
43                 try {
44                         am = (AdminInterface) Naming.lookup(___IntNames.AdminManager);
45                 } catch (Exception e1) {
46                         System.out.println("Error accessing remote authentication: "
47                                         + e1.toString());
48                 }
49                 setTitle("Deleting requests");
50                 try {
51                         this.houses = am.getDeletionRequests();
52                         init();
53                 } catch (Exception e) {
54                         e.printStackTrace();
55                 }
56         }
57
58         private void init() throws Exception {
59                 setBounds(100, 100, 600, 450);
60                 contentPane = new JPanel();
61                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
62                 setContentPane(contentPane);
63                 contentPane.setLayout(null);
64
65                 JLabel lblNewLabel = new JLabel();
66                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
67                 lblNewLabel.setBounds(23, 41, 536, 33);
68                 contentPane.add(lblNewLabel);
69                 if (houses.isEmpty())
70                         lblNewLabel.setText("There are not houses to be deleted");
71                 else
72                         lblNewLabel.setText("List of houses to be deleted:");
73                 JScrollPane scrollPane = new JScrollPane();
74                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
75                 scrollPane.setBounds(23, 113, 536, 271);
76                 contentPane.add(scrollPane);
77
78                 table = new JTable() {
79                         private static final long serialVersionUID = 1L;
80
81                         public boolean isCellEditable(int row, int column) {
82                                 return false;
83                         };
84                 };
85                 scrollPane.setViewportView(table);
86                 tableModel = new DefaultTableModel(null, new String[] { "House Name",
87                                 "Bedrooms", "Kitchens", "Baths", "Parkings", "Livings" });
88
89                 table.setModel(tableModel);
90                 Enumeration<RuralHouse> en = houses.elements();
91                 RuralHouse rh;
92                 JButton btnNewButton = new JButton("Confirm Deletion");
93                 btnNewButton.addActionListener(new ActionListener() {
94                         public void actionPerformed(ActionEvent e) {
95                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
96                                         HouseInterface hm = null;
97                                         try {
98                                                 hm = (HouseInterface) Naming
99                                                                 .lookup(___IntNames.HouseManager);
100                                         } catch (Exception e1) {
101                                                 System.out
102                                                                 .println("Error accessing remote authentication: "
103                                                                                 + e1.toString());
104                                         }
105                                         RuralHouse rh = houses.get(table.getSelectedRow());
106
107                                         try {
108                                                 hm.removeHouse(rh, rh.getOwner());
109                                                 am.removeHouseDeletionRequests(rh);
110                                                 am.saveInstance();
111                                         } catch (RemoteException e1) {
112                                                 e1.printStackTrace();
113                                         }
114                                         ((DefaultTableModel) table.getModel()).removeRow(houses
115                                                         .indexOf(rh));
116                                         houses.remove(rh);
117
118                                 }
119                         }
120                 });
121                 btnNewButton.setBounds(90, 396, 169, 25);
122                 contentPane.add(btnNewButton);
123
124                 JButton btnNewButton_1 = new JButton("Deny Deletion");
125                 btnNewButton_1.addActionListener(new ActionListener() {
126                         public void actionPerformed(ActionEvent e) {
127                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
128                                         RuralHouse rh = houses.get(table.getSelectedRow());
129                                         ((DefaultTableModel) table.getModel()).removeRow(houses
130                                                         .indexOf(rh));
131                                         houses.remove(rh);
132                                         try {
133                                                 am.removeHouseDeletionRequests(rh);
134                                                 am.saveInstance();
135                                         } catch (RemoteException e1) {
136                                                 // TODO Auto-generated catch block
137                                                 e1.printStackTrace();
138                                         }
139
140                                 }
141                         }
142                 });
143                 btnNewButton_1.setBounds(291, 396, 169, 25);
144                 contentPane.add(btnNewButton_1);
145                 while (en.hasMoreElements()) {
146                         rh = en.nextElement();
147                         Vector<Object> row = new Vector<Object>();
148                         row.add(rh.getHouseName());
149                         row.add(rh.getFeatures().getnRooms());
150                         row.add(rh.getFeatures().getnKitchens());
151                         row.add(rh.getFeatures().getnBaths());
152                         row.add(rh.getFeatures().getnParkings());
153                         row.add(rh.getFeatures().getnLivings());
154                         tableModel.addRow(row);
155                 }
156
157         }
158 }