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