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