eb6308f04d67f2fd6934e6a71f5b370486c3674b
[RRRRHHHH_Code] / ruralHouses / src / gui / ModifyOfferGUI.java
1 package gui;
2
3 import java.awt.Color;
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;
14 import java.sql.Date;
15 import java.text.DateFormat;
16 import java.util.Calendar;
17 import java.util.Locale;
18 import java.util.Vector;
19
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;
25
26 import businessLogic.OfferManager;
27
28 import com.toedter.calendar.JCalendar;
29
30 import domain.Offer;
31 import domain.RuralHouse;
32 import exceptions.BadDates;
33 import exceptions.OverlappingOfferExists;
34
35 public class ModifyOfferGUI extends JFrame {
36
37         private static final long serialVersionUID = 1L;
38
39         private JComboBox<RuralHouse> jComboBox1;
40         private JLabel jLabel1 = new JLabel();
41         private JLabel jLabel2 = new JLabel();
42         private JTextField jTextField1 = new JTextField();
43         private JLabel jLabel3 = new JLabel();
44         private JTextField jTextField2 = new JTextField();
45         private JLabel jLabel4 = new JLabel();
46         private JTextField jTextField3 = new JTextField();
47         private JButton jButton1 = new JButton();
48         // Code for JCalendar
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 jLabel1_o = new JLabel();
56         private JComboBox<Offer> comboBox_o;
57
58         public ModifyOfferGUI(Vector<RuralHouse> v) {
59                 try {
60                         jbInit(v);
61                 } catch (Exception e) {
62                         e.printStackTrace();
63                 }
64         }
65
66         private void jbInit(Vector<RuralHouse> v) throws Exception {
67                 this.getContentPane().setLayout(null);
68                 this.setSize(new Dimension(513, 433));
69                 this.setTitle("Set availability");
70
71                 jComboBox1 = new JComboBox<RuralHouse>(v);
72
73                 comboBox_o = new JComboBox<Offer>(
74                                 ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
75                 DateFormat dateformat1 = DateFormat.getDateInstance(1,
76                                 jCalendar1.getLocale());
77                 if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
78                                 .isEmpty()) {
79                         jTextField1.setText(dateformat1.format(((Offer) comboBox_o
80                                         .getSelectedItem()).getFirstDay()));
81                         jTextField2.setText(dateformat1.format(((Offer) comboBox_o
82                                         .getSelectedItem()).getLastDay()));             
83                         jLabel4.setText(Float.toString(((Offer) comboBox_o
84                                         .getSelectedItem()).getPrice()));
85                         jTextField3.setText(Float.toString(((Offer) comboBox_o
86                                         .getSelectedItem()).getPrice()));
87                 } else {
88                         jLabel5.setText("There are no offers for the selected rural house");
89                         jCalendar1.setEnabled(false);
90                         jCalendar2.setEnabled(false);
91                         jButton1.setEnabled(false);
92                         comboBox_o.setEnabled(false);
93                         jTextField3.setEnabled(false);
94                 }
95                 jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
96                 jLabel1.setText("List of houses:");
97                 jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
98                 jLabel2.setText("First day :");
99                 jLabel2.setBounds(new Rectangle(25, 75, 85, 25));
100                 jTextField1.setBounds(new Rectangle(25, 265, 220, 25));
101                 jTextField1.setEditable(false);
102                 jLabel3.setText("Last day :");
103                 jLabel3.setBounds(new Rectangle(260, 75, 75, 25));
104                 jTextField2.setBounds(new Rectangle(260, 265, 220, 25));
105                 jTextField2.setEditable(false);
106                 jLabel4.setText("Price:");
107                 jLabel4.setBounds(new Rectangle(260, 30, 75, 20));
108                 jTextField3.setBounds(new Rectangle(350, 30, 115, 20));
109                 jTextField3.setText("0");
110                 jButton1.setText("Accept");
111                 jButton1.setBounds(new Rectangle(100, 360, 130, 30));
112                 jTextField3.addFocusListener(new FocusListener() {
113                         public void focusGained(FocusEvent e) {
114                         }
115
116                         public void focusLost(FocusEvent e) {
117                                 jTextField3_focusLost();
118                         }
119                 });
120
121                 jComboBox1.addItemListener(new ItemListener() {
122
123                         @Override
124                         public void itemStateChanged(ItemEvent arg0) {
125                                 Vector<Offer> vo = ((RuralHouse) jComboBox1.getSelectedItem()).offers;
126                                 comboBox_o.removeAllItems();
127                                 if (!((RuralHouse) jComboBox1.getSelectedItem()).offers
128                                                 .isEmpty()) {
129                                         jCalendar1.setEnabled(true);
130                                         jCalendar2.setEnabled(true);
131                                         jButton1.setEnabled(true);
132                                         comboBox_o.setEnabled(true);
133                                         jTextField3.setEnabled(true);
134                                         jLabel5.setText("");
135                                         for (Offer of : vo) {
136                                                 comboBox_o.addItem(of);
137                                         }
138                                 } else {
139                                         jLabel5.setText("There are no offers for the selected rural house");
140                                         jCalendar1.setEnabled(false);
141                                         jCalendar2.setEnabled(false);
142                                         jButton1.setEnabled(false);
143                                         comboBox_o.setEnabled(false);
144                                         jTextField3.setEnabled(false);
145                                         
146                                 }
147                         }
148
149                 });
150
151                 comboBox_o.addItemListener(new ItemListener() {
152
153                         @Override
154                         public void itemStateChanged(ItemEvent arg0) {
155                                 if (arg0.getStateChange() == ItemEvent.SELECTED) {
156                                         Offer of = (Offer) comboBox_o.getSelectedItem();
157                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
158                                                         jCalendar1.getLocale());
159                                         jTextField1.setText(dateformat1.format(of.getFirstDay()));
160                                         jTextField2.setText(dateformat1.format(of.getLastDay()));
161                                 }
162                         }
163
164                 });
165
166                 jButton1.addActionListener(new ActionListener() {
167                         public void actionPerformed(ActionEvent e) {
168                                 jButton1_actionPerformed(e);
169                         }
170                 });
171                 jButton2.setText("Cancel");
172                 jButton2.setBounds(new Rectangle(270, 360, 130, 30));
173                 jButton2.addActionListener(new ActionListener() {
174                         public void actionPerformed(ActionEvent e) {
175                                 jButton2_actionPerformed(e);
176                         }
177                 });
178                 jLabel5.setBounds(new Rectangle(100, 320, 300, 20));
179                 jLabel5.setForeground(Color.red);
180                 jLabel5.setSize(new Dimension(305, 20));
181                 jCalendar1.setBounds(new Rectangle(25, 100, 220, 165));
182                 jCalendar2.setBounds(new Rectangle(260, 100, 220, 165));
183
184                 // Code for JCalendar
185                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
186                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
187                                 if (propertychangeevent.getPropertyName().equals("locale")) {
188                                         jCalendar1.setLocale((Locale) propertychangeevent
189                                                         .getNewValue());
190                                         DateFormat dateformat = DateFormat.getDateInstance(1,
191                                                         jCalendar1.getLocale());
192                                         jTextField1.setText(dateformat.format(calendarInicio
193                                                         .getTime()));
194                                 } else if (propertychangeevent.getPropertyName().equals(
195                                                 "calendar")) {
196                                         calendarInicio = (Calendar) propertychangeevent
197                                                         .getNewValue();
198                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
199                                                         jCalendar1.getLocale());
200                                         jTextField1.setText(dateformat1.format(calendarInicio
201                                                         .getTime()));
202                                         jCalendar1.setCalendar(calendarInicio);
203                                 }
204                         }
205                 });
206
207                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
208                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
209                                 if (propertychangeevent.getPropertyName().equals("locale")) {
210                                         jCalendar2.setLocale((Locale) propertychangeevent
211                                                         .getNewValue());
212                                         DateFormat dateformat = DateFormat.getDateInstance(1,
213                                                         jCalendar2.getLocale());
214                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
215                                 } else if (propertychangeevent.getPropertyName().equals(
216                                                 "calendar")) {
217                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
218                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
219                                                         jCalendar2.getLocale());
220                                         jTextField2.setText(dateformat1.format(calendarFin
221                                                         .getTime()));
222                                         jCalendar2.setCalendar(calendarFin);
223                                 }
224                         }
225                 });
226
227                 this.getContentPane().add(jCalendar2, null);
228                 this.getContentPane().add(jCalendar1, null);
229                 this.getContentPane().add(jLabel5, null);
230                 this.getContentPane().add(jButton2, null);
231                 this.getContentPane().add(jButton1, null);
232                 this.getContentPane().add(jTextField3, null);
233                 this.getContentPane().add(jLabel4, null);
234                 this.getContentPane().add(jTextField2, null);
235                 this.getContentPane().add(jLabel3, null);
236                 this.getContentPane().add(jTextField1, null);
237                 this.getContentPane().add(jLabel2, null);
238                 this.getContentPane().add(jLabel1, null);
239                 this.getContentPane().add(jComboBox1, null);
240                 jLabel1_o.setText("List of offers:");
241                 jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
242                 jLabel1_o.setBounds(25, 44, 95, 20);
243
244                 getContentPane().add(jLabel1_o);
245                 comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
246                 comboBox_o.setBounds(115, 44, 115, 20);
247
248                 getContentPane().add(comboBox_o);
249         }
250
251         private void jButton1_actionPerformed(ActionEvent e) {
252                 RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
253                 Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
254                 // Remove the hour:minute:second:ms from the date
255                 firstDay = Date.valueOf(firstDay.toString());
256                 Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
257                 // Remove the hour:minute:second:ms from the date
258                 lastDay = Date.valueOf(lastDay.toString());
259                 
260
261                 try {
262
263                         // It could be to trigger an exception if the introduced string is
264                         // not a number
265                         float price = Float.parseFloat(jTextField3.getText());
266
267                         // Obtain the business logic from a StartWindow class (local or
268                         // remote)
269                         OfferManager offerM = new OfferManager();
270                         offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
271                         offerM.createOffer(ruralHouse, firstDay, lastDay, price);
272
273                         jLabel5.setText("Offer modified");
274
275                 } catch (java.lang.NumberFormatException e1) {
276                         jLabel5.setText(jTextField3.getText() + " is not a valid price");
277                 } catch (BadDates e1) {
278                         jLabel5.setText("Last day is before first day in the offer");
279                 } catch (Exception e1) {
280                         e1.printStackTrace();
281                 }
282         }
283
284         private void jButton2_actionPerformed(ActionEvent e) {
285                 this.setVisible(false);
286         }
287
288         private void jTextField3_focusLost() {
289                 try {
290                         new Integer(jTextField3.getText());
291                         jLabel5.setText("");
292                 } catch (NumberFormatException ex) {
293                         jLabel5.setText("Error: Please introduce a number");
294                 }
295         }
296 }