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