Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mineavtaler;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- * This is the project's main window. The application entry-point is also placed
- * in this class
- *
- * @author Tomas Sandven
- */
- public class MineAvtaler extends JFrame implements ActionListener
- {
- private static final long serialVersionUID = -5700357531421820495L;
- /*
- * GUI Components
- */
- private JScrollPane listScroll;
- private JList appointmentList;
- private JPanel buttonColumn;
- private JButton newAppointmentButton;
- private JButton removeAppointmentButton;
- private JButton showAppointmentButton;
- private JButton editAppointmentButton;
- private JButton exitButton;
- /*
- * Other fields
- */
- private Avtaledata listeData;
- // A mouselistener for the double click event on the list
- private MouseListener appointmentListMouseListener;
- /**
- * The application's entry point
- */
- public static void main(String[] args)
- {
- // Try setting windows default look-and-feel
- try
- {
- String laf = UIManager.getSystemLookAndFeelClassName();
- UIManager.setLookAndFeel(laf);
- }
- catch (Exception e)
- {}
- // Create and display the main window
- MineAvtaler window = new MineAvtaler();
- window.setVisible(true);
- }
- /**
- * MineAvtaler constructor. Sets default settings
- */
- public MineAvtaler()
- {
- /*
- * Initialization
- */
- this.listeData = new Avtaledata();
- // Create a new MouseListener (I hate how I have to override every
- // abstract function)
- this.appointmentListMouseListener = new MouseListener()
- {
- public void mouseClicked(MouseEvent e)
- {
- // If doubleclick
- if (e.getClickCount() == 2)
- {
- // Get the list object and find the index
- JList list = (JList) e.getSource();
- int index = list.getSelectedIndex();
- // If a row is selected...
- if (index != -1)
- {
- // Fetch the ListModel from the JList object, cast it to
- // the Avtaledata class and fetch the selected Avtale
- // object - then display it's details
- ((Avtaledata) list.getModel()).getAvtale(index).displayDetails();
- }
- }
- }
- public void mouseEntered(MouseEvent e)
- {
- }
- public void mouseExited(MouseEvent e)
- {
- }
- public void mousePressed(MouseEvent e)
- {
- }
- public void mouseReleased(MouseEvent e)
- {
- }
- };
- /*
- * Window GUI settings
- */
- // Set window size
- this.setSize(640, 240);
- this.setMinimumSize(new Dimension(640, 240));
- this.setResizable(false);
- // Appear at default OS window location
- this.setLocationByPlatform(true);
- // Set window title
- this.setTitle("Mine avtaler");
- // Initialize application GUI
- this.initializeComponents();
- }
- /**
- * Actionlistener
- */
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource().equals(this.newAppointmentButton))
- {
- this.newAppointment();
- }
- if (e.getSource().equals(this.removeAppointmentButton))
- {
- this.removeAppointment();
- }
- if (e.getSource().equals(this.showAppointmentButton))
- {
- this.showAppointment();
- }
- if (e.getSource().equals(this.editAppointmentButton))
- {
- this.editAppointment();
- }
- if (e.getSource().equals(this.exitButton))
- {
- this.exit();
- }
- }
- /**
- * Initialize GUI components
- *
- * GUI creation convention borrowed from C#. Place all GUI instantiation and
- * configuration in one function and call it from the constructor
- */
- public void initializeComponents()
- {
- // Initialize and configure components
- this.setLayout(new BorderLayout());
- this.listScroll = new JScrollPane();
- this.appointmentList = new JList(this.listeData);
- this.appointmentList.addMouseListener(this.appointmentListMouseListener);
- this.buttonColumn = new JPanel();
- this.buttonColumn.setLayout(new GridLayout(5, 1));
- this.newAppointmentButton = new JButton();
- this.newAppointmentButton.setText("Ny avtale");
- this.newAppointmentButton.addActionListener(this);
- this.removeAppointmentButton = new JButton();
- this.removeAppointmentButton.setText("Fjern avtale");
- this.removeAppointmentButton.addActionListener(this);
- this.showAppointmentButton = new JButton();
- this.showAppointmentButton.setText("Vis avtale");
- this.showAppointmentButton.addActionListener(this);
- this.editAppointmentButton = new JButton();
- this.editAppointmentButton.setText("Endre avtale");
- this.editAppointmentButton.addActionListener(this);
- this.exitButton = new JButton();
- this.exitButton.setText("Avslutt");
- this.exitButton.addActionListener(this);
- // Link components
- this.listScroll.setViewportView(this.appointmentList);
- this.buttonColumn.add(newAppointmentButton);
- this.buttonColumn.add(removeAppointmentButton);
- this.buttonColumn.add(showAppointmentButton);
- this.buttonColumn.add(editAppointmentButton);
- this.buttonColumn.add(exitButton);
- this.add(this.listScroll, BorderLayout.CENTER);
- this.add(this.buttonColumn, BorderLayout.LINE_END);
- }
- /**
- * Add a new appointment to the list
- */
- private void newAppointment()
- {
- this.listeData.nyAvtale();
- }
- /**
- * Remove an appointment from the list
- */
- private void removeAppointment()
- {
- // Check if index is -1 (none selected) then delete that Avtale object
- int index = this.appointmentList.getSelectedIndex();
- if (index != -1)
- {
- this.listeData.slettAvtale(index);
- }
- // If none is selected, request one from the user
- else
- {
- Avtale a = this.listeData.getAvtale();
- if(a != null)
- {
- this.listeData.slettAvtale(a);
- }
- }
- }
- /**
- * Show details about an appointment
- */
- private void showAppointment()
- {
- // Check if index is -1 (none selected) then display details
- int index = this.appointmentList.getSelectedIndex();
- if (index != -1)
- {
- this.listeData.getAvtale(index).displayDetails();
- }
- // If none is selected, request one from the user
- else
- {
- Avtale a = this.listeData.getAvtale();
- if(a != null)
- {
- a.displayDetails();
- }
- }
- }
- /**
- * Edit an appointment
- */
- private void editAppointment()
- {
- // Check if index is -1 (none selected) then edit the Avtale object
- int index = this.appointmentList.getSelectedIndex();
- if (index != -1)
- {
- this.listeData.getAvtale(index).editDetails();
- }
- // If none is selected, request one from the user
- else
- {
- Avtale a = this.listeData.getAvtale();
- if(a != null)
- {
- a.editDetails();
- }
- }
- // Refresh the list
- this.appointmentList.updateUI();
- }
- /**
- * Exit the application
- */
- private void exit()
- {
- // Dispose of window resources
- dispose();
- // Exit application
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement