×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:05 PM
Views: 2190
Tags: no tags
  1. package inventorysystem;
  2.  
  3. import inventorysystem.gui.login;
  4. import java.awt.Dimension;
  5. import java.util.Vector;
  6. import javax.swing.GroupLayout;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLayeredPane;
  9. import javax.swing.JPanel;
  10. import interfaces.Switchable;
  11. import inventorysystem.gui.Products;
  12. import inventorysystem.gui.Supplier;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import javax.swing.JMenu;
  16. import javax.swing.JMenuItem;
  17. import javax.swing.JOptionPane;
  18.  
  19. /**
  20.  *
  21.  * @author Epic
  22.  */
  23. public class InventorySystem {
  24.     private static GroupLayout panelHolder = null;
  25.     private JLayeredPane pane = null;
  26.     private static Vector panelStack;
  27.     static  Switchable[] switchables;
  28.  
  29.    
  30.  
  31.     public InventorySystem() {
  32.     }
  33.    
  34.     public InventorySystem(String title) {
  35.          /*
  36.         * Step 1: Create a  vector that  will hold all the panels this application will use.
  37.         * Reason : this is done so we can swap between panels when we need to.
  38.         * Step 2: Create a JLayeredPane this pane will hold the current panel in view
  39.         * our initial panel is obviously the login panel. after which we create the JFrame to
  40.         * hold our application together.
  41.         */
  42.         panelStack = new Vector();
  43.         pane = new JLayeredPane();
  44.        
  45.         login login = new login();
  46.        
  47.         pane.add(login);
  48.         panelStack.add(0,login);
  49.        
  50.         panelHolder = new javax.swing.GroupLayout(pane);
  51.         pane.setLayout(panelHolder);
  52.  
  53.         panelHolder.setHorizontalGroup(
  54.                 panelHolder.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  55.                 .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 1200, Short.MAX_VALUE)
  56.         );
  57.         panelHolder.setVerticalGroup(
  58.                 panelHolder.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  59.                 .addComponent(login, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
  60.         );
  61.        
  62.        
  63.        
  64.         applicationFrame();
  65.        
  66.     }
  67.  
  68.     private JFrame applicationFrame(){
  69.         JFrame frame = new JFrame("Inventory SYS");
  70.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  71.         frame.setLocationRelativeTo(null);
  72.         frame.setSize(new Dimension(400, 300));
  73.         frame.add(pane);
  74.         frame.setJMenuBar(topMenu());
  75.         frame.setVisible(true);
  76.         frame.show(true);
  77.         return frame;
  78.     }
  79.      private javax.swing.JMenuBar topMenu(){
  80.          javax.swing.JMenuBar topMenu = new javax.swing.JMenuBar();
  81.          JMenu fileMenu = new JMenu("File");
  82.          fileMenu.add(new JMenuItem("Exit"));
  83.          
  84.          //Action menu
  85.          JMenu navigateMenu = new JMenu("Navigate");
  86.          JMenuItem productItem = new JMenuItem("Product");
  87.          productItem.addActionListener(this::addNewProduct);
  88.          navigateMenu.add(productItem);
  89.          
  90.          JMenuItem supplerItem = new JMenuItem("Add Suppler");
  91.          supplerItem.addActionListener(this::addNewSuppler);
  92.          navigateMenu.add(supplerItem);
  93.          
  94.          topMenu.add(fileMenu);
  95.          topMenu.add(navigateMenu);
  96.          return topMenu;
  97.      }
  98.      
  99.     /**
  100.      * @param args the command line arguments
  101.      */
  102.     public static void main(String[] args) {
  103.        InventorySystem is = new InventorySystem("");
  104.     }
  105.     /**
  106.      * switch between panels on the jframe as a means to show different parts of the application
  107.      * in one frame.
  108.      * This is a static method because other classes would use the static vector, which is
  109.      * used as an instance variable will throw a NULL-POINTER-EXCEPTION
  110.      * @param panel
  111.      * @param isLoggedIn to determine if user is logged in
  112.      */
  113.     public static void switchPanel(JPanel panel,Boolean isLoggedIn)
  114.     {
  115.         JPanel currentPanel;
  116.         // STEP1: get the  current panel ontop the panel stack
  117.         // Step 2: if the stack is not empty and if we are not trying to replace a panel with itself
  118.         // STep 3: then switch the panel with the one we want to see
  119.         // Precaution: only a loggedin user can change panel or enter application.
  120.         if(isLoggedIn)
  121.         {
  122.             if(!panelStack.isEmpty())
  123.             {
  124.                 currentPanel =  (JPanel) panelStack.get(0);
  125.                 if(currentPanel instanceof Switchable && panel instanceof Switchable)
  126.                 {
  127.                     if(currentPanel != panel){
  128.                         panelHolder.replace(currentPanel, panel);
  129.                         panelStack.add(0,panel);
  130.                     }
  131.                 }
  132.             }
  133.         }else{
  134.             JOptionPane.showMessageDialog(null, "Login to visit "+panel.getName()+" panel.");
  135.         }
  136.     }
  137.    
  138.    
  139. //<editor-fold defaultstate="collapsed" desc="Action listener methods">
  140.     /**
  141.      * Action to call new product panel
  142.      * @param evt
  143.      */
  144.     private void addNewProduct(java.awt.event.ActionEvent evt){
  145.          switchPanel(new Products(),login.isLoggedIn());
  146.      }
  147.     private void addNewSuppler(java.awt.event.ActionEvent evt)
  148.      {
  149.          switchPanel(new Supplier(), login.isLoggedIn());
  150.      }
  151.    
  152.     //</editor-fold>
  153. }
  154.