
// ImageViewer.java
// Michael Kolling and David J Barnes 
// version 0.4
// Modified by Andrew Davison, Jan 2018, ad@fivedots.coe.psu.ac.th

/* ImageViewer is the main class of the image viewer application. It builds
   and displays the application GUI and initialises all other components. 

   This version displays a label and a menu.
   Uses an inner class listener called MenuHandler for the two menu items
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ImageViewer extends JFrame
{
  private JMenuItem openItem, quitItem;   // GUI components with actions

  public ImageViewer()
  {
    super("ImageViewer 0.4");

    // create the GUI
    makeMenuBar();

    Container c = getContentPane();
    JLabel label = new JLabel("I am a label. I can display some text.");
    c.add(label);

    // set close behaviour for JFrame as exit
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pack(); // reduce frame size to fit GUI component
    setVisible(true);
  }  // end of ImageViewer()



  // Create the main frame's menu bar.
  private void makeMenuBar()
  {
    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);
        
    // create the File menu
    JMenu fileMenu = new JMenu("File");
    menubar.add(fileMenu);
        
    openItem = new JMenuItem("Open");
    openItem.addActionListener( new MenuHandler() );
    fileMenu.add(openItem);

    quitItem = new JMenuItem("Quit");
    quitItem.addActionListener( new MenuHandler());
    fileMenu.add(quitItem);
  }  // end of makeMenuBar()


  // ----------------------- inner class ----------------------

  class MenuHandler implements ActionListener
  {

    public void actionPerformed(ActionEvent event) 
    // Receive notification of an action
    { 
      Object src = event.getSource();
      if (src == openItem)
        openFile();
      else if (src == quitItem)
        System.exit(0);
      else
        System.out.println("Cannot process action event for " + 
                                           event.getActionCommand());
    }  // end of actionPerformed()


    private void openFile()
    // open a Swing file chooser to select a new image file
    {
      // test output, until we do this properly
      System.out.println("open file");
    }  // end of openFile()
    
  }  // end of MenuHandler inner class


  // ----------------------------------------------

  public static void main(String[] args) 
  { new ImageViewer(); } 

} // end of ImageViewer class
