Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,92 @@
import javafx.scene.image.ImageView;

/**
* This class controls the list items in the browse rooms screen room search
* results list. Due to the weird way that lists are updated in JavaFX,
* this class actually loads the FXML and then assigns itself as the controller
* for the screen; things are typically done the other way around in JavaFX.
* This class controls the list items in the browse rooms screen room search results list.
*
* Due to the weird way that lists are updated in JavaFX, this class actually loads the FXML and
* then assigns itself as the controller for the screen; things are typically done the other way
* around in JavaFX.
*/
public class BrowseRoomsListItemController implements Initializable {

@FXML
private Node root;

@FXML
private Label roomCategoryLabel;

@FXML
private Label bedsInfoLabel;

@FXML
private Label roomPriceLabel;

@FXML
private ImageView roomThumbnailView;

@FXML
private Label roomDescriptionLabel;


private BrowseRoomsScreenController parentController;

private RoomSearchResult roomData;

public BrowseRoomsListItemController() {
// TODO
// Intentionally blank
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
// Intentionally blank
}

/**
* Sets the data on the screen to the values from the search result data.
*
*
* @param roomData The room information returned from the search.
*/
public void setData(RoomSearchResult roomData) {
this.roomData = roomData;
roomCategoryLabel.setText(roomData.getRoomCategory().getName());
roomThumbnailView.setImage(roomData.getRoomCategory().getImage());
bedsInfoLabel.setText(roomData.getRoomCategory().getBedsInfo());
// TODO We should handle currency more flexibly at some point.
// TODO: We should handle currency more flexibly at some point
String priceString = "$" + roomData.getRoomPrice() + " / night";
roomPriceLabel.setText(priceString);
roomDescriptionLabel.setText(
roomData.getRoomCategory().getDescription()
);
roomDescriptionLabel.setText(roomData.getRoomCategory().getDescription());
}

/**
* @return The graphical representation of this list item.
* @return The graphical representation of this list item.
*/
public Node getView() {
return root;
}


/**
* Assigns a reference the the controller of the class containing the list that this item is
* displayed in.
*
* This is needed to be able to handle the buttons in the list item easily.
*
* @param parentController
*/
public void setParentController(BrowseRoomsScreenController parentController) {
this.parentController = parentController;
}

/**
* Notifies the parent controller to add the room to the selected rooms list.
*/
@FXML
protected void onAddToBookingButtonClicked() {
// TODO
private void onAddToBookingButtonClicked() {
roomData.setNumAvailable(roomData.getNumAvailable() - 1);
parentController.onRoomSelected(roomData);
}

@FXML
protected void onRoomThumbnailClicked() {
private void onRoomThumbnailClicked() {
// TODO: Display fullsize image.
}

}
Original file line number Diff line number Diff line change
@@ -1,141 +1,205 @@
package com.gitrekt.resort.controller;

import com.gitrekt.resort.model.RoomSearchResult;
import com.gitrekt.resort.model.entities.RoomCategory;
import com.gitrekt.resort.model.services.BookingService;
import com.gitrekt.resort.view.BrowseRoomsListItem;
import com.gitrekt.resort.view.DeletableListItem;
import java.math.BigDecimal;
import com.gitrekt.resort.view.SelectedRoomListItem;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.chrono.ChronoLocalDate;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Tooltip;
import javafx.util.Callback;

/**
* FXML Controller class for the browse rooms screen.
*/
public class BrowseRoomsScreenController implements Initializable,
DeletableListItemDeletionListener {

public class BrowseRoomsScreenController implements Initializable {

@FXML
private ListView<RoomSearchResult> roomsListView;

@FXML
private ListView<RoomSearchResult> selectedRoomsListView;

@FXML
private DatePicker checkInDatePicker;

@FXML
private DatePicker checkOutDatePicker;


@FXML
private Button findAvailableRoomsButton;

private final ObservableList<RoomSearchResult> roomSearchResults;

private final ObservableList<RoomSearchResult> selectedRooms;

public BrowseRoomsScreenController() {
roomSearchResults = FXCollections.observableArrayList();
selectedRooms = FXCollections.observableArrayList();
}

@Override
public void initialize(URL url, ResourceBundle rb) {
roomsListView.setItems(roomSearchResults);
roomsListView.setCellFactory(param -> new BrowseRoomsListItem() {
roomsListView.setCellFactory(
param -> new BrowseRoomsListItem(this) {
{
// Don't touch. Magic.
prefWidthProperty().bind(roomsListView.widthProperty());
}
});
roomsListView.setPlaceholder(new Label("No results."));
selectedRoomsListView.setCellFactory(
param -> new DeletableListItem(this)
);
selectedRoomsListView.setCellFactory(param -> new SelectedRoomListItem(this));
selectedRoomsListView.setItems(selectedRooms);
selectedRoomsListView.setPlaceholder(new Label("No rooms selected"));
initializeDatePickers();
}


/**
* Called by the search result list item controller to notify this class that the
* "add to booking" button for the class has been clicked.
*
* @param roomData The RoomSearchResult contained in the clicked item.
*/
public void onRoomSelected(RoomSearchResult roomData) {
// If the currently selected room was the last in it's category, hide it from the list.
if(roomData.getNumAvailable() == 0) {
roomSearchResults.remove(roomData);
}
selectedRooms.add(roomData);
}

/**
* Called by the selected rooms list item controller to notify this controller that the room
* has been unselected and should be removed from the selected rooms list.
*
* @param roomData The RoomSearchResult for the room that was unselected.
*/
public void onRoomUnselected(RoomSearchResult roomData) {
// If the unselected room was the last available, add back it to the list of results.
if(roomData.getNumAvailable() == 1) {
roomSearchResults.add(roomData);
}
selectedRooms.remove(roomData);
}

/**
* Ensures that the date pickers only allow selection of dates within the valid booking date
* range, as defined in the specifications document.
*
* Chief among these rules is that bookings may not be placed more than one day in advance.
*/
private void initializeDatePickers() {
// TODO
Callback<DatePicker, DateCell> dayCellFactory =
(final DatePicker datePicker) -> new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);

if(item.isAfter(LocalDate.now().plusYears(1))) {
setDisable(true);
}
if(item.isBefore(ChronoLocalDate.from(LocalDate.now()))) {
setDisable(true);
}
}
};
// Disable selecting invalid check-in/check-out dates
checkInDatePicker.setDayCellFactory(dayCellFactory);
checkOutDatePicker.setDayCellFactory(dayCellFactory);
}


/**
* Clears the results list; doesn't really need to do anything else at the moment.
*/
@FXML
private void onCheckInDateSelected() {
// TODO
roomSearchResults.clear();
}


/**
* Validates the checkout date to ensure that it is at least one day after the checkin date.
*
* If it isn't, disables the room search button until it is.
*/
@FXML
private void onCheckOutDateSelected() {
// TODO
LocalDate checkOutDate = checkOutDatePicker.getValue();
LocalDate checkInDate = checkInDatePicker.getValue();
if(checkOutDate.isBefore(checkInDate) || checkOutDate.isEqual(checkInDate)) {
checkOutDatePicker.getStyleClass().add("invalidField");
checkOutDatePicker.setTooltip(
new Tooltip("Checkout date cannot be on or before checkin date!")
);
findAvailableRoomsButton.setDisable(true);
} else {
checkOutDatePicker.getStyleClass().remove("invalidField");
checkOutDatePicker.setTooltip(null);
findAvailableRoomsButton.setDisable(false);
}
roomSearchResults.clear();
}


/**
* Sorts results list by price, high to low.
*/
private void sortResultsByPrice() {
roomSearchResults.sort(
(RoomSearchResult r1, RoomSearchResult r2) ->
(RoomSearchResult r1, RoomSearchResult r2) ->
r1.getRoomPrice().compareTo(r2.getRoomPrice())
);
}


/**
* Returns to the previous screen.
*/
@FXML
private void onBackButtonClicked() {
ScreenManager.getInstance().switchToScreen(
"/fxml/GuestHomeScreen.fxml"
);
ScreenManager.getInstance().switchToScreen("/fxml/GuestHomeScreen.fxml");
}


/**
* Searches the database for rooms that are available in the given date range.
*/
@FXML
private void onFindAvailableRoomsButtonClicked() {
// The new date api is great. Converting back and forth, not so much.
LocalDate checkInDateTemp = checkInDatePicker.getValue();
LocalDate checkOutDateTemp = checkOutDatePicker.getValue();
Instant temp1 = Instant.from(
checkInDateTemp.atStartOfDay(ZoneId.systemDefault())
);
Instant temp2 = Instant.from(
checkOutDateTemp.atStartOfDay(ZoneId.systemDefault())
);
Instant temp1 = Instant.from(checkInDateTemp.atStartOfDay(ZoneId.systemDefault()));
Instant temp2 = Instant.from(checkOutDateTemp.atStartOfDay(ZoneId.systemDefault()));
Date checkInDate = Date.from(temp1);
Date checkOutDate = Date.from(temp2);

// Clear any existing results
roomSearchResults.clear();

selectedRooms.clear();

// Get the new results
BookingService bookingService = new BookingService();
roomSearchResults.addAll(
bookingService.getRoomTypesAvailable(checkInDate, checkOutDate)
);
roomSearchResults.addAll(bookingService.getRoomTypesAvailable(checkInDate, checkOutDate));
}

@FXML
private void onNextButtonClicked() {
// TODO replace with the packages screen first - this is temporary

if(selectedRooms.size() > 0) {
ScreenManager.getInstance().switchToScreen(
"/fxml/PlaceBookingScreen.fxml"
);
ScreenManager.getInstance().switchToScreen("/fxml/PlaceBookingScreen.fxml");
}
}

// TODO: Determine if really necessary
/**
* Handles item deletion events for deletable list items.
*/
@Override
public void onItemDeleted(DeletableListItem item) {
// TODO implement
}


}

This file was deleted.

Loading