Système de réservation

  1. /*
  2.  * MainPanel.java
  3.  * The package panels contains the different class executing the different windows of each function of the FlightSystem Reservation.
  4.  */
  5. package reservation.system.panels;
  6.  
  7. import reservation.*;
  8. import reservation.system.functions.*;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.filechooser.*;
  12. import java.awt.Dimension;
  13. import java.awt.event.*;
  14. import java.io.*;
  15.  
  16. /**
  17.  * Create the object MainPanel. It define the first window where the user can chose the command to execute and that displays the result of the command.
  18.  * @author Texier Mathieu and Frederic Bidon
  19.  */
  20. public class MainPanel extends Panels {
  21.    
  22.     /** Creates new form MainPanel */
  23.     public MainPanel () {
  24.     }
  25.    
  26.     /**
  27.      * Constructor of this class. Do all the instance for create the graphical window.
  28.      * @param mainDlg Parent object
  29.      */
  30.     public MainPanel (final CustomDialog mainDlg) {
  31.         actionLabel = new JLabel ();
  32.         actionComboBox = new JComboBox ();
  33.         jScrollPane1 = new JScrollPane ();
  34.         resultTextArea = new JTextArea ();
  35.        
  36.         actionLabel.setText ("Action");
  37.         add (actionLabel);
  38.        
  39.         actionComboBox.addActionListener (new ActionListener () {
  40.             public void actionPerformed (ActionEvent evt) {
  41.                 Action (mainDlg);
  42.             }
  43.         });
  44.         add (actionComboBox);
  45.        
  46.         resultTextArea.setEditable (false);
  47.         jScrollPane1.setViewportView (resultTextArea);
  48.         add (jScrollPane1);
  49.         init ();
  50.     }
  51.    
  52.     /**
  53.      * Determine the command chosen by the user to create the apropriate window.
  54.      */
  55.     private void Action (CustomDialog mainDlg) {
  56.         int nAction = actionComboBox.getSelectedIndex ();
  57.         String retValue = "Not executed \n";
  58.         JFileChooser chooser = null;
  59.         ExampleFileFilter filter;
  60.         Functions action;
  61.        
  62.         switch (nAction) {
  63.             case CustomDialog.STORE:
  64.                 retValue = Store ();
  65.                 break;
  66.                
  67.             case CustomDialog.READ_COMMAND:
  68.                 retValue = ReadComand ();
  69.                 break;
  70.                
  71.             case CustomDialog.RELOAD:
  72.                 retValue = Reload ();
  73.                 break;
  74.                
  75.             case CustomDialog.CREATE:
  76.             case CustomDialog.RESERVE:
  77.             case CustomDialog.CANCEL:
  78.             case CustomDialog.SELECT:
  79.             case CustomDialog.FLIGHTS:
  80.                 CustomDialog dlg = new CustomDialog (mainDlg, true, (String) actionComboBox.getSelectedItem (),nAction);
  81.                 dlg.process ();
  82.                 retValue = dlg.getRetValue ();
  83.                 break;
  84.             default:
  85.                 CustomDialog.alert (new Exception ("Wrong slot value."));
  86.         }
  87.         init ();
  88.         resultTextArea.append (retValue);
  89.     }
  90.    
  91.     /**
  92.      * Open a file selector dialog and read interprete the Request Comand Message that are contained
  93.      * @return the appropriate result
  94.      */    
  95.     static public String ReadComand () {
  96.         JFileChooser chooser = new JFileChooser ();
  97.         ExampleFileFilter filter = new ExampleFileFilter ();
  98.         filter.addExtension ("rcm");
  99.         filter.setDescription ("request command message");
  100.         chooser.setFileFilter (filter);
  101.         if(chooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) {
  102.             File file = chooser.getSelectedFile ();
  103.             try {
  104.                 BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (file)));
  105.                 Command mode = new Command (in,Mode.QUIET);
  106.                 mode.process ();
  107.                 return "File Loaded";
  108.             } catch (FileNotFoundException e){CustomDialog.alert (new Exception ("error while opening file :" + file.getAbsolutePath ()));}
  109.         }
  110.         return "Read command failed";
  111.     }
  112.    
  113.     /**
  114.      * Open a file selector dialog and deserialize the containing Flight System
  115.      * @return the appropriate result
  116.      */    
  117.     static public String Reload () {
  118.         Functions action = new Reload ();
  119.         JFileChooser chooser = new JFileChooser ();
  120.         ExampleFileFilter filter = new ExampleFileFilter ();
  121.         filter.addExtension ("out");
  122.         filter.setDescription ("*.out");
  123.         chooser.setFileFilter (filter);
  124.         if(chooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) {
  125.             String fileName[] = new String [1];
  126.             File datafile = chooser.getSelectedFile ();
  127.             fileName[0] = datafile.getAbsolutePath ();
  128.             try {
  129.                 return action.execute (fileName) + "\n";
  130.             }catch (Exception e) {CustomDialog.alert (e);}
  131.         }
  132.         return " Reload Failed";
  133.     }
  134.    
  135.     /**
  136.      * Open a file selector dialog and store the serialzed Flight System
  137.      * @return the appropriate result
  138.      */    
  139.     static public String Store () {
  140.         Functions action = new Store ();
  141.         JFileChooser chooser = new JFileChooser ();
  142.         ExampleFileFilter filter = new ExampleFileFilter ();
  143.         filter.addExtension ("out");
  144.         filter.setDescription ("*.out");
  145.         chooser.setFileFilter (filter);
  146.         if(chooser.showSaveDialog (null) == JFileChooser.APPROVE_OPTION) {
  147.             String fileName[] = new String [1];
  148.             File datafile = chooser.getSelectedFile ();
  149.             fileName[0] = datafile.getAbsolutePath ();
  150.             try {
  151.                 return action.execute (fileName) + "\n";
  152.             }catch (Exception e) {CustomDialog.alert (e);}
  153.         }
  154.         return "Store failed";
  155.     }
  156.     /**
  157.      * Causes this container to lay out its components.
  158.      */
  159.     public void doLayout () {
  160.         actionLabel.setBounds (30, 10, 80, 20);
  161.         actionComboBox.setBounds (100, 10, 110, 20);
  162.         jScrollPane1.setBounds (20, 40, 450, 90);
  163.     }
  164.    
  165.     /**
  166.      * Set the size of the panel
  167.      * @return an instance of Dimension that represents the minimum size of this container.
  168.      */
  169.     public Dimension getMinimumSize () {
  170.         return new Dimension (500,140);
  171.     }
  172.    
  173.     /**
  174.      * Set the size of the panel
  175.      * @return an instance of Dimension that represents the preferred size of this container.
  176.      */
  177.     public Dimension getPreferredSize () {
  178.         return new Dimension (500,140);
  179.     }
  180.    
  181.     /**
  182.      * Initialize the number of the field and fill them with default values.
  183.      * Dispaly the label of the Window.
  184.      */
  185.     public void init () {
  186.         actionComboBox.removeAllItems ();
  187.         try {
  188.             actionComboBox.insertItemAt ("Reload", CustomDialog.RELOAD);
  189.             actionComboBox.insertItemAt ("Read command", CustomDialog.READ_COMMAND);
  190.             actionComboBox.insertItemAt ("Create", CustomDialog.CREATE);
  191.             if (fs.getFlightList ().length > 0) {
  192.                 actionComboBox.insertItemAt ("Store", CustomDialog.STORE);
  193.                 actionComboBox.insertItemAt ("Reserve", CustomDialog.RESERVE);
  194.                 actionComboBox.insertItemAt ("Flights", CustomDialog.FLIGHTS);
  195.                 if (fs.getBookingList ().length > 0) {
  196.                     actionComboBox.insertItemAt ("Cancel", CustomDialog.CANCEL);
  197.                     actionComboBox.insertItemAt ("Select", CustomDialog.SELECT);
  198.                 }
  199.             }
  200.         }
  201.         catch (Exception e) {CustomDialog.alert (e);}
  202.     }
  203.    
  204.     /**
  205.      * Proceed the execution of the action
  206.      * @return the result of the request.
  207.      * @param unUsed Not used
  208.      * @throws Exception if the command is not executed.
  209.      */
  210.     public String execute (String[] unUsed) throws Exception {
  211.         return "";
  212.     }
  213.    
  214.     private JComboBox actionComboBox;
  215.     private JLabel actionLabel;
  216.     private JScrollPane jScrollPane1;
  217.     private JTextArea resultTextArea;
  218. }
  219.  

contact - faire un lien