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