4 import java.text.DateFormat;
10 import java.awt.event.*;
13 import businessLogic.OfferManager;
15 import com.toedter.calendar.JCalendar;
18 import domain.RuralHouse;
19 import exceptions.OverlappingOfferExists;
20 import exceptions.BadDates;
24 public class SetAvailability2GUI extends JFrame {
25 private static final long serialVersionUID = 1L;
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();
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("");
47 public SetAvailability2GUI(Vector<RuralHouse> v)
59 private void jbInit(Vector<RuralHouse> v) throws Exception
61 this.getContentPane().setLayout(null);
62 this.setSize(new Dimension(513, 433));
63 this.setTitle("Set availability");
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()
86 public void focusGained(FocusEvent e)
90 public void focusLost(FocusEvent e)
92 jTextField3_focusLost();
95 jButton1.addActionListener(new ActionListener()
97 public void actionPerformed(ActionEvent e)
99 jButton1_actionPerformed(e);
102 jButton2.setText("Cancel");
103 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
104 jButton2.addActionListener(new ActionListener()
106 public void actionPerformed(ActionEvent e)
108 jButton2_actionPerformed(e);
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));
117 // Code for JCalendar
118 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
120 public void propertyChange(PropertyChangeEvent propertychangeevent)
122 if (propertychangeevent.getPropertyName().equals("locale"))
124 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
125 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
126 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
128 else if (propertychangeevent.getPropertyName().equals("calendar"))
130 calendarInicio = (Calendar) propertychangeevent.getNewValue();
131 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
132 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
133 jCalendar1.setCalendar(calendarInicio);
138 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
140 public void propertyChange(PropertyChangeEvent propertychangeevent)
142 if (propertychangeevent.getPropertyName().equals("locale"))
144 jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
145 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
146 jTextField2.setText(dateformat.format(calendarFin.getTime()));
148 else if (propertychangeevent.getPropertyName().equals("calendar"))
150 calendarFin = (Calendar) propertychangeevent.getNewValue();
151 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
152 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
153 jCalendar2.setCalendar(calendarFin);
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);
174 getContentPane().add(lblNewLabel);
177 private void jButton1_actionPerformed(ActionEvent e)
179 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
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());
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());
196 //It could be to trigger an exception if the introduced string is not a number
197 float price= Float.parseFloat(jTextField3.getText());
199 //Obtain the business logic from a StartWindow class (local or remote)
200 OfferManager offerM = new OfferManager();
202 Offer o = offerM.createOffer(ruralHouse, firstDay, lastDay, price);
203 System.out.println("Offer created: "+o.toString());
205 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 (NullPointerException e1){
215 jLabel5.setText("Bad dates or there exists an overlapping offer");
216 } catch (Exception e1) {
218 e1.printStackTrace();
221 private void jButton2_actionPerformed(ActionEvent e)
223 this.setVisible(false);
226 private void jTextField3_focusLost()
230 new Integer (jTextField3.getText());
233 catch (NumberFormatException ex)
235 jLabel5.setText("Error: Introduce a number");