Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / gui / QueryAvailabilityGUI2.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.Rectangle;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.ItemEvent;
10 import java.awt.event.ItemListener;
11 import java.awt.event.MouseAdapter;
12 import java.awt.event.MouseEvent;
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15 import java.rmi.Naming;
16 import java.rmi.RemoteException;
17 import java.sql.Date;
18 import java.text.DateFormat;
19 import java.util.Calendar;
20 import java.util.Locale;
21 import java.util.Vector;
22
23 import javax.swing.ButtonGroup;
24 import javax.swing.DefaultComboBoxModel;
25 import javax.swing.JButton;
26 import javax.swing.JComboBox;
27 import javax.swing.JFrame;
28 import javax.swing.JLabel;
29 import javax.swing.JMenu;
30 import javax.swing.JMenuBar;
31 import javax.swing.JMenuItem;
32 import javax.swing.JPopupMenu;
33 import javax.swing.JRadioButton;
34 import javax.swing.JTextField;
35 import javax.swing.SwingConstants;
36
37 import com.toedter.calendar.JCalendar;
38 import common.HouseInterface;
39
40 import configuration.___IntNames;
41 import domain.Districs;
42 import domain.RuralHouse;
43
44 public class QueryAvailabilityGUI2 extends JFrame {
45         private static final long serialVersionUID = 1L;
46
47         private JComboBox<String> jComboBox1;
48         private JLabel jLabel2 = new JLabel();
49         private JTextField jTextField1 = new JTextField();
50         private JLabel jLabel3 = new JLabel();
51         private JTextField jTextField2 = new JTextField();
52         private JLabel jLabel4 = new JLabel();
53         private JTextField jTextField3 = new JTextField();
54         private JButton jButton1 = new JButton();
55
56         // Code for JCalendar
57         private JCalendar jCalendar1 = new JCalendar();
58         private JCalendar jCalendar2 = new JCalendar();
59         private Calendar calendarInicio = null;
60         private Calendar calendarFin = null;
61         private JButton jButton2 = new JButton();
62         private JLabel jLabel5 = new JLabel();
63         private final JLabel lblNewLabel = new JLabel("");
64         private final JRadioButton ruralHouseName = new JRadioButton(
65                         "Use RuralHouse name");
66         private final JRadioButton district = new JRadioButton("Use District Name");
67         private final ButtonGroup buttonGroup = new ButtonGroup();
68         private final JMenuBar menuBar = new JMenuBar();
69         private final JMenu mnMoreOptions = new JMenu("Search Options");
70         private final JMenuItem PopUpM = new JMenuItem("More Filters");
71         private final JPopupMenu popupMenu = new JPopupMenu();
72         private final JLabel lblNewLabel_1 = new JLabel("Number of bedrooms:");
73         private JTextField nBedrooms;
74         private JTextField nKitchens;
75         private final JLabel lblExtraFeatures = new JLabel("EXTRA FEATURES");
76         private final JLabel lblNumberOfBaths = new JLabel("Number of baths:");
77         private final JTextField nBaths = new JTextField();
78         private final JLabel lblParkings = new JLabel("Parking slots:");
79         private final JTextField nParkings = new JTextField();
80         private HouseInterface houseMan = null;
81         private final JLabel lblNumberOfLivings = new JLabel("Number of Livings:");
82         private final JTextField nLivings = new JTextField();
83         private final JLabel feedback = new JLabel("");
84
85         public QueryAvailabilityGUI2() {
86                 
87                 try {
88                         houseMan =  (HouseInterface) Naming
89                                         .lookup(___IntNames.HouseManager);
90                 } catch (Exception e1) {
91                         System.out.println("Error accessing remote authentication: "
92                                         + e1.toString());
93                 }
94                 nLivings.setText("0");
95                 nLivings.setColumns(10);
96                 nParkings.setText("0");
97                 nParkings.setColumns(10);
98                 nBaths.setText("0");
99                 nBaths.setColumns(10);
100                 try {
101                         jbInit();
102                 } catch (Exception e) {
103                         e.printStackTrace();
104                 }
105         }
106
107         private void jbInit() throws Exception {
108                 this.getContentPane().setLayout(null);
109                 this.setSize(new Dimension(550, 500));
110                 this.setTitle("Query Availability");
111
112                 jComboBox1 = new JComboBox<String>(new DefaultComboBoxModel<String>(
113                                 Districs.longNames()));
114                 jComboBox1.setEnabled(false);
115                 jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
116                 jLabel2.setText("First day :");
117                 jLabel2.setBounds(new Rectangle(20, 134, 85, 25));
118                 jTextField1.setBounds(new Rectangle(20, 350, 220, 25));
119                 jTextField1.setEditable(false);
120                 jLabel3.setText("Last day :");
121                 jLabel3.setBounds(new Rectangle(281, 134, 75, 25));
122                 jTextField2.setBounds(new Rectangle(281, 350, 220, 20));
123                 jTextField2.setEditable(false);
124                 jLabel4.setText("Rural House name:");
125                 jLabel4.setBounds(new Rectangle(275, 30, 128, 20));
126                 jTextField3.setBounds(new Rectangle(413, 30, 115, 20));
127                 jButton1.setText("Search");
128                 jButton1.setBounds(new Rectangle(60, 377, 165, 30));
129                 jButton1.addActionListener(new ActionListener() {
130                         public void actionPerformed(ActionEvent e) {
131                                 jButton1_actionPerformed(e);
132                         }
133                 });
134                 jButton2.setText("Cancel");
135                 jButton2.setBounds(new Rectangle(305, 377, 162, 30));
136                 jButton2.addActionListener(new ActionListener() {
137                         public void actionPerformed(ActionEvent e) {
138                                 jButton2_actionPerformed(e);
139                         }
140                 });
141                 jLabel5.setBounds(new Rectangle(207, 600, 305, 20));
142                 jLabel5.setForeground(Color.red);
143                 jLabel5.setSize(new Dimension(305, 20));
144                 jCalendar1.setBounds(new Rectangle(20, 184, 220, 165));
145                 jCalendar2.setBounds(new Rectangle(281, 184, 220, 165));
146
147                 // Code for JCalendar
148                 this.jCalendar1.addPropertyChangeListener(new PropertyChangeListener() {
149                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
150                                 if (propertychangeevent.getPropertyName().equals("locale")) {
151                                         jCalendar1.setLocale((Locale) propertychangeevent
152                                                         .getNewValue());
153                                         DateFormat dateformat = DateFormat.getDateInstance(1,
154                                                         jCalendar1.getLocale());
155                                         jTextField1.setText(dateformat.format(calendarInicio
156                                                         .getTime()));
157                                 } else if (propertychangeevent.getPropertyName().equals(
158                                                 "calendar")) {
159                                         calendarInicio = (Calendar) propertychangeevent
160                                                         .getNewValue();
161                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
162                                                         jCalendar1.getLocale());
163                                         jTextField1.setText(dateformat1.format(calendarInicio
164                                                         .getTime()));
165                                         jCalendar1.setCalendar(calendarInicio);
166                                 }
167                         }
168                 });
169
170                 this.jCalendar2.addPropertyChangeListener(new PropertyChangeListener() {
171                         public void propertyChange(PropertyChangeEvent propertychangeevent) {
172                                 if (propertychangeevent.getPropertyName().equals("locale")) {
173                                         jCalendar2.setLocale((Locale) propertychangeevent
174                                                         .getNewValue());
175                                         DateFormat dateformat = DateFormat.getDateInstance(1,
176                                                         jCalendar2.getLocale());
177                                         jTextField2.setText(dateformat.format(calendarFin.getTime()));
178                                 } else if (propertychangeevent.getPropertyName().equals(
179                                                 "calendar")) {
180                                         calendarFin = (Calendar) propertychangeevent.getNewValue();
181                                         DateFormat dateformat1 = DateFormat.getDateInstance(1,
182                                                         jCalendar2.getLocale());
183                                         jTextField2.setText(dateformat1.format(calendarFin
184                                                         .getTime()));
185                                         jCalendar2.setCalendar(calendarFin);
186                                 }
187                         }
188                 });
189                 popupMenu.setBounds(75, 125, 58, 16);
190
191                 addPopup(getContentPane(), popupMenu);
192                 lblExtraFeatures.setHorizontalAlignment(SwingConstants.CENTER);
193
194                 popupMenu.add(lblExtraFeatures);
195
196                 popupMenu.add(lblNewLabel_1);
197
198                 nBedrooms = new JTextField();
199                 nBedrooms.setText("0");
200                 popupMenu.add(nBedrooms);
201                 nBedrooms.setColumns(10);
202
203                 JLabel lblNumberOfKitchens = new JLabel("Number of kitchens:");
204                 popupMenu.add(lblNumberOfKitchens);
205
206                 nKitchens = new JTextField();
207                 nKitchens.setText("0");
208                 popupMenu.add(nKitchens);
209                 nKitchens.setColumns(10);
210
211                 popupMenu.add(lblNumberOfBaths);
212
213                 popupMenu.add(nBaths);
214
215                 popupMenu.add(lblParkings);
216
217                 popupMenu.add(nParkings);
218
219                 popupMenu.add(lblNumberOfLivings);
220
221                 popupMenu.add(nLivings);
222
223                 this.getContentPane().add(jCalendar2, null);
224                 this.getContentPane().add(jCalendar1, null);
225                 this.getContentPane().add(jLabel5, null);
226                 this.getContentPane().add(jButton2, null);
227                 this.getContentPane().add(jButton1, null);
228                 this.getContentPane().add(jTextField3, null);
229                 this.getContentPane().add(jLabel4, null);
230                 this.getContentPane().add(jTextField2, null);
231                 this.getContentPane().add(jLabel3, null);
232                 this.getContentPane().add(jTextField1, null);
233                 this.getContentPane().add(jLabel2, null);
234                 this.getContentPane().add(jComboBox1, null);
235                 lblNewLabel.setBounds(115, 301, 298, 38);
236
237                 getContentPane().add(lblNewLabel);
238                 ;
239                 district.setBounds(20, 69, 128, 20);
240                 getContentPane().add(district);
241                 buttonGroup.add(ruralHouseName);
242                 buttonGroup.add(district);
243                 ruralHouseName.setSelected(true);
244                 ruralHouseName.setBounds(276, 60, 177, 38);
245                 district.addItemListener(new ItemListener() {
246                         @Override
247                         public void itemStateChanged(ItemEvent e) {
248                                 int state = e.getStateChange();
249                                 if (state == ItemEvent.SELECTED) {
250                                         jComboBox1.setEnabled(true);
251                                         jCalendar1.setEnabled(false);
252                                         jCalendar2.setEnabled(false);
253                                 } else if (state == ItemEvent.DESELECTED) {
254                                         jComboBox1.setEnabled(false);
255
256                                 }
257                         }
258                 });
259
260                 ruralHouseName.addItemListener(new ItemListener() {
261                         @Override
262                         public void itemStateChanged(ItemEvent e) {
263                                 int state = e.getStateChange();
264                                 if (state == ItemEvent.SELECTED) {
265                                         jTextField3.setEnabled(true);
266                                         jCalendar1.setEnabled(true);
267                                         jCalendar2.setEnabled(true);
268                                 } else if (state == ItemEvent.DESELECTED) {
269                                         jTextField3.setEnabled(false);
270                                 }
271                         }
272                 });
273                 getContentPane().add(ruralHouseName);
274
275                 JLabel lblNewLabel_2 = new JLabel("District name:");
276                 lblNewLabel_2.setBounds(20, 30, 85, 17);
277                 getContentPane().add(lblNewLabel_2);
278                 feedback.setHorizontalAlignment(SwingConstants.CENTER);
279                 feedback.setForeground(Color.RED);
280                 feedback.setBounds(134, 413, 269, 17);
281
282                 getContentPane().add(feedback);
283
284                 setJMenuBar(menuBar);
285
286                 menuBar.add(mnMoreOptions);
287                 PopUpM.addActionListener(new ActionListener() {
288                         public void actionPerformed(ActionEvent arg0) {
289                                 popupMenu.show(QueryAvailabilityGUI2.this, popupMenu.getX(),
290                                                 popupMenu.getY());
291                         }
292                 });
293
294                 mnMoreOptions.add(PopUpM);
295         }
296
297         private void jButton1_actionPerformed(ActionEvent e) {
298                 if (this.district.isSelected()) {
299                         Vector<RuralHouse> houses = null;
300                         try {
301                                 houses = houseMan.getHouses(null, null, jComboBox1
302                                                 .getSelectedItem().toString(), Integer.parseInt(nBedrooms
303                                                 .getText()), Integer.parseInt(nKitchens.getText()), Integer
304                                                 .parseInt(nBaths.getText()), Integer.parseInt(nParkings
305                                                 .getText()), Integer.parseInt(nLivings.getText()));
306                         } catch (NumberFormatException | RemoteException e1) {
307                                 // TODO Auto-generated catch block
308                                 e1.printStackTrace();
309                         }
310                         listOfHousesGUI list = new listOfHousesGUI(houses);
311                         list.setVisible(true);
312                 } else if (this.ruralHouseName.isSelected()) {
313                         try {
314                                 RuralHouse rh = houseMan.getHouses(null, jTextField3.getText(), null,
315                                                 0, 0, 0, 0, 0).get(0);
316                                 // The next instruction creates a java.sql.Date object from the
317                                 // date selected in the JCalendar object
318                                 Date firstDay = new Date(jCalendar1.getCalendar().getTime()
319                                                 .getTime());
320                                 // The next instruction removes the hour, minute, second and ms
321                                 // from the date
322                                 // This has to be made because the date will be stored in db4o
323                                 // as a java.util.Date object
324                                 // that would store those data, and that would give problems
325                                 // when comparing dates later
326                                 firstDay = Date.valueOf(firstDay.toString());
327                                 
328                                 Date lastDay = new Date(jCalendar2.getCalendar().getTime()
329                                                 .getTime());
330                                 // Remove the hour:minute:second:ms from the date
331                                 lastDay = Date.valueOf(lastDay.toString());
332                                 if(firstDay!=null&&lastDay!=null&&firstDay.before(lastDay)){
333                                 HouseFeaturesGUI hou = new HouseFeaturesGUI(rh, firstDay,
334                                                 lastDay);
335                                 hou.setVisible(true);
336                                 }else{
337                                         feedback.setText("Wrong Dates");
338                                 }
339                                 
340                         } catch (Exception e1) {
341                                 feedback.setText("Not matching houses");
342                         }
343                 }
344         }
345
346         private void jButton2_actionPerformed(ActionEvent e) {
347                 this.setVisible(false);
348         }
349
350         private static void addPopup(Component component, final JPopupMenu popup) {
351                 component.addMouseListener(new MouseAdapter() {
352                         public void mousePressed(MouseEvent e) {
353                                 if (e.isPopupTrigger()) {
354                                         showMenu(e);
355                                 }
356                         }
357
358                         public void mouseReleased(MouseEvent e) {
359                                 if (e.isPopupTrigger()) {
360                                         showMenu(e);
361                                 }
362                         }
363
364                         private void showMenu(MouseEvent e) {
365                                 popup.show(e.getComponent(), e.getX(), e.getY());
366                         }
367                 });
368         }
369 }