5 import java.text.DateFormat;
11 import java.awt.event.*;
13 import businessLogic.OfferManager;
15 import com.toedter.calendar.JCalendar;
18 import domain.RuralHouse;
20 public class ModifyOfferGUI extends JFrame {
22 private static final long serialVersionUID = 1L;
24 private JComboBox<RuralHouse> jComboBox1;
25 private JLabel jLabel1 = new JLabel();
26 private JLabel jLabel2 = new JLabel();
27 private JTextField jTextField1 = new JTextField();
28 private JLabel jLabel3 = new JLabel();
29 private JTextField jTextField2 = new JTextField();
30 private JLabel jLabel4 = new JLabel();
31 private JTextField jTextField3 = new JTextField();
32 private JButton jButton1 = new JButton();
34 private JCalendar jCalendar1 = new JCalendar();
35 private JCalendar jCalendar2 = new JCalendar();
36 private Calendar calendarInicio = null;
37 private Calendar calendarFin = null;
38 private JButton jButton2 = new JButton();
39 private JLabel jLabel5 = new JLabel();
40 private final JLabel jLabel1_o = new JLabel();
41 private JComboBox<Offer> comboBox_o;
43 public ModifyOfferGUI(Vector<RuralHouse> v) {
52 private void jbInit(Vector<RuralHouse> v) throws Exception {
53 this.getContentPane().setLayout(null);
54 this.setSize(new Dimension(513, 433));
55 this.setTitle("Set availability");
57 jComboBox1 = new JComboBox<RuralHouse>(v);
59 comboBox_o = new JComboBox<Offer>(((RuralHouse)jComboBox1.getSelectedItem()).getAllOffers());
61 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
62 jTextField1.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getFirstDay()));
63 jTextField2.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getLastDay()));
65 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
66 jLabel1.setText("List of houses:");
67 jLabel1.setBounds(new Rectangle(25, 12, 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() {
83 public void focusGained(FocusEvent e) {}
84 public void focusLost(FocusEvent e) {
85 jTextField3_focusLost();
89 jComboBox1.addItemListener(new ItemListener() {
92 public void itemStateChanged(ItemEvent arg0) {
93 Vector<Offer> vo = ((RuralHouse)jComboBox1.getSelectedItem()).offers;
94 comboBox_o.removeAllItems();
96 comboBox_o.addItem(of);
103 comboBox_o.addItemListener(new ItemListener() {
106 public void itemStateChanged(ItemEvent arg0) {
107 if(arg0.getStateChange() == ItemEvent.SELECTED){
108 Offer of = (Offer) comboBox_o.getSelectedItem();
109 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
110 jTextField1.setText(dateformat1.format(of.getFirstDay()));
111 jTextField2.setText(dateformat1.format(of.getLastDay()));
117 jButton1.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 jButton1_actionPerformed(e);
122 jButton2.setText("Cancel");
123 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
124 jButton2.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 jButton2_actionPerformed(e);
129 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
130 jLabel5.setForeground(Color.red);
131 jLabel5.setSize(new Dimension(305, 20));
132 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
133 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
135 // Code for JCalendar
136 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
137 public void propertyChange(PropertyChangeEvent propertychangeevent) {
138 if (propertychangeevent.getPropertyName().equals("locale")) {
139 jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
140 DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
141 jTextField1.setText(dateformat.format(calendarInicio.getTime()));
143 else if (propertychangeevent.getPropertyName().equals("calendar")) {
144 calendarInicio = (Calendar) propertychangeevent.getNewValue();
145 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
146 jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
147 jCalendar1.setCalendar(calendarInicio);
152 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
153 public void propertyChange(PropertyChangeEvent propertychangeevent) {
154 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")) {
160 calendarFin = (Calendar) propertychangeevent.getNewValue();
161 DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
162 jTextField2.setText(dateformat1.format(calendarFin.getTime()));
163 jCalendar2.setCalendar(calendarFin);
168 this.getContentPane().add(jCalendar2, null);
169 this.getContentPane().add(jCalendar1, null);
170 this.getContentPane().add(jLabel5, null);
171 this.getContentPane().add(jButton2, null);
172 this.getContentPane().add(jButton1, null);
173 this.getContentPane().add(jTextField3, null);
174 this.getContentPane().add(jLabel4, null);
175 this.getContentPane().add(jTextField2, null);
176 this.getContentPane().add(jLabel3, null);
177 this.getContentPane().add(jTextField1, null);
178 this.getContentPane().add(jLabel2, null);
179 this.getContentPane().add(jLabel1, null);
180 this.getContentPane().add(jComboBox1, null);
181 jLabel1_o.setText("List of offers:");
182 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
183 jLabel1_o.setBounds(25, 44, 95, 20);
185 getContentPane().add(jLabel1_o);
186 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
187 comboBox_o.setBounds(115, 44, 115, 20);
189 getContentPane().add(comboBox_o);
192 private void jButton1_actionPerformed(ActionEvent e) {
193 RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
194 Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
195 //Remove the hour:minute:second:ms from the date
196 firstDay=Date.valueOf(firstDay.toString());
197 Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
198 //Remove the hour:minute:second:ms from the date
199 lastDay=Date.valueOf(lastDay.toString());
200 //It could be to trigger an exception if the introduced string is not a number
201 float price= Float.parseFloat(jTextField3.getText());
203 //Obtain the business logic from a StartWindow class (local or remote)
204 OfferManager offerM = new OfferManager();
205 offerM.deleteOffer(ruralHouse, (Offer)comboBox_o.getSelectedItem());
206 offerM.createOffer(ruralHouse, firstDay, lastDay, price);
208 this.setVisible(false);
210 catch (Exception e1) {
211 e1.printStackTrace();
215 private void jButton2_actionPerformed(ActionEvent e) {
216 this.setVisible(false);
219 private void jTextField3_focusLost() {
221 new Integer (jTextField3.getText());
224 catch (NumberFormatException ex) {
225 jLabel5.setText("Error: Please introduce a number");