Given code uploaded
[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
10 import exceptions.OfferCanNotBeBooked;
11
12 import java.beans.*;
13
14 import java.sql.Date;
15 import java.text.*;
16 import java.util.*;
17
18 import javax.swing.*;
19 import java.awt.*;
20 import java.awt.event.*;
21
22 public class BookRuralHouseGUI extends JFrame {
23         private static final long serialVersionUID = 1L;
24
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();
35
36         // Code for JCalendar
37         private JCalendar jCalendar1 = new JCalendar();
38         private Calendar myCalendar = null;
39         private JLabel jLabel5 = new JLabel();
40
41         public BookRuralHouseGUI() {
42                 try {
43                         jbInit();
44                 }
45                 catch(Exception e) {
46                         e.printStackTrace();
47                 }
48         }
49
50         public BookRuralHouseGUI(int houseNumber, Date firstDay, Date lastDay) {
51                 try {
52                         jbInit();
53
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);
58
59                         Date first= new Date((long)(firstDay.getTime()));
60                         jTextField2.setText(dateformat1.format(first));
61                         GregorianCalendar cal=new GregorianCalendar();
62                         cal.setTime(first);
63                         int year=cal.get(Calendar.YEAR);
64                         int month=cal.get(Calendar.MONTH);
65                         int day=cal.get(Calendar.DAY_OF_MONTH);
66
67                         JYearChooser yc=jCalendar1.getYearChooser();
68                 JMonthChooser mc=   jCalendar1.getMonthChooser();
69                 JDayChooser dc= jCalendar1.getDayChooser();
70
71                         yc.setYear(year);
72                 mc.setMonth(month);
73                 dc.setDay(day); */
74
75                 }
76                 catch(Exception e) {
77                         e.printStackTrace();
78                 }
79         }
80
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();
88
89                 jComboBox1 = new JComboBox(ruralHouses);
90
91                 jLabel1.setBounds(new Rectangle(15, 10, 115, 20));
92                 jComboBox1.setBounds(new Rectangle(120, 10, 175, 20));
93
94                 jTextField3.addFocusListener(new FocusListener() {
95                         public void focusGained(FocusEvent e) {}
96                         public void focusLost(FocusEvent e) {
97                                 jTextField3_focusLost();
98                         }
99                 });
100                 jTextField4.addFocusListener(new FocusListener() {
101                         public void focusGained(FocusEvent e) { }
102                         public void focusLost(FocusEvent e) {
103                                 jTextField4_focusLost();
104                         }
105                 });
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) {
123                                 // House code
124                                 RuralHouse house=(RuralHouse)jComboBox1.getSelectedItem();
125                                 // Arrival date
126                                 Date firstDay= new Date(jCalendar1.getCalendar().getTime().getTime());
127                                 firstDay=Date.valueOf(firstDay.toString());
128                                 // Last day
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));
132                                 // Contact telephone
133                                 String telephone=jTextField4.getText();
134                                 try {
135                                         //Obtain the business logic from a StartWindow class (local or remote)
136                                         ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
137
138                                         Booking book=facade.createBooking(house, firstDay, lastDay, telephone);
139                                         System.out.println(book.getPrice());
140                                         if (book!=null) {
141                                                 BookRuralHouseConfirmationWindow confirmWindow=new BookRuralHouseConfirmationWindow(book);
142                                                 confirmWindow.setVisible(true);
143                                         }
144                                 }
145                                 catch (OfferCanNotBeBooked e1) {
146                                         jLabel5.setText("Error: Booking is not possible");
147                                         JFrame a = new SearchOffersGUI();
148                                         a.setVisible(true);
149                                 }
150                                 catch (Exception e1) {
151                                         e1.printStackTrace();
152                                 }
153                         }
154                 });
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);
161                         }
162                 });
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);
178
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()));
186                                 }
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);
192                                 }
193                         } 
194                 });
195         }
196
197         private void jButton3_actionPerformed(ActionEvent e) {
198                 this.setVisible(false);
199         }
200
201         private void jTextField3_focusLost() {
202                 try {
203                         new Integer (jTextField3.getText());
204                         jLabel5.setText("");
205                 }
206                 catch (NumberFormatException ex) {
207                         jLabel5.setText("Error: Please introduce a number");
208                 }
209         }
210
211         private void jTextField4_focusLost() {
212                 try {
213                         new Integer (jTextField4.getText());
214                         jLabel5.setText("");
215                 }
216                 catch (NumberFormatException ex) {
217                         jLabel5.setText("Error: Please introduce a number");
218                 }
219         }
220
221 }