3 import businessLogic.ApplicationFacadeInterface;
5 import com.toedter.calendar.*;
8 import domain.RuralHouse;
9 import exceptions.OfferCanNotBeBooked;
19 import java.awt.event.*;
23 public class BookRuralHouseGUI extends JFrame {
24 private static final long serialVersionUID = 1L;
25 public static final long MILLSECS_PER_DAY= 24 * 60 * 60 * 1000;
27 private JLabel jLabel1 = new JLabel();
28 private JComboBox jComboBox1;
29 Vector<RuralHouse> ruralHouses;
30 private JLabel jLabel2 = new JLabel();
31 private JLabel jLabel3 = new JLabel();
32 private JLabel jLabel4 = new JLabel();
33 private JTextField jTextField2 = new JTextField();
34 private JTextField jTextField3 = new JTextField();
35 private JTextField jTextField4 = new JTextField();
36 private JButton jButton2 = new JButton();
37 private JButton jButton3 = new JButton();
40 private JCalendar jCalendar1 = new JCalendar();
41 private Calendar calendarMio = null;
42 private JLabel jLabel5 = new JLabel();
46 public BookRuralHouseGUI()
59 public BookRuralHouseGUI(int houseNumber,Date firstDay,Date lastDay)
66 // Put the "houseNumber", "firstDay" and "lastDay" in the graphical components of the user interface
67 for (int i=0; i<ruralHouses.size();i++){
68 if (((RuralHouse)ruralHouses.get(i)).getHouseNumber()==houseNumber) {
69 jComboBox1.setSelectedIndex(i);
74 Calendar c=new GregorianCalendar();
76 //jCalendar1.setCalendar(new GregorianCalendar(firstDay.getYear()+1900,firstDay.getMonth(),firstDay.getDate()));
77 jCalendar1.setCalendar(c);
79 long numberOfDays = (long) (lastDay.getTime()-firstDay.getTime()) / MILLSECS_PER_DAY;
80 jTextField3.setText(Long.toString(numberOfDays));
91 private void jbInit() throws Exception
93 this.getContentPane().setLayout(null);
94 this.setSize(new Dimension(410, 413));
95 this.setTitle("Book Rural House");
96 jLabel1.setText("Rural house:");
97 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
98 ruralHouses=facade.getAllRuralHouses();
100 jComboBox1 = new JComboBox(ruralHouses);
102 jLabel1.setBounds(new Rectangle(15, 10, 115, 20));
103 jComboBox1.setBounds(new Rectangle(120, 10, 175, 20));
106 jTextField3.addFocusListener(new FocusListener()
108 public void focusGained(FocusEvent e)
112 public void focusLost(FocusEvent e)
114 jTextField3_focusLost();
117 jTextField4.addFocusListener(new FocusListener()
119 public void focusGained(FocusEvent e)
123 public void focusLost(FocusEvent e)
125 jTextField4_focusLost();
128 jLabel2.setText("Arrival day:");
129 jLabel2.setBounds(new Rectangle(15, 40, 115, 20));
130 jLabel3.setText("Number of nights:");
131 jLabel3.setBounds(new Rectangle(15, 240, 115, 20));
132 jLabel4.setText("Telephone contact number:");
133 jLabel4.setBounds(new Rectangle(15, 270, 140, 20));
134 jTextField2.setBounds(new Rectangle(155, 205, 140, 20));
135 jTextField2.setEditable(false);
136 jTextField3.setBounds(new Rectangle(155, 240, 140, 20));
137 jTextField3.setText("0");
138 jTextField4.setBounds(new Rectangle(155, 270, 140, 20));
139 jTextField4.setText("0");
140 jButton2.setText("Accept");
141 jButton2.setBounds(new Rectangle(50, 345, 130, 30));
142 jButton2.setSize(new Dimension(130, 30));
143 jButton2.addActionListener(new ActionListener()
145 public void actionPerformed(ActionEvent e)
148 RuralHouse house=(RuralHouse)jComboBox1.getSelectedItem();
150 Date firstDay= new Date(jCalendar1.getCalendar().getTime().getTime());
152 firstDay=Date.valueOf(firstDay.toString());
154 // Number of days expressed in milliseconds
155 long nights=1000*60*60*24* Integer.parseInt(jTextField3.getText());
156 Date lastDay= new Date((long)(firstDay.getTime()+nights));
158 String telephone=jTextField4.getText();
161 //Obtain the business logic from a StartWindow class (local or remote)
162 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
164 Booking book=facade.createBooking(house, firstDay, lastDay, telephone);
166 BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
167 confirmWindow.setVisible(true);
168 jLabel5.setText("Booking made");
171 else jLabel5.setText("There is not available offer for these dates");
172 } catch (OfferCanNotBeBooked e1) {
173 jLabel5.setText("Error: It is not possible to book");
174 JFrame a = new QueryAvailabilityGUI();
177 } catch (Exception e1) {
179 e1.printStackTrace();
182 jButton3.setText("Cancel");
183 jButton3.setBounds(new Rectangle(220, 345, 130, 30));
184 jButton3.setSize(new Dimension(130, 30));
185 jButton3.addActionListener(new ActionListener()
187 public void actionPerformed(ActionEvent e)
189 jButton3_actionPerformed(e);
192 jLabel5.setBounds(new Rectangle(50, 310, 300, 20));
193 jLabel5.setForeground(Color.red);
194 jCalendar1.setBounds(new Rectangle(155, 50, 235, 145));
195 this.getContentPane().add(jCalendar1, null);
196 this.getContentPane().add(jLabel5, null);
197 this.getContentPane().add(jButton3, null);
198 this.getContentPane().add(jButton2, null);
199 this.getContentPane().add(jTextField4, null);
200 this.getContentPane().add(jTextField3, null);
201 this.getContentPane().add(jTextField2, null);
202 this.getContentPane().add(jLabel4, null);
203 this.getContentPane().add(jLabel3, null);
204 this.getContentPane().add(jLabel2, null);
205 this.getContentPane().add(jComboBox1, null);
206 this.getContentPane().add(jLabel1, null);
208 // Code for JCalendar
209 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
211 public void propertyChange(PropertyChangeEvent propertychangeevent)
213 if (propertychangeevent.getPropertyName().equals("locale"))
215 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
216 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
217 jTextField2.setText(dateformat.format(calendarMio.getTime()));
220 else if (propertychangeevent.getPropertyName().equals("calendar"))
222 calendarMio = (Calendar) propertychangeevent.getNewValue();
223 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
224 jTextField2.setText(dateformat1.format(calendarMio.getTime()));
225 jCalendar1.setCalendar(calendarMio);
232 private void jButton3_actionPerformed(ActionEvent e)
234 this.setVisible(false);
237 public void setConfirmBooking(boolean b){
238 if (b) jLabel5.setText("Booking made");
241 private void jTextField3_focusLost()
245 new Integer (jTextField3.getText());
248 catch (NumberFormatException ex)
250 jLabel5.setText("Error: Introduce a number");
254 private void jTextField4_focusLost()
258 new Integer (jTextField4.getText());
261 catch (NumberFormatException ex)
263 jLabel5.setText("Error: Introduce a number");