import java.awt.Color;
import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
+import businessLogic.BookingManager;
+import domain.Booking;
import domain.Offer;
import domain.RuralHouse;
-
-
+import exceptions.OfferCanNotBeBooked;
public class HouseFeaturesGUI extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
-
+ private RuralHouse rh;
+ private JTextField telIn;
+ private int row;
/**
* Create the frame.
*/
- public HouseFeaturesGUI(RuralHouse rh, Date FirstDay, Date LastDay) {
-
+ public HouseFeaturesGUI(RuralHouse RH, Date FirstDay, Date LastDay) {
+ this.rh = RH;
this.getContentPane().setLayout(null);
setBounds(100, 100, 500, 583);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
-
+
lblDistrict = new JLabel("District:");
lblDistrict.setBounds(23, 67, 70, 14);
lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
District_f = new JTextField();
District_f.setEditable(false);
District_f.setBounds(103, 64, 86, 20);
District_f.setColumns(10);
-
+
lblDescription = new JLabel("Description:");
lblDescription.setBounds(215, 67, 90, 14);
lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
description_f = new JTextField();
description_f.setEditable(false);
description_f.setBounds(238, 99, 178, 129);
description_f.setColumns(10);
-
+
lblKitchen = new JLabel("Kitchens:");
lblKitchen.setBounds(23, 211, 70, 14);
lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
kitchens_f = new JTextField();
kitchens_f.setEditable(false);
kitchens_f.setBounds(103, 202, 86, 20);
kitchens_f.setColumns(10);
-
+
lblRooms = new JLabel("Rooms:");
lblRooms.setBounds(23, 102, 70, 14);
lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
rooms_f = new JTextField();
rooms_f.setEditable(false);
rooms_f.setBounds(103, 99, 86, 20);
rooms_f.setColumns(10);
-
+
lblLivings = new JLabel("Living rooms:");
lblLivings.setBounds(30, 236, 63, 14);
lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
lRooms_f = new JTextField();
lRooms_f.setEditable(false);
lRooms_f.setBounds(103, 233, 86, 20);
lRooms_f.setColumns(10);
-
+
lblParkings = new JLabel("Parkings:");
lblParkings.setBounds(23, 174, 70, 14);
lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
parkings_f = new JTextField();
parkings_f.setEditable(false);
parkings_f.setBounds(103, 171, 86, 20);
parkings_f.setColumns(10);
-
+
lblBaths = new JLabel("Baths:");
lblBaths.setBounds(23, 143, 70, 14);
lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
-
+
baths_f = new JTextField();
baths_f.setEditable(false);
baths_f.setBounds(103, 140, 86, 20);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(40, 311, 376, 183);
contentPane.add(scrollPane);
-
- table = new JTable();
- tableModel = new DefaultTableModel(null,new String[] {"Offer #", "FirstDay", "LastDay", "Price"});
+
+ JTable table = new JTable() {
+ private static final long serialVersionUID = 1L;
+
+ public boolean isCellEditable(int row, int column) {
+ return false;
+ };
+ };
+ tableModel = new DefaultTableModel(null, new String[] { "Offer #",
+ "FirstDay", "LastDay", "Price" });
table.setModel(tableModel);
scrollPane.setViewportView(table);
-
+
JButton btnBookSelected = new JButton("Book SelectedOffer");
- btnBookSelected.setBounds(127, 511, 178, 23);
+ btnBookSelected.setBounds(238, 505, 178, 23);
contentPane.add(btnBookSelected);
-
- JLabel lblNewLabel = new JLabel("Green: Suit your dates. Red: Do not suit your dates");
+
+ btnBookSelected.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+
+ jButton_ActionPerformed(arg0);
+ }
+
+ });
+
+ JLabel lblNewLabel = new JLabel(
+ "Green: Suit your dates. Red: Do not suit your dates");
lblNewLabel.setEnabled(false);
lblNewLabel.setBounds(152, 277, 261, 23);
contentPane.add(lblNewLabel);
+
+ JLabel lblNewLabel_1 = new JLabel("Telephone num:");
+ lblNewLabel_1.setBounds(10, 509, 83, 14);
+ contentPane.add(lblNewLabel_1);
+
+ telIn = new JTextField();
+ telIn.setBounds(99, 505, 129, 20);
+ contentPane.add(telIn);
+ telIn.setColumns(10);
+ table.addMouseListener( new MouseAdapter(){
+ @Override
+ public void mouseClicked(MouseEvent arg0) {
+ row = table.getSelectedRow();
+ }
+ });
Enumeration<Offer> rhs = rh.getAllOffers().elements();
- while(rhs.hasMoreElements()){
+ while (rhs.hasMoreElements()) {
Offer of = rhs.nextElement();
Vector<Object> row = new Vector<Object>();
row.add(of.getOfferNumber());
row.add(of.getLastDay());
row.add(of.getPrice());
tableModel.addRow(row);
-
-
+
}
- table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
- @Override
- public Component getTableCellRendererComponent(JTable table,
- Object value, boolean isSelected, boolean hasFocus, int row, int col) {
-
- super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
-
- Date firstDay = (Date) table.getModel().getValueAt(row, 1);
- Date lastDay = (Date) table.getModel().getValueAt(row, 2);
- if (FirstDay.before(firstDay)&&LastDay.after(lastDay)) {
- setBackground(Color.GREEN);
- setForeground(Color.BLACK);
- } else {
- setBackground(Color.RED);
- setForeground(Color.BLACK);
- }
- return this;
- }
+ table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
+ @Override
+ public Component getTableCellRendererComponent(JTable table,
+ Object value, boolean isSelected, boolean hasFocus,
+ int row, int col) {
+
+ super.getTableCellRendererComponent(table, value, isSelected,
+ hasFocus, row, col);
+
+ Date firstDay = (Date) table.getModel().getValueAt(row, 1);
+ Date lastDay = (Date) table.getModel().getValueAt(row, 2);
+ if (FirstDay != null && LastDay != null) {
+ if (FirstDay.before(firstDay) || FirstDay.equals(firstDay)
+ && LastDay.after(lastDay)
+ || LastDay.equals(lastDay)) {
+ setBackground(Color.GREEN);
+ setForeground(Color.BLACK);
+ } else {
+ setBackground(Color.RED);
+ setForeground(Color.BLACK);
+ }
+ }
+ return this;
+ }
});
}
+
+ private void jButton_ActionPerformed(ActionEvent arg0) {
+ BookingManager bookingM = new BookingManager();
+ //RegExp to see if telephone number is correct??TODO
+ Booking book = null;
+ try {
+ book = bookingM.createBooking(rh, rh.offers.get(row).getFirstDay(), rh.offers.get(row).getLastDay(), telIn.getText());
+ } catch (OfferCanNotBeBooked e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ if (book != null) {
+ BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
+ book);
+ confirmWindow.setVisible(true);
+ }
+ }
}