Projects / Hotel Management System / src / hoteldraft / ServicesController.java
ServicesController.java
Raw
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hoteldraft;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.List;
import java.util.ResourceBundle;
import javafx.animation.RotateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import static javafx.scene.paint.Color.rgb;
import javafx.stage.Stage;
import javafx.util.Duration;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ServicesController implements Initializable {

    private double xOffSet = 0;
    private double yOffSet = 0;

    private Stage stage;
    private Scene scene;
    private Parent root;
    @FXML
    private ImageView settingsImg;
    @FXML
    private Label exit;
    @FXML
    private Label minimize;
    @FXML
    private Button settingsImgBtn;
    @FXML
    private ListView roomList;
    @FXML
    private ListView foodList;
    @FXML
    private ListView spaList;
    @FXML
    private ListView gymList;
    @FXML
    private ListView transfList;
    @FXML
    private ListView drList;
    @FXML
    private ListView cleanList;
    @FXML
    private ListView flowerList;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //retrieve from DB
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Services");
        List<Services> SList = query.list();
        session.close();
        for (Services s : SList) {
            roomList.getItems().add(s.getRoomNo());
            foodList.getItems().add(s.getFoodOrder());
            spaList.getItems().add(s.getSpa());
            gymList.getItems().add(s.getGym());
            transfList.getItems().add(s.getTransportation());
            drList.getItems().add(s.getDoctor());
            cleanList.getItems().add(s.getCleaning());
            flowerList.getItems().add(s.getFlower());
        }

        //to change settings imageview color
        Lighting lighting = new Lighting(new Light.Distant(45, 90, rgb(105, 152, 171)));
        ColorAdjust bright = new ColorAdjust(0, 1, 1, 1);
        lighting.setContentInput(bright);
        lighting.setSurfaceScale(0.0);
        settingsImg.setEffect(lighting);

        // to rotate settings button on hover
        RotateTransition rotation = new RotateTransition(Duration.seconds(0.5), settingsImgBtn);
        rotation.setCycleCount(1);
        rotation.setByAngle(180);
        settingsImgBtn.setOnMouseEntered(e -> rotation.play());
        settingsImgBtn.setOnMouseExited(e -> rotation.pause());

        //Listeners to select all row if one listview item is selected
        roomList.getSelectionModel().selectedItemProperty().addListener(e -> {
            roomList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = roomList.getSelectionModel().getSelectedIndex();
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        foodList.getSelectionModel().selectedItemProperty().addListener(e -> {
            foodList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = foodList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        spaList.getSelectionModel().selectedItemProperty().addListener(e -> {
            spaList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = spaList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        gymList.getSelectionModel().selectedItemProperty().addListener(e -> {
            gymList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = gymList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        transfList.getSelectionModel().selectedItemProperty().addListener(e -> {
            transfList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = transfList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        drList.getSelectionModel().selectedItemProperty().addListener(e -> {
            drList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = drList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        cleanList.getSelectionModel().selectedItemProperty().addListener(e -> {
            cleanList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = cleanList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            flowerList.getSelectionModel().select(index);
        });
        flowerList.getSelectionModel().selectedItemProperty().addListener(e -> {
            flowerList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
            int index = flowerList.getSelectionModel().getSelectedIndex();
            roomList.getSelectionModel().select(index);
            foodList.getSelectionModel().select(index);
            spaList.getSelectionModel().select(index);
            gymList.getSelectionModel().select(index);
            transfList.getSelectionModel().select(index);
            drList.getSelectionModel().select(index);
            cleanList.getSelectionModel().select(index);
        });
    }
    
    @FXML
    public void delete(ActionEvent event) {
        Services services = new Services(Integer.valueOf(String.valueOf(roomList.getSelectionModel().selectedItemProperty().getValue())));
        
        roomList.getItems().remove(roomList.getSelectionModel().selectedItemProperty().getValue());
        foodList.getItems().remove(foodList.getSelectionModel().selectedItemProperty().getValue());
        spaList.getItems().remove(spaList.getSelectionModel().selectedItemProperty().getValue());
        gymList.getItems().remove(gymList.getSelectionModel().selectedItemProperty().getValue());
        transfList.getItems().remove(transfList.getSelectionModel().selectedItemProperty().getValue());
        drList.getItems().remove(drList.getSelectionModel().selectedItemProperty().getValue());
        cleanList.getItems().remove(cleanList.getSelectionModel().selectedItemProperty().getValue());
        flowerList.getItems().remove(flowerList.getSelectionModel().selectedItemProperty().getValue());
        
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.delete(services);
        tx.commit();
        session.close();
    }

    //Methods to close and minimize window.
    @FXML
    private void close_app(javafx.scene.input.MouseEvent event) {
        System.exit(0);
    }

    @FXML
    private void minimize_app(javafx.scene.input.MouseEvent event) {
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        stage.setIconified(true);
    }

    /*---------------------class switching events---------------------*/
    @FXML
    public void switchToReservations(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Reservations.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    public void switchToNewReservation(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("NewReservation.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();

    }

    @FXML
    public void switchToLoggedIn(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("LoggedIn.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    public void switchToRooms(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Rooms.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    public void switchToServices(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Services.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    public void switchToSearch(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Search.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    public void switchToSettings(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Settings.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }
    
    @FXML
    public void switchToNewServices(ActionEvent e) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("NewServices.fxml"));

        root.setOnMousePressed((event) -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        root.setOnMouseDragged((event) -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }
}