Advertisement
Guest User

FelerzinhaXp

a guest
Oct 3rd, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.58 KB | None | 0 0
  1. package com.amdema.asclient;
  2.  
  3. import com.amdema.asclient.controller.*;
  4. import com.amdema.asclient.model.Colaborador;
  5.  
  6. import java.io.IOException;
  7. import java.net.ServerSocket;
  8. import java.net.URL;
  9. import java.time.format.DateTimeFormatter;
  10. import java.util.Locale;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. import com.amdema.asclient.utils.Alerta;
  15. import com.amdema.asclient.utils.Window;
  16. import javafx.application.Application;
  17.  
  18.  
  19. import javafx.application.Platform;
  20. import javafx.fxml.FXMLLoader;
  21. import javafx.scene.Parent;
  22. import javafx.scene.Scene;
  23. import javafx.scene.image.Image;
  24. import javafx.stage.Stage;
  25. import javafx.stage.StageStyle;
  26. import javax.imageio.ImageIO;
  27.  
  28. public class MainApp extends Application {
  29.  
  30.     public static Stage mainStage;
  31.     public static Stage loadingStage;
  32.     public static Stage reportStage;
  33.     public static ReportController mainController;
  34.  
  35.     // AMDEMA PYTHON SERVER RUNNING AT ASIA MACHINE
  36.     public static final String SERVERURL = "http://10.13.30.252:5000";
  37.     //public static final String SERVERURL = "http://0.0.0.0:5000";
  38.  
  39.     // FTP SERVER RUNNING AT ASIA MACHINE
  40.     public static final String FTPURL = "10.13.30.252";
  41.     //public static final String FTPURL = "localhost";
  42.     public static final String FTPUSER = "FTP_ASQUALITY";
  43.     public static final String FTPPASSWORD = "amdema@2018";
  44.  
  45.     // LOGGED IN COLABORADOR
  46.     public static Colaborador colaborador;
  47.     public static long validade = 3; // Validade das senhas dos colaboradores em meses
  48.     public static ServerSocket programLock;
  49.     // TODO: Change it to a util function or a collection of functions
  50.     public static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy", new Locale("pt", "BR"));
  51.  
  52.     // STYLE TO BE ADDED TO EVERY NEW STAGE
  53.     public static URL style;
  54.  
  55.     @Override
  56.     public void start(Stage stage) throws Exception {
  57.         MainApp.mainStage = stage;
  58.  
  59.         Parent root = FXMLLoader.load(getClass().getResource("/fxml/Login.fxml"));
  60.         style = getClass().getResource("/styles/bootstrap3.css");
  61.  
  62.         try {
  63.             programLock = new ServerSocket(5003);
  64.         } catch (Exception ex) {
  65.             Alerta.alertarErro("Erro", "O Sistema já está aberto em outra aplicação!");
  66.             System.exit(0);
  67.         }
  68.  
  69.         // Adiciona um listener, qnd fechar o programa, libera o socket
  70.         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  71.                     try {
  72.                         programLock.close();
  73.                     } catch (IOException e) {
  74.                         e.printStackTrace();
  75.                     }
  76.                 })
  77.         );
  78.  
  79.         Scene scene = new Scene(root);
  80.         scene.getStylesheets().add("/styles/bootstrap3.css");
  81.  
  82.         // Adiciona o Icon Tray
  83.         Platform.setImplicitExit(false);
  84.         javax.swing.SwingUtilities.invokeLater(this::addAppToTray);
  85.  
  86.         MainApp.mainStage.setScene(scene);
  87.         MainApp.mainStage.setTitle("Login");
  88.         MainApp.mainStage.setResizable(true);
  89.         MainApp.mainStage.sizeToScene();
  90.         MainApp.mainStage.centerOnScreen();
  91.         MainApp.mainStage.show();
  92.         MainApp.mainStage.getIcons().clear();
  93.         Image icon = new Image("/images/logo_icon_desk.png");
  94.         MainApp.mainStage.getIcons().add(icon);
  95.         createWindows();
  96.     }
  97.  
  98.     /**
  99.      * The main() method is ignored in correctly deployed JavaFX application.
  100.      * main() serves only as fallback in case the application can not be
  101.      * launched through deployment artifacts, e.g., in IDEs with limited FX
  102.      * support. NetBeans ignores main().
  103.      *
  104.      * @param args the command line arguments
  105.      */
  106.     public static void main(String[] args) {
  107.         launch(args);
  108.     }
  109.  
  110.     public static void createWindows() {
  111.         Window.addWindow(new Window<VideosDeAjudaController>(
  112.                 "VideosDeAjuda",
  113.                 "Videos de Ajuda",
  114.                 true, false));
  115.  
  116.  
  117.         Window.addWindow(new Window<SenhaEsquecidaController>(
  118.                 "SenhaEsquecida",
  119.                 "Pedir Nova Senha",
  120.                 false, false));
  121.  
  122.  
  123.         Window.addWindow(new Window<MinhasOcorrenciasController>(
  124.                 "MinhasOcorrencias",
  125.                 "Minhas Ocorrências",
  126.                 true, false));
  127.  
  128.         Window.addWindow(new Window<RelatorioEficaciaController>(
  129.                 "RelatorioEficacia",
  130.                 "Relatório Eficácia",
  131.                 true, false));
  132.  
  133.         Window.addWindow(new Window<DetalheApontamentoController>(
  134.                 "DetalheApontamento",
  135.                 "Detalhes Ocorrência",
  136.                 false, false));
  137.  
  138.         Window.addWindow(new Window<RelatorioLossController>(
  139.                 "RelatorioLoss",
  140.                 "Relatório de Loss",
  141.                 true, false));
  142.  
  143.         Window.addWindow(new Window<RelatorioController>(
  144.                 "Relatorio",
  145.                 "Relatório de Ocorrências",
  146.                 true, false));
  147.  
  148.         Window.addWindow(new Window<ListaQualidadesController>(
  149.                 "ListaQualidades",
  150.                 "Planos de Ação",
  151.                 true, false));
  152.  
  153.         Window.addWindow(new Window<CadastroQualidadeController>(
  154.                 "CadastroQualidade",
  155.                 "Plano de Ação",
  156.                 false, false));
  157.     }
  158.  
  159.     public static LoadingGeralController loading(FXMLLoader loader) {
  160.  
  161.         Parent root = null;
  162.         try {
  163.             root = loader.load();
  164.         } catch (IOException ex) {
  165.             Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
  166.         }
  167.  
  168.         LoadingGeralController lc = loader.getController();
  169.  
  170.         Scene scene = new Scene(root);
  171.         scene.getStylesheets().add("/styles/bootstrap3.css");
  172.         loadingStage = new Stage();
  173.         loadingStage.setScene(scene);
  174.         loadingStage.initStyle(StageStyle.UNDECORATED);
  175.         loadingStage.setResizable(false);
  176.         loadingStage.centerOnScreen();
  177.  
  178.         lc.setLabel("Carregando...");
  179.  
  180.         loadingStage.show();
  181.  
  182.         loadingStage.toFront();
  183.         loadingStage.setAlwaysOnTop(true);
  184.         loadingStage.requestFocus();
  185.  
  186.         return lc;
  187.     }
  188.  
  189.     public void abrirMinhasOcorrencias() {
  190.         // Get current Window status
  191.         int status = Window.getWindow("MinhasOcorrencias").getStatus();
  192.         // Open the Window
  193.         Window<MinhasOcorrenciasController> window = Window.openWindow("MinhasOcorrencias");
  194.  
  195.         // Test if the new status is ERROR, else if the old status was closed
  196.         if (window.getStatus() == Window.ERROR) {
  197.             // TODO: PopUp de Erro
  198.         } else if (status == Window.CLOSED) {
  199.             // Call the loading function
  200.             window.getController().show();
  201.         }
  202.     }
  203.  
  204.     private void addAppToTray() {
  205.         try {
  206.             // Pega o Toolkit AWT.
  207.             java.awt.Toolkit.getDefaultToolkit();
  208.  
  209.             // Testa se o OS suporta Icon Tray
  210.             if (!java.awt.SystemTray.isSupported()) {
  211.                 System.out.println("No system tray support, application exiting.");
  212.             } else {
  213.  
  214.                 // Setup do icon tray
  215.                 java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();
  216.                 java.awt.Image image = ImageIO.read(getClass().getClassLoader().getResource("images/logo_icon_tray.png"));
  217.                 java.awt.TrayIcon trayIcon = new java.awt.TrayIcon(image);
  218.  
  219.                 // Duplo clique mostra o MainStage dnv
  220.                 trayIcon.addActionListener(event -> Platform.runLater(this::showStage));
  221.  
  222.                 // Adiciona itens ao menu do Icon Tray
  223.                 java.awt.MenuItem openItem = new java.awt.MenuItem("Abrir");
  224.                 openItem.addActionListener(event -> Platform.runLater(() -> {
  225.                     reportStage.show();
  226.                 }));
  227.  
  228.                 // Adiciona itens ao menu do Icon Tray
  229.                 java.awt.MenuItem openOcorrencias = new java.awt.MenuItem("Minhas Ocorrências");
  230.                 openOcorrencias.addActionListener(event -> Platform.runLater(() -> {
  231.                     abrirMinhasOcorrencias();
  232.                 }));
  233.  
  234.                 // Configura a fonte do Menu
  235.                 java.awt.Font defaultFont = java.awt.Font.decode(null);
  236.                 java.awt.Font boldFont = defaultFont.deriveFont(java.awt.Font.BOLD);
  237.                 openItem.setFont(boldFont);
  238.  
  239.                 // Adiciona o Sair do programa
  240.                 java.awt.MenuItem exitItem = new java.awt.MenuItem("Sair");
  241.                 exitItem.addActionListener(event -> {
  242.                     tray.remove(trayIcon);
  243.                     Platform.exit();
  244.                 });
  245.  
  246.                 final java.awt.PopupMenu popup = new java.awt.PopupMenu();
  247.                 popup.add(openItem);
  248.                 popup.addSeparator();
  249.                 popup.add(exitItem);
  250.                 trayIcon.setPopupMenu(popup);
  251.  
  252.                 // Adiciona o Icon Tray ao Tray
  253.                 trayIcon.setImageAutoSize(true);
  254.                 tray.add(trayIcon);
  255.             }
  256.         } catch (java.awt.AWTException e) {
  257.             System.out.println("Unable to init system tray");
  258.             e.printStackTrace();
  259.         } catch (IOException e) {
  260.             e.printStackTrace();
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * Shows the application stage and ensures that it is brought ot the front
  266.      * of all stages.
  267.      */
  268.     private void showStage() {
  269.         if (mainStage != null) {
  270.             mainStage.show();
  271.             mainStage.toFront();
  272.         }
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement