3db660dd1539794460ad5b70854b947cd690fb1d
[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.Date;
12 import java.util.Enumeration;
13 import java.util.Vector;
14
15 import javax.swing.JButton;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.JTable;
21 import javax.swing.border.EmptyBorder;
22 import javax.swing.table.DefaultTableCellRenderer;
23 import javax.swing.table.DefaultTableModel;
24
25 import common.BookingInterface;
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                 this.bookings = this.off.getBookings();
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 (bookings.isEmpty())
72                         lblNewLabel
73                                         .setText("There are not bookings to be confirmed or denied");
74                 else
75                         lblNewLabel.setText("List of bookings:");
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
81                 table = new JTable() {
82                         private static final long serialVersionUID = 1L;
83
84                         public boolean isCellEditable(int row, int column) {
85                                 return false;
86                         };
87                 };
88                 scrollPane.setViewportView(table);
89                 tableModel = new DefaultTableModel(null,
90                                 new String[] { "Booking Number", "Booking Date", "Name",
91                                                 "E-mail", "Telephone" });
92
93                 table.setModel(tableModel);
94
95                 JButton btnNewButton = new JButton("Confirm Booking");
96                 btnNewButton.addActionListener(new ActionListener() {
97                         public void actionPerformed(ActionEvent e) {
98                                 try {
99                                         bookM = (BookingInterface) Naming
100                                                         .lookup(___IntNames.BookingManager);
101                                 } catch (Exception e1) {
102                                         System.out
103                                                         .println("Error accessing remote authentication: "
104                                                                         + e1.toString());
105                                 }
106                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
107                                         if (table.getRowCount() != 0
108                                                         && table.getSelectedRow() != -1) {
109                                                 Booking book = bookings.get(table.getSelectedRow());
110                                                 try {
111                                                         bookM.acceptBooking(book);
112                                                 } catch (RemoteException e1) {
113                                                         e1.printStackTrace();
114                                                 }
115                                         }
116
117                                 }
118                         }
119                 });
120                 btnNewButton.setBounds(33, 396, 169, 25);
121                 contentPane.add(btnNewButton);
122
123                 JButton btnDenyAddition = new JButton("Deny Booking");
124                 btnDenyAddition.addActionListener(new ActionListener() {
125                         public void actionPerformed(ActionEvent arg0) {
126                                 try {
127                                         bookM = (BookingInterface) Naming
128                                                         .lookup(___IntNames.BookingManager);
129                                 } catch (Exception e1) {
130                                         System.out
131                                                         .println("Error accessing remote authentication: "
132                                                                         + e1.toString());
133                                 }
134                                 if (table.getRowCount() != 0 && table.getSelectedRow() != -1) {
135                                         Booking book = bookings.get(table.getSelectedRow());
136                                         try {
137                                                 bookM.denyBooking(book);
138                                         } catch (RemoteException e) {
139                                                 e.printStackTrace();
140                                         }
141                                         bookings.remove(book);
142
143                                 }
144                         }
145                 });
146                 btnDenyAddition.setBounds(390, 395, 169, 25);
147                 contentPane.add(btnDenyAddition);
148
149                 Enumeration<Booking> en = this.bookings.elements();
150                 Booking book;
151                 while (en.hasMoreElements()) {
152                         book = en.nextElement();
153                         Vector<Object> row = new Vector<Object>();
154                         row.add(book.getBookNumber());
155                         row.add(book.getBookDate());
156                         row.add(book.getClient().getName());
157                         row.add(book.getClient().getMailAccount());
158                         row.add(book.getClient().getTelephone());
159                         tableModel.addRow(row);
160                 }
161                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
162                         /**
163                          * 
164                          */
165                         private static final long serialVersionUID = 1L;
166
167                         public int daysBetween(Date d1, Date d2) {
168                                 return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
169                         }
170
171                         @Override
172                         public Component getTableCellRendererComponent(JTable table,
173                                         Object value, boolean isSelected, boolean hasFocus,
174                                         int row, int col) {
175
176                                 super.getTableCellRendererComponent(table, value, isSelected,
177                                                 hasFocus, row, col);
178
179                                 Date bookDay = (Date) table.getModel().getValueAt(row, 1);
180                                 Date currentDay = new java.util.Date(System.currentTimeMillis());
181
182                                 if (daysBetween(bookDay, currentDay) > 3) {
183                                         setBackground(Color.RED);
184                                         setForeground(Color.BLACK);
185                                 } else {
186                                         setBackground(Color.GREEN);
187                                         setForeground(Color.BLACK);
188                                 }
189
190                                 return this;
191                         }
192                 });
193         }
194
195 }