106a78bafd5ce2110025f6cbe581431051360aed
[RRRRHHHH_Code] / ruralHouses / src / gui / ModifyHouseGUI.java
1 package gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.event.ItemEvent;
6 import java.awt.event.ItemListener;
7
8
9 import javax.swing.DefaultComboBoxModel;
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JTextField;
16 import javax.swing.SwingConstants;
17 import javax.swing.border.EmptyBorder;
18
19 import businessLogic.HouseManager;
20 import domain.Districs;
21 import domain.HouseFeatures;
22 import domain.Owner;
23 import domain.RuralHouse;
24
25 public class ModifyHouseGUI extends JFrame {
26
27         /**
28          * 
29          */
30         private static final long serialVersionUID = 1L;
31         private JPanel contentPane;
32         private Owner owner;
33         private JLabel lblDistrict;
34         private JLabel feedback;
35         private JLabel lblDescription;
36         private JTextField description_f;
37         private JLabel lblKitchen;
38         private JTextField kitchens_f;
39         private JLabel lblRooms;
40         private JTextField rooms_f;
41         private JLabel lblLivings;
42         private JTextField lRooms_f;
43         private JLabel lblParkings;
44         private JTextField parkings_f;
45         private JLabel lblBaths;
46         private String[] distric;
47         private JTextField baths_f;
48         private JButton btnConfirm;
49         private JComboBox<RuralHouse> houseBox;
50         private RuralHouse rh;
51
52         /**
53          * Create the frame.
54          */
55         public ModifyHouseGUI(Owner o) {
56                 this.distric=Districs.longNames();
57                 JComboBox<String> comboBox = new JComboBox<String>(new DefaultComboBoxModel<String>(
58                                 this.distric));
59                 this.getContentPane().setLayout(null);
60                 owner = o;
61                 setBounds(100, 100, 500, 583);
62                 contentPane = new JPanel();
63                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
64                 setContentPane(contentPane);
65
66                 JLabel lblCode = new JLabel("House Name:");
67                 lblCode.setBounds(15, 88, 64, 14);
68                 lblCode.setHorizontalAlignment(SwingConstants.RIGHT);
69
70                 lblDistrict = new JLabel("District:");
71                 lblDistrict.setBounds(39, 119, 70, 14);
72                 lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
73
74                 lblDescription = new JLabel("Description:");
75                 lblDescription.setBounds(231, 88, 90, 14);
76                 lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
77
78                 description_f = new JTextField();
79                 description_f.setBounds(241, 113, 178, 129);
80                 description_f.setColumns(10);
81
82                 lblKitchen = new JLabel("Kitchens:");
83                 lblKitchen.setBounds(230, 316, 70, 14);
84                 lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
85
86                 kitchens_f = new JTextField();
87                 kitchens_f.setBounds(318, 313, 86, 20);
88                 kitchens_f.setColumns(10);
89
90                 lblRooms = new JLabel("Rooms:");
91                 lblRooms.setBounds(39, 316, 70, 14);
92                 lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
93
94                 rooms_f = new JTextField();
95                 rooms_f.setBounds(127, 313, 86, 20);
96                 rooms_f.setColumns(10);
97
98                 lblLivings = new JLabel("Living rooms:");
99                 lblLivings.setBounds(237, 354, 63, 14);
100                 lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
101
102                 lRooms_f = new JTextField();
103                 lRooms_f.setBounds(318, 351, 86, 20);
104                 lRooms_f.setColumns(10);
105
106                 lblParkings = new JLabel("Parkings:");
107                 lblParkings.setBounds(39, 404, 70, 14);
108                 lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
109
110                 parkings_f = new JTextField();
111                 parkings_f.setBounds(127, 401, 86, 20);
112                 parkings_f.setColumns(10);
113
114                 lblBaths = new JLabel("Baths:");
115                 lblBaths.setBounds(39, 354, 70, 14);
116                 lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
117
118                 baths_f = new JTextField();
119                 baths_f.setBounds(127, 351, 86, 20);
120                 baths_f.setColumns(10);
121
122                 btnConfirm = new JButton("Confirm");
123                 btnConfirm.setBounds(145, 462, 69, 23);
124                 btnConfirm.addActionListener(new ActionListener() {
125                         public void actionPerformed(ActionEvent arg0) {
126
127                                 RuralHouse newRh = new RuralHouse(rh.getHouseName(), owner,
128                                                 description_f.getText(), (String)comboBox.getSelectedItem(),
129                                                 new HouseFeatures(new Integer(rooms_f.getText()),
130                                                                 new Integer(kitchens_f.getText()), new Integer(
131                                                                                 baths_f.getText()), new Integer(
132                                                                                 lRooms_f.getText()), new Integer(
133                                                                                 parkings_f.getText())));
134                                 HouseManager hm = new HouseManager();
135
136                                 if (hm.registerNewHouse(newRh)) {
137                                         o.getRuralHouses().remove(rh);
138                                         o.getRuralHouses().add(newRh);
139                                         houseBox.removeItem(rh);
140                                         feedback.setText("House properly modified");
141                                 } else
142                                         feedback.setText("Imposible to modify the house");
143
144                         }
145                 });
146
147                 houseBox = new JComboBox<RuralHouse>(o.getRuralHouses());
148                 if (!o.getRuralHouses().isEmpty()) {
149                         rh = (RuralHouse) houseBox.getSelectedItem();                   
150                         comboBox.setSelectedItem(rh.getDistrict());             
151                         description_f.setText(rh.getDescription());
152                         kitchens_f.setText(Integer
153                                         .toString(rh.getFeatures().getnKitchens()));
154                         rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
155                         lRooms_f.setText(Integer.toString(rh.getFeatures().getnLivings()));
156                         parkings_f.setText(Integer
157                                         .toString(rh.getFeatures().getnParkings()));
158                         baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
159                         houseBox.setBounds(89, 85, 124, 20);
160                 }else{
161                         feedback.setText("Not available houses");
162                         btnConfirm.setEnabled(false);
163                 }
164                 houseBox.addItemListener(new ItemListener() {
165                         @Override
166                         public void itemStateChanged(ItemEvent e) {
167                                 rh = (RuralHouse) houseBox.getSelectedItem();
168                                 comboBox.setSelectedItem(rh.getDistrict());
169                                 description_f.setText(rh.getDescription());
170                                 kitchens_f.setText(Integer.toString(rh.getFeatures()
171                                                 .getnKitchens()));
172                                 rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
173                                 lRooms_f.setText(Integer.toString(rh.getFeatures()
174                                                 .getnLivings()));
175                                 parkings_f.setText(Integer.toString(rh.getFeatures()
176                                                 .getnParkings()));
177                                 baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
178
179                         }
180                 });
181                 contentPane.setLayout(null);
182                 contentPane.add(lblParkings);
183                 contentPane.add(parkings_f);
184                 contentPane.add(lblRooms);
185                 contentPane.add(rooms_f);
186                 contentPane.add(lblCode);
187                 contentPane.add(houseBox);
188                 contentPane.add(lblBaths);
189                 contentPane.add(baths_f);
190                 contentPane.add(lblDistrict);
191                 contentPane.add(lblLivings);
192                 contentPane.add(lRooms_f);
193                 contentPane.add(lblKitchen);
194                 contentPane.add(kitchens_f);
195                 contentPane.add(description_f);
196                 contentPane.add(lblDescription);
197                 contentPane.add(btnConfirm);
198
199                 feedback = new JLabel("");
200                 feedback.setBounds(189, 510, 195, 23);
201                 contentPane.add(feedback);
202                 
203                 comboBox.setBounds(127, 116, 86, 20);
204                 contentPane.add(comboBox);
205         }
206 }