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