705b3517842294b0b1e990479de06e18bbae4abf
[RRRRHHHH_Code] / ruralHouses / src / gui / BookRuralHouseGUI.java
1 package gui;
2
3 import businessLogic.ApplicationFacadeInterface;
4
5 import com.toedter.calendar.*;
6
7 import domain.Booking;
8 import domain.RuralHouse;
9 import exceptions.OfferCanNotBeBooked;
10
11 import java.beans.*;
12 import java.sql.Date;
13 import java.text.*;
14 import java.util.*;
15
16 import javax.swing.*;
17
18 import java.awt.*;
19 import java.awt.event.*;
20
21
22
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;
26
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();
38   
39   // Code for JCalendar
40   private JCalendar jCalendar1 = new JCalendar();
41   private Calendar calendarMio = null;
42   private JLabel jLabel5 = new JLabel();
43   
44   
45
46 public BookRuralHouseGUI()
47  {
48     try
49     {
50       jbInit();
51       
52      
53     }
54     catch(Exception e)
55     {
56       e.printStackTrace();
57     }
58   }
59   public BookRuralHouseGUI(int houseNumber,Date firstDay,Date lastDay)
60   {
61     try
62     {
63       jbInit();
64       
65       
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);
70                   break;
71           }
72       }
73       
74       Calendar c=new GregorianCalendar();
75       c.setTime(firstDay);
76       //jCalendar1.setCalendar(new GregorianCalendar(firstDay.getYear()+1900,firstDay.getMonth(),firstDay.getDate()));
77       jCalendar1.setCalendar(c);
78       
79       long numberOfDays = (long) (lastDay.getTime()-firstDay.getTime()) / MILLSECS_PER_DAY;
80       jTextField3.setText(Long.toString(numberOfDays));
81       
82
83       
84     }
85     catch(Exception e)
86     {
87       e.printStackTrace();
88     }
89   }
90
91   private void jbInit() throws Exception
92   {
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();
99
100         jComboBox1 = new JComboBox(ruralHouses);
101
102     jLabel1.setBounds(new Rectangle(15, 10, 115, 20));
103     jComboBox1.setBounds(new Rectangle(120, 10, 175, 20));
104
105     
106     jTextField3.addFocusListener(new FocusListener()
107       {
108           public void focusGained(FocusEvent e)
109           {
110           }
111   
112           public void focusLost(FocusEvent e)
113           {
114             jTextField3_focusLost();
115           }
116       });
117     jTextField4.addFocusListener(new FocusListener()
118       {
119           public void focusGained(FocusEvent e)
120           {
121           }
122   
123           public void focusLost(FocusEvent e)
124           {
125             jTextField4_focusLost();
126           }
127       });
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()
144       {
145         public void actionPerformed(ActionEvent e)
146         {
147                 // House code
148                 RuralHouse house=(RuralHouse)jComboBox1.getSelectedItem();
149                 // Arrival date
150                 Date firstDay= new Date(jCalendar1.getCalendar().getTime().getTime());
151
152                 firstDay=Date.valueOf(firstDay.toString());
153                 // Last day
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));
157                 // Telephone contact
158                 String telephone=jTextField4.getText();
159                 try {
160                                 
161                         //Obtain the business logic from a StartWindow class (local or remote)
162                         ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
163                                                 
164                         Booking book=facade.createBooking(house, firstDay, lastDay, telephone);
165                                 if (book!=null) {
166                                 BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
167                                 confirmWindow.setVisible(true);
168                                 jLabel5.setText("Booking made");
169
170                                 }
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();
175                             a.setVisible(true);
176                         
177         } catch (Exception e1) {
178                         
179                         e1.printStackTrace();
180         }}
181       });
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()
186       {
187         public void actionPerformed(ActionEvent e)
188         {
189           jButton3_actionPerformed(e);
190         }
191       });
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);
207     
208     // Code for JCalendar
209     this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
210     {
211       public void propertyChange(PropertyChangeEvent propertychangeevent)
212       {
213         if (propertychangeevent.getPropertyName().equals("locale"))
214         {
215           jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
216           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
217           jTextField2.setText(dateformat.format(calendarMio.getTime()));
218           
219         }
220         else if (propertychangeevent.getPropertyName().equals("calendar"))
221         {
222           calendarMio = (Calendar) propertychangeevent.getNewValue();
223           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
224           jTextField2.setText(dateformat1.format(calendarMio.getTime()));
225           jCalendar1.setCalendar(calendarMio);
226         }
227       } 
228     });
229   }
230  
231
232   private void jButton3_actionPerformed(ActionEvent e)
233   {
234     this.setVisible(false);
235   }
236
237   public void setConfirmBooking(boolean b){
238           if (b) jLabel5.setText("Booking made");
239   }
240  
241  private void jTextField3_focusLost()
242  {
243    try
244   {
245     new Integer (jTextField3.getText());
246     jLabel5.setText("");
247   }
248   catch (NumberFormatException ex)
249   {
250     jLabel5.setText("Error: Introduce a number");
251   }
252  }
253  
254  private void jTextField4_focusLost()
255  {
256    try
257   {
258     new Integer (jTextField4.getText());
259     jLabel5.setText("");
260   }
261   catch (NumberFormatException ex)
262   {
263     jLabel5.setText("Error: Introduce a number");
264   }
265  }
266 }