Merge branch 'master' of https://xp-dev.com/git/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses / src / gui / listOfHousesGUI.java
diff --git a/ruralHouses/src/gui/listOfHousesGUI.java b/ruralHouses/src/gui/listOfHousesGUI.java
new file mode 100644 (file)
index 0000000..c144ccc
--- /dev/null
@@ -0,0 +1,100 @@
+package gui;
+
+import java.awt.Font;
+import java.awt.Rectangle;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.Enumeration;
+import java.util.Vector;
+
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.border.EmptyBorder;
+import javax.swing.table.DefaultTableModel;
+
+import domain.RuralHouse;
+
+public class listOfHousesGUI extends JFrame {
+
+       /**
+        * 
+        */
+       private static final long serialVersionUID = 1L;
+       private JPanel contentPane;
+       private JTable table;
+       private DefaultTableModel tableModel;
+       private Vector<RuralHouse> houses;
+       /**
+        * Create the frame.
+        */
+       public listOfHousesGUI(Vector<RuralHouse> rhs) {
+               try {
+                       this.houses=rhs;
+                       init();
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+       }
+
+       private void init() throws Exception {
+               setBounds(100, 100, 600, 450);
+               contentPane = new JPanel();
+               contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
+               setContentPane(contentPane);
+               contentPane.setLayout(null);
+
+               JLabel lblNewLabel = new JLabel();
+               lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
+               lblNewLabel.setBounds(23, 41, 536, 33);
+               contentPane.add(lblNewLabel);
+               if (houses.isEmpty())
+                       lblNewLabel.setText("There are not houses matching your search");
+               else
+                       lblNewLabel.setText("List of houses that match your search:");
+               JScrollPane scrollPane = new JScrollPane();
+               scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
+               scrollPane.setBounds(23, 113, 536, 271);
+               contentPane.add(scrollPane);
+
+               table = new JTable() {
+               private static final long serialVersionUID = 1L;
+
+               public boolean isCellEditable(int row, int column) {                
+                       return false;               
+               };
+           };
+               scrollPane.setViewportView(table);
+               tableModel = new DefaultTableModel(null, new String[] {
+                               "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
+                               "Livings" });
+               
+               //Maybe there is a better way to avoid interaction.
+               //table.setEnabled(false);
+               table.setModel(tableModel);
+               Enumeration<RuralHouse> en = houses.elements();
+               RuralHouse rh;
+               table.addMouseListener( new MouseAdapter(){                     
+                       @Override
+                       public void mouseClicked(MouseEvent arg0) {
+                               int row = table.getSelectedRow();
+                               HouseFeaturesGUI feat = new HouseFeaturesGUI(houses.get(row),null,null);
+                               feat.setVisible(true);
+                       }
+               });
+               while (en.hasMoreElements()) {
+                       rh = en.nextElement();
+                       Vector<Object> row = new Vector<Object>();
+                       row.add(rh.getHouseName());
+                       row.add(rh.getFeatures().getnRooms());
+                       row.add(rh.getFeatures().getnKitchens());
+                       row.add(rh.getFeatures().getnBaths());
+                       row.add(rh.getFeatures().getnParkings());
+                       row.add(rh.getFeatures().getnLivings());
+                       tableModel.addRow(row);
+               }
+
+       }
+}