3 import java.rmi.RMISecurityManager;
5 import java.text.DateFormat;
11 import java.awt.event.*;
13 import businessLogic.OfferManager;
15 import com.toedter.calendar.JCalendar;
17 import configuration.ConfigXML;
19 import domain.RuralHouse;
20 import exceptions.OverlappingOfferExists;
21 import exceptions.BadDates;
25 public class SetAvailability2GUI extends JFrame {
26 private static final long serialVersionUID = 1L;
29 private JComboBox jComboBox1;
30 private JLabel jLabel1 = new JLabel();
31 private JLabel jLabel2 = new JLabel();
32 private JTextField jTextField1 = new JTextField();
33 private JLabel jLabel3 = new JLabel();
34 private JTextField jTextField2 = new JTextField();
35 private JLabel jLabel4 = new JLabel();
36 private JTextField jTextField3 = new JTextField();
37 private JButton jButton1 = new JButton();
40 private JCalendar jCalendar1 = new JCalendar();
41 private JCalendar jCalendar2 = new JCalendar();
42 private Calendar calendarInicio = null;
43 private Calendar calendarFin = null;
44 private JButton jButton2 = new JButton();
45 private JLabel jLabel5 = new JLabel();
46 private final JLabel lblNewLabel = new JLabel("");
49 public SetAvailability2GUI(Vector<RuralHouse> v)
61 private void jbInit(Vector<RuralHouse> v) throws Exception
63 this.getContentPane().setLayout(null);
64 this.setSize(new Dimension(513, 433));
65 this.setTitle("Set availability");
68 jComboBox1 = new JComboBox(v);
69 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
70 jLabel1.setText("List of houses:");
71 jLabel1.setBounds(new Rectangle(25, 30, 95, 20));
72 jLabel2.setText("First day :");
73 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
74 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
75 jTextField1.setEditable(false);
76 jLabel3.setText("Last day :");
77 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
78 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
79 jTextField2.setEditable(false);
80 jLabel4.setText("Price:");
81 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
82 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
83 jTextField3.setText("0");
84 jButton1.setText("Accept");
85 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
86 jTextField3.addFocusListener(new FocusListener()
88 public void focusGained(FocusEvent e)
92 public void focusLost(FocusEvent e)
94 jTextField3_focusLost();
97 jButton1.addActionListener(new ActionListener()
99 public void actionPerformed(ActionEvent e)
101 jButton1_actionPerformed(e);
104 jButton2.setText("Cancel");
105 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
106 jButton2.addActionListener(new ActionListener()
108 public void actionPerformed(ActionEvent e)
110 jButton2_actionPerformed(e);
113 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
114 jLabel5.setForeground(Color.red);
115 jLabel5.setSize(new Dimension(305, 20));
116 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
117 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
119 // Code for JCalendar
120 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
122 public void propertyChange(PropertyChangeEvent propertychangeevent)
124 if (propertychangeevent.getPropertyName().equals("locale"))
126 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
127 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
128 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
130 else if (propertychangeevent.getPropertyName().equals("calendar"))
132 calendarInicio = (Calendar) propertychangeevent.getNewValue();
133 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
134 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
135 jCalendar1.setCalendar(calendarInicio);
140 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
142 public void propertyChange(PropertyChangeEvent propertychangeevent)
144 if (propertychangeevent.getPropertyName().equals("locale"))
146 jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
147 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
148 jTextField2.setText(dateformat.format(calendarFin.getTime()));
150 else if (propertychangeevent.getPropertyName().equals("calendar"))
152 calendarFin = (Calendar) propertychangeevent.getNewValue();
153 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
154 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
155 jCalendar2.setCalendar(calendarFin);
161 this.getContentPane().add(jCalendar2, null);
162 this.getContentPane().add(jCalendar1, null);
163 this.getContentPane().add(jLabel5, null);
164 this.getContentPane().add(jButton2, null);
165 this.getContentPane().add(jButton1, null);
166 this.getContentPane().add(jTextField3, null);
167 this.getContentPane().add(jLabel4, null);
168 this.getContentPane().add(jTextField2, null);
169 this.getContentPane().add(jLabel3, null);
170 this.getContentPane().add(jTextField1, null);
171 this.getContentPane().add(jLabel2, null);
172 this.getContentPane().add(jLabel1, null);
173 this.getContentPane().add(jComboBox1, null);
174 lblNewLabel.setBounds(115, 301, 298, 38);
176 getContentPane().add(lblNewLabel);
179 private void jButton1_actionPerformed(ActionEvent e)
181 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
183 // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object
184 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
185 // The next instruction removes the hour, minute, second and ms from the date
186 // This has to be made because the date will be stored in db4o as a java.util.Date object
187 // that would store those data, and that would give problems when comparing dates later
188 firstDay=Date.valueOf(firstDay.toString());
191 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
192 //Remove the hour:minute:second:ms from the date
193 lastDay=Date.valueOf(lastDay.toString());
198 //It could be to trigger an exception if the introduced string is not a number
199 float price= Float.parseFloat(jTextField3.getText());
201 //Obtain the business logic from a StartWindow class (local or remote)
202 OfferManager offerM = new OfferManager();
204 Offer o = offerM.createOffer(ruralHouse, firstDay, lastDay, price);
205 System.out.println("Offer created: "+o.toString());
207 jLabel5.setText("Offer created");
209 } catch (java.lang.NumberFormatException e1) {
210 jLabel5.setText(jTextField3.getText()+ " is not a valid price");
211 } catch (OverlappingOfferExists e1) {
212 jLabel5.setText("There exists an overlapping offer");
214 catch (BadDates e1) {
215 jLabel5.setText("Last day is before first day in the offer");
216 } catch (NullPointerException e1){
217 jLabel5.setText("Bad dates or there exists an overlapping offer");
218 } catch (Exception e1) {
220 e1.printStackTrace();
223 private void jButton2_actionPerformed(ActionEvent e)
225 this.setVisible(false);
228 private void jTextField3_focusLost()
232 new Integer (jTextField3.getText());
235 catch (NumberFormatException ex)
237 jLabel5.setText("Error: Introduce a number");