Guest User

Untitled

a guest
Jul 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.72 KB | None | 0 0
  1. public class MainFrame {
  2.    
  3.     //egentligen private static void
  4.     public static void createAndShowGUI() {
  5.        
  6.         final Session session = new Session();
  7.        
  8.         JFrame frame = new JFrame("Dimensions helper");
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.         frame.setLocationRelativeTo(null);
  11.  
  12.         final JPanel contentPane = new JPanel();
  13.         contentPane.setLayout(new CardLayout(5, 5));
  14.        
  15.         /* Skapar instancer av samtliga windows-klasser och lägger till dem
  16.          * i contentpanelen. Contentpanelen styrs av en CardLayout, vilken
  17.          * fungerar på så vis att endast en panel visas åt gången. När man
  18.          * klickar på knapparna next respektive previous anropas CardLayouts
  19.          * funktioner cardlayout.next() resp cardlayout.previous() som ändrar
  20.          * vad som ska visas på skärmen.
  21.          */
  22.        
  23.         /*
  24.          * Placeholder
  25.          */
  26.        
  27.         String un = "u008228";
  28.         String proj = "Mars";
  29.         String prod = "Mars";
  30.        
  31.         Win1 win1 = new Win1(un, proj, prod);
  32.         contentPane.add(win1);
  33.         Win2 win2 = new Win2();
  34.         contentPane.add(win2);
  35.  
  36.         JPanel buttonPanel = new JPanel();
  37.         final JButton previousButton = new JButton("< PREVIOUS");
  38.         previousButton.setEnabled(false);
  39.         final JButton nextButton = new JButton("NEXT >");
  40.         final JButton cancelButton = new JButton("CANCEL");
  41.         buttonPanel.add(cancelButton);
  42.         buttonPanel.add(previousButton);
  43.         buttonPanel.add(nextButton);
  44.        
  45.         /* Skapar eventlistener och eventhandler för previous-knappen. Som default
  46.          * är knappen disablad för att förhindra att användaren ska kunna gå till
  47.          * sista panelen via första. Vid varje anrop kollar den mot Interface
  48.          * Verifiable:s getIdentifier()-funktion för att se om användaren är på login-
  49.          * rutan. Om så disablas knappen återigen. getIdentifier() är överlagrad för
  50.          * samtliga window-klasser och returnerar en unik string för varje klass.
  51.          */
  52.        
  53. //        previousButton.addActionListener(new ActionListener() {
  54. //            public void actionPerformed(ActionEvent ae) {
  55. //              CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  56. //              cardLayout.previous(contentPane);
  57. //              Component[] contents = contentPane.getComponents();
  58. //              nextButton.setText("NEXT");
  59. //                for(Component component : contents) {
  60. //                    if(component instanceof Interface1 && component.isVisible()) {
  61. //                      Verifiable window = (Verifiable)component;
  62. //                      if(window.getIdentifier().equals("FIRST")) {
  63. //                          previousButton.setEnabled(false);
  64. //                      }
  65. //                      break;
  66. //                  }
  67. //              }
  68. //            }
  69.            
  70. //        });
  71.        
  72.        
  73.         /* Skapar eventlistener och eventhandler för next-knappen. Varje gång den
  74.          * anropas kollar den om användaren har fyllt i rätt information. Detta görs
  75.          * via ett interface Verifiable som innehåller isDataValid()-funktionen. Denna
  76.          * är överlagrad för samtliga window-klasser och returnerar true om
  77.          * samtliga åtgärder i klassen har gått igenom.
  78.          */
  79.        
  80.         nextButton.addActionListener(new ActionListener() {
  81.             public void actionPerformed(ActionEvent ae) {
  82.                 nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  83.                 Component[] contents = contentPane.getComponents();
  84.                 for(Component component : contents) {
  85.                     if(component.isVisible() && component instanceof Interface1) {
  86.                         Interface1 window = (Interface1) component;
  87.                         String username = window.getUsername();
  88.                         String password = window.getPassword();
  89.                         boolean login = session.isLoginValid(username, password);
  90.                        
  91.                         if(login) {
  92.                             nextButton.setCursor(Cursor.getDefaultCursor());
  93.                             CardLayout cardLayout = (CardLayout) contentPane.getLayout();
  94.                             cardLayout.next(contentPane);
  95.                             previousButton.setEnabled(true);
  96.                            
  97.                         } else {
  98.                             JOptionPane.showMessageDialog(component, "Wrong username and/or password",
  99.                                     "Error", JOptionPane.ERROR_MESSAGE);
  100.                         }
  101.                         break;
  102.                        
  103.                     } else if(component.isVisible() && component instanceof Interface2) {
  104.                         Interface2 window = (Interface2) component;
  105.                         String items = window.getItemsname();
  106.                        
  107.                        
  108.                         session.test(items);
  109.                     }
  110.                     nextButton.setCursor(Cursor.getDefaultCursor());
  111.                     //break;
  112.                 }
  113.             }
  114.         });
  115.        
  116.        
  117.        
  118. //        nextButton.addActionListener(new ActionListener() {
  119. //            public void actionPerformed(ActionEvent ae) {
  120. //              //TODO: Anropa sessions states
  121. //             
  122. //            }
  123. //        });
  124.        
  125.        
  126.        
  127.        
  128.  
  129.         cancelButton.addActionListener(new ActionListener() {
  130.             public void actionPerformed(ActionEvent ae) {
  131.                 session.terminateConnection();
  132.                 System.exit(0);
  133.             }
  134.            
  135.         });
  136.  
  137.         frame.add(contentPane);
  138.         frame.add(buttonPanel, BorderLayout.PAGE_END);
  139.         frame.setSize(400, 400);
  140.         frame.setVisible(true);
  141.     }
  142.    
  143.    
  144.  
  145.     public static void main(String[] args) {
  146.         SwingUtilities.invokeLater(new Runnable() {
  147.             public void run() {
  148.                 createAndShowGUI();
  149.             }
  150.         });
  151.     }
  152. }
Add Comment
Please, Sign In to add comment