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