2106e88a429642262767ab639b3d7cfffb6409d5
[RRRRHHHH_Code] / ruralHouses client / src / gui / listOfBookingRequestsGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Font;
6 import java.awt.Rectangle;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.rmi.Naming;
10 import java.rmi.RemoteException;
11 import java.util.Enumeration;
12 import java.util.Vector;
13
14 import javax.swing.JButton;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTable;
20 import javax.swing.border.EmptyBorder;
21 import javax.swing.table.DefaultTableCellRenderer;
22 import javax.swing.table.DefaultTableModel;
23
24 import common.BookingInterface;
25 import common.HouseInterface;
26 import common.OfferInterface;
27 import configuration.___IntNames;
28 import domain.Booking;
29 import domain.Offer;
30
31 public class listOfBookingRequestsGUI extends JFrame {
32
33         /**
34          * 
35          */
36         private static final long serialVersionUID = 1L;
37         private JPanel contentPane;
38         private JTable table;
39         private Offer off;
40         private BookingInterface bookM = null;
41         private DefaultTableModel tableModel;
42         private Vector<Booking> bookings = new Vector<Booking>();
43
44         /**
45          * Create the frame.
46          */
47         public listOfBookingRequestsGUI(Offer of) {
48                 setTitle("Adding requests");
49                 this.off = of;
50
51                 try {
52
53                         init();
54                 } catch (Exception e) {
55                         e.printStackTrace();
56                 }
57         }
58
59         private void init() throws Exception {
60                 setBounds(100, 100, 600, 500);
61                 contentPane = new JPanel();
62                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
63                 setContentPane(contentPane);
64                 contentPane.setLayout(null);
65
66                 BookingInterface bm = null;
67                 
68                 try {
69                         bm = (BookingInterface) Naming
70                                         .lookup(___IntNames.BookingManager);
71
72                 } catch (Exception e1) {
73                         System.out.println("Error accessing remote authentication: "
74                                         + e1.toString());
75                 }
76                 try {
77                         this.bookings = bm.getOffersbookings(this.off);
78                 } catch (RemoteException e1) {
79                         e1.printStackTrace();
80                 }
81                 
82                 JLabel lblNewLabel = new JLabel();
83                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
84                 lblNewLabel.setBounds(23, 41, 536, 33);
85                 contentPane.add(lblNewLabel);
86                 JScrollPane scrollPane = new JScrollPane();
87                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
88                 scrollPane.setBounds(23, 113, 536, 271);
89                 contentPane.add(scrollPane);
90
91                 table = new JTable() {
92                         private static final long serialVersionUID = 1L;
93
94                         public boolean isCellEditable(int row, int column) {
95                                 return false;
96                         };
97                 };
98                 scrollPane.setViewportView(table);
99                 tableModel = new DefaultTableModel(null,
100                                 new String[] { "Booking Number", "Booking Date", "Name",
101                                                 "E-mail", "Telephone" });
102
103                 table.setModel(tableModel);
104
105                 JButton btnNewButton = new JButton("Confirm Booking");
106                 btnNewButton.addActionListener(new ActionListener() {
107                         public void actionPerformed(ActionEvent e) {
108                                 try {
109                                         bookM = (BookingInterface) Naming
110                                                         .lookup(___IntNames.BookingManager);
111                                 } catch (Exception e1) {
112                                         System.out
113                                                         .println("Error accessing remote authentication: "
114                                                                         + e1.toString());
115                                 }
116                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
117                                         if (table.getRowCount() != 0
118                                                         && table.getSelectedRow() != -1) {
119                                                 Booking book = bookings.get(table.getSelectedRow());
120                                                 try {
121                                                         bookM.acceptBooking(book);
122                                                 } catch (RemoteException e1) {
123                                                         e1.printStackTrace();
124                                                 }
125                                                 contentPane.setVisible(false);
126
127                                         }
128
129                                 }
130                         }
131                 });
132                 btnNewButton.setBounds(33, 396, 169, 25);
133                 contentPane.add(btnNewButton);
134
135                 JButton btnDenyAddition = new JButton("Deny Booking");
136                 btnDenyAddition.addActionListener(new ActionListener() {
137                         public void actionPerformed(ActionEvent arg0) {
138                                 try {
139                                         bookM = (BookingInterface) Naming
140                                                         .lookup(___IntNames.BookingManager);
141                                 } catch (Exception e1) {
142                                         System.out
143                                                         .println("Error accessing remote authentication: "
144                                                                         + e1.toString());
145                                 }
146                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
147                                         Booking book = bookings.get(table.getSelectedRow());
148                                         try {
149                                                 bookM.denyBooking(book);
150                                         } catch (RemoteException e) {
151                                                 e.printStackTrace();
152                                         }
153                                         ((DefaultTableModel) table.getModel()).removeRow(table
154                                                         .getSelectedRow());
155                                         bookings.remove(book);
156
157                                 }
158                         }
159                 });
160                 if (bookings.isEmpty())
161                         lblNewLabel
162                                         .setText("There are not bookings to be confirmed or denied");
163                 else {
164                         lblNewLabel.setText("List of bookings:");
165                         if (this.bookings.get(0).getOffer().isBooked()) {
166                                 btnDenyAddition.setEnabled(false);
167                                 btnNewButton.setEnabled(false);
168                         }
169                 }
170                 
171                 btnDenyAddition.setBounds(390, 395, 169, 25);
172                 contentPane.add(btnDenyAddition);
173
174                 Enumeration<Booking> en = this.bookings.elements();
175                 Booking book;
176                 while (en.hasMoreElements()) {
177                         book = en.nextElement();
178                         Vector<Object> row = new Vector<Object>();
179                         row.add(book.getBookNumber());
180                         row.add(book.getBookDate());
181                         row.add(book.getClient().getName());
182                         row.add(book.getClient().getMailAccount());
183                         row.add(book.getClient().getTelephone());
184                         tableModel.addRow(row);
185                 }
186                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
187                         /**
188                          * 
189                          */
190                         private static final long serialVersionUID = 1L;
191
192                         @Override
193                         public Component getTableCellRendererComponent(JTable table,
194                                         Object value, boolean isSelected, boolean hasFocus,
195                                         int row, int col) {
196
197                                 super.getTableCellRendererComponent(table, value, isSelected,
198                                                 hasFocus, row, col);
199
200                                 if (!bookings.get(row).getOffer().isBooked()) {
201                                         setBackground(Color.RED);
202                                         setForeground(Color.BLACK);
203                                 } else {
204                                         setBackground(Color.GREEN);
205                                         setForeground(Color.BLACK);
206                                 }
207
208                                 return this;
209                         }
210                 });
211         }
212
213 }