982e022f3fc01b9dff8686f0a7084146d9290882
[RRRRHHHH_Code] / ruralHouses / src / gui / DeleteOwnerGUI.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.Owner;
26
27 public class DeleteOwnerGUI extends JFrame {
28
29         /**
30          * 
31          */
32         private static final long serialVersionUID = 1L;
33         private JPanel contentPane;
34         private JTable table;
35
36         private DefaultTableModel tableModel;
37         private AdminInterface AdM = null;
38         private Vector<Owner> owners = new Vector<Owner>();
39
40         /**
41          * Create the frame.
42          */
43         public DeleteOwnerGUI() {
44                 setTitle("Current owners:");
45                 try {
46                         init();
47                 } catch (Exception e) {
48                         e.printStackTrace();
49                 }
50         }
51
52         private void init() throws Exception {
53                 setBounds(100, 100, 600, 500);
54                 contentPane = new JPanel();
55                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
56                 setContentPane(contentPane);
57                 contentPane.setLayout(null);
58                 try {
59                         AdM = (AdminInterface) Naming
60                                         .lookup(___IntNames.AdminManager);
61                 } catch (Exception e1) {
62                         System.out
63                                         .println("Error accessing remote authentication: "
64                                                         + e1.toString());
65                 }
66                 this.owners = AdM.getAllOwners();
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 (this.owners.isEmpty())
72                         lblNewLabel
73                                         .setText("There are not owners in the system");
74                 else
75                         lblNewLabel.setText("List of owners:");
76                 JScrollPane scrollPane = new JScrollPane();
77                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
78                 scrollPane.setBounds(23, 113, 536, 271);
79                 contentPane.add(scrollPane);
80                 final JLabel feedback = new JLabel("");
81                 feedback.setBounds(134, 447, 307, 14);
82                 contentPane.add(feedback);
83                 JButton btnNewButton = new JButton("Delete from the system");
84                 btnNewButton.addActionListener(new ActionListener() {
85                         public void actionPerformed(ActionEvent e) {
86                                 if (table.getRowCount()!=0 && table.getSelectedRow() != -1) {
87                                         AccountInterface acm = null;
88                                         
89                                         try {
90                                                 acm = (AccountInterface) Naming
91                                                                 .lookup(___IntNames.AccountManager);
92                                         } catch (Exception e1) {
93                                                 System.out.println("Error accessing remote authentication: "
94                                                                 + e1.toString());
95                                         }
96
97                                         try {
98                                                 if(acm.removeAccount(table.getSelectedRow()))
99                                                         {
100                                                         feedback.setText("Deleted from the system");
101                                                         }
102                                         } catch (RemoteException e1) {
103                                                 e1.printStackTrace();
104                                         }
105                                         
106                                         ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
107                                 }
108                         }
109                 });
110                 btnNewButton.setBounds(88, 396, 428, 40);
111                 contentPane.add(btnNewButton);
112                 table = new JTable() {
113                         private static final long serialVersionUID = 1L;
114
115                         public boolean isCellEditable(int row, int column) {
116                                 return false;
117                         };
118                 };
119                 
120                 scrollPane.setViewportView(table);      
121                 tableModel = new DefaultTableModel(null, new String[] {
122                                 "Name", "E-mail", "Bank Account" });
123
124                 table.setModel(tableModel);
125                 
126                 
127                 Enumeration<Owner> rhs = this.owners.elements();
128                 while (rhs.hasMoreElements()) {
129                         Owner own = rhs.nextElement();
130                         Vector<Object> row = new Vector<Object>();
131                         row.add(own.getName());
132                         row.add(own.getMailAccount());
133                         row.add(own.getBankAccount());
134                         tableModel.addRow(row);
135                 }
136                 
137                 
138         }
139 }