Bugs when deleting houses and offers fixed and GUI's adapated for empty cases
[RRRRHHHH_Code] / ruralHouses / src / gui / ModifyOfferGUI.java
index b5fc2af..eb6308f 100644 (file)
@@ -29,9 +29,11 @@ import com.toedter.calendar.JCalendar;
 
 import domain.Offer;
 import domain.RuralHouse;
+import exceptions.BadDates;
+import exceptions.OverlappingOfferExists;
+
+public class ModifyOfferGUI extends JFrame {
 
-public class ModifyOfferGUI extends JFrame  {
-       
        private static final long serialVersionUID = 1L;
 
        private JComboBox<RuralHouse> jComboBox1;
@@ -53,11 +55,10 @@ public class ModifyOfferGUI extends JFrame  {
        private final JLabel jLabel1_o = new JLabel();
        private JComboBox<Offer> comboBox_o;
 
-       public ModifyOfferGUI(Vector<RuralHouse> v)     {
-               try     {
+       public ModifyOfferGUI(Vector<RuralHouse> v) {
+               try {
                        jbInit(v);
-               }
-               catch (Exception e) {
+               } catch (Exception e) {
                        e.printStackTrace();
                }
        }
@@ -68,13 +69,29 @@ public class ModifyOfferGUI extends JFrame  {
                this.setTitle("Set availability");
 
                jComboBox1 = new JComboBox<RuralHouse>(v);
-               
-               comboBox_o = new JComboBox<Offer>(((RuralHouse)jComboBox1.getSelectedItem()).getAllOffers());
-               
-               DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
-               jTextField1.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getFirstDay()));
-               jTextField2.setText(dateformat1.format(((Offer) comboBox_o.getSelectedItem()).getLastDay()));
-               
+
+               comboBox_o = new JComboBox<Offer>(
+                               ((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers());
+               DateFormat dateformat1 = DateFormat.getDateInstance(1,
+                               jCalendar1.getLocale());
+               if (!((RuralHouse) jComboBox1.getSelectedItem()).getAllOffers()
+                               .isEmpty()) {
+                       jTextField1.setText(dateformat1.format(((Offer) comboBox_o
+                                       .getSelectedItem()).getFirstDay()));
+                       jTextField2.setText(dateformat1.format(((Offer) comboBox_o
+                                       .getSelectedItem()).getLastDay()));             
+                       jLabel4.setText(Float.toString(((Offer) comboBox_o
+                                       .getSelectedItem()).getPrice()));
+                       jTextField3.setText(Float.toString(((Offer) comboBox_o
+                                       .getSelectedItem()).getPrice()));
+               } else {
+                       jLabel5.setText("There are no offers for the selected rural house");
+                       jCalendar1.setEnabled(false);
+                       jCalendar2.setEnabled(false);
+                       jButton1.setEnabled(false);
+                       comboBox_o.setEnabled(false);
+                       jTextField3.setEnabled(false);
+               }
                jComboBox1.setBounds(new Rectangle(115, 12, 115, 20));
                jLabel1.setText("List of houses:");
                jLabel1.setBounds(new Rectangle(25, 12, 95, 20));
@@ -93,40 +110,59 @@ public class ModifyOfferGUI extends JFrame  {
                jButton1.setText("Accept");
                jButton1.setBounds(new Rectangle(100, 360, 130, 30));
                jTextField3.addFocusListener(new FocusListener() {
-                       public void focusGained(FocusEvent e) {}
+                       public void focusGained(FocusEvent e) {
+                       }
+
                        public void focusLost(FocusEvent e) {
                                jTextField3_focusLost();
                        }
                });
-               
+
                jComboBox1.addItemListener(new ItemListener() {
 
                        @Override
                        public void itemStateChanged(ItemEvent arg0) {
-                               Vector<Offer> vo = ((RuralHouse)jComboBox1.getSelectedItem()).offers;
+                               Vector<Offer> vo = ((RuralHouse) jComboBox1.getSelectedItem()).offers;
                                comboBox_o.removeAllItems();
-                               for (Offer of: vo){
-                                       comboBox_o.addItem(of);
+                               if (!((RuralHouse) jComboBox1.getSelectedItem()).offers
+                                               .isEmpty()) {
+                                       jCalendar1.setEnabled(true);
+                                       jCalendar2.setEnabled(true);
+                                       jButton1.setEnabled(true);
+                                       comboBox_o.setEnabled(true);
+                                       jTextField3.setEnabled(true);
+                                       jLabel5.setText("");
+                                       for (Offer of : vo) {
+                                               comboBox_o.addItem(of);
+                                       }
+                               } else {
+                                       jLabel5.setText("There are no offers for the selected rural house");
+                                       jCalendar1.setEnabled(false);
+                                       jCalendar2.setEnabled(false);
+                                       jButton1.setEnabled(false);
+                                       comboBox_o.setEnabled(false);
+                                       jTextField3.setEnabled(false);
+                                       
                                }
-               
                        }
-                       
+
                });
-               
+
                comboBox_o.addItemListener(new ItemListener() {
 
                        @Override
                        public void itemStateChanged(ItemEvent arg0) {
-                               if(arg0.getStateChange() == ItemEvent.SELECTED){
+                               if (arg0.getStateChange() == ItemEvent.SELECTED) {
                                        Offer of = (Offer) comboBox_o.getSelectedItem();
-                                       DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
+                                       DateFormat dateformat1 = DateFormat.getDateInstance(1,
+                                                       jCalendar1.getLocale());
                                        jTextField1.setText(dateformat1.format(of.getFirstDay()));
                                        jTextField2.setText(dateformat1.format(of.getLastDay()));
                                }
                        }
-                       
+
                });
-               
+
                jButton1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                jButton1_actionPerformed(e);
@@ -149,33 +185,43 @@ public class ModifyOfferGUI extends JFrame  {
                this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
                        public void propertyChange(PropertyChangeEvent propertychangeevent) {
                                if (propertychangeevent.getPropertyName().equals("locale")) {
-                                       jCalendar1.setLocale((Locale) propertychangeevent.getNewValue());
-                                       DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar1.getLocale());
-                                       jTextField1.setText(dateformat.format(calendarInicio.getTime()));
-                               }
-                               else if (propertychangeevent.getPropertyName().equals("calendar")) {
-                                       calendarInicio = (Calendar) propertychangeevent.getNewValue();
-                                       DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar1.getLocale());
-                                       jTextField1.setText(dateformat1.format(calendarInicio.getTime()));
+                                       jCalendar1.setLocale((Locale) propertychangeevent
+                                                       .getNewValue());
+                                       DateFormat dateformat = DateFormat.getDateInstance(1,
+                                                       jCalendar1.getLocale());
+                                       jTextField1.setText(dateformat.format(calendarInicio
+                                                       .getTime()));
+                               } else if (propertychangeevent.getPropertyName().equals(
+                                               "calendar")) {
+                                       calendarInicio = (Calendar) propertychangeevent
+                                                       .getNewValue();
+                                       DateFormat dateformat1 = DateFormat.getDateInstance(1,
+                                                       jCalendar1.getLocale());
+                                       jTextField1.setText(dateformat1.format(calendarInicio
+                                                       .getTime()));
                                        jCalendar1.setCalendar(calendarInicio);
                                }
-                       } 
+                       }
                });
 
                this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
                        public void propertyChange(PropertyChangeEvent propertychangeevent) {
                                if (propertychangeevent.getPropertyName().equals("locale")) {
-                                       jCalendar2.setLocale((Locale) propertychangeevent.getNewValue());
-                                       DateFormat dateformat = DateFormat.getDateInstance(1, jCalendar2.getLocale());
+                                       jCalendar2.setLocale((Locale) propertychangeevent
+                                                       .getNewValue());
+                                       DateFormat dateformat = DateFormat.getDateInstance(1,
+                                                       jCalendar2.getLocale());
                                        jTextField2.setText(dateformat.format(calendarFin.getTime()));
-                               }
-                               else if (propertychangeevent.getPropertyName().equals("calendar")) {
+                               } else if (propertychangeevent.getPropertyName().equals(
+                                               "calendar")) {
                                        calendarFin = (Calendar) propertychangeevent.getNewValue();
-                                       DateFormat dateformat1 = DateFormat.getDateInstance(1, jCalendar2.getLocale());
-                                       jTextField2.setText(dateformat1.format(calendarFin.getTime()));
+                                       DateFormat dateformat1 = DateFormat.getDateInstance(1,
+                                                       jCalendar2.getLocale());
+                                       jTextField2.setText(dateformat1.format(calendarFin
+                                                       .getTime()));
                                        jCalendar2.setCalendar(calendarFin);
                                }
-                       } 
+                       }
                });
 
                this.getContentPane().add(jCalendar2, null);
@@ -194,47 +240,56 @@ public class ModifyOfferGUI extends JFrame  {
                jLabel1_o.setText("List of offers:");
                jLabel1_o.setBounds(new Rectangle(25, 30, 95, 20));
                jLabel1_o.setBounds(25, 44, 95, 20);
-               
+
                getContentPane().add(jLabel1_o);
                comboBox_o.setBounds(new Rectangle(115, 30, 115, 20));
                comboBox_o.setBounds(115, 44, 115, 20);
-               
+
                getContentPane().add(comboBox_o);
        }
 
        private void jButton1_actionPerformed(ActionEvent e) {
-               RuralHouse ruralHouse=((RuralHouse)jComboBox1.getSelectedItem());
-               Date firstDay=new Date(jCalendar1.getCalendar().getTime().getTime());
-               //Remove the hour:minute:second:ms from the date 
-               firstDay=Date.valueOf(firstDay.toString());
-               Date lastDay=new Date(jCalendar2.getCalendar().getTime().getTime());
-               //Remove the hour:minute:second:ms from the date 
-               lastDay=Date.valueOf(lastDay.toString());
-               //It could be to trigger an exception if the introduced string is not a number
-               float price= Float.parseFloat(jTextField3.getText());
+               RuralHouse ruralHouse = ((RuralHouse) jComboBox1.getSelectedItem());
+               Date firstDay = new Date(jCalendar1.getCalendar().getTime().getTime());
+               // Remove the hour:minute:second:ms from the date
+               firstDay = Date.valueOf(firstDay.toString());
+               Date lastDay = new Date(jCalendar2.getCalendar().getTime().getTime());
+               // Remove the hour:minute:second:ms from the date
+               lastDay = Date.valueOf(lastDay.toString());
+               
+
                try {
-                       //Obtain the business logic from a StartWindow class (local or remote)
+
+                       // It could be to trigger an exception if the introduced string is
+                       // not a number
+                       float price = Float.parseFloat(jTextField3.getText());
+
+                       // Obtain the business logic from a StartWindow class (local or
+                       // remote)
                        OfferManager offerM = new OfferManager();
-                       offerM.deleteOffer(ruralHouse, (Offer)comboBox_o.getSelectedItem());
-                       offerM.createOffer(ruralHouse, firstDay, lastDay, price); 
+                       offerM.deleteOffer(ruralHouse, (Offer) comboBox_o.getSelectedItem());
+                       offerM.createOffer(ruralHouse, firstDay, lastDay, price);
 
-                       this.setVisible(false);
-               }
-               catch (Exception e1) {
+                       jLabel5.setText("Offer modified");
+
+               } catch (java.lang.NumberFormatException e1) {
+                       jLabel5.setText(jTextField3.getText() + " is not a valid price");
+               } catch (BadDates e1) {
+                       jLabel5.setText("Last day is before first day in the offer");
+               } catch (Exception e1) {
                        e1.printStackTrace();
                }
        }
-       
+
        private void jButton2_actionPerformed(ActionEvent e) {
                this.setVisible(false);
        }
 
        private void jTextField3_focusLost() {
                try {
-                       new Integer (jTextField3.getText());
+                       new Integer(jTextField3.getText());
                        jLabel5.setText("");
-               }
-               catch (NumberFormatException ex) {
+               } catch (NumberFormatException ex) {
                        jLabel5.setText("Error: Please introduce a number");
                }
        }