Possibility of registering new owners added
[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.util.Date;
10 import java.util.Enumeration;
11 import java.util.Vector;
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.JTextField;
19 import javax.swing.SwingConstants;
20 import javax.swing.border.EmptyBorder;
21 import javax.swing.table.DefaultTableCellRenderer;
22 import javax.swing.table.DefaultTableModel;
23
24 import businessLogic.BookingManager;
25 import domain.Booking;
26 import domain.Offer;
27 import domain.RuralHouse;
28
29 public class HouseFeaturesGUI extends JFrame {
30
31         /**
32          * 
33          */
34         private static final long serialVersionUID = 1L;
35         private JPanel contentPane;
36         private JLabel lblDistrict;
37         private JTextField District_f;
38         private JLabel lblDescription;
39         private JTextField description_f;
40         private JLabel lblKitchen;
41         private JTextField kitchens_f;
42         private JLabel lblRooms;
43         private JTextField rooms_f;
44         private JLabel lblLivings;
45         private JTextField lRooms_f;
46         private JLabel lblParkings;
47         private JTextField parkings_f;
48         private JLabel lblBaths;
49         private JTextField baths_f;
50         private JTable table;
51         private DefaultTableModel tableModel;
52         private RuralHouse rh;
53         private JTextField telIn;
54         private int row;
55         private JLabel labelPhone;
56
57         /**
58          * Create the frame.
59          */
60
61         public HouseFeaturesGUI(RuralHouse RH, final Date FirstDay,
62                         final Date LastDay) {
63                 this.rh = RH;
64                 this.getContentPane().setLayout(null);
65                 setBounds(100, 100, 500, 583);
66                 contentPane = new JPanel();
67                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
68                 setContentPane(contentPane);
69
70                 lblDistrict = new JLabel("District:");
71                 lblDistrict.setBounds(23, 67, 70, 14);
72                 lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
73
74                 District_f = new JTextField();
75                 District_f.setEditable(false);
76                 District_f.setBounds(103, 64, 86, 20);
77                 District_f.setColumns(10);
78
79                 lblDescription = new JLabel("Description:");
80                 lblDescription.setBounds(215, 67, 90, 14);
81                 lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
82
83                 description_f = new JTextField();
84                 description_f.setEditable(false);
85                 description_f.setBounds(238, 99, 178, 129);
86                 description_f.setColumns(10);
87
88                 lblKitchen = new JLabel("Kitchens:");
89                 lblKitchen.setBounds(23, 211, 70, 14);
90                 lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
91
92                 kitchens_f = new JTextField();
93                 kitchens_f.setEditable(false);
94                 kitchens_f.setBounds(103, 202, 86, 20);
95                 kitchens_f.setColumns(10);
96
97                 lblRooms = new JLabel("Rooms:");
98                 lblRooms.setBounds(23, 102, 70, 14);
99                 lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
100
101                 rooms_f = new JTextField();
102                 rooms_f.setEditable(false);
103                 rooms_f.setBounds(103, 99, 86, 20);
104                 rooms_f.setColumns(10);
105
106                 lblLivings = new JLabel("Living rooms:");
107                 lblLivings.setBounds(30, 236, 63, 14);
108                 lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
109
110                 lRooms_f = new JTextField();
111                 lRooms_f.setEditable(false);
112                 lRooms_f.setBounds(103, 233, 86, 20);
113                 lRooms_f.setColumns(10);
114
115                 lblParkings = new JLabel("Parkings:");
116                 lblParkings.setBounds(23, 174, 70, 14);
117                 lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
118
119                 parkings_f = new JTextField();
120                 parkings_f.setEditable(false);
121                 parkings_f.setBounds(103, 171, 86, 20);
122                 parkings_f.setColumns(10);
123
124                 lblBaths = new JLabel("Baths:");
125                 lblBaths.setBounds(23, 143, 70, 14);
126                 lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
127
128                 baths_f = new JTextField();
129                 baths_f.setEditable(false);
130                 baths_f.setBounds(103, 140, 86, 20);
131                 baths_f.setColumns(10);
132                 contentPane.setLayout(null);
133                 contentPane.add(lblParkings);
134                 contentPane.add(parkings_f);
135                 contentPane.add(lblRooms);
136                 contentPane.add(rooms_f);
137                 contentPane.add(lblBaths);
138                 contentPane.add(baths_f);
139                 contentPane.add(lblDistrict);
140                 contentPane.add(District_f);
141                 contentPane.add(lblLivings);
142                 contentPane.add(lRooms_f);
143                 contentPane.add(lblKitchen);
144                 contentPane.add(kitchens_f);
145                 contentPane.add(description_f);
146                 contentPane.add(lblDescription);
147                 parkings_f.setText(Integer.toString(rh.getFeatures().getnParkings()));
148                 rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
149                 baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
150                 District_f.setText(rh.getDistrict());
151                 kitchens_f.setText(Integer.toString(rh.getFeatures().getnKitchens()));
152                 description_f.setText(rh.getDescription());
153                 lRooms_f.setText(Integer.toString(rh.getFeatures().getnLivings()));
154                 JLabel lblAvailbleOffers = new JLabel("Availble Offers:");
155                 lblAvailbleOffers.setBounds(30, 286, 86, 14);
156                 contentPane.add(lblAvailbleOffers);
157                 JScrollPane scrollPane = new JScrollPane();
158                 scrollPane.setBounds(40, 311, 376, 183);
159                 contentPane.add(scrollPane);
160
161                 table = new JTable() {
162                         private static final long serialVersionUID = 1L;
163
164                         public boolean isCellEditable(int row, int column) {
165                                 return false;
166                         };
167                 };
168                 tableModel = new DefaultTableModel(null, new String[] { "Offer #",
169                                 "FirstDay", "LastDay", "Price" });
170                 table.setModel(tableModel);
171                 scrollPane.setViewportView(table);
172
173                 JButton btnBookSelected = new JButton("Book SelectedOffer");
174                 btnBookSelected.setBounds(238, 505, 178, 23);
175                 contentPane.add(btnBookSelected);
176
177                 btnBookSelected.addActionListener(new ActionListener() {
178                         public void actionPerformed(ActionEvent arg0) {
179
180                                 jButton_ActionPerformed(arg0);
181                         }
182
183                 });
184
185                 JLabel lblNewLabel = new JLabel(
186                                 "Green: Suit your dates. Red: Do not suit your dates");
187                 lblNewLabel.setEnabled(false);
188                 lblNewLabel.setBounds(170, 282, 261, 23);
189                 contentPane.add(lblNewLabel);
190
191                 JLabel lblNewLabel_1 = new JLabel("Telephone num:");
192                 lblNewLabel_1.setBounds(10, 509, 83, 14);
193                 contentPane.add(lblNewLabel_1);
194
195                 telIn = new JTextField();
196                 telIn.setBounds(99, 505, 129, 20);
197                 contentPane.add(telIn);
198                 telIn.setColumns(10);
199
200                 labelPhone = new JLabel("");
201                 labelPhone.setBounds(238, 252, 178, 14);
202                 contentPane.add(labelPhone);
203
204                 table.addMouseListener(new MouseAdapter() {
205                         @Override
206                         public void mouseClicked(MouseEvent arg0) {
207                                 row = table.getSelectedRow();
208                         }
209                 });
210                 Enumeration<Offer> rhs = rh.getAllOffers().elements();
211                 while (rhs.hasMoreElements()) {
212                         Offer of = rhs.nextElement();
213                         if (of.getBookings().size()==1&&of.getBookings().get(0).isAccepted()) {
214                                 Vector<Object> row = new Vector<Object>();
215                                 row.add(of.getOfferNumber());
216                                 row.add(of.getFirstDay());
217                                 row.add(of.getLastDay());
218                                 row.add(of.getPrice());
219                                 tableModel.addRow(row);
220                         }
221                 }
222                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
223                         /**
224                          * 
225                          */
226                         private static final long serialVersionUID = 1L;
227
228                         @Override
229                         public Component getTableCellRendererComponent(JTable table,
230                                         Object value, boolean isSelected, boolean hasFocus,
231                                         int row, int col) {
232
233                                 super.getTableCellRendererComponent(table, value, isSelected,
234                                                 hasFocus, row, col);
235
236                                 Date firstDay = (Date) table.getModel().getValueAt(row, 1);
237                                 Date lastDay = (Date) table.getModel().getValueAt(row, 2);
238                                 if (FirstDay != null && LastDay != null) {
239                                         if (LastDay.before(lastDay) || FirstDay.after(firstDay)) {
240                                                 setBackground(Color.RED);
241                                                 setForeground(Color.BLACK);
242                                         } else {
243                                                 setBackground(Color.GREEN);
244                                                 setForeground(Color.BLACK);
245                                         }
246                                 }
247                                 return this;
248                         }
249                 });
250         }
251
252         private void jButton_ActionPerformed(ActionEvent arg0) {
253                 BookingManager bookingM = new BookingManager();
254                 // RegExp to see if telephone number is correct??TODO
255                 if (telIn.getText().matches("[976]\\d{2}[.\\- ]?\\d{3}[.\\- ]?\\d{3}")) {
256                         labelPhone.setText("");
257                         Booking book = null;
258                         try {
259                                 if (table.getRowCount() != 0)
260                                         book = bookingM.createBooking(rh, rh.offers.get(row)
261                                                         .getFirstDay(), rh.offers.get(row).getLastDay(),
262                                                         telIn.getText());
263                         } catch (Exception e) {
264                                 e.printStackTrace();
265                         }
266                         if (book != null) {
267                                 BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
268                                                 book);
269                                 confirmWindow.setVisible(true);
270                         }
271                 } else {
272                         labelPhone.setText("Phone format is wrong");
273
274                 }
275         }
276 }