4 import java.text.DateFormat;
10 import java.awt.event.*;
12 import businessLogic.ApplicationFacadeInterface;
14 import com.toedter.calendar.JCalendar;
17 import domain.RuralHouse;
18 import exceptions.OverlappingOfferExists;
19 import exceptions.BadDates;
23 public class SetAvailability2GUI extends JFrame {
24 private static final long serialVersionUID = 1L;
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();
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("");
45 public SetAvailability2GUI(Vector<RuralHouse> v)
57 private void jbInit(Vector<RuralHouse> v) throws Exception
59 this.getContentPane().setLayout(null);
60 this.setSize(new Dimension(513, 433));
61 this.setTitle("Set availability");
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()
84 public void focusGained(FocusEvent e)
88 public void focusLost(FocusEvent e)
90 jTextField3_focusLost();
93 jButton1.addActionListener(new ActionListener()
95 public void actionPerformed(ActionEvent e)
97 jButton1_actionPerformed(e);
100 jButton2.setText("Cancel");
101 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
102 jButton2.addActionListener(new ActionListener()
104 public void actionPerformed(ActionEvent e)
106 jButton2_actionPerformed(e);
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));
115 // Code for JCalendar
116 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
118 public void propertyChange(PropertyChangeEvent propertychangeevent)
120 if (propertychangeevent.getPropertyName().equals("locale"))
122 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
123 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
124 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
126 else if (propertychangeevent.getPropertyName().equals("calendar"))
128 calendarInicio = (Calendar) propertychangeevent.getNewValue();
129 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
130 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
131 jCalendar1.setCalendar(calendarInicio);
136 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
138 public void propertyChange(PropertyChangeEvent propertychangeevent)
140 if (propertychangeevent.getPropertyName().equals("locale"))
142 jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
143 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
144 jTextField2.setText(dateformat.format(calendarFin.getTime()));
146 else if (propertychangeevent.getPropertyName().equals("calendar"))
148 calendarFin = (Calendar) propertychangeevent.getNewValue();
149 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
150 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
151 jCalendar2.setCalendar(calendarFin);
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);
172 getContentPane().add(lblNewLabel);
175 private void jButton1_actionPerformed(ActionEvent e)
177 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
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());
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());
194 //It could be to trigger an exception if the introduced string is not a number
195 float price= Float.parseFloat(jTextField3.getText());
197 //Obtain the business logic from a StartWindow class (local or remote)
198 ApplicationFacadeInterface facade=StartWindow.getBusinessLogic();
200 Offer o = facade.createOffer(ruralHouse, firstDay, lastDay, price);
201 System.out.println("Offer created: "+o.toString());
204 jLabel5.setText("Bad dates or there exists an overlapping offer");
205 else jLabel5.setText("Offer created");
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");
212 catch (BadDates e1) {
213 jLabel5.setText("Last day is before first day in the offer");
214 } catch (Exception e1) {
216 e1.printStackTrace();
219 private void jButton2_actionPerformed(ActionEvent e)
221 this.setVisible(false);
224 private void jTextField3_focusLost()
228 new Integer (jTextField3.getText());
231 catch (NumberFormatException ex)
233 jLabel5.setText("Error: Introduce a number");