Booking confirmation and e-mail service added. Some bugs to be solved.
[RRRRHHHH_Code] / ruralHouses / src / gui / AddOffersGUI.java
1 package gui;
2 import java.awt.Color;
3 import java.awt.Dimension;
4 import java.awt.Rectangle;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.FocusEvent;
8 import java.awt.event.FocusListener;
9 import java.beans.PropertyChangeEvent;
10 import java.beans.PropertyChangeListener;
11 import java.sql.Date;
12 import java.text.DateFormat;
13 import java.util.Calendar;
14 import java.util.Locale;
15 import java.util.Vector;
16
17 import javax.swing.JButton;
18 import javax.swing.JComboBox;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21 import javax.swing.JTextField;
22
23 import businessLogic.OfferManager;
24
25 import com.toedter.calendar.JCalendar;
26
27 import domain.RuralHouse;
28 import exceptions.BadDates;
29 import exceptions.OverlappingOfferExists;
30
31
32
33 public class AddOffersGUI extends JFrame  {
34 private static final long serialVersionUID = 1L;
35
36         
37   private JComboBox<RuralHouse> jComboBox1;
38   private JLabel jLabel1 = new JLabel();
39   private JLabel jLabel2 = new JLabel();
40   private JTextField jTextField1 = new JTextField();
41   private JLabel jLabel3 = new JLabel();
42   private JTextField jTextField2 = new JTextField();
43   private JLabel jLabel4 = new JLabel();
44   private JTextField jTextField3 = new JTextField();
45   private JButton jButton1 = new JButton();
46   
47   // Code for JCalendar
48   private JCalendar jCalendar1 = new JCalendar();
49   private JCalendar jCalendar2 = new JCalendar();
50   private Calendar calendarInicio = null;
51   private Calendar calendarFin = null;
52   private JButton jButton2 = new JButton();
53   private JLabel jLabel5 = new JLabel();
54   private final JLabel lblNewLabel = new JLabel("");
55   
56
57   public AddOffersGUI(Vector<RuralHouse> v)
58   {
59     try
60     {
61       jbInit(v);
62     }
63     catch(Exception e)
64     {
65       e.printStackTrace();
66     }
67   }
68
69   private void jbInit(Vector<RuralHouse> v) throws Exception
70   {
71     this.getContentPane().setLayout(null);
72     this.setSize(new Dimension(513, 433));
73     this.setTitle("Set availability");
74     
75     
76     jComboBox1 = new JComboBox<RuralHouse>(v);
77     jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
78     jLabel1.setText("List of houses:");
79     jLabel1.setBounds(new Rectangle(25, 30, 95, 20));
80     jLabel2.setText("First day :");
81     jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
82     jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
83     jTextField1.setEditable(false);
84     jLabel3.setText("Last day :");
85     jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
86     jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
87     jTextField2.setEditable(false);
88     jLabel4.setText("Price:");
89     jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
90     jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
91     jTextField3.setText("0");
92     jButton1.setText("Accept");
93     jButton1.setBounds(new Rectangle(100, 360, 130, 30));
94     jTextField3.addFocusListener(new FocusListener()
95       {
96           public void focusGained(FocusEvent e)
97           {
98           }
99   
100           public void focusLost(FocusEvent e)
101           {
102             jTextField3_focusLost();
103           }
104       });
105     jButton1.addActionListener(new ActionListener()
106       {
107         public void actionPerformed(ActionEvent e)
108         {
109           jButton1_actionPerformed(e);
110         }
111       });
112     jButton2.setText("Cancel");
113     jButton2.setBounds(new Rectangle(270, 360, 130, 30));
114     jButton2.addActionListener(new ActionListener()
115       {
116         public void actionPerformed(ActionEvent e)
117         {
118           jButton2_actionPerformed(e);
119         }
120       });
121     jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
122     jLabel5.setForeground(Color.red);
123     jLabel5.setSize(new Dimension(305, 20));
124     jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
125     jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
126     
127     // Code for  JCalendar
128     this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
129     {
130       public void propertyChange(PropertyChangeEvent propertychangeevent)
131       {
132         if (propertychangeevent.getPropertyName().equals("locale"))
133         {
134           jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
135           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
136           jTextField1.setText(dateformat.format(calendarInicio.getTime()));
137         }
138         else if (propertychangeevent.getPropertyName().equals("calendar"))
139         {
140           calendarInicio = (Calendar) propertychangeevent.getNewValue();
141           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
142           jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
143           jCalendar1.setCalendar(calendarInicio);
144         }
145       } 
146     });
147     
148     this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
149     {
150       public void propertyChange(PropertyChangeEvent propertychangeevent)
151       {
152         if (propertychangeevent.getPropertyName().equals("locale"))
153         {
154           jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
155           DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
156           jTextField2.setText(dateformat.format(calendarFin.getTime()));
157         }
158         else if (propertychangeevent.getPropertyName().equals("calendar"))
159         {
160           calendarFin = (Calendar) propertychangeevent.getNewValue();
161           DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
162           jTextField2.setText(dateformat1.format(calendarFin.getTime()));
163           jCalendar2.setCalendar(calendarFin);
164         }
165       } 
166     });
167     
168     
169     this.getContentPane().add(jCalendar2, null);
170     this.getContentPane().add(jCalendar1, null);
171     this.getContentPane().add(jLabel5, null);
172     this.getContentPane().add(jButton2, null);
173     this.getContentPane().add(jButton1, null);
174     this.getContentPane().add(jTextField3, null);
175     this.getContentPane().add(jLabel4, null);
176     this.getContentPane().add(jTextField2, null);
177     this.getContentPane().add(jLabel3, null);
178     this.getContentPane().add(jTextField1, null);
179     this.getContentPane().add(jLabel2, null);
180     this.getContentPane().add(jLabel1, null);
181     this.getContentPane().add(jComboBox1, null);
182     lblNewLabel.setBounds(115, 301, 298, 38);
183     
184     getContentPane().add(lblNewLabel);
185   }
186
187   private void jButton1_actionPerformed(ActionEvent e)
188   {
189                 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
190
191                 // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object
192                 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
193                 // The next instruction removes the hour, minute, second and ms from the date
194                 // This has to be made because the date will be stored in db4o as a java.util.Date object 
195                 // that would store those data, and that would give problems when comparing dates later
196                 firstDay=Date.valueOf(firstDay.toString());
197                 
198                 
199                 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
200             //Remove the hour:minute:second:ms from the date 
201                 lastDay=Date.valueOf(lastDay.toString());
202                 
203                 
204                 try {
205
206                         //It could be to trigger an exception if the introduced string is not a number
207                         float price= Float.parseFloat(jTextField3.getText());
208
209                         //Obtain the business logic from a StartWindow class (local or remote)
210                         OfferManager offerM = new OfferManager();
211
212                         offerM.createOffer(ruralHouse, firstDay, lastDay, price); 
213
214                         jLabel5.setText("Offer created");
215
216                 } catch (java.lang.NumberFormatException e1) {
217                         jLabel5.setText(jTextField3.getText()+ " is not a valid price");
218                 } catch (OverlappingOfferExists e1) {
219                         jLabel5.setText("There exists an overlapping offer");
220                 }
221                 catch (BadDates e1) {
222                         jLabel5.setText("Last day is before first day in the offer");
223                 } catch (NullPointerException e1){
224                         jLabel5.setText("Bad dates or there exists an overlapping offer");
225                 } catch (Exception e1) {
226                 
227                         e1.printStackTrace();
228                 } 
229   }
230   private void jButton2_actionPerformed(ActionEvent e)
231   {
232     this.setVisible(false);
233   }
234   
235  private void jTextField3_focusLost()
236  {
237    try
238   {
239     new Integer (jTextField3.getText());
240     jLabel5.setText("");
241   }
242   catch (NumberFormatException ex)
243   {
244     jLabel5.setText("Error: Introduce a number");
245   }
246  }
247 }