Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / gui / ModifyHouseGUI.java
index e7bd38d..6b64b19 100644 (file)
@@ -5,19 +5,18 @@ import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 
-import javax.swing.GroupLayout;
-import javax.swing.GroupLayout.Alignment;
+import javax.swing.DefaultComboBoxModel;
 import javax.swing.JButton;
 import javax.swing.JComboBox;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
-import javax.swing.LayoutStyle.ComponentPlacement;
 import javax.swing.SwingConstants;
 import javax.swing.border.EmptyBorder;
 
 import businessLogic.HouseManager;
+import domain.Districs;
 import domain.HouseFeatures;
 import domain.Owner;
 import domain.RuralHouse;
@@ -31,7 +30,6 @@ public class ModifyHouseGUI extends JFrame {
        private JPanel contentPane;
        private Owner owner;
        private JLabel lblDistrict;
-       private JTextField District_f;
        private JLabel feedback;
        private JLabel lblDescription;
        private JTextField description_f;
@@ -44,15 +42,21 @@ public class ModifyHouseGUI extends JFrame {
        private JLabel lblParkings;
        private JTextField parkings_f;
        private JLabel lblBaths;
+       private String[] distric;
        private JTextField baths_f;
        private JButton btnConfirm;
        private JComboBox<RuralHouse> houseBox;
+       private  JComboBox<String> comboBox;
        private RuralHouse rh;
 
        /**
         * Create the frame.
         */
-       public ModifyHouseGUI(Owner o) {
+       public ModifyHouseGUI(final Owner o) {
+               this.distric=Districs.longNames();
+                comboBox = new JComboBox<String>(new DefaultComboBoxModel<String>(
+
+                               this.distric));
                this.getContentPane().setLayout(null);
                owner = o;
                setBounds(100, 100, 500, 583);
@@ -68,10 +72,6 @@ public class ModifyHouseGUI extends JFrame {
                lblDistrict.setBounds(39, 119, 70, 14);
                lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
 
-               District_f = new JTextField();
-               District_f.setBounds(127, 116, 86, 20);
-               District_f.setColumns(10);
-
                lblDescription = new JLabel("Description:");
                lblDescription.setBounds(231, 88, 90, 14);
                lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -126,7 +126,7 @@ public class ModifyHouseGUI extends JFrame {
                        public void actionPerformed(ActionEvent arg0) {
 
                                RuralHouse newRh = new RuralHouse(rh.getHouseName(), owner,
-                                               description_f.getText(), District_f.getText(),
+                                               description_f.getText(), (String)comboBox.getSelectedItem(),
                                                new HouseFeatures(new Integer(rooms_f.getText()),
                                                                new Integer(kitchens_f.getText()), new Integer(
                                                                                baths_f.getText()), new Integer(
@@ -135,6 +135,9 @@ public class ModifyHouseGUI extends JFrame {
                                HouseManager hm = new HouseManager();
 
                                if (hm.registerNewHouse(newRh)) {
+                                       owner.getRuralHouses().remove(rh);
+                                       owner.getRuralHouses().add(newRh);
+                                       houseBox.removeItem(rh);
                                        feedback.setText("House properly modified");
                                } else
                                        feedback.setText("Imposible to modify the house");
@@ -144,8 +147,8 @@ public class ModifyHouseGUI extends JFrame {
 
                houseBox = new JComboBox<RuralHouse>(o.getRuralHouses());
                if (!o.getRuralHouses().isEmpty()) {
-                       rh = (RuralHouse) houseBox.getSelectedItem();
-                       District_f.setText(rh.getDistrict());
+                       rh = (RuralHouse) houseBox.getSelectedItem();                   
+                       comboBox.setSelectedItem(rh.getDistrict());             
                        description_f.setText(rh.getDescription());
                        kitchens_f.setText(Integer
                                        .toString(rh.getFeatures().getnKitchens()));
@@ -157,12 +160,13 @@ public class ModifyHouseGUI extends JFrame {
                        houseBox.setBounds(89, 85, 124, 20);
                }else{
                        feedback.setText("Not available houses");
+                       btnConfirm.setEnabled(false);
                }
                houseBox.addItemListener(new ItemListener() {
                        @Override
                        public void itemStateChanged(ItemEvent e) {
                                rh = (RuralHouse) houseBox.getSelectedItem();
-                               District_f.setText(rh.getDistrict());
+                               comboBox.setSelectedItem(rh.getDistrict());
                                description_f.setText(rh.getDescription());
                                kitchens_f.setText(Integer.toString(rh.getFeatures()
                                                .getnKitchens()));
@@ -185,7 +189,6 @@ public class ModifyHouseGUI extends JFrame {
                contentPane.add(lblBaths);
                contentPane.add(baths_f);
                contentPane.add(lblDistrict);
-               contentPane.add(District_f);
                contentPane.add(lblLivings);
                contentPane.add(lRooms_f);
                contentPane.add(lblKitchen);
@@ -197,5 +200,8 @@ public class ModifyHouseGUI extends JFrame {
                feedback = new JLabel("");
                feedback.setBounds(189, 510, 195, 23);
                contentPane.add(feedback);
+               
+               comboBox.setBounds(127, 116, 86, 20);
+               contentPane.add(comboBox);
        }
 }