39d0bc9f13f940873798b428ef1910f120a8cbdc
[RRRRHHHH_Code] / ruralHouses / src / gui / HouseFeaturesGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.MouseAdapter;
8 import java.awt.event.MouseEvent;
9 import java.rmi.Naming;
10 import java.util.Date;
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.JTextField;
21 import javax.swing.SwingConstants;
22 import javax.swing.border.EmptyBorder;
23 import javax.swing.table.DefaultTableCellRenderer;
24 import javax.swing.table.DefaultTableModel;
25
26 import common.BookingInterface;
27
28 import configuration.___IntNames;
29 import domain.Booking;
30 import domain.Client;
31 import domain.Offer;
32 import domain.RuralHouse;
33
34 public class HouseFeaturesGUI extends JFrame {
35
36         /**
37          * 
38          */
39         private static final long serialVersionUID = 1L;
40         private JPanel contentPane;
41         private JLabel lblDistrict;
42         private JTextField District_f;
43         private JLabel lblDescription;
44         private JTextField description_f;
45         private JLabel lblKitchen;
46         private JTextField kitchens_f;
47         private JLabel lblRooms;
48         private JTextField rooms_f;
49         private JLabel lblLivings;
50         private JTextField lRooms_f;
51         private JLabel lblParkings;
52         private JTextField parkings_f;
53         private JLabel lblBaths;
54         private JTextField baths_f;
55         private JTable table;
56         private DefaultTableModel tableModel;
57         private RuralHouse rh;
58         private Vector<Offer> offers= new Vector<Offer>();
59         private JTextField telIn;
60         private int row;
61         private JLabel labelPhone;
62         private JLabel lblName;
63         private JTextField nameField;
64         private JLabel lblEmail;
65         private JTextField mailField;
66
67         /**
68          * Create the frame.
69          */
70
71         public HouseFeaturesGUI(RuralHouse RH, final Date FirstDay,
72                         final Date LastDay) {
73                 this.rh = RH;
74                 this.getContentPane().setLayout(null);
75                 setBounds(100, 100, 500, 700);
76                 contentPane = new JPanel();
77                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
78                 setContentPane(contentPane);
79
80                 lblDistrict = new JLabel("District:");
81                 lblDistrict.setBounds(23, 67, 70, 14);
82                 lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
83
84                 District_f = new JTextField();
85                 District_f.setEditable(false);
86                 District_f.setBounds(103, 64, 86, 20);
87                 District_f.setColumns(10);
88
89                 lblDescription = new JLabel("Description:");
90                 lblDescription.setBounds(215, 67, 90, 14);
91                 lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
92
93                 description_f = new JTextField();
94                 description_f.setEditable(false);
95                 description_f.setBounds(238, 99, 178, 129);
96                 description_f.setColumns(10);
97
98                 lblKitchen = new JLabel("Kitchens:");
99                 lblKitchen.setBounds(23, 211, 70, 14);
100                 lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
101
102                 kitchens_f = new JTextField();
103                 kitchens_f.setEditable(false);
104                 kitchens_f.setBounds(103, 202, 86, 20);
105                 kitchens_f.setColumns(10);
106
107                 lblRooms = new JLabel("Rooms:");
108                 lblRooms.setBounds(23, 102, 70, 14);
109                 lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
110
111                 rooms_f = new JTextField();
112                 rooms_f.setEditable(false);
113                 rooms_f.setBounds(103, 99, 86, 20);
114                 rooms_f.setColumns(10);
115
116                 lblLivings = new JLabel("Living rooms:");
117                 lblLivings.setBounds(30, 236, 63, 14);
118                 lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
119
120                 lRooms_f = new JTextField();
121                 lRooms_f.setEditable(false);
122                 lRooms_f.setBounds(103, 233, 86, 20);
123                 lRooms_f.setColumns(10);
124
125                 lblParkings = new JLabel("Parkings:");
126                 lblParkings.setBounds(23, 174, 70, 14);
127                 lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
128
129                 parkings_f = new JTextField();
130                 parkings_f.setEditable(false);
131                 parkings_f.setBounds(103, 171, 86, 20);
132                 parkings_f.setColumns(10);
133
134                 lblBaths = new JLabel("Baths:");
135                 lblBaths.setBounds(23, 143, 70, 14);
136                 lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
137
138                 baths_f = new JTextField();
139                 baths_f.setEditable(false);
140                 baths_f.setBounds(103, 140, 86, 20);
141                 baths_f.setColumns(10);
142                 contentPane.setLayout(null);
143                 contentPane.add(lblParkings);
144                 contentPane.add(parkings_f);
145                 contentPane.add(lblRooms);
146                 contentPane.add(rooms_f);
147                 contentPane.add(lblBaths);
148                 contentPane.add(baths_f);
149                 contentPane.add(lblDistrict);
150                 contentPane.add(District_f);
151                 contentPane.add(lblLivings);
152                 contentPane.add(lRooms_f);
153                 contentPane.add(lblKitchen);
154                 contentPane.add(kitchens_f);
155                 contentPane.add(description_f);
156                 contentPane.add(lblDescription);
157                 parkings_f.setText(Integer.toString(rh.getFeatures().getnParkings()));
158                 rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
159                 baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
160                 District_f.setText(rh.getDistrict());
161                 kitchens_f.setText(Integer.toString(rh.getFeatures().getnKitchens()));
162                 description_f.setText(rh.getDescription());
163                 lRooms_f.setText(Integer.toString(rh.getFeatures().getnLivings()));
164                 JLabel lblAvailbleOffers = new JLabel("Availble Offers:");
165                 lblAvailbleOffers.setBounds(30, 286, 86, 14);
166                 contentPane.add(lblAvailbleOffers);
167                 JScrollPane scrollPane = new JScrollPane();
168                 scrollPane.setBounds(40, 311, 376, 183);
169                 contentPane.add(scrollPane);
170
171                 table = new JTable() {
172                         private static final long serialVersionUID = 1L;
173
174                         public boolean isCellEditable(int row, int column) {
175                                 return false;
176                         };
177                 };
178                 tableModel = new DefaultTableModel(null, new String[] { "Offer #",
179                                 "FirstDay", "LastDay", "Price" });
180                 table.setModel(tableModel);
181                 scrollPane.setViewportView(table);
182
183                 JButton btnBookSelected = new JButton("Book SelectedOffer");
184                 btnBookSelected.setBounds(238, 614, 178, 23);
185                 contentPane.add(btnBookSelected);
186
187                 btnBookSelected.addActionListener(new ActionListener() {
188                         public void actionPerformed(ActionEvent arg0) {
189
190                                 jButton_ActionPerformed(arg0);
191                         }
192
193                 });
194
195                 JLabel lblNewLabel = new JLabel(
196                                 "Green: Suit your dates. Red: Do not suit your dates");
197                 lblNewLabel.setEnabled(false);
198                 lblNewLabel.setBounds(170, 282, 261, 23);
199                 contentPane.add(lblNewLabel);
200
201                 JLabel lblNewLabel_1 = new JLabel("Telephone num:");
202                 lblNewLabel_1.setBounds(10, 618, 83, 14);
203                 contentPane.add(lblNewLabel_1);
204
205                 telIn = new JTextField();
206                 telIn.setBounds(103, 615, 129, 20);
207                 contentPane.add(telIn);
208                 telIn.setColumns(10);
209
210                 labelPhone = new JLabel("");
211                 labelPhone.setBounds(238, 252, 178, 14);
212                 contentPane.add(labelPhone);
213
214                 lblName = new JLabel("Name:");
215                 lblName.setBounds(10, 516, 46, 14);
216                 contentPane.add(lblName);
217
218                 nameField = new JTextField();
219                 nameField.setBounds(103, 513, 178, 20);
220                 contentPane.add(nameField);
221                 nameField.setColumns(10);
222
223                 lblEmail = new JLabel("E-mail:");
224                 lblEmail.setBounds(10, 565, 46, 14);
225                 contentPane.add(lblEmail);
226
227                 mailField = new JTextField();
228                 mailField.setBounds(103, 562, 178, 20);
229                 contentPane.add(mailField);
230                 mailField.setColumns(10);
231
232                 table.addMouseListener(new MouseAdapter() {
233                         @Override
234                         public void mouseClicked(MouseEvent arg0) {
235                                 row = table.getSelectedRow();
236                         }
237                 });
238                 
239                 Enumeration<Offer> rhs = rh.getAllOffers().elements();
240                 while (rhs.hasMoreElements()) {
241                         Offer of = rhs.nextElement();
242                         if (of.getBookings() == null
243                                         || !of.isBooked()) {
244                                 offers.add(of);
245                                 Vector<Object> row = new Vector<Object>();
246                                 row.add(of.getOfferNumber());
247                                 row.add(of.getFirstDay());
248                                 row.add(of.getLastDay());
249                                 row.add(of.getPrice());
250                                 tableModel.addRow(row);
251                         }
252                 }
253                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
254                         /**
255                          * 
256                          */
257                         private static final long serialVersionUID = 1L;
258
259                         @Override
260                         public Component getTableCellRendererComponent(JTable table,
261                                         Object value, boolean isSelected, boolean hasFocus,
262                                         int row, int col) {
263
264                                 super.getTableCellRendererComponent(table, value, isSelected,
265                                                 hasFocus, row, col);
266
267                                 Date firstDay = (Date) table.getModel().getValueAt(row, 1);
268                                 Date lastDay = (Date) table.getModel().getValueAt(row, 2);
269                                 if (FirstDay != null && LastDay != null) {
270                                         if (LastDay.before(lastDay) || FirstDay.after(firstDay)) {
271                                                 setBackground(Color.RED);
272                                                 setForeground(Color.BLACK);
273                                         } else {
274                                                 setBackground(Color.GREEN);
275                                                 setForeground(Color.BLACK);
276                                         }
277                                 }
278                                 return this;
279                         }
280                 });
281         }
282
283         private void jButton_ActionPerformed(ActionEvent arg0) {
284
285                 BookingInterface bookingM = null;
286                 try {
287                         bookingM = (BookingInterface) Naming
288                                         .lookup(___IntNames.BookingManager);
289                 } catch (Exception e1) {
290                         System.out.println("Error accessing remote authentication: "
291                                         + e1.toString());
292                 }
293                 
294                 if (telIn.getText().matches("[976]\\d{2}[.\\- ]?\\d{3}[.\\- ]?\\d{3}")
295                                 && mailField
296                                 .getText()
297                                 .matches(
298                                                 "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")
299                 && !nameField.getText().isEmpty()) {
300                         labelPhone.setText("");
301                         Vector<Booking> book = null;
302                         try {
303                                 if (table.getRowCount() != 0) {
304                                         Client cl = new Client(nameField.getText(),
305                                                         mailField.getText(), telIn.getText());
306                                         book = bookingM.createBooking(rh, offers.get(row)
307                                                         .getFirstDay(), offers.get(row).getLastDay(),cl);
308                                 }
309                         } catch (Exception e) {
310                                 e.printStackTrace();
311                         }
312                         if (book != null) {
313                                 BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
314                                                 book.lastElement());
315                                 confirmWindow.setVisible(true);
316                         }
317                 } else {
318                         labelPhone.setText("Bad formatted data.");
319
320                 }
321         }
322 }