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