imports leaned
[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
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.JTable;
19 import javax.swing.JTextField;
20 import javax.swing.SwingConstants;
21 import javax.swing.border.EmptyBorder;
22 import javax.swing.table.DefaultTableCellRenderer;
23 import javax.swing.table.DefaultTableModel;
24
25 import businessLogic.BookingManager;
26 import domain.Booking;
27 import domain.Offer;
28 import domain.RuralHouse;
29
30 public class HouseFeaturesGUI extends JFrame {
31
32         /**
33          * 
34          */
35         private static final long serialVersionUID = 1L;
36         private JPanel contentPane;
37         private JLabel lblDistrict;
38         private JTextField District_f;
39         private JLabel lblDescription;
40         private JTextField description_f;
41         private JLabel lblKitchen;
42         private JTextField kitchens_f;
43         private JLabel lblRooms;
44         private JTextField rooms_f;
45         private JLabel lblLivings;
46         private JTextField lRooms_f;
47         private JLabel lblParkings;
48         private JTextField parkings_f;
49         private JLabel lblBaths;
50         private JTextField baths_f;
51         private JTable table;
52         private DefaultTableModel tableModel;
53         private DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
54         private RuralHouse rh;
55         private JTextField telIn;
56         private int row;
57
58         /**
59          * Create the frame.
60          */
61
62         public HouseFeaturesGUI(RuralHouse RH, final Date FirstDay, 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(152, 277, 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                 table.addMouseListener( new MouseAdapter(){                     
200                         @Override
201                         public void mouseClicked(MouseEvent arg0) {
202                                 row = table.getSelectedRow();
203                         }
204                 });
205                 Enumeration<Offer> rhs = rh.getAllOffers().elements();
206                 while (rhs.hasMoreElements()) {
207                         Offer of = rhs.nextElement();
208                         Vector<Object> row = new Vector<Object>();
209                         row.add(of.getOfferNumber());
210                         row.add(of.getFirstDay());
211                         row.add(of.getLastDay());
212                         row.add(of.getPrice());
213                         tableModel.addRow(row);
214
215                 }
216                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
217                         @Override
218                         public Component getTableCellRendererComponent(JTable table,
219                                         Object value, boolean isSelected, boolean hasFocus,
220                                         int row, int col) {
221
222                                 super.getTableCellRendererComponent(table, value, isSelected,
223                                                 hasFocus, row, col);
224
225                                 Date firstDay = (Date) table.getModel().getValueAt(row, 1);
226                                 Date lastDay = (Date) table.getModel().getValueAt(row, 2);
227                                 if (FirstDay != null && LastDay != null) {
228                                         if (LastDay.before(lastDay)  ||  FirstDay.after(firstDay)) {
229                                                 setBackground(Color.RED);
230                                                 setForeground(Color.BLACK);
231                                         } else {
232                                                 setBackground(Color.GREEN);
233                                                 setForeground(Color.BLACK);
234                                         }
235                                 }
236                                 return this;
237                         }
238                 });
239         }
240
241         private void jButton_ActionPerformed(ActionEvent arg0) {
242                 BookingManager bookingM = new BookingManager();
243                 //RegExp to see if telephone number is correct??TODO
244                 Booking book = null;
245                 try {
246                         if (table.getRowCount()!=0)
247                                 book = bookingM.createBooking(rh, rh.offers.get(row).getFirstDay(), rh.offers.get(row).getLastDay(), telIn.getText());
248                 } catch (Exception e) {
249                         // TODO Auto-generated catch block
250                         e.printStackTrace();
251                 } 
252                 if (book != null) {
253                         BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
254                                         book);
255                         confirmWindow.setVisible(true);
256                 }
257         }
258 }