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;
12 import java.text.DateFormat;
13 import java.util.Calendar;
14 import java.util.Locale;
15 import java.util.Vector;
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;
23 import businessLogic.OfferManager;
25 import com.toedter.calendar.JCalendar;
27 import domain.RuralHouse;
28 import exceptions.BadDates;
29 import exceptions.OverlappingOfferExists;
33 public class AddOffersGUI extends JFrame {
34 private static final long serialVersionUID = 1L;
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();
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("");
57 public AddOffersGUI(Vector<RuralHouse> v)
69 private void jbInit(Vector<RuralHouse> v) throws Exception
71 this.getContentPane().setLayout(null);
72 this.setSize(new Dimension(513, 433));
73 this.setTitle("Set availability");
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()
96 public void focusGained(FocusEvent e)
100 public void focusLost(FocusEvent e)
102 jTextField3_focusLost();
105 jButton1.addActionListener(new ActionListener()
107 public void actionPerformed(ActionEvent e)
109 jButton1_actionPerformed(e);
112 jButton2.setText("Cancel");
113 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
114 jButton2.addActionListener(new ActionListener()
116 public void actionPerformed(ActionEvent e)
118 jButton2_actionPerformed(e);
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));
127 // Code for JCalendar
128 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
130 public void propertyChange(PropertyChangeEvent propertychangeevent)
132 if (propertychangeevent.getPropertyName().equals("locale"))
134 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
135 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
136 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
138 else if (propertychangeevent.getPropertyName().equals("calendar"))
140 calendarInicio = (Calendar) propertychangeevent.getNewValue();
141 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
142 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
143 jCalendar1.setCalendar(calendarInicio);
148 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
150 public void propertyChange(PropertyChangeEvent propertychangeevent)
152 if (propertychangeevent.getPropertyName().equals("locale"))
154 jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
155 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
156 jTextField2.setText(dateformat.format(calendarFin.getTime()));
158 else if (propertychangeevent.getPropertyName().equals("calendar"))
160 calendarFin = (Calendar) propertychangeevent.getNewValue();
161 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
162 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
163 jCalendar2.setCalendar(calendarFin);
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);
184 getContentPane().add(lblNewLabel);
187 private void jButton1_actionPerformed(ActionEvent e)
189 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
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());
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());
206 //It could be to trigger an exception if the introduced string is not a number
207 float price= Float.parseFloat(jTextField3.getText());
209 //Obtain the business logic from a StartWindow class (local or remote)
210 OfferManager offerM = new OfferManager();
212 offerM.createOffer(ruralHouse, firstDay, lastDay, price);
214 jLabel5.setText("Offer created");
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");
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) {
227 e1.printStackTrace();
230 private void jButton2_actionPerformed(ActionEvent e)
232 this.setVisible(false);
235 private void jTextField3_focusLost()
239 new Integer (jTextField3.getText());
242 catch (NumberFormatException ex)
244 jLabel5.setText("Error: Introduce a number");