3 import businessLogic.ApplicationFacadeInterface;
5 import com.toedter.calendar.*;
8 import domain.RuralHouse;
10 import exceptions.OfferCanNotBeBooked;
20 import java.awt.event.*;
22 public class BookRuralHouseGUI extends JFrame {
23 private static final long serialVersionUID = 1L;
25 private JLabel jLabel1 = new JLabel();
26 private JComboBox jComboBox1;
27 private JLabel jLabel2 = new JLabel();
28 private JLabel jLabel3 = new JLabel();
29 private JLabel jLabel4 = new JLabel();
30 private JTextField jTextField2 = new JTextField();
31 private JTextField jTextField3 = new JTextField();
32 private JTextField jTextField4 = new JTextField();
33 private JButton jButton2 = new JButton();
34 private JButton jButton3 = new JButton();
37 private JCalendar jCalendar1 = new JCalendar();
38 private Calendar myCalendar = null;
39 private JLabel jLabel5 = new JLabel();
41 public BookRuralHouseGUI() {
50 public BookRuralHouseGUI(int houseNumber, Date firstDay, Date lastDay) {
54 /*jTextField1.setText(Integer.toString(houseNumber));
55 long nights=(lastDay.getTime()-firstDay.getTime())/(1000*60*60*24);
56 jTextField3.setText(Long.toString(nights));
57 DateFormat dateformat1 = DateFormat.getDateInstance(1);
59 Date first= new Date((long)(firstDay.getTime()));
60 jTextField2.setText(dateformat1.format(first));
61 GregorianCalendar cal=new GregorianCalendar();
63 int year=cal.get(Calendar.YEAR);
64 int month=cal.get(Calendar.MONTH);
65 int day=cal.get(Calendar.DAY_OF_MONTH);
67 JYearChooser yc=jCalendar1.getYearChooser();
68 JMonthChooser mc= jCalendar1.getMonthChooser();
69 JDayChooser dc= jCalendar1.getDayChooser();
81 private void jbInit() throws Exception {
82 this.getContentPane().setLayout(null);
83 this.setSize(new Dimension(430, 440));
84 this.setTitle("Book Rural House");
85 jLabel1.setText("Rural house:");
86 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
87 Vector<RuralHouse> ruralHouses=facade.getAllRuralHouses();
89 jComboBox1 = new JComboBox(ruralHouses);
91 jLabel1.setBounds(new Rectangle(15, 10, 115, 20));
92 jComboBox1.setBounds(new Rectangle(120, 10, 175, 20));
94 jTextField3.addFocusListener(new FocusListener() {
95 public void focusGained(FocusEvent e) {}
96 public void focusLost(FocusEvent e) {
97 jTextField3_focusLost();
100 jTextField4.addFocusListener(new FocusListener() {
101 public void focusGained(FocusEvent e) { }
102 public void focusLost(FocusEvent e) {
103 jTextField4_focusLost();
106 jLabel2.setText("Arrival day:");
107 jLabel2.setBounds(new Rectangle(15, 40, 115, 20));
108 jLabel3.setText("Number of nights:");
109 jLabel3.setBounds(new Rectangle(15, 240, 115, 20));
110 jLabel4.setText("Telephone contact number:");
111 jLabel4.setBounds(new Rectangle(15, 270, 140, 20));
112 jTextField2.setBounds(new Rectangle(155, 205, 140, 20));
113 jTextField2.setEditable(false);
114 jTextField3.setBounds(new Rectangle(155, 240, 140, 20));
115 jTextField3.setText("0");
116 jTextField4.setBounds(new Rectangle(155, 270, 140, 20));
117 jTextField4.setText("0");
118 jButton2.setText("Accept");
119 jButton2.setBounds(new Rectangle(50, 345, 130, 30));
120 jButton2.setSize(new Dimension(130, 30));
121 jButton2.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent e) {
124 RuralHouse house=(RuralHouse)jComboBox1.getSelectedItem();
126 Date firstDay= new Date(jCalendar1.getCalendar().getTime().getTime());
127 firstDay=Date.valueOf(firstDay.toString());
129 // Number of days expressed in milliseconds
130 long nights=1000*60*60*24* Integer.parseInt(jTextField3.getText());
131 Date lastDay= new Date((long)(firstDay.getTime()+nights));
133 String telephone=jTextField4.getText();
135 //Obtain the business logic from a StartWindow class (local or remote)
136 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
138 Booking book=facade.createBooking(house, firstDay, lastDay, telephone);
140 System.out.println(book.getPrice());
141 BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
142 confirmWindow.setVisible(true);
145 catch (OfferCanNotBeBooked e1) {
146 jLabel5.setText("Error: Booking is not possible");
147 JFrame a = new SearchOffersGUI();
150 catch (Exception e1) {
151 e1.printStackTrace();
155 jButton3.setText("Cancel");
156 jButton3.setBounds(new Rectangle(220, 345, 130, 30));
157 jButton3.setSize(new Dimension(130, 30));
158 jButton3.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 jButton3_actionPerformed(e);
163 jLabel5.setBounds(new Rectangle(50, 310, 300, 20));
164 jLabel5.setForeground(Color.red);
165 jCalendar1.setBounds(new Rectangle(155, 50, 235, 145));
166 this.getContentPane().add(jCalendar1, null);
167 this.getContentPane().add(jLabel5, null);
168 this.getContentPane().add(jButton3, null);
169 this.getContentPane().add(jButton2, null);
170 this.getContentPane().add(jTextField4, null);
171 this.getContentPane().add(jTextField3, null);
172 this.getContentPane().add(jTextField2, null);
173 this.getContentPane().add(jLabel4, null);
174 this.getContentPane().add(jLabel3, null);
175 this.getContentPane().add(jLabel2, null);
176 this.getContentPane().add(jComboBox1, null);
177 this.getContentPane().add(jLabel1, null);
179 // Code for JCalendar
180 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
181 public void propertyChange(PropertyChangeEvent propertychangeevent) {
182 if (propertychangeevent.getPropertyName().equals("locale")) {
183 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
184 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
185 jTextField2.setText(dateformat.format(myCalendar.getTime()));
187 else if (propertychangeevent.getPropertyName().equals("calendar")) {
188 myCalendar = (Calendar) propertychangeevent.getNewValue();
189 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
190 jTextField2.setText(dateformat1.format(myCalendar.getTime()));
191 jCalendar1.setCalendar(myCalendar);
197 private void jButton3_actionPerformed(ActionEvent e) {
198 this.setVisible(false);
201 private void jTextField3_focusLost() {
203 new Integer (jTextField3.getText());
206 catch (NumberFormatException ex) {
207 jLabel5.setText("Error: Please introduce a number");
211 private void jTextField4_focusLost() {
213 new Integer (jTextField4.getText());
216 catch (NumberFormatException ex) {
217 jLabel5.setText("Error: Please introduce a number");