4 import java.awt.Dimension;
5 import java.awt.Rectangle;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.FocusListener;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
15 import java.text.DateFormat;
16 import java.util.Calendar;
17 import java.util.Locale;
18 import java.util.Vector;
20 import javax.swing.JButton;
21 import javax.swing.JComboBox;
22 import javax.swing.JFrame;
23 import javax.swing.JLabel;
24 import javax.swing.JTextField;
26 import businessLogic.OfferManager;
28 import com.toedter.calendar.JCalendar;
31 import domain.RuralHouse;
32 import exceptions.BadDates;
34 public class ModifyOfferGUI extends JFrame {
36 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();
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 jLabel1_o = new JLabel();
55 private JComboBox<Offer> comboBox_o;
57 public ModifyOfferGUI(Vector<RuralHouse> v) {
60 } catch (Exception e) {
65 private void jbInit(Vector<RuralHouse> v) throws Exception {
66 this.getContentPane().setLayout(null);
67 this.setSize(new Dimension(513, 433));
68 this.setTitle("Set availability");
70 jComboBox1 = new JComboBox<RuralHouse>(v);
72 comboBox_o = new JComboBox<Offer>(
73 ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
74 DateFormat dateformat1 = DateFormat.getDateInstance(1,
75 jCalendar1.getLocale());
76 if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
78 jTextField1.setText(dateformat1.format(((Offer) comboBox_o
79 .getSelectedItem()).getFirstDay()));
80 jTextField2.setText(dateformat1.format(((Offer) comboBox_o
81 .getSelectedItem()).getLastDay()));
82 jLabel4.setText(Float.toString(((Offer) comboBox_o
83 .getSelectedItem()).getPrice()));
84 jTextField3.setText(Float.toString(((Offer) comboBox_o
85 .getSelectedItem()).getPrice()));
87 jLabel5.setText("There are no offers for the selected rural house");
88 jCalendar1.setEnabled(false);
89 jCalendar2.setEnabled(false);
90 jButton1.setEnabled(false);
91 comboBox_o.setEnabled(false);
92 jTextField3.setEnabled(false);
94 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
95 jLabel1.setText("List of houses:");
96 jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
97 jLabel2.setText("First day :");
98 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
99 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
100 jTextField1.setEditable(false);
101 jLabel3.setText("Last day :");
102 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
103 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
104 jTextField2.setEditable(false);
105 jLabel4.setText("Price:");
106 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
107 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
108 jTextField3.setText("0");
109 jButton1.setText("Accept");
110 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
111 jTextField3.addFocusListener(new FocusListener() {
112 public void focusGained(FocusEvent e) {
115 public void focusLost(FocusEvent e) {
116 jTextField3_focusLost();
120 jComboBox1.addItemListener(new ItemListener() {
123 public void itemStateChanged(ItemEvent arg0) {
124 Vector<Offer> vo = ((RuralHouse) jComboBox1.getSelectedItem()).offers;
125 comboBox_o.removeAllItems();
126 if (!((RuralHouse) jComboBox1.getSelectedItem()).offers
128 jCalendar1.setEnabled(true);
129 jCalendar2.setEnabled(true);
130 jButton1.setEnabled(true);
131 comboBox_o.setEnabled(true);
132 jTextField3.setEnabled(true);
134 for (Offer of : vo) {
135 comboBox_o.addItem(of);
138 jLabel5.setText("There are no offers for the selected rural house");
139 jCalendar1.setEnabled(false);
140 jCalendar2.setEnabled(false);
141 jButton1.setEnabled(false);
142 comboBox_o.setEnabled(false);
143 jTextField3.setEnabled(false);
150 comboBox_o.addItemListener(new ItemListener() {
153 public void itemStateChanged(ItemEvent arg0) {
154 if (arg0.getStateChange() == ItemEvent.SELECTED) {
155 Offer of = (Offer) comboBox_o.getSelectedItem();
156 DateFormat dateformat1 = DateFormat.getDateInstance(1,
157 jCalendar1.getLocale());
158 jTextField1.setText(dateformat1.format(of.getFirstDay()));
159 jTextField2.setText(dateformat1.format(of.getLastDay()));
165 jButton1.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e) {
167 jButton1_actionPerformed(e);
170 jButton2.setText("Cancel");
171 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
172 jButton2.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e) {
174 jButton2_actionPerformed(e);
177 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
178 jLabel5.setForeground(Color.red);
179 jLabel5.setSize(new Dimension(305, 20));
180 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
181 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
183 // Code for JCalendar
184 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
185 public void propertyChange(PropertyChangeEvent propertychangeevent) {
186 if (propertychangeevent.getPropertyName().equals("locale")) {
187 jCalendar1.setLocale((Locale) propertychangeevent
189 DateFormat dateformat = DateFormat.getDateInstance(1,
190 jCalendar1.getLocale());
191 jTextField1.setText(dateformat.format(calendarInicio
193 } else if (propertychangeevent.getPropertyName().equals(
195 calendarInicio = (Calendar) propertychangeevent
197 DateFormat dateformat1 = DateFormat.getDateInstance(1,
198 jCalendar1.getLocale());
199 jTextField1.setText(dateformat1.format(calendarInicio
201 jCalendar1.setCalendar(calendarInicio);
206 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
207 public void propertyChange(PropertyChangeEvent propertychangeevent) {
208 if (propertychangeevent.getPropertyName().equals("locale")) {
209 jCalendar2.setLocale((Locale) propertychangeevent
211 DateFormat dateformat = DateFormat.getDateInstance(1,
212 jCalendar2.getLocale());
213 jTextField2.setText(dateformat.format(calendarFin.getTime()));
214 } else if (propertychangeevent.getPropertyName().equals(
216 calendarFin = (Calendar) propertychangeevent.getNewValue();
217 DateFormat dateformat1 = DateFormat.getDateInstance(1,
218 jCalendar2.getLocale());
219 jTextField2.setText(dateformat1.format(calendarFin
221 jCalendar2.setCalendar(calendarFin);
226 this.getContentPane().add(jCalendar2, null);
227 this.getContentPane().add(jCalendar1, null);
228 this.getContentPane().add(jLabel5, null);
229 this.getContentPane().add(jButton2, null);
230 this.getContentPane().add(jButton1, null);
231 this.getContentPane().add(jTextField3, null);
232 this.getContentPane().add(jLabel4, null);
233 this.getContentPane().add(jTextField2, null);
234 this.getContentPane().add(jLabel3, null);
235 this.getContentPane().add(jTextField1, null);
236 this.getContentPane().add(jLabel2, null);
237 this.getContentPane().add(jLabel1, null);
238 this.getContentPane().add(jComboBox1, null);
239 jLabel1_o.setText("List of offers:");
240 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
241 jLabel1_o.setBounds(25, 44, 95, 20);
243 getContentPane().add(jLabel1_o);
244 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
245 comboBox_o.setBounds(115, 44, 115, 20);
247 getContentPane().add(comboBox_o);
250 private void jButton1_actionPerformed(ActionEvent e) {
251 RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
252 Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
253 // Remove the hour:minute:second:ms from the date
254 firstDay = Date.valueOf(firstDay.toString());
255 Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
256 // Remove the hour:minute:second:ms from the date
257 lastDay = Date.valueOf(lastDay.toString());
262 // It could be to trigger an exception if the introduced string is
264 float price = Float.parseFloat(jTextField3.getText());
266 // Obtain the business logic from a StartWindow class (local or
268 OfferManager offerM = new OfferManager();
269 offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
270 offerM.createOffer(ruralHouse, firstDay, lastDay, price);
272 jLabel5.setText("Offer modified");
274 } catch (java.lang.NumberFormatException e1) {
275 jLabel5.setText(jTextField3.getText() + " is not a valid price");
276 } catch (BadDates e1) {
277 jLabel5.setText("Last day is before first day in the offer");
278 } catch (Exception e1) {
279 e1.printStackTrace();
283 private void jButton2_actionPerformed(ActionEvent e) {
284 this.setVisible(false);
287 private void jTextField3_focusLost() {
289 new Integer(jTextField3.getText());
291 } catch (NumberFormatException ex) {
292 jLabel5.setText("Error: Please introduce a number");