booking system

  1. /*
  2.  * CustomDialog.java
  3.  * The package reservation contains the different class allowing to choose the action to realise.
  4.  */
  5. package reservation;
  6.  
  7. import reservation.system.panels.*;
  8. import reservation.system.FlightSystem;
  9. import javax.swing.*;
  10. import java.awt.event.*;
  11. import java.awt.BorderLayout;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14.  
  15.  
  16. /**
  17.  * This class hold CustomDialog structure. This object analyse the instruction written by the user and determine according to the first element the command to execute.
  18.  * Then it instancie a new object which depends to the command and which will execute the instruction of the user.
  19.  * The new object instantiated create a new window corresponding to the command to execute.
  20.  * @author Texier Mathieu and Frederic Bidon
  21.  */
  22. public class CustomDialog extends JDialog implements Mode {
  23.    
  24.     /**Integer defining the command RELOAD.*/
  25.     static final public int RELOAD = 0;
  26.     /**Integer defining the command READ_COMMAND.*/
  27.     static final public int READ_COMMAND = 1;
  28.     /**Integer defining the command CREATE.*/
  29.     static final public int CREATE = 2;
  30.     /**Integer defining the command STORE.*/
  31.     static final public int STORE = 3;
  32.     /**Integer defining the command RESERVE.*/
  33.     static final public int RESERVE = 4;
  34.     /**Integer defining the command FLIGHTS.*/
  35.     static final public int FLIGHTS = 5;
  36.     /**Integer defining the command CANCEL.*/
  37.     static final public int CANCEL = 6;
  38.     /**Integer defining the command SELECT.*/
  39.     static final public int SELECT = 7;
  40.    
  41.     /**Create a new instance with the level of verbosity QUIET*/
  42.     public CustomDialog () {
  43.         this (QUIET);
  44.     }
  45.    
  46.     /**
  47.      * Create a new instance of CustomDialog
  48.      * @param verboseMode Integer defining the level of verbosity to execute the program.
  49.      */
  50.     public CustomDialog (int verboseMode) {
  51.         super ();
  52.         setModal (true);
  53.         String title = "Flight System";
  54.         this.VerboseMode = VerboseMode;
  55.        
  56.         String laf = UIManager.getSystemLookAndFeelClassName ();
  57.         try {
  58.             UIManager.setLookAndFeel (laf);
  59.         } catch (UnsupportedLookAndFeelException exc) {
  60.             System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
  61.         } catch (Exception exc) {
  62.             System.err.println ("Error loading " + laf + ": " + exc);
  63.         }
  64.        
  65.         addWindowListener (new WindowAdapter () {
  66.             public void windowClosing (WindowEvent evt) {
  67.                 try {
  68.                     if (FlightSystem.getInstance ().getFlightList ().length > 0) {
  69.                         int res = JOptionPane.showConfirmDialog (null, "Would you like to save the dataBase ?", "save", JOptionPane.YES_NO_OPTION);
  70.                         if (res == JOptionPane.OK_OPTION)
  71.                             MainPanel.Store ();
  72.                     }
  73.                 } catch (Exception e) {alert (e);}
  74.                 System.exit (0);
  75.             }
  76.         });
  77.        
  78.         setResizable (false);
  79.         setTitle (title);
  80.         action = new MainPanel (this);
  81.        
  82.         JLabel titleLabel = new JLabel ();
  83.         titleLabel.setFont (new Font ("MS Sans Serif", 1, 18));
  84.         titleLabel.setHorizontalAlignment (SwingConstants.CENTER);
  85.         titleLabel.setText (title);
  86.        
  87.         getContentPane ().add (titleLabel, BorderLayout.NORTH);
  88.         getContentPane ().add ((Panels)action, BorderLayout.CENTER);
  89.         pack ();
  90.     }
  91.    
  92.     /**
  93.      * This method determine the number of element of the user's instruction and analyse the first element of this one.
  94.      * If it corresponds to an available command of the system, it create a new object depending of the command.
  95.      * This new object will execute the instruction with the other elements of the command.
  96.      * @param owner Dilog or frame from which the CustomDialog is issue
  97.      * @param modal Modal mode
  98.      * @param title The title of the window
  99.      * @param command The identifier of the command (RELOAD, CREATE, CANCEL ...)
  100.      */
  101.     public CustomDialog (CustomDialog owner, boolean modal, String title, int command) {
  102.         super (owner, modal);
  103.         this.owner = owner;
  104.        
  105.         String laf = UIManager.getSystemLookAndFeelClassName ();
  106.         try {
  107.             UIManager.setLookAndFeel (laf);
  108.         } catch (UnsupportedLookAndFeelException exc) {
  109.             System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
  110.         } catch (Exception exc) {
  111.             System.err.println ("Error loading " + laf + ": " + exc);
  112.         }
  113.        
  114.         setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
  115.         setResizable (false);
  116.         setTitle (title);
  117.        
  118.         JLabel titleLabel = new JLabel ();
  119.         titleLabel.setFont (new java.awt.Font ("MS Sans Serif", 1, 18));
  120.         titleLabel.setHorizontalAlignment (SwingConstants.CENTER);
  121.         titleLabel.setText (title);
  122.        
  123.         switch(command){
  124.             case CREATE:
  125.                 action = new CreatePanel ();
  126.                 break;
  127.             case RESERVE:
  128.                 action = new ReservePanel ();
  129.                 break;
  130.             case CANCEL:
  131.                 action = new CancelPanel ();
  132.                 break;
  133.             case SELECT:
  134.                 action = new SelectPanel ();
  135.                 break;
  136.             case FLIGHTS:
  137.                 action = new FlightsPanel ();
  138.                 break;
  139.             default:
  140.                 action = new MainPanel ();
  141.                 break;
  142.         }
  143.         ((Panels) action).init ();
  144.         FootPanel footPanel = new FootPanel ();
  145.        
  146.         getContentPane ().add (titleLabel, BorderLayout.NORTH);
  147.         getContentPane ().add ( (Panels) action, BorderLayout.CENTER);
  148.         getContentPane ().add (footPanel, BorderLayout.SOUTH);
  149.         pack ();
  150.     }
  151.    
  152.    
  153.     /**
  154.      * This method create a new dialog window according to the selected command.
  155.      */
  156.     public void process () {
  157.         if (owner != null)
  158.             ((Panels) owner.action).init ();
  159.         show ();
  160.     }
  161.    
  162.    
  163.     /**
  164.      * This method allows to access the private string retValue.
  165.      * @return the string retValue.
  166.      */
  167.     public String getRetValue () {
  168.         return retValue;
  169.     }
  170.    
  171.    
  172.     /**
  173.      * Open a new window to specify the produced error by the user.
  174.      * @param exception The message that will be displayed in the message box
  175.      */
  176.     static public void alert (Exception exception) {
  177.         JOptionPane.showMessageDialog (null, exception.getMessage (), "Error", JOptionPane.ERROR_MESSAGE);
  178.         if (VerboseMode == DEBUG)
  179.             exception.printStackTrace ();
  180.     }
  181.    
  182.    
  183.     /**
  184.      * Open a new dialog window to give more informations to the user.
  185.      * @param info The message that will be displayed in the message box
  186.      */
  187.     static public void alert (String info) {
  188.         JOptionPane.showMessageDialog (null, info, "Information", JOptionPane.INFORMATION_MESSAGE);
  189.     }
  190.    
  191.    
  192.     /**
  193.      * Hide the dialog
  194.      */
  195.     protected void quit (){
  196.         hide ();
  197.     }
  198.    
  199.     private CustomDialog owner;
  200.    
  201.     private  Action action = new MainPanel ();
  202.    
  203.     private String retValue = new String ();
  204.    
  205.     static private int VerboseMode = NORMAL;
  206.    
  207.     private class FootPanel extends JPanel {
  208.        
  209.         /** Creates new form footPanel */
  210.         public FootPanel () {
  211.             validButton = new JButton ();
  212.             cancelButton = new JButton ();
  213.            
  214.             validButton.setText ("Valid");
  215.             add (validButton);
  216.            
  217.             validButton.addActionListener (new ActionListener () {
  218.                 public void actionPerformed (ActionEvent evt) {
  219.                     try {
  220.                         retValue = action.execute (null);
  221.                     } catch (Exception e) {
  222.                         CustomDialog.alert (e);
  223.                     }
  224.                     quit ();
  225.                 }
  226.             });
  227.             cancelButton.setText ("Cancel");
  228.             cancelButton.addActionListener (new ActionListener () {
  229.                 public void actionPerformed (ActionEvent evt) {
  230.                     quit ();
  231.                 }
  232.             });
  233.             add (cancelButton);
  234.         }
  235.         private JButton cancelButton;
  236.         private JButton validButton;
  237.     }
  238. }

contact - link to this site