Merged the two branches, DB separated
[RRRRHHHH_Code] / ruralHouses / src / gui / QueryAvailabilityGUI2.java
index d95dcfe..6f4358e 100644 (file)
@@ -1,29 +1,50 @@
 package gui;
 
-import java.beans.*;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Rectangle;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.rmi.Naming;
+import java.rmi.RemoteException;
 import java.sql.Date;
 import java.text.DateFormat;
-import java.util.*;
+import java.util.Calendar;
+import java.util.Locale;
+import java.util.Vector;
 
-import javax.swing.*;
-
-import java.awt.*;
-import java.awt.event.*;
-
-import businessLogic.HouseManager;
-import businessLogic.OfferManager;
+import javax.swing.ButtonGroup;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
+import javax.swing.JRadioButton;
+import javax.swing.JTextField;
+import javax.swing.SwingConstants;
 
 import com.toedter.calendar.JCalendar;
+import common.HouseInterface;
 
-import domain.Offer;
+import configuration.___IntNames;
+import domain.Districs;
 import domain.RuralHouse;
-import exceptions.OverlappingOfferExists;
-import exceptions.BadDates;
 
 public class QueryAvailabilityGUI2 extends JFrame {
        private static final long serialVersionUID = 1L;
 
-       private JComboBox jComboBox1;
+       private JComboBox<String> jComboBox1;
        private JLabel jLabel2 = new JLabel();
        private JTextField jTextField1 = new JTextField();
        private JLabel jLabel3 = new JLabel();
@@ -42,7 +63,7 @@ public class QueryAvailabilityGUI2 extends JFrame {
        private final JLabel lblNewLabel = new JLabel("");
        private final JRadioButton ruralHouseName = new JRadioButton(
                        "Use RuralHouse name");
-       private final   JRadioButton district = new JRadioButton("Use district name");
+       private final JRadioButton district = new JRadioButton("Use District Name");
        private final ButtonGroup buttonGroup = new ButtonGroup();
        private final JMenuBar menuBar = new JMenuBar();
        private final JMenu mnMoreOptions = new JMenu("Search Options");
@@ -56,11 +77,25 @@ public class QueryAvailabilityGUI2 extends JFrame {
        private final JTextField nBaths = new JTextField();
        private final JLabel lblParkings = new JLabel("Parking slots:");
        private final JTextField nParkings = new JTextField();
-       private HouseManager houseMan = new HouseManager();
+       private HouseInterface houseMan = null;
+       private final JLabel lblNumberOfLivings = new JLabel("Number of Livings:");
+       private final JTextField nLivings = new JTextField();
+       private final JLabel feedback = new JLabel("");
 
        public QueryAvailabilityGUI2() {
-               nParkings.setText("");
+               
+               try {
+                       houseMan =  (HouseInterface) Naming
+                                       .lookup(___IntNames.HouseManager);
+               } catch (Exception e1) {
+                       System.out.println("Error accessing remote authentication: "
+                                       + e1.toString());
+               }
+               nLivings.setText("0");
+               nLivings.setColumns(10);
+               nParkings.setText("0");
                nParkings.setColumns(10);
+               nBaths.setText("0");
                nBaths.setColumns(10);
                try {
                        jbInit();
@@ -72,10 +107,11 @@ public class QueryAvailabilityGUI2 extends JFrame {
        private void jbInit() throws Exception {
                this.getContentPane().setLayout(null);
                this.setSize(new Dimension(550, 500));
-               this.setTitle("Set availability");
+               this.setTitle("Query Availability");
 
-               //TODO here we should add the districts of villatripas this is just for testing purposes
-               jComboBox1 = new JComboBox(this.houseMan.getAllRuralHouses());
+               jComboBox1 = new JComboBox<String>(new DefaultComboBoxModel<String>(
+                               Districs.longNames()));
+               jComboBox1.setEnabled(false);
                jComboBox1.setBounds(new Rectangle(115, 30, 115, 20));
                jLabel2.setText("First day :");
                jLabel2.setBounds(new Rectangle(20, 134, 85, 25));
@@ -87,9 +123,8 @@ public class QueryAvailabilityGUI2 extends JFrame {
                jTextField2.setEditable(false);
                jLabel4.setText("Rural House name:");
                jLabel4.setBounds(new Rectangle(275, 30, 128, 20));
-               jTextField3.setEnabled(false);
                jTextField3.setBounds(new Rectangle(413, 30, 115, 20));
-               jButton1.setText("Accept");
+               jButton1.setText("Search");
                jButton1.setBounds(new Rectangle(60, 377, 165, 30));
                jButton1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
@@ -152,33 +187,39 @@ public class QueryAvailabilityGUI2 extends JFrame {
                        }
                });
                popupMenu.setBounds(75, 125, 58, 16);
-               
+
                addPopup(getContentPane(), popupMenu);
                lblExtraFeatures.setHorizontalAlignment(SwingConstants.CENTER);
-               
+
                popupMenu.add(lblExtraFeatures);
-               
+
                popupMenu.add(lblNewLabel_1);
-               
+
                nBedrooms = new JTextField();
+               nBedrooms.setText("0");
                popupMenu.add(nBedrooms);
                nBedrooms.setColumns(10);
-               
+
                JLabel lblNumberOfKitchens = new JLabel("Number of kitchens:");
                popupMenu.add(lblNumberOfKitchens);
-               
+
                nKitchens = new JTextField();
+               nKitchens.setText("0");
                popupMenu.add(nKitchens);
                nKitchens.setColumns(10);
-               
+
                popupMenu.add(lblNumberOfBaths);
-               
+
                popupMenu.add(nBaths);
-               
+
                popupMenu.add(lblParkings);
-               
+
                popupMenu.add(nParkings);
 
+               popupMenu.add(lblNumberOfLivings);
+
+               popupMenu.add(nLivings);
+
                this.getContentPane().add(jCalendar2, null);
                this.getContentPane().add(jCalendar1, null);
                this.getContentPane().add(jLabel5, null);
@@ -194,12 +235,12 @@ public class QueryAvailabilityGUI2 extends JFrame {
                lblNewLabel.setBounds(115, 301, 298, 38);
 
                getContentPane().add(lblNewLabel);
-
-               district.setSelected(true);;
+               ;
                district.setBounds(20, 69, 128, 20);
                getContentPane().add(district);
                buttonGroup.add(ruralHouseName);
                buttonGroup.add(district);
+               ruralHouseName.setSelected(true);
                ruralHouseName.setBounds(276, 60, 177, 38);
                district.addItemListener(new ItemListener() {
                        @Override
@@ -207,53 +248,105 @@ public class QueryAvailabilityGUI2 extends JFrame {
                                int state = e.getStateChange();
                                if (state == ItemEvent.SELECTED) {
                                        jComboBox1.setEnabled(true);
+                                       jCalendar1.setEnabled(false);
+                                       jCalendar2.setEnabled(false);
                                } else if (state == ItemEvent.DESELECTED) {
                                        jComboBox1.setEnabled(false);
+
                                }
                        }
                });
-               
+
                ruralHouseName.addItemListener(new ItemListener() {
                        @Override
                        public void itemStateChanged(ItemEvent e) {
                                int state = e.getStateChange();
                                if (state == ItemEvent.SELECTED) {
                                        jTextField3.setEnabled(true);
+                                       jCalendar1.setEnabled(true);
+                                       jCalendar2.setEnabled(true);
                                } else if (state == ItemEvent.DESELECTED) {
                                        jTextField3.setEnabled(false);
                                }
                        }
                });
                getContentPane().add(ruralHouseName);
-               
+
                JLabel lblNewLabel_2 = new JLabel("District name:");
                lblNewLabel_2.setBounds(20, 30, 85, 17);
                getContentPane().add(lblNewLabel_2);
-               
+               feedback.setHorizontalAlignment(SwingConstants.CENTER);
+               feedback.setForeground(Color.RED);
+               feedback.setBounds(134, 413, 269, 17);
+
+               getContentPane().add(feedback);
+
                setJMenuBar(menuBar);
-               
+
                menuBar.add(mnMoreOptions);
                PopUpM.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
-                               popupMenu.show(QueryAvailabilityGUI2.this,popupMenu.getX(), popupMenu.getY());
+                               popupMenu.show(QueryAvailabilityGUI2.this, popupMenu.getX(),
+                                               popupMenu.getY());
                        }
                });
-               
+
                mnMoreOptions.add(PopUpM);
        }
 
        private void jButton1_actionPerformed(ActionEvent e) {
-               if(this.district.isSelected()){
-                       //show list of houses GUI
-               }
-               else if(this.ruralHouseName.isSelected()){
-                       //show features of specific house
+               if (this.district.isSelected()) {
+                       Vector<RuralHouse> houses = null;
+                       try {
+                               houses = houseMan.getHouses(null, null, jComboBox1
+                                               .getSelectedItem().toString(), Integer.parseInt(nBedrooms
+                                               .getText()), Integer.parseInt(nKitchens.getText()), Integer
+                                               .parseInt(nBaths.getText()), Integer.parseInt(nParkings
+                                               .getText()), Integer.parseInt(nLivings.getText()));
+                       } catch (NumberFormatException | RemoteException e1) {
+                               // TODO Auto-generated catch block
+                               e1.printStackTrace();
+                       }
+                       listOfHousesGUI list = new listOfHousesGUI(houses);
+                       list.setVisible(true);
+               } else if (this.ruralHouseName.isSelected()) {
+                       try {
+                               RuralHouse rh = houseMan.getHouses(null, jTextField3.getText(), null,
+                                               0, 0, 0, 0, 0).get(0);
+                               // The next instruction creates a java.sql.Date object from the
+                               // date selected in the JCalendar object
+                               Date firstDay = new Date(jCalendar1.getCalendar().getTime()
+                                               .getTime());
+                               // The next instruction removes the hour, minute, second and ms
+                               // from the date
+                               // This has to be made because the date will be stored in db4o
+                               // as a java.util.Date object
+                               // that would store those data, and that would give problems
+                               // when comparing dates later
+                               firstDay = Date.valueOf(firstDay.toString());
+                               
+                               Date lastDay = new Date(jCalendar2.getCalendar().getTime()
+                                               .getTime());
+                               // Remove the hour:minute:second:ms from the date
+                               lastDay = Date.valueOf(lastDay.toString());
+                               if(firstDay!=null&&lastDay!=null&&firstDay.before(lastDay)){
+                               HouseFeaturesGUI hou = new HouseFeaturesGUI(rh, firstDay,
+                                               lastDay);
+                               hou.setVisible(true);
+                               }else{
+                                       feedback.setText("Wrong Dates");
+                               }
+                               
+                       } catch (Exception e1) {
+                               feedback.setText("Not matching houses");
+                       }
                }
        }
 
        private void jButton2_actionPerformed(ActionEvent e) {
                this.setVisible(false);
        }
+
        private static void addPopup(Component component, final JPopupMenu popup) {
                component.addMouseListener(new MouseAdapter() {
                        public void mousePressed(MouseEvent e) {
@@ -261,11 +354,13 @@ public class QueryAvailabilityGUI2 extends JFrame {
                                        showMenu(e);
                                }
                        }
+
                        public void mouseReleased(MouseEvent e) {
                                if (e.isPopupTrigger()) {
                                        showMenu(e);
                                }
                        }
+
                        private void showMenu(MouseEvent e) {
                                popup.show(e.getComponent(), e.getX(), e.getY());
                        }