Advertisement
Guest User

MineAvtaler.java

a guest
May 3rd, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.43 KB | None | 0 0
  1. package mineavtaler;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. import javax.swing.*;
  7.  
  8. /**
  9.  * This is the project's main window. The application entry-point is also placed
  10.  * in this class
  11.  *
  12.  * @author Tomas Sandven
  13.  */
  14. public class MineAvtaler extends JFrame implements ActionListener
  15. {
  16.     private static final long serialVersionUID = -5700357531421820495L;
  17.  
  18.     /*
  19.      * GUI Components
  20.      */
  21.     private JScrollPane listScroll;
  22.     private JList appointmentList;
  23.  
  24.     private JPanel buttonColumn;
  25.     private JButton newAppointmentButton;
  26.     private JButton removeAppointmentButton;
  27.     private JButton showAppointmentButton;
  28.     private JButton editAppointmentButton;
  29.     private JButton exitButton;
  30.  
  31.     /*
  32.      * Other fields
  33.      */
  34.     private Avtaledata listeData;
  35.  
  36.     // A mouselistener for the double click event on the list
  37.     private MouseListener appointmentListMouseListener;
  38.  
  39.     /**
  40.      * The application's entry point
  41.      */
  42.     public static void main(String[] args)
  43.     {
  44.         // Try setting windows default look-and-feel
  45.         try
  46.         {
  47.             String laf = UIManager.getSystemLookAndFeelClassName();
  48.             UIManager.setLookAndFeel(laf);
  49.         }
  50.         catch (Exception e)
  51.         {}
  52.  
  53.         // Create and display the main window
  54.         MineAvtaler window = new MineAvtaler();
  55.         window.setVisible(true);
  56.     }
  57.  
  58.     /**
  59.      * MineAvtaler constructor. Sets default settings
  60.      */
  61.     public MineAvtaler()
  62.     {
  63.         /*
  64.          * Initialization
  65.          */
  66.         this.listeData = new Avtaledata();
  67.         // Create a new MouseListener (I hate how I have to override every
  68.         // abstract function)
  69.         this.appointmentListMouseListener = new MouseListener()
  70.         {
  71.             public void mouseClicked(MouseEvent e)
  72.             {
  73.                 // If doubleclick
  74.                 if (e.getClickCount() == 2)
  75.                 {
  76.                     // Get the list object and find the index
  77.                     JList list = (JList) e.getSource();
  78.                     int index = list.getSelectedIndex();
  79.  
  80.                     // If a row is selected...
  81.                     if (index != -1)
  82.                     {
  83.                         // Fetch the ListModel from the JList object, cast it to
  84.                         // the Avtaledata class and fetch the selected Avtale
  85.                         // object - then display it's details
  86.                         ((Avtaledata) list.getModel()).getAvtale(index).displayDetails();
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             public void mouseEntered(MouseEvent e)
  92.             {
  93.             }
  94.  
  95.             public void mouseExited(MouseEvent e)
  96.             {
  97.             }
  98.  
  99.             public void mousePressed(MouseEvent e)
  100.             {
  101.             }
  102.  
  103.             public void mouseReleased(MouseEvent e)
  104.             {
  105.             }
  106.         };
  107.  
  108.         /*
  109.          * Window GUI settings
  110.          */
  111.  
  112.         // Set window size
  113.         this.setSize(640, 240);
  114.         this.setMinimumSize(new Dimension(640, 240));
  115.         this.setResizable(false);
  116.  
  117.         // Appear at default OS window location
  118.         this.setLocationByPlatform(true);
  119.  
  120.         // Set window title
  121.         this.setTitle("Mine avtaler");
  122.  
  123.         // Initialize application GUI
  124.         this.initializeComponents();
  125.     }
  126.  
  127.     /**
  128.      * Actionlistener
  129.      */
  130.     public void actionPerformed(ActionEvent e)
  131.     {
  132.         if (e.getSource().equals(this.newAppointmentButton))
  133.         {
  134.             this.newAppointment();
  135.         }
  136.         if (e.getSource().equals(this.removeAppointmentButton))
  137.         {
  138.             this.removeAppointment();
  139.         }
  140.         if (e.getSource().equals(this.showAppointmentButton))
  141.         {
  142.             this.showAppointment();
  143.         }
  144.         if (e.getSource().equals(this.editAppointmentButton))
  145.         {
  146.             this.editAppointment();
  147.         }
  148.         if (e.getSource().equals(this.exitButton))
  149.         {
  150.             this.exit();
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Initialize GUI components
  156.      *
  157.      * GUI creation convention borrowed from C#. Place all GUI instantiation and
  158.      * configuration in one function and call it from the constructor
  159.      */
  160.     public void initializeComponents()
  161.     {
  162.         // Initialize and configure components
  163.         this.setLayout(new BorderLayout());
  164.  
  165.         this.listScroll = new JScrollPane();
  166.  
  167.         this.appointmentList = new JList(this.listeData);
  168.         this.appointmentList.addMouseListener(this.appointmentListMouseListener);
  169.  
  170.         this.buttonColumn = new JPanel();
  171.         this.buttonColumn.setLayout(new GridLayout(5, 1));
  172.  
  173.         this.newAppointmentButton = new JButton();
  174.         this.newAppointmentButton.setText("Ny avtale");
  175.         this.newAppointmentButton.addActionListener(this);
  176.  
  177.         this.removeAppointmentButton = new JButton();
  178.         this.removeAppointmentButton.setText("Fjern avtale");
  179.         this.removeAppointmentButton.addActionListener(this);
  180.  
  181.         this.showAppointmentButton = new JButton();
  182.         this.showAppointmentButton.setText("Vis avtale");
  183.         this.showAppointmentButton.addActionListener(this);
  184.  
  185.         this.editAppointmentButton = new JButton();
  186.         this.editAppointmentButton.setText("Endre avtale");
  187.         this.editAppointmentButton.addActionListener(this);
  188.  
  189.         this.exitButton = new JButton();
  190.         this.exitButton.setText("Avslutt");
  191.         this.exitButton.addActionListener(this);
  192.  
  193.         // Link components
  194.         this.listScroll.setViewportView(this.appointmentList);
  195.  
  196.         this.buttonColumn.add(newAppointmentButton);
  197.         this.buttonColumn.add(removeAppointmentButton);
  198.         this.buttonColumn.add(showAppointmentButton);
  199.         this.buttonColumn.add(editAppointmentButton);
  200.         this.buttonColumn.add(exitButton);
  201.  
  202.         this.add(this.listScroll, BorderLayout.CENTER);
  203.         this.add(this.buttonColumn, BorderLayout.LINE_END);
  204.     }
  205.  
  206.     /**
  207.      * Add a new appointment to the list
  208.      */
  209.     private void newAppointment()
  210.     {
  211.         this.listeData.nyAvtale();
  212.     }
  213.  
  214.     /**
  215.      * Remove an appointment from the list
  216.      */
  217.     private void removeAppointment()
  218.     {
  219.         // Check if index is -1 (none selected) then delete that Avtale object
  220.         int index = this.appointmentList.getSelectedIndex();
  221.         if (index != -1)
  222.         {
  223.             this.listeData.slettAvtale(index);
  224.         }
  225.         // If none is selected, request one from the user
  226.         else
  227.         {
  228.             Avtale a = this.listeData.getAvtale();
  229.            
  230.             if(a != null)
  231.             {
  232.                 this.listeData.slettAvtale(a);
  233.             }
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Show details about an appointment
  239.      */
  240.     private void showAppointment()
  241.     {
  242.         // Check if index is -1 (none selected) then display details
  243.         int index = this.appointmentList.getSelectedIndex();
  244.         if (index != -1)
  245.         {
  246.             this.listeData.getAvtale(index).displayDetails();
  247.         }
  248.         // If none is selected, request one from the user
  249.         else
  250.         {
  251.             Avtale a = this.listeData.getAvtale();
  252.            
  253.             if(a != null)
  254.             {
  255.                 a.displayDetails();
  256.             }
  257.         }
  258.     }
  259.  
  260.     /**
  261.      * Edit an appointment
  262.      */
  263.     private void editAppointment()
  264.     {
  265.         // Check if index is -1 (none selected) then edit the Avtale object
  266.         int index = this.appointmentList.getSelectedIndex();
  267.         if (index != -1)
  268.         {
  269.             this.listeData.getAvtale(index).editDetails();
  270.         }
  271.         // If none is selected, request one from the user
  272.         else
  273.         {
  274.             Avtale a = this.listeData.getAvtale();
  275.            
  276.             if(a != null)
  277.             {
  278.                 a.editDetails();
  279.             }
  280.         }
  281.        
  282.         // Refresh the list
  283.         this.appointmentList.updateUI();
  284.     }
  285.  
  286.     /**
  287.      * Exit the application
  288.      */
  289.     private void exit()
  290.     {
  291.         // Dispose of window resources
  292.         dispose();
  293.  
  294.         // Exit application
  295.         System.exit(0);
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement