f72d1cc024e6a6afe6a22144a2c9bba40fe3c855
[RRRRHHHH_Code] / ruralHouses / src / gui / ownerAddittionRequests.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.AccountManager;
20 import businessLogic.AdminManager;
21 import domain.Account;
22 import domain.Administrator;
23
24 public class ownerAddittionRequests extends JFrame {
25
26         /**
27          * 
28          */
29         private static final long serialVersionUID = 1L;
30         private JPanel contentPane;
31         private JTable table;
32         private DefaultTableModel tableModel;
33         private AdminManager am = new AdminManager();
34         private Vector<Account> accounts;
35         /**
36          * Create the frame.
37          */
38         public  ownerAddittionRequests()  {
39                 setTitle("Adding requests");
40                 try {
41                         this.accounts= am.getOwnerAdditionReuests();
42                         init();
43                 } catch (Exception e) {
44                         e.printStackTrace();
45                 }
46         }
47
48         private void init() throws Exception {
49                 setBounds(100, 100, 600, 450);
50                 contentPane = new JPanel();
51                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
52                 setContentPane(contentPane);
53                 contentPane.setLayout(null);
54
55                 JLabel lblNewLabel = new JLabel();
56                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
57                 lblNewLabel.setBounds(23, 41, 536, 33);
58                 contentPane.add(lblNewLabel);
59                 if (accounts.isEmpty())
60                         lblNewLabel.setText("There are not owners to be added");
61                 else
62                         lblNewLabel.setText("List of owners to be added:");
63                 JScrollPane scrollPane = new JScrollPane();
64                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
65                 scrollPane.setBounds(23, 113, 536, 271);
66                 contentPane.add(scrollPane);
67
68                 table = new JTable() {
69                 private static final long serialVersionUID = 1L;
70
71                 public boolean isCellEditable(int row, int column) {                
72                         return false;               
73                 };
74             };
75                 scrollPane.setViewportView(table);
76                 tableModel = new DefaultTableModel(null, new String[] {
77                                 "Name", "E-mail", "Bank Account" });
78                 
79                 //Maybe there is a better way to avoid interaction.
80                 //table.setEnabled(false);
81                 table.setModel(tableModel);
82                 
83                 JButton btnNewButton = new JButton("Confirm Addition");
84                 btnNewButton.addActionListener(new ActionListener() {
85                         public void actionPerformed(ActionEvent e) {
86                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
87                                         AccountManager ama = new AccountManager();
88                                         Account accou = accounts.get(table.getSelectedRow());
89                                         //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that.
90                                         ama.addAccount(accou);
91                                         am.removeOwnerAdditionRequests(accou);;
92                                         Administrator.saveInstance();
93                                         ((DefaultTableModel)table.getModel()).removeRow(accounts.indexOf(accou));
94                                         accounts.remove(accou);
95                                 }
96                         }
97                 });
98                 btnNewButton.setBounds(88, 396, 169, 25);
99                 contentPane.add(btnNewButton);
100                 
101                 JButton btnDenyAddition = new JButton("Deny Addition");
102                 btnDenyAddition.addActionListener(new ActionListener() {
103                         public void actionPerformed(ActionEvent arg0) {
104                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
105                                         Account acc = accounts.get(table.getSelectedRow());
106                                         am.removeOwnerAdditionRequests(acc);
107                                         Administrator.saveInstance();
108                                         ((DefaultTableModel)table.getModel()).removeRow(accounts.indexOf(acc));
109                                         accounts.remove(acc);
110                                 }
111                         }
112                 });
113                 btnDenyAddition.setBounds(300, 396, 169, 25);
114                 contentPane.add(btnDenyAddition);
115                 Enumeration<Account> en = accounts.elements();
116                 Account acc;
117                 
118                 while (en.hasMoreElements()) {
119                         acc = en.nextElement();
120                         Vector<Object> row = new Vector<Object>();
121                         row.add(acc.getOwner().getName());
122                         row.add(acc.getOwner().getMailAccount());
123                         row.add(acc.getOwner().getBankAccount());                       
124                         tableModel.addRow(row);
125                 }
126
127         }
128 }