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

/****
 *
 * Creates an Easy Grader Window and menubars.
 *
 */

public class EasyGraderUI extends JFrame {
	
	public EasyGraderUI () {
      createAndShowGUI();
   }
	
   /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
   private void createAndShowGUI()
   {
      //Create and set up the window.
      JFrame frame = new JFrame("Easy Grader");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      makeIconImage(frame);
      makeMenuBar(frame);      
      makeToolBar(frame);
      makeContentPane(frame);
      
      //Display the window.
      frame.pack();
      frame.setVisible(true);
   }

   private void makeIconImage(JFrame frame) {
       String urlStr = "http://www.csc.calpoly.edu/~ddrexler/projects/work/grader/images/logo100.png";
       try 
       {
          URL url = new URL(urlStr);
          frame.setIconImage(new ImageIcon(url).getImage());
       } 
       catch (java.net.MalformedURLException e) 
       {         
         throw new Error("unexpected MalformedURLException");
       } 
   }
   
   private void makeMenuBar(JFrame frame) {
      JMenuBar menuBar;
      JMenu menu;
      JMenuItem menuItem;
      
      menuBar = new JMenuBar();
      menu = new file_ui.FileMenu();
      menuBar.add(menu);
      menu = new edit_ui.EditMenu();
      menuBar.add(menu);
      menu = new view_ui.ViewMenu();
      menuBar.add(menu);
      menu = new student_ui.StudentMenu();
      menuBar.add(menu);
      menu = new item_ui.ItemMenu();
      menuBar.add(menu);
      menu = new window_ui.WindowMenu();
      menuBar.add(menu);
      menu = new about_ui.AboutMenu();
      menuBar.add(menu);      
      
      frame.setJMenuBar(menuBar);
   }
   
   private void makeContentPane(JFrame frame) {
     JLabel label = new JLabel();
     label.setOpaque(true);
     label.setPreferredSize(new Dimension(800, 600));

     //add the label to the content pane.
     frame.getContentPane().add(label, BorderLayout.EAST);      
   }
   
   private void makeToolBar(JFrame frame) {
      JToolBar toolBar = new JToolBar();
      toolBar.setFloatable(false);
      toolBar.setRollover(true);
      
      addButtons(toolBar);
      frame.add(toolBar, BorderLayout.NORTH);      
   }
   
   private void addButtons(JToolBar toolBar) {
      JButton button = null;

      //first button
      button = makeNavigationButton("new", "NEW",
                                      "Create a new class or section",
                                      "New");
      button.setBorderPainted(false);
      toolBar.add(button);
      button = makeNavigationButton("open", "OPEN",
                                      "Open an existing class file",
                                      "Open");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("save", "SAVE",
                                      "Writes the entire file to disk",
                                      "Save");
      toolBar.add(button);
      toolBar.addSeparator();
      button.setBorderPainted(false);
      button = makeNavigationButton("print", "PRINT",
                                      "Prints the current class file",
                                      "Print");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("printpreview", "PRINTPREVIEW",
                                      "Preview the printed version of the gradebook",
                                      "Print Preview");
      toolBar.add(button);
      toolBar.addSeparator();
      button.setBorderPainted(false);
      button = makeNavigationButton("cut", "CUT",
                                      "Take the selected data fields and place it onto the clip board",
                                      "Cut");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("copy", "COPY",
                                      "Place a copy of the selected data fields onto the clip board",
                                      "Copy");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("paste", "PASTE",
                                      "Place whatever data fields is on the clip board onto the selected field",
                                      "Paste");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("clear", "CLEAR",
                                      "Place null entries in the selected field",
                                      "Clear");
      toolBar.add(button);
      toolBar.addSeparator();
      button.setBorderPainted(false);
      button = makeNavigationButton("undo", "UNDO",
                                      "Reverse the last change made",
                                      "Undo");
      toolBar.add(button);
      button.setBorderPainted(false);
      button = makeNavigationButton("redo", "REDO",
                                      "Re-apply the last change made",
                                      "Redo");
      toolBar.add(button);
      button.setBorderPainted(false);   
      
   }
   
   protected JButton makeNavigationButton(String imageName,
                                        String actionCommand,
                                        String toolTipText,
                                        String altText) 
   {
     //Look for the image.
     String imgLocation = "http://www.csc.calpoly.edu/~schang/grader/prototype/images/"
                          + imageName
                          + ".gif";
                          
     JButton button = new JButton();
     button.setActionCommand(actionCommand);
     button.setToolTipText(toolTipText);
     //button.addActionListener(this);
                        
     try {
         URL imageURL = new URL(imgLocation);
         button.setIcon(new ImageIcon(imageURL, altText));
     }
     catch (java.net.MalformedURLException e) 
     {         
        button.setText(altText);
        throw new Error("Resource not found: " + imgLocation);
     } 

     return button;
 }

}