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;
11 import java.rmi.Naming;
13 import java.text.DateFormat;
14 import java.util.Calendar;
15 import java.util.Locale;
16 import java.util.Vector;
18 import javax.swing.JButton;
19 import javax.swing.JComboBox;
20 import javax.swing.JFrame;
21 import javax.swing.JLabel;
22 import javax.swing.JTextField;
24 import com.toedter.calendar.JCalendar;
25 import common.OfferInterface;
27 import configuration.___IntNames;
28 import domain.RuralHouse;
29 import exceptions.BadDates;
30 import exceptions.OverlappingOfferExists;
34 public class AddOffersGUI extends JFrame {
35 private static final long serialVersionUID = 1L;
38 private JComboBox<RuralHouse> jComboBox1;
39 private JLabel jLabel1 = new JLabel();
40 private JLabel jLabel2 = new JLabel();
41 private JTextField jTextField1 = new JTextField();
42 private JLabel jLabel3 = new JLabel();
43 private JTextField jTextField2 = new JTextField();
44 private JLabel jLabel4 = new JLabel();
45 private JTextField jTextField3 = new JTextField();
46 private JButton jButton1 = new JButton();
49 private JCalendar jCalendar1 = new JCalendar();
50 private JCalendar jCalendar2 = new JCalendar();
51 private Calendar calendarInicio = null;
52 private Calendar calendarFin = null;
53 private JButton jButton2 = new JButton();
54 private JLabel jLabel5 = new JLabel();
55 private final JLabel lblNewLabel = new JLabel("");
58 public AddOffersGUI(Vector<RuralHouse> v)
70 private void jbInit(Vector<RuralHouse> v) throws Exception
72 this.getContentPane().setLayout(null);
73 this.setSize(new Dimension(513, 433));
74 this.setTitle("Set availability");
77 jComboBox1 = new JComboBox<RuralHouse>(v);
78 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
79 jLabel1.setText("List of houses:");
80 jLabel1.setBounds(new Rectangle(25, 30, 95, 20));
81 jLabel2.setText("First day :");
82 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
83 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
84 jTextField1.setEditable(false);
85 jLabel3.setText("Last day :");
86 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
87 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
88 jTextField2.setEditable(false);
89 jLabel4.setText("Price:");
90 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
91 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
92 jTextField3.setText("0");
93 jButton1.setText("Accept");
94 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
95 jTextField3.addFocusListener(new FocusListener()
97 public void focusGained(FocusEvent e)
101 public void focusLost(FocusEvent e)
103 jTextField3_focusLost();
106 jButton1.addActionListener(new ActionListener()
108 public void actionPerformed(ActionEvent e)
110 jButton1_actionPerformed(e);
113 jButton2.setText("Cancel");
114 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
115 jButton2.addActionListener(new ActionListener()
117 public void actionPerformed(ActionEvent e)
119 jButton2_actionPerformed(e);
122 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
123 jLabel5.setForeground(Color.red);
124 jLabel5.setSize(new Dimension(305, 20));
125 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
126 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
128 // Code for JCalendar
129 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener()
131 public void propertyChange(PropertyChangeEvent propertychangeevent)
133 if (propertychangeevent.getPropertyName().equals("locale"))
135 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
136 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
137 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
139 else if (propertychangeevent.getPropertyName().equals("calendar"))
141 calendarInicio = (Calendar) propertychangeevent.getNewValue();
142 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
143 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
144 jCalendar1.setCalendar(calendarInicio);
149 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener()
151 public void propertyChange(PropertyChangeEvent propertychangeevent)
153 if (propertychangeevent.getPropertyName().equals("locale"))
155 jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
156 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
157 jTextField2.setText(dateformat.format(calendarFin.getTime()));
159 else if (propertychangeevent.getPropertyName().equals("calendar"))
161 calendarFin = (Calendar) propertychangeevent.getNewValue();
162 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
163 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
164 jCalendar2.setCalendar(calendarFin);
170 this.getContentPane().add(jCalendar2, null);
171 this.getContentPane().add(jCalendar1, null);
172 this.getContentPane().add(jLabel5, null);
173 this.getContentPane().add(jButton2, null);
174 this.getContentPane().add(jButton1, null);
175 this.getContentPane().add(jTextField3, null);
176 this.getContentPane().add(jLabel4, null);
177 this.getContentPane().add(jTextField2, null);
178 this.getContentPane().add(jLabel3, null);
179 this.getContentPane().add(jTextField1, null);
180 this.getContentPane().add(jLabel2, null);
181 this.getContentPane().add(jLabel1, null);
182 this.getContentPane().add(jComboBox1, null);
183 lblNewLabel.setBounds(115, 301, 298, 38);
185 getContentPane().add(lblNewLabel);
188 private void jButton1_actionPerformed(ActionEvent e)
190 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
192 // The next instruction creates a java.sql.Date object from the date selected in the JCalendar object
193 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
194 // The next instruction removes the hour, minute, second and ms from the date
195 // This has to be made because the date will be stored in db4o as a java.util.Date object
196 // that would store those data, and that would give problems when comparing dates later
197 firstDay=Date.valueOf(firstDay.toString());
200 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
201 //Remove the hour:minute:second:ms from the date
202 lastDay=Date.valueOf(lastDay.toString());
207 //It could be to trigger an exception if the introduced string is not a number
208 float price= Float.parseFloat(jTextField3.getText());
210 //Obtain the business logic from a StartWindow class (local or remote)
211 OfferInterface offerM = null;
213 offerM = (OfferInterface) Naming
214 .lookup(___IntNames.OfferManager);
215 } catch (Exception e1) {
216 System.out.println("Error accessing remote authentication: "
220 offerM.createOffer(ruralHouse, firstDay, lastDay, price);
222 jLabel5.setText("Offer created");
224 } catch (java.lang.NumberFormatException e1) {
225 jLabel5.setText(jTextField3.getText()+ " is not a valid price");
226 } catch (OverlappingOfferExists e1) {
227 jLabel5.setText("There exists an overlapping offer");
229 catch (BadDates e1) {
230 jLabel5.setText("Last day is before first day in the offer");
231 } catch (NullPointerException e1){
232 jLabel5.setText("Bad dates or there exists an overlapping offer");
233 } catch (Exception e1) {
235 e1.printStackTrace();
238 private void jButton2_actionPerformed(ActionEvent e)
240 this.setVisible(false);
243 private void jTextField3_focusLost()
247 new Integer (jTextField3.getText());
250 catch (NumberFormatException ex)
252 jLabel5.setText("Error: Introduce a number");