cleaning
[RRRRHHHH_Code] / ruralHouses / src / gui / listOfHousesGUI.java
1 package gui;
2
3 import java.awt.Font;
4 import java.awt.Rectangle;
5 import java.awt.event.MouseAdapter;
6 import java.awt.event.MouseEvent;
7 import java.util.Enumeration;
8 import java.util.Vector;
9
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JScrollPane;
14 import javax.swing.JTable;
15 import javax.swing.border.EmptyBorder;
16 import javax.swing.table.DefaultTableModel;
17
18 import domain.RuralHouse;
19
20 public class listOfHousesGUI extends JFrame {
21
22         /**
23          * 
24          */
25         private static final long serialVersionUID = 1L;
26         private JPanel contentPane;
27         private JTable table;
28         private DefaultTableModel tableModel;
29         private Vector<RuralHouse> houses;
30         /**
31          * Create the frame.
32          */
33         public listOfHousesGUI(Vector<RuralHouse> rhs) {
34                 try {
35                         this.houses=rhs;
36                         init();
37                 } catch (Exception e) {
38                         e.printStackTrace();
39                 }
40         }
41
42         private void init() throws Exception {
43                 setBounds(100, 100, 600, 450);
44                 contentPane = new JPanel();
45                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
46                 setContentPane(contentPane);
47                 contentPane.setLayout(null);
48
49                 JLabel lblNewLabel = new JLabel();
50                 lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 27));
51                 lblNewLabel.setBounds(23, 41, 536, 33);
52                 contentPane.add(lblNewLabel);
53                 if (houses.isEmpty())
54                         lblNewLabel.setText("There are not houses matching your search");
55                 else
56                         lblNewLabel.setText("List of houses that match your search:");
57                 JScrollPane scrollPane = new JScrollPane();
58                 scrollPane.setBounds(new Rectangle(45, 305, 320, 116));
59                 scrollPane.setBounds(23, 113, 536, 271);
60                 contentPane.add(scrollPane);
61
62                 table = new JTable() {
63                 private static final long serialVersionUID = 1L;
64
65                 public boolean isCellEditable(int row, int column) {                
66                         return false;               
67                 };
68             };
69                 scrollPane.setViewportView(table);
70                 tableModel = new DefaultTableModel(null, new String[] {
71                                 "House Name", "Bedrooms", "Kitchens", "Baths", "Parkings",
72                                 "Livings" });
73                 
74                 //Maybe there is a better way to avoid interaction.
75                 //table.setEnabled(false);
76                 table.setModel(tableModel);
77                 Enumeration<RuralHouse> en = houses.elements();
78                 RuralHouse rh;
79                 table.addMouseListener( new MouseAdapter(){                     
80                         @Override
81                         public void mouseClicked(MouseEvent arg0) {
82                                 int row = table.getSelectedRow();
83                                 HouseFeaturesGUI feat = new HouseFeaturesGUI(houses.get(row),null,null);
84                                 feat.setVisible(true);
85                         }
86                 });
87                 while (en.hasMoreElements()) {
88                         rh = en.nextElement();
89                         Vector<Object> row = new Vector<Object>();
90                         row.add(rh.getHouseName());
91                         row.add(rh.getFeatures().getnRooms());
92                         row.add(rh.getFeatures().getnKitchens());
93                         row.add(rh.getFeatures().getnBaths());
94                         row.add(rh.getFeatures().getnParkings());
95                         row.add(rh.getFeatures().getnLivings());
96                         tableModel.addRow(row);
97                 }
98
99         }
100 }