3 import businessLogic.BookingManager;
4 import businessLogic.HouseManager;
6 import com.toedter.calendar.*;
9 import domain.RuralHouse;
10 import exceptions.OfferCanNotBeBooked;
20 import java.awt.event.*;
24 public class BookRuralHouseGUI extends JFrame {
25 private static final long serialVersionUID = 1L;
26 public static final long MILLSECS_PER_DAY= 24 * 60 * 60 * 1000;
28 private JLabel jLabel1 = new JLabel();
29 private JComboBox jComboBox1;
30 Vector<RuralHouse> ruralHouses;
31 private JLabel jLabel2 = new JLabel();
32 private JLabel jLabel3 = new JLabel();
33 private JLabel jLabel4 = new JLabel();
34 private JTextField jTextField2 = new JTextField();
35 private JTextField jTextField3 = new JTextField();
36 private JTextField jTextField4 = new JTextField();
37 private JButton jButton2 = new JButton();
38 private JButton jButton3 = new JButton();
41 private JCalendar jCalendar1 = new JCalendar();
42 private Calendar calendarMio = null;
43 private JLabel jLabel5 = new JLabel();
47 public BookRuralHouseGUI()
60 public BookRuralHouseGUI(String houseName,Date firstDay,Date lastDay)
67 // Put the "houseNumber", "firstDay" and "lastDay" in the graphical components of the user interface
68 for (int i=0; i<ruralHouses.size();i++){
69 if (((RuralHouse)ruralHouses.get(i)).getHouseName()==houseName) {
70 jComboBox1.setSelectedIndex(i);
75 Calendar c=new GregorianCalendar();
77 //jCalendar1.setCalendar(new GregorianCalendar(firstDay.getYear()+1900,firstDay.getMonth(),firstDay.getDate()));
78 jCalendar1.setCalendar(c);
80 long numberOfDays = (long) (lastDay.getTime()-firstDay.getTime()) / MILLSECS_PER_DAY;
81 jTextField3.setText(Long.toString(numberOfDays));
92 private void jbInit() throws Exception
94 this.getContentPane().setLayout(null);
95 this.setSize(new Dimension(410, 413));
96 this.setTitle("Book Rural House");
97 jLabel1.setText("Rural house:");
98 HouseManager houseM = new HouseManager();
99 ruralHouses=houseM.getAllRuralHouses();
101 jComboBox1 = new JComboBox(ruralHouses);
103 jLabel1.setBounds(new Rectangle(15, 10, 115, 20));
104 jComboBox1.setBounds(new Rectangle(120, 10, 175, 20));
107 jTextField3.addFocusListener(new FocusListener()
109 public void focusGained(FocusEvent e)
113 public void focusLost(FocusEvent e)
115 jTextField3_focusLost();
118 jTextField4.addFocusListener(new FocusListener()
120 public void focusGained(FocusEvent e)
124 public void focusLost(FocusEvent e)
126 jTextField4_focusLost();
129 jLabel2.setText("Arrival day:");
130 jLabel2.setBounds(new Rectangle(15, 40, 115, 20));
131 jLabel3.setText("Number of nights:");
132 jLabel3.setBounds(new Rectangle(15, 240, 115, 20));
133 jLabel4.setText("Telephone contact number:");
134 jLabel4.setBounds(new Rectangle(15, 270, 140, 20));
135 jTextField2.setBounds(new Rectangle(155, 205, 140, 20));
136 jTextField2.setEditable(false);
137 jTextField3.setBounds(new Rectangle(155, 240, 140, 20));
138 jTextField3.setText("0");
139 jTextField4.setBounds(new Rectangle(155, 270, 140, 20));
140 jTextField4.setText("0");
141 jButton2.setText("Accept");
142 jButton2.setBounds(new Rectangle(50, 345, 130, 30));
143 jButton2.setSize(new Dimension(130, 30));
144 jButton2.addActionListener(new ActionListener()
146 public void actionPerformed(ActionEvent e)
149 RuralHouse house=(RuralHouse)jComboBox1.getSelectedItem();
151 Date firstDay= new Date(jCalendar1.getCalendar().getTime().getTime());
153 firstDay=Date.valueOf(firstDay.toString());
155 // Number of days expressed in milliseconds
156 long nights=1000*60*60*24* Integer.parseInt(jTextField3.getText());
157 Date lastDay= new Date((long)(firstDay.getTime()+nights));
159 String telephone=jTextField4.getText();
162 //Obtain the business logic from a StartWindow class (local or remote)
163 BookingManager bookingM = new BookingManager();
165 Booking book=bookingM.createBooking(house, firstDay, lastDay, telephone);
167 BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
168 confirmWindow.setVisible(true);
169 jLabel5.setText("Booking made");
172 else jLabel5.setText("There is not available offer for these dates");
173 } catch (OfferCanNotBeBooked e1) {
174 jLabel5.setText("Error: It is not possible to book");
175 JFrame a = new QueryAvailabilityGUI();
178 } catch (Exception e1) {
180 e1.printStackTrace();
183 jButton3.setText("Cancel");
184 jButton3.setBounds(new Rectangle(220, 345, 130, 30));
185 jButton3.setSize(new Dimension(130, 30));
186 jButton3.addActionListener(new ActionListener()
188 public void actionPerformed(ActionEvent e)
190 jButton3_actionPerformed(e);
193 jLabel5.setBounds(new Rectangle(50, 310, 300, 20));
194 jLabel5.setForeground(Color.red);
195 jCalendar1.setBounds(new Rectangle(155, 50, 235, 145));
196 this.getContentPane().add(jCalendar1, null);
197 this.getContentPane().add(jLabel5, null);
198 this.getContentPane().add(jButton3, null);
199 this.getContentPane().add(jButton2, null);
200 this.getContentPane().add(jTextField4, null);
201 this.getContentPane().add(jTextField3, null);
202 this.getContentPane().add(jTextField2, null);
203 this.getContentPane().add(jLabel4, null);
204 this.getContentPane().add(jLabel3, null);
205 this.getContentPane().add(jLabel2, null);
206 this.getContentPane().add(jComboBox1, null);
207 this.getContentPane().add(jLabel1, null);
209 // Code for JCalendar
210 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
212 public void propertyChange(PropertyChangeEvent propertychangeevent)
214 if (propertychangeevent.getPropertyName().equals("locale"))
216 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
217 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
218 jTextField2.setText(dateformat.format(calendarMio.getTime()));
221 else if (propertychangeevent.getPropertyName().equals("calendar"))
223 calendarMio = (Calendar) propertychangeevent.getNewValue();
224 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
225 jTextField2.setText(dateformat1.format(calendarMio.getTime()));
226 jCalendar1.setCalendar(calendarMio);
233 private void jButton3_actionPerformed(ActionEvent e)
235 this.setVisible(false);
238 public void setConfirmBooking(boolean b){
239 if (b) jLabel5.setText("Booking made");
242 private void jTextField3_focusLost()
246 new Integer (jTextField3.getText());
249 catch (NumberFormatException ex)
251 jLabel5.setText("Error: Introduce a number");
255 private void jTextField4_focusLost()
259 new Integer (jTextField4.getText());
262 catch (NumberFormatException ex)
264 jLabel5.setText("Error: Introduce a number");