1d78a5303cf119df1ee384bc7e01f4fb5e505fb7
[RRRRHHHH_Code] / ruralHouses client / src / gui / listOfOwnerAddittionRequests.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.AccountInterface;
22 import common.AdminInterface;
23
24 import configuration.___IntNames;
25 import domain.Account;
26
27 public class listOfOwnerAddittionRequests 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<Account> accounts;
38         /**
39          * Create the frame.
40          */
41         public  listOfOwnerAddittionRequests()  {
42                 try {
43                         am = (AdminInterface) Naming
44                                         .lookup(___IntNames.AdminManager);
45                 } catch (Exception e1) {
46                         System.out.println("Error accessing remote authentication: "
47                                         + e1.toString());
48                 }               
49                 setTitle("Adding requests");
50                 try {
51                         this.accounts= am.getOwnerAdditionRequests();
52                         init();
53                 } catch (Exception e) {
54                         e.printStackTrace();
55                 }
56         }
57
58         private void init() throws Exception {
59                 
60                 try {
61                         am = (AdminInterface) Naming
62                                         .lookup(___IntNames.AdminManager);
63                 } catch (Exception e1) {
64                         System.out.println("Error accessing remote authentication: "
65                                         + e1.toString());
66                 }
67                 setBounds(100, 100, 600, 450);
68                 contentPane = new JPanel();
69                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
70                 setContentPane(contentPane);
71                 contentPane.setLayout(null);
72
73                 JLabel lblNewLabel = new JLabel();
74                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
75                 lblNewLabel.setBounds(23, 41, 536, 33);
76                 contentPane.add(lblNewLabel);
77                 if (accounts.isEmpty())
78                         lblNewLabel.setText("There are not owners to be added");
79                 else
80                         lblNewLabel.setText("List of owners to be added:");
81                 JScrollPane scrollPane = new JScrollPane();
82                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
83                 scrollPane.setBounds(23, 113, 536, 271);
84                 contentPane.add(scrollPane);
85
86                 table = new JTable() {
87                 private static final long serialVersionUID = 1L;
88
89                 public boolean isCellEditable(int row, int column) {                
90                         return false;               
91                 };
92             };
93                 scrollPane.setViewportView(table);
94                 tableModel = new DefaultTableModel(null, new String[] {
95                                 "Name", "E-mail", "Bank Account" });
96                 
97                 //Maybe there is a better way to avoid interaction.
98                 //table.setEnabled(false);
99                 table.setModel(tableModel);
100                 
101                 JButton btnNewButton = new JButton("Confirm Addition");
102                 btnNewButton.addActionListener(new ActionListener() {
103                         public void actionPerformed(ActionEvent e) {
104                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
105                                         AccountInterface acm = null;
106                                         
107                                         try {
108                                                 acm = (AccountInterface) Naming
109                                                                 .lookup(___IntNames.AccountManager);
110                                         } catch (Exception e1) {
111                                                 System.out.println("Error accessing remote authentication: "
112                                                                 + e1.toString());
113                                         }
114
115                                         try {
116                                                 acm.addAccount(table.getSelectedRow());
117                                                 am.removeOwnerAdditionRequests(table.getSelectedRow());
118                                                 am.saveInstance();
119                                         } catch (RemoteException e1) {
120                                                 // TODO Auto-generated catch block
121                                                 e1.printStackTrace();
122                                         }
123                         
124                                         ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
125                                 }
126                         }
127                 });
128                 btnNewButton.setBounds(88, 396, 169, 25);
129                 contentPane.add(btnNewButton);
130                 
131                 JButton btnDenyAddition = new JButton("Deny Addition");
132                 btnDenyAddition.addActionListener(new ActionListener() {
133                         public void actionPerformed(ActionEvent arg0) {
134                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
135                                         try {
136                                                 am.removeOwnerAdditionRequests(table.getSelectedRow());
137                                                 am.saveInstance();
138                                         } catch (RemoteException e) {
139                                                 // TODO Auto-generated catch block
140                                                 e.printStackTrace();
141                                         }
142                                         
143                                         ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
144                                         accounts.remove(table.getSelectedRow());
145                                 }
146                         }
147                 });
148                 btnDenyAddition.setBounds(300, 396, 169, 25);
149                 contentPane.add(btnDenyAddition);
150                 Enumeration<Account> en = accounts.elements();
151                 Account acc;
152                 
153                 while (en.hasMoreElements()) {
154                         acc = en.nextElement();
155                         Vector<Object> row = new Vector<Object>();
156                         row.add(acc.getOwner().getName());
157                         row.add(acc.getOwner().getMailAccount());
158                         row.add(acc.getOwner().getBankAccount());                       
159                         tableModel.addRow(row);
160                 }
161
162         }
163 }