Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.52 KB | None | 0 0
  1. package View;
  2.  
  3. import Controller.Controller;
  4. import Controller.Item;
  5. import Controller.User;
  6. import javafx.collections.FXCollections;
  7. import javafx.collections.ObservableList;
  8. import javafx.geometry.Insets;
  9. import javafx.geometry.Pos;
  10. import javafx.scene.Scene;
  11. import javafx.scene.control.*;
  12. import javafx.scene.control.TableView;
  13. import javafx.scene.control.cell.PropertyValueFactory;
  14. import javafx.scene.input.KeyCode;
  15. import javafx.scene.layout.BorderPane;
  16. import javafx.scene.layout.GridPane;
  17. import javafx.scene.layout.HBox;
  18. import javafx.scene.layout.VBox;
  19. import javafx.stage.Stage;
  20.  
  21. import java.text.NumberFormat;
  22.  
  23. /**
  24.  * Tommy is primary contributor for this class.
  25.  */
  26. public class View {
  27.    private Controller c;
  28.  
  29.    private Stage primaryStage = new Stage();
  30.  
  31.    // Objects used in loginView
  32.    private Label loginLabel = new Label();
  33.    private Label userIdLabel = new Label("Username:");
  34.    private Label userPassword = new Label("User Password:");
  35.    private Button quitButton = new Button();
  36.    private Button loginButton = new Button("Login");
  37.    private TextField userNameField = new TextField();
  38.    private PasswordField passwordField = new PasswordField();
  39.    private GridPane gridPane = new GridPane();
  40.    private HBox loginHBox = new HBox();
  41.    private VBox loginVBox = new VBox();
  42.    private Scene loginScene = new Scene(getloginVBox(), 1280, 720);
  43.  
  44.    // Objects used in mainView
  45.    private Label labelMainUserName = new Label();
  46.    private Label labelAccessLevel = new Label();
  47.    private Label uniqueItems = new Label();
  48.    private Label totalQuantity = new Label();
  49.    private Label addQuantityLabel = new Label("Quantity");
  50.    private Label addNameLabel = new Label("Name:");
  51.    private Label addDescriptionLabel = new Label("Description:");
  52.    private Button logoutButton = new Button("Logout");
  53.    private Button mainQuitButton = new Button("Quit");
  54.    private Button addButton = new Button("Add");
  55.    private Button editButton = new Button("Edit");
  56.    private Button deleteButton = new Button("Delete");
  57.    private Button inventoryButton = new Button("Inventory");
  58.    private Button userEdit = new Button("User Edit");
  59.    private Button applyButton = new Button("Apply");
  60.    private Button cancelButton = new Button("Cancel");
  61.    private TextField searchField = new TextField();
  62.    private TextField addName = new TextField();
  63.    private TextField addQuantity = new TextField();
  64.    private TextField addDescription = new TextField();
  65.    private TableView inventoryTable = new TableView<>();
  66.    private TableView userTable = new TableView<>();
  67.    private HBox mainHBox = new HBox();
  68.    private HBox mainHBoxAdd = new HBox();
  69.    private HBox mainBottomHBox = new HBox();
  70.    private VBox mainTopVBox = new VBox();
  71.    private VBox mainRightVBox = new VBox();
  72.    private VBox mainLeftVBox = new VBox();
  73.    private VBox mainCenterVBox = new VBox();
  74.    private BorderPane borderpane = new BorderPane();
  75.    private Scene mainScene = new Scene(borderpane, 1280, 720);
  76.  
  77.    // Constructor for View class
  78.    View(Controller c, Stage stage) {
  79.       this.c = c;
  80.  
  81.       this.primaryStage = stage;
  82.  
  83.       loginView();
  84.  
  85.       primaryStage.setTitle("Inventory Management version. 0.1");
  86.       primaryStage.setScene(loginScene);
  87.       primaryStage.show();
  88.    }
  89.  
  90.    // load CSS from a stylesheet.
  91.    private Scene loadCSS(Scene scene) {
  92.       scene.getStylesheets().add("/View/stylesheet.css");
  93.       return scene;
  94.    }
  95.  
  96.    // Get method for login label.
  97.    private Label getLoginLabel() {
  98.       loginLabel.setId("login_login_label");
  99.       loginLabel.setText("Login");
  100.       return loginLabel;
  101.    }
  102.  
  103.    // Get method for quit button in login View.
  104.    private Button getQuitButton() {
  105.       quitButton.setId("login_quit_button");
  106.       quitButton.setText("Quit");
  107.       quitButton.setOnAction(event -> {
  108.          primaryStage.close();
  109.          c.closeConnection();
  110.       });
  111.       return quitButton;
  112.    }
  113.  
  114.    // Get method for Login button in login view
  115.    private Button getLoginButton() {
  116.       loginButton.setId("login_login_button");
  117.       loginButton.setText("Login");
  118.       loginButton.setOnAction(e -> {
  119.          String user = userNameField.getText();
  120.          String pass = passwordField.getText();
  121.  
  122.          if (c.verifyUser(user, pass)) {
  123.             mainHBox.getChildren().clear();
  124.             mainHBoxAdd.getChildren().clear();
  125.             mainBottomHBox.getChildren().clear();
  126.             mainTopVBox.getChildren().clear();
  127.             mainRightVBox.getChildren().clear();
  128.             mainLeftVBox.getChildren().clear();
  129.             mainCenterVBox.getChildren().clear();
  130.             borderpane.getChildren().clear();
  131.  
  132.             primaryStage.setScene(mainView());
  133.             primaryStage.show();
  134.          } else {
  135.             System.out.println("login error");
  136.          }
  137.       });
  138.       return loginButton;
  139.    }
  140.  
  141.    // Get method for login gridpane.
  142.    private GridPane getLoginGridPane() {
  143.       gridPane.setId("login_gridpane");
  144.       gridPane.add(userIdLabel, 0, 0);
  145.       gridPane.add(userNameField, 1, 0);
  146.       gridPane.add(userPassword, 0,1);
  147.       gridPane.add(passwordField, 1,1);
  148.       return gridPane;
  149.    }
  150.  
  151.    // Get method for loginHBox.
  152.    private HBox getloginHbox() {
  153.       loginHBox.setId("login_hbox");
  154.       loginHBox.setAlignment(Pos.CENTER);
  155.       loginHBox.getChildren().add(getQuitButton());
  156.       loginHBox.getChildren().add(getLoginButton());
  157.       return loginHBox;
  158.    }
  159.  
  160.    // Get method for loginVBox.
  161.    private VBox getloginVBox() {
  162.       loginVBox.setId("login_vbox");
  163.       loginVBox.getChildren().add(getLoginLabel());
  164.       loginVBox.getChildren().add(getLoginGridPane());
  165.       loginVBox.getChildren().add(getloginHbox());
  166.       return loginVBox;
  167.    }
  168.  
  169.    // Get method for loginView returns login scene.
  170.    private Scene loginView() {
  171.  
  172.       loadCSS(loginScene);
  173.  
  174.       userNameField.setPromptText("Username");
  175.       passwordField.setPromptText("Password");
  176.  
  177.       // TODO: Only used for testing purposes needs to be deleted at completion
  178.       userNameField.setText("mikk7506");
  179.       passwordField.setText("12345");
  180.  
  181.       return loginScene;
  182.    }
  183.  
  184.    /********************************************************************************************************************
  185.     * MAIN VIEW RELATED METHODS STARTS HERE:
  186.     *******************************************************************************************************************/
  187.  
  188.    // Get method for show the username of the currently logged in user.
  189.    private Label getCurrentUserName() {
  190.       String user = userNameField.getText();
  191.       labelMainUserName.setId("main_current_user_label");
  192.       labelMainUserName.setText("Current User: " + user);
  193.       return labelMainUserName;
  194.    }
  195.  
  196.    // Get method for show what level a user has.
  197.    private Label getCurrentUserAccessLevel() {
  198.       labelAccessLevel.setId("main_current_user_access_level_label");
  199.       labelAccessLevel.setText("Access Level: ");
  200.       return labelAccessLevel;
  201.    }
  202.  
  203.    // Get method for inventory Table.
  204.    private TableView getInventoryTable() {
  205.       inventoryTable.setId("inventory_table");
  206.  
  207.       TableColumn<Item, Integer> idColumn = new TableColumn("ITEM NUMBER");
  208.       idColumn.setMinWidth(150);
  209.       idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
  210.  
  211.       TableColumn<Item, Integer> quantityColumn = new TableColumn("QUANTITY");
  212.       quantityColumn.setMinWidth(100);
  213.       quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
  214.  
  215.       TableColumn<Item, String> nameColumn = new TableColumn("NAME");
  216.       nameColumn.setMinWidth(100);
  217.       nameColumn.setCellValueFactory(new PropertyValueFactory("name"));
  218.  
  219.       TableColumn<Item, String> descriptionColumn = new TableColumn("DESCRIPTION");
  220.       descriptionColumn.setMinWidth(450);
  221.       descriptionColumn.setCellValueFactory(new PropertyValueFactory("description"));
  222.  
  223.       inventoryTable.setPadding(new Insets(10,10,10,10));
  224.       ObservableList<Item> itemList = c.getItems();
  225.       inventoryTable.setItems(itemList);
  226.       inventoryTable.getColumns().addAll(idColumn, quantityColumn, nameColumn, descriptionColumn);
  227.  
  228.       uniqueItems.setText("Unique items: " + NumberFormat.getIntegerInstance().format(itemList.size()));
  229.       int total = 0;
  230.       for (Item i : itemList) {
  231.          total += i.getQuantity();
  232.       }
  233.       totalQuantity.setText("Total quantity: " + NumberFormat.getIntegerInstance().format(total));
  234.       return inventoryTable;
  235.    }
  236.  
  237.    // Get method for User Table
  238.    private TableView getUserTable() {
  239.       TableView userTable = new TableView();
  240.  
  241.       TableColumn<User, String> userColumn = new TableColumn("USERNAME");
  242.       userColumn.setMinWidth(50);
  243.       userColumn.setCellValueFactory(new PropertyValueFactory<>("username"));
  244.  
  245.       TableColumn<User, String> passColumn = new TableColumn("PASSWORD");
  246.       passColumn.setMinWidth(50);
  247.       passColumn.setCellValueFactory(new PropertyValueFactory<>("password"));
  248.  
  249.       TableColumn<User, Integer> accessColumn = new TableColumn("ACCESS LEVEL");
  250.       accessColumn.setMinWidth(100);
  251.       accessColumn.setCellValueFactory(new PropertyValueFactory("acces_lvl"));
  252.  
  253.       TableColumn<User, String> eMailColumn = new TableColumn("EMAIL ADDRESS");
  254.       eMailColumn.setMinWidth(450);
  255.       eMailColumn.setCellValueFactory(new PropertyValueFactory("email"));
  256.  
  257.       userTable.setPadding(new Insets(10,10,10,10));
  258.       ObservableList<User> userList = c.getUsers();
  259.       userTable.setItems(userList);
  260.       userTable.getColumns().addAll(userColumn, passColumn, accessColumn, eMailColumn);
  261.  
  262.       return userTable;
  263.    }
  264.  
  265.    // Method used for allowing search in Inventory Table
  266.    private void search() {
  267.       searchField.setPromptText("Search");
  268.       searchField.setOnKeyPressed(e -> {
  269.          if (e.getCode().equals(KeyCode.ENTER)) {
  270.             ObservableList<Item> list = c.getItems();
  271.             ObservableList<Item> searchResults = FXCollections.observableArrayList();
  272.             String searchStr = searchField.getText().toLowerCase();
  273.  
  274.             for (Item i : list) {
  275.                if (i.getName().toLowerCase().contains(searchStr)) {
  276.                   searchResults.add(i);
  277.                } else if (i.getDescription().toLowerCase().contains(searchStr)) {
  278.                   searchResults.add(i);
  279.                } else if (Integer.toString(i.getId()).contains(searchStr)) {
  280.                   searchResults.add(i);
  281.                }
  282.             }
  283.             inventoryTable.setItems(searchResults);
  284.          }
  285.       });
  286.  
  287.       searchField.lengthProperty().addListener((observable, oldValue, newValue) -> {
  288.          if (searchField.getText().equals(null) || searchField.getText().length() == 0) {
  289.             inventoryTable.setItems(c.getItems());
  290.          }
  291.       });
  292.    }
  293.  
  294.    /**
  295.     * Get Method for Logout Button.
  296.     *
  297.     * @return
  298.     */
  299.  
  300.    private Button getLogoutButton() {
  301.       logoutButton.setOnAction(event -> {
  302.          inventoryTable.getColumns().clear();
  303.          userTable.getColumns().clear();
  304.  
  305.          c.setLoggedUser(null);
  306.          primaryStage.setScene(loginView());
  307.          primaryStage.show();
  308.       });
  309.       return logoutButton;
  310.    }
  311.  
  312.    // Get method for Quit Button in Main View
  313.    private Button getMainQuitButton() {
  314.       mainQuitButton.setOnAction(e -> {
  315.          primaryStage.close();
  316.          c.closeConnection();
  317.       });
  318.       return mainQuitButton;
  319.    }
  320.  
  321.    //Get Method for Apply Button
  322.    private Button getApplyButton() {
  323.       applyButton.setOnAction(event -> {
  324.          try {
  325.             int quantity = Integer.parseInt(addQuantity.getText());
  326.             String name = addName.getText();
  327.             String description = addDescription.getText();
  328.             Item item = new Item(quantity, name, description);
  329.             c.addItemToDb(item);
  330.  
  331.             mainCenterVBox.getChildren().remove(mainHBoxAdd);
  332.          } catch (NumberFormatException ex) {
  333.             System.out.println("You need to enter an integer you noob!");
  334.          }
  335.       });
  336.       return applyButton;
  337.    }
  338.  
  339.    // Get Method for cancel adding to Inventory Table.
  340.    private Button getCancelButton() {
  341.       cancelButton.setOnAction(event -> {
  342.          mainCenterVBox.getChildren().remove(mainHBoxAdd);
  343.       });
  344.       return cancelButton;
  345.    }
  346.  
  347.    // Method for AddMenuBox.
  348.    public void addMenuBox(HBox hbox) {
  349.       addQuantity.setMinWidth(50);
  350.       addQuantity.setMaxWidth(50);
  351.       hbox.getChildren().add(addQuantityLabel);
  352.       hbox.getChildren().add(addQuantity);
  353.       hbox.getChildren().add(addNameLabel);
  354.       hbox.getChildren().add(addName);
  355.       hbox.getChildren().add(addDescriptionLabel);
  356.       hbox.getChildren().add(addDescription);
  357.       hbox.getChildren().add(getApplyButton());
  358.       hbox.getChildren().add(getCancelButton());
  359.    }
  360.  
  361.    // Get Method for AddButton.
  362.    public Button getAddButton() {
  363.       addButton.setOnAction(event -> {
  364.          addQuantity.setText("");
  365.          addName.setText("");
  366.          addDescription.setText("");
  367.  
  368.          mainCenterVBox.getChildren().add(mainHBoxAdd);
  369.          addMenuBox(mainHBoxAdd);
  370.       });
  371.       return addButton;
  372.    }
  373.  
  374.    private Scene mainView() {
  375.  
  376.       loadCSS(mainScene);
  377.       getInventoryTable();
  378.       userTable = getUserTable();
  379.       search();
  380.  
  381.       primaryStage.setOnCloseRequest(e -> c.closeConnection());
  382.  
  383.       mainHBox.setId("main_hbox");
  384.       mainHBox.setPadding(new Insets (20,10,20,10));
  385.       mainHBox.setAlignment(Pos.TOP_CENTER);
  386.       mainHBox.getChildren().add(getCurrentUserName());
  387.       mainHBox.getChildren().add(getCurrentUserAccessLevel());
  388.  
  389.       mainTopVBox.setId("main_top_vbox");
  390.       mainTopVBox.setAlignment(Pos.TOP_CENTER);
  391.       mainTopVBox.setSpacing(30);
  392.       mainTopVBox.setPadding(new Insets(20,10,20,10));
  393.       mainTopVBox.getChildren().add(mainHBox);
  394.       mainTopVBox.getChildren().add(searchField);
  395.  
  396.       mainRightVBox.setId("main_right_vbox");
  397.       mainRightVBox.setPadding(new Insets(20,10,20,10));
  398.       mainRightVBox.getChildren().add(getAddButton());
  399.       mainRightVBox.getChildren().add(editButton);
  400.       mainRightVBox.getChildren().add(deleteButton);
  401.  
  402.       mainLeftVBox.setId("main_left_vbox");
  403.       mainLeftVBox.setPadding(new Insets(20,10,20,10));
  404.       mainLeftVBox.getChildren().add(inventoryButton);
  405.       mainLeftVBox.getChildren().add(userEdit);
  406.       mainLeftVBox.getChildren().add(getLogoutButton());
  407.       mainLeftVBox.getChildren().add(getMainQuitButton());
  408.  
  409.       mainCenterVBox.setId("main_center_vbox");
  410.       mainCenterVBox.setPadding(new Insets(5,5,5,5));
  411.       mainCenterVBox.getChildren().add(inventoryTable);
  412.  
  413.       mainBottomHBox.setId("main_bottom_hbox");
  414.       mainBottomHBox.setPadding(new Insets(20,10,10,20));
  415.       mainBottomHBox.setAlignment(Pos.BOTTOM_CENTER);
  416.       mainBottomHBox.setSpacing(200);
  417.       mainBottomHBox.getChildren().add(uniqueItems);
  418.       mainBottomHBox.getChildren().add(totalQuantity);
  419.  
  420.       borderpane.setId("main_border_pane");
  421.       borderpane.setRight(mainRightVBox);
  422.       borderpane.setLeft(mainLeftVBox);
  423.       borderpane.setTop(mainTopVBox);
  424.       borderpane.setCenter(mainCenterVBox);
  425.       borderpane.setBottom(mainBottomHBox);
  426.  
  427.       inventoryButton.setOnAction(e -> {
  428.          mainCenterVBox.getChildren().clear();
  429.          mainCenterVBox.getChildren().add(inventoryTable);
  430.       });
  431.  
  432.       userEdit.setOnAction(e -> {
  433.          mainCenterVBox.getChildren().clear();
  434.          mainCenterVBox.getChildren().add(userTable);
  435.       });
  436.       return mainScene;
  437.     }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement