Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / gui / RequestNewHouseGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.rmi.Naming;
7 import java.rmi.RemoteException;
8
9 import javax.swing.DefaultComboBoxModel;
10 import javax.swing.GroupLayout;
11 import javax.swing.GroupLayout.Alignment;
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.LayoutStyle.ComponentPlacement;
19 import javax.swing.SwingConstants;
20 import javax.swing.border.EmptyBorder;
21
22 import common.AdminInterface;
23
24 import configuration.___IntNames;
25 import domain.Districs;
26 import domain.HouseFeatures;
27 import domain.Owner;
28 import domain.RuralHouse;
29
30 public class RequestNewHouseGUI extends JFrame {
31
32         /**
33          * 
34          */
35         private static final long serialVersionUID = 1L;
36         private JPanel contentPane;
37         private Owner owner;
38         private JLabel lblCode ;
39         private JLabel feedback = new JLabel("");
40         private JTextField Code_f;
41         private JLabel lblTown;
42         private JLabel lblDescription;
43         private JTextField description_f;
44         private JLabel lblKitchen;
45         private JTextField kitchens_f;
46         private JLabel lblRooms;
47         private JTextField rooms_f;
48         private JLabel lblLivings;
49         private JTextField lRooms_f;
50         private JLabel lblParkings;
51         private JTextField parkings_f;
52         private JLabel lblBaths;
53         private JTextField baths_f;
54         private JButton btnRegister;
55         private AdminInterface am = null;
56         private JComboBox<String> comboBox;
57
58
59         /**
60          * Create the frame.
61          */
62         public RequestNewHouseGUI(Owner o) {
63                 comboBox = new JComboBox<String>(new DefaultComboBoxModel<String>(
64                                 Districs.longNames()));
65                 try {
66                         am = (AdminInterface) Naming
67                                         .lookup(___IntNames.AdminManager);
68                 } catch (Exception e1) {
69                         System.out.println("Error accessing remote authentication: "
70                                         + e1.toString());
71                 }
72                 this.setTitle("New House");
73                 setBackground(Color.WHITE);
74                 this.getContentPane().setLayout(null);
75                 owner = o;
76                 setBounds(100, 100, 500, 583);
77                 contentPane = new JPanel();
78                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
79                 setContentPane(contentPane);
80                 
81                 lblCode = new JLabel("House Name:");
82                 lblCode.setHorizontalAlignment(SwingConstants.RIGHT);
83                 
84                 Code_f = new JTextField();
85                 Code_f.setColumns(10);
86                 
87                 lblTown = new JLabel("District:");
88                 lblTown.setHorizontalAlignment(SwingConstants.RIGHT);
89                 
90                 lblDescription = new JLabel("Description(optional):");
91                 lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
92                 
93                 description_f = new JTextField();
94                 description_f.setToolTipText("");
95                 description_f.setHorizontalAlignment(SwingConstants.LEFT);
96                 description_f.setColumns(10);
97                 
98                 lblKitchen = new JLabel("Kitchens:");
99                 lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
100                 
101                 kitchens_f = new JTextField();
102                 kitchens_f.setColumns(10);
103                 
104                 lblRooms = new JLabel("Rooms:");
105                 lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
106                 
107                 rooms_f = new JTextField();
108                 rooms_f.setColumns(10);
109                 
110                 lblLivings = new JLabel("Living rooms:");
111                 lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
112                 
113                 lRooms_f = new JTextField();
114                 lRooms_f.setColumns(10);
115                 
116                 lblParkings = new JLabel("Parkings:");
117                 lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
118                 
119                 parkings_f = new JTextField();
120                 parkings_f.setColumns(10);
121                 
122                 lblBaths = new JLabel("Baths:");
123                 lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
124                 
125                 baths_f = new JTextField();
126                 baths_f.setColumns(10);
127                 
128                 btnRegister = new JButton("Request registration");
129                 btnRegister.addActionListener(new ActionListener() {
130                         public void actionPerformed(ActionEvent arg0) {
131                                 RuralHouse rh = null;
132                                 try {
133                                         //TODO when the house is not added show a warning to the user. Method below returns a boolean stating that.
134                                         rh = new RuralHouse(Code_f.getText(),
135                                                                 owner, description_f.getText(),
136                                                                 (String) comboBox.getSelectedItem(), new HouseFeatures(Integer.parseInt(rooms_f.getText()),
137                                                                                 Integer.parseInt(kitchens_f.getText()),
138                                                                                 Integer.parseInt(baths_f.getText()),
139                                                                                 Integer.parseInt(lRooms_f.getText()),
140                                                                                 Integer.parseInt(parkings_f.getText())) );
141                                 
142                                 }
143                                 catch(NumberFormatException e){
144                                         e.printStackTrace();
145                                 }
146                                 
147                                 try {
148                                         if(am.addAdditionRequest(rh)){
149                                                 am.saveInstance();
150                                                 feedback.setText("Request sended");
151                                         }else{
152                                                 feedback.setText("Request cannot be sended(Already added)");
153                                         }
154                                 } catch (RemoteException e) {
155                                         // TODO Auto-generated catch block
156                                         e.printStackTrace();
157                                 }
158                         }
159                 });
160                 
161                 
162                 
163                 
164                 GroupLayout gl_contentPane = new GroupLayout(contentPane);
165                 gl_contentPane.setHorizontalGroup(
166                         gl_contentPane.createParallelGroup(Alignment.LEADING)
167                                 .addGroup(gl_contentPane.createSequentialGroup()
168                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
169                                                 .addGroup(gl_contentPane.createSequentialGroup()
170                                                         .addContainerGap()
171                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
172                                                                 .addGroup(gl_contentPane.createSequentialGroup()
173                                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
174                                                                                 .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
175                                                                                         .addGroup(gl_contentPane.createSequentialGroup()
176                                                                                                 .addComponent(lblTown, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
177                                                                                                 .addGap(18)
178                                                                                                 .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE))
179                                                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
180                                                                                                 .addGroup(gl_contentPane.createSequentialGroup()
181                                                                                                         .addComponent(lblLivings)
182                                                                                                         .addGap(18)
183                                                                                                         .addComponent(lRooms_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
184                                                                                                 .addGroup(gl_contentPane.createSequentialGroup()
185                                                                                                         .addComponent(lblCode)
186                                                                                                         .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
187                                                                                                         .addComponent(Code_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
188                                                                                 .addGroup(gl_contentPane.createSequentialGroup()
189                                                                                         .addComponent(lblBaths, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
190                                                                                         .addGap(18)
191                                                                                         .addComponent(baths_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
192                                                                         .addGap(29)
193                                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
194                                                                                 .addComponent(lblDescription)
195                                                                                 .addComponent(description_f, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)))
196                                                                 .addGroup(gl_contentPane.createSequentialGroup()
197                                                                         .addComponent(lblKitchen, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
198                                                                         .addGap(18)
199                                                                         .addComponent(kitchens_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
200                                                                 .addGroup(gl_contentPane.createSequentialGroup()
201                                                                         .addComponent(lblRooms, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
202                                                                         .addGap(18)
203                                                                         .addComponent(rooms_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
204                                                 .addGroup(gl_contentPane.createSequentialGroup()
205                                                         .addGap(18)
206                                                         .addComponent(lblParkings, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
207                                                         .addPreferredGap(ComponentPlacement.UNRELATED)
208                                                         .addComponent(parkings_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
209                                                 .addGroup(gl_contentPane.createSequentialGroup()
210                                                         .addGap(140)
211                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
212                                                                 .addComponent(feedback, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
213                                                                 .addComponent(btnRegister))))
214                                         .addContainerGap(97, Short.MAX_VALUE))
215                 );
216                 gl_contentPane.setVerticalGroup(
217                         gl_contentPane.createParallelGroup(Alignment.LEADING)
218                                 .addGroup(gl_contentPane.createSequentialGroup()
219                                         .addGap(20)
220                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
221                                                 .addGroup(gl_contentPane.createSequentialGroup()
222                                                         .addGap(63)
223                                                         .addComponent(lblDescription)
224                                                         .addPreferredGap(ComponentPlacement.RELATED)
225                                                         .addComponent(description_f, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
226                                                 .addGroup(gl_contentPane.createSequentialGroup()
227                                                         .addGap(60)
228                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
229                                                                 .addComponent(lblCode)
230                                                                 .addComponent(Code_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
231                                                         .addPreferredGap(ComponentPlacement.UNRELATED)
232                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
233                                                                 .addComponent(lblTown)
234                                                                 .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
235                                                         .addGap(18)
236                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
237                                                                 .addComponent(lblLivings)
238                                                                 .addComponent(lRooms_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
239                                         .addGap(63)
240                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
241                                                 .addGroup(gl_contentPane.createSequentialGroup()
242                                                         .addGap(3)
243                                                         .addComponent(lblRooms))
244                                                 .addComponent(rooms_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
245                                         .addPreferredGap(ComponentPlacement.RELATED)
246                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
247                                                 .addComponent(kitchens_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
248                                                 .addGroup(gl_contentPane.createSequentialGroup()
249                                                         .addGap(3)
250                                                         .addComponent(lblKitchen)))
251                                         .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
252                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
253                                                 .addGroup(gl_contentPane.createSequentialGroup()
254                                                         .addGap(3)
255                                                         .addComponent(lblBaths))
256                                                 .addComponent(baths_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
257                                         .addGap(45)
258                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
259                                                 .addComponent(lblParkings)
260                                                 .addComponent(parkings_f, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
261                                         .addGap(41)
262                                         .addComponent(feedback, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
263                                         .addPreferredGap(ComponentPlacement.UNRELATED)
264                                         .addComponent(btnRegister)
265                                         .addGap(54))
266                 );
267                 contentPane.setLayout(gl_contentPane);
268         }
269 }