Advertisement
Guest User

MainView.java

a guest
Oct 18th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.12 KB | None | 0 0
  1.  
  2.     /*
  3.      * Program developed by Hassan Althaf.
  4.      * Copyright © 2015, Hassan Althaf.
  5.      * Website: http://hassanalthaf.com
  6.      */
  7.     package com.HassanAlthaf.StockControlSystem.View;
  8.    
  9.     import com.HassanAlthaf.StockControlSystem.Stocks.StockController;
  10.     import com.HassanAlthaf.StockControlSystem.Stocks.StockItem;
  11.     import com.HassanAlthaf.StockControlSystem.Users.User;
  12.     import com.HassanAlthaf.StockControlSystem.Users.UserController;
  13.     import java.io.IOException;
  14.     import java.net.URL;
  15.     import java.sql.SQLException;
  16.     import java.util.ArrayList;
  17.     import java.util.ResourceBundle;
  18.     import javafx.collections.FXCollections;
  19.     import javafx.collections.ObservableList;
  20.     import javafx.event.ActionEvent;
  21.     import javafx.fxml.FXML;
  22.     import javafx.fxml.FXMLLoader;
  23.     import javafx.fxml.Initializable;
  24.     import javafx.scene.Parent;
  25.     import javafx.scene.Scene;
  26.     import javafx.scene.control.Button;
  27.     import javafx.scene.control.Label;
  28.     import javafx.scene.control.Menu;
  29.     import javafx.scene.control.MenuItem;
  30.     import javafx.scene.control.TableColumn;
  31.     import javafx.scene.control.TableView;
  32.     import javafx.scene.control.TextField;
  33.     import javafx.scene.control.cell.PropertyValueFactory;
  34.     import javafx.scene.layout.AnchorPane;
  35.     import javafx.scene.text.Text;
  36.     import javafx.stage.Stage;
  37.    
  38.     /**
  39.      *
  40.      * @author hassan
  41.      */
  42.     public class MainView implements Initializable {
  43.        
  44.         @FXML
  45.         private Parent mainWindow;
  46.        
  47.         @FXML
  48.         private AnchorPane main;
  49.        
  50.         @FXML
  51.         private AnchorPane about;
  52.        
  53.         @FXML
  54.         private AnchorPane addInventoryItem;
  55.        
  56.         @FXML
  57.         private AnchorPane listInventory;
  58.        
  59.         @FXML
  60.         private AnchorPane listUsers;
  61.        
  62.         private AnchorPane activePage;
  63.        
  64.         @FXML
  65.         private TextField productNameField;
  66.        
  67.         @FXML
  68.         private TextField availableQuantityField;
  69.        
  70.         @FXML
  71.         private TextField unitPriceField;
  72.        
  73.         @FXML
  74.         private TextField reorderLevelField;
  75.        
  76.         private final StockController stockController;
  77.        
  78.         @FXML
  79.         private Text addItemErrors;
  80.        
  81.         @FXML
  82.         private TableView<StockItem> stockList;
  83.        
  84.         @FXML
  85.         private TableColumn productNameColumn;
  86.        
  87.         @FXML
  88.         private TableColumn unitPriceColumn;
  89.        
  90.         @FXML
  91.         private TableColumn availableQuantityColumn;
  92.        
  93.         @FXML
  94.         private TableColumn reorderLevelColumn;
  95.        
  96.         @FXML
  97.         private TableColumn totalValueColumn;
  98.        
  99.         @FXML
  100.         private TableView<User> usersList;
  101.        
  102.         @FXML
  103.         private TableColumn usernameColumn;
  104.        
  105.         @FXML
  106.         private TableColumn rankColumn;
  107.        
  108.         @FXML
  109.         private Label totalStockValueLabel;
  110.        
  111.         private ObservableList<StockItem> stockItemData;
  112.        
  113.         private ObservableList<User> userListData;
  114.        
  115.         private UserController userController;
  116.        
  117.         @FXML
  118.         private Menu userMenu;
  119.        
  120.         @FXML
  121.         private MenuItem addStockMenuItem;
  122.        
  123.         @FXML
  124.         private Button deleteStockItemButton;
  125.        
  126.         public MainView() {
  127.             this.stockController = new StockController();
  128.         }
  129.        
  130.         public void setUserController(UserController userController) {
  131.             this.userController = userController;
  132.         }
  133.        
  134.         public void show(Parent loginWindow) throws IOException, SQLException {
  135.             Scene scene  = new Scene(this.mainWindow);
  136.            
  137.             Stage stage = new Stage();
  138.            
  139.             stage.setScene(scene);
  140.             stage.setTitle("Stock Control System");
  141.             stage.setResizable(false);
  142.             stage.show();
  143.            
  144.             this.updateViewForUser();
  145.             this.initializeStats();
  146.            
  147.             Stage loginStage = (Stage) loginWindow.getScene().getWindow();
  148.             loginStage.close();
  149.            
  150.             this.activePage = this.main;
  151.         }
  152.        
  153.         public void menuClick(ActionEvent event) throws SQLException, IOException {
  154.             Object source = event.getSource();
  155.            
  156.             String id = null;
  157.            
  158.             if (source instanceof MenuItem) {
  159.                 MenuItem clickedItem = (MenuItem) source;
  160.                 id = clickedItem.getId();
  161.             }
  162.            
  163.             this.authenticateAccess();
  164.             this.updateViewForUser();
  165.            
  166.             if (id.equals("aboutMenuItem")) {
  167.                 this.activePage.setOpacity(0);
  168.                 this.about.toFront();
  169.                 this.about.setOpacity(1);
  170.                 this.activePage = this.about;
  171.             } else if (id.equals("homeMenuItem")) {
  172.                 this.activePage.setOpacity(0);
  173.                 this.main.toFront();
  174.                 this.main.setOpacity(1);
  175.                 this.activePage = this.main;
  176.             } else if (id.equals("addStockMenuItem")) {
  177.                 this.authenticateAdminRights();
  178.                 this.activePage.setOpacity(0);
  179.                 this.addInventoryItem.toFront();
  180.                 this.addInventoryItem.setOpacity(1);
  181.                 this.activePage = this.addInventoryItem;
  182.             } else if (id.equals("listStocksMenuItem")) {
  183.                 this.activePage.setOpacity(0);
  184.                 this.listInventory.toFront();
  185.                 this.populateStocksList();
  186.                 this.listInventory.setOpacity(1);
  187.                 this.activePage = this.listInventory;
  188.             } else if (id.equals("loadUsersList")) {
  189.                 this.authenticateAdminRights();
  190.                 this.activePage.setOpacity(0);
  191.                 this.listUsers.toFront();
  192.                 this.populateUsersList();
  193.                 this.listUsers.setOpacity(1);
  194.                 this.activePage = this.listUsers;
  195.             }
  196.         }
  197.        
  198.         public void authenticateAccess() throws IOException, SQLException {
  199.             this.userController.updateLoggedInUser();
  200.            
  201.             if(this.userController.userIsDisabled()) {
  202.                 this.logUserOut();
  203.             }
  204.         }
  205.        
  206.         public void authenticateAdminRights() throws IOException, SQLException {
  207.             this.userController.updateLoggedInUser();
  208.            
  209.             if(!this.userController.isAdmin()) {
  210.                 this.logUserOut();
  211.             }
  212.         }
  213.        
  214.         public void addStockItem(ActionEvent event) throws SQLException, IOException {
  215.             this.authenticateAdminRights();
  216.            
  217.             boolean response = this.stockController.addStockItem(
  218.                     this.productNameField.getText(),
  219.                     this.availableQuantityField.getText(),
  220.                     this.unitPriceField.getText(),
  221.                     this.reorderLevelField.getText()
  222.             );
  223.    
  224.             if (response == false) {
  225.                 ArrayList<String> errors = this.stockController.getReturnMessages();
  226.                 String text = "";
  227.    
  228.                 for (String line : errors) {
  229.                     text = text + line + "\n";
  230.                 }
  231.    
  232.                 text = text.substring(0, text.length() - 1);
  233.    
  234.                 this.addItemErrors.setText(text);
  235.             } else {
  236.                 this.initializeStats();
  237.                 this.addItemErrors.setText("Successfully added!");
  238.             }
  239.         }
  240.        
  241.         public void populateStocksList() throws IOException, SQLException {
  242.             ArrayList<StockItem> stockItems = null;
  243.            
  244.             this.authenticateAccess();
  245.            
  246.             try {
  247.                 stockItems = this.stockController.fetchAllStockItems();
  248.             } catch(SQLException exception) {
  249.                 System.out.println("Failed");
  250.             }
  251.            
  252.             this.stockItemData = FXCollections.observableArrayList(stockItems);
  253.            
  254.             this.productNameColumn.setCellValueFactory(
  255.                     new PropertyValueFactory<>("productName")
  256.             );
  257.            
  258.             this.availableQuantityColumn.setCellValueFactory(
  259.                     new PropertyValueFactory<>("availableQuantity")
  260.             );
  261.    
  262.             this.unitPriceColumn.setCellValueFactory(
  263.                     new PropertyValueFactory<>("unitPrice")
  264.             );
  265.    
  266.             this.reorderLevelColumn.setCellValueFactory(
  267.                     new PropertyValueFactory<>("reorderLevel")
  268.             );
  269.            
  270.             this.totalValueColumn.setCellValueFactory(
  271.                     new PropertyValueFactory<>("totalValue")
  272.             );
  273.            
  274.             this.stockList.setItems(this.stockItemData);
  275.         }
  276.        
  277.         public void populateUsersList() throws IOException, SQLException {
  278.             this.authenticateAdminRights();
  279.            
  280.             ArrayList<User> usersList = null;
  281.            
  282.             try {
  283.                 usersList = this.userController.fetchAllUsers();
  284.             } catch(SQLException exception) {
  285.                 System.out.println("Failed");
  286.             }
  287.            
  288.             this.userListData = FXCollections.observableArrayList(usersList);
  289.            
  290.             this.usernameColumn.setCellValueFactory(
  291.                     new PropertyValueFactory<>("username")
  292.             );
  293.            
  294.             this.rankColumn.setCellValueFactory(
  295.                     new PropertyValueFactory<>("rank")
  296.             );
  297.            
  298.             this.usersList.setItems(this.userListData);
  299.         }
  300.        
  301.         public void initializeStats() throws IOException, SQLException {
  302.             this.authenticateAccess();
  303.             this.setTotalStockValueLabel();
  304.         }
  305.        
  306.         public void setTotalStockValueLabel() {
  307.             ArrayList<StockItem> stockItems = null;
  308.            
  309.             try {
  310.                 stockItems = this.stockController.fetchAllStockItems();
  311.             } catch(Exception exception) {
  312.                 System.out.println(exception);
  313.             }
  314.            
  315.             Double totalStockValue = 0.0;
  316.            
  317.             for(StockItem stockItem : stockItems) {
  318.                 totalStockValue = totalStockValue + Double.parseDouble(stockItem.getTotalValue());
  319.             }
  320.            
  321.             this.totalStockValueLabel.setText(String.format("%.2f", totalStockValue));
  322.         }
  323.        
  324.         public void editStockItem(ActionEvent event) throws IOException, SQLException {
  325.             this.authenticateAccess();
  326.             if(this.stockList.getSelectionModel().getSelectedItem() != null) {
  327.                 FXMLLoader loader = new FXMLLoader(getClass().getResource("EditStockItemDialog.fxml"));
  328.                 Parent editStockItemDialog = loader.load();
  329.                 EditStockItemDialogView editStockItemDialogView = loader.getController();
  330.                
  331.                 editStockItemDialogView.setStockItemObject(this.stockList.getSelectionModel().getSelectedItem());
  332.                 editStockItemDialogView.setStockController(this.stockController);
  333.                 editStockItemDialogView.setMainView(this);
  334.                 editStockItemDialogView.show();
  335.             }
  336.            
  337.         }
  338.        
  339.         public void deleteStockItem(ActionEvent event) throws SQLException, IOException {
  340.             this.authenticateAdminRights();
  341.            
  342.             if(this.stockList.getSelectionModel().getSelectedItem() != null) {
  343.                 this.stockController.removeStockItem(this.stockList.getSelectionModel().getSelectedItem().getID());
  344.             }
  345.            
  346.             this.initializeStats();
  347.             this.populateStocksList();
  348.         }
  349.        
  350.         public void logoutUser(ActionEvent event) throws IOException {
  351.             this.logUserOut();
  352.         }
  353.        
  354.         public void logUserOut() throws IOException {
  355.             this.userController.logout();
  356.            
  357.             FXMLLoader loader = new FXMLLoader(getClass().getResource("LoginWindow.fxml"));
  358.             Parent loginWindow = loader.load();
  359.             LoginWindowView  loginWindowView = loader.getController();
  360.            
  361.             loginWindowView.show(this.mainWindow);
  362.         }
  363.        
  364.         public void addUser(ActionEvent event) throws IOException, SQLException {
  365.             this.authenticateAdminRights();
  366.            
  367.             FXMLLoader loader = new FXMLLoader(getClass().getResource("AddUserDialog.fxml"));
  368.             Parent addUserDialog = loader.load();
  369.             AddUserDialogView addUserDialogView = loader.getController();
  370.            
  371.             addUserDialogView.setMainView(this);
  372.             addUserDialogView.setUserController(this.userController);
  373.             addUserDialogView.show();
  374.         }
  375.        
  376.         public void editUser(ActionEvent event) throws IOException, SQLException {
  377.             this.authenticateAdminRights();
  378.            
  379.             if(this.usersList.getSelectionModel().getSelectedItem() != null) {
  380.                 FXMLLoader loader = new FXMLLoader(getClass().getResource("EditUserDialog.fxml"));
  381.                 Parent editUserDialog = loader.load();
  382.                 EditUserDialogView editUserDialogView = loader.getController();
  383.                
  384.                 editUserDialogView.setMainView(this);
  385.                 editUserDialogView.setUserController(this.userController);
  386.                 editUserDialogView.setUserEntity(this.usersList.getSelectionModel().getSelectedItem());
  387.                 editUserDialogView.show();
  388.             }
  389.         }
  390.        
  391.         public void deleteUser(ActionEvent event) throws SQLException, IOException {
  392.             this.authenticateAdminRights();
  393.            
  394.             if(this.usersList.getSelectionModel().getSelectedItem() != null) {
  395.                 this.userController.deleteUser(this.usersList.getSelectionModel().getSelectedItem().getID());
  396.             }
  397.            
  398.             this.populateUsersList();
  399.         }
  400.        
  401.         public void updateViewForUser() throws IOException, SQLException {
  402.             this.userController.updateLoggedInUser();
  403.            
  404.             int rank = userController.getLoggedInUser().getRank();
  405.            
  406.             if(rank == UserController.RANK_DISABLED_USER) {
  407.                 this.logUserOut();
  408.             } else if(rank == UserController.RANK_REGULAR_USER) {
  409.                 this.userMenu.setDisable(true);
  410.                 this.addStockMenuItem.setDisable(true);
  411.                 this.deleteStockItemButton.setDisable(true);
  412.             } else if(rank == UserController.RANK_ADMINISTRATOR) {
  413.                 this.userMenu.setDisable(false);
  414.                 this.addStockMenuItem.setDisable(false);
  415.                 this.deleteStockItemButton.setDisable(false);
  416.             }
  417.         }
  418.        
  419.         @Override
  420.         public void initialize(URL url, ResourceBundle rb) {
  421.            
  422.         }      
  423.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement