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