/home/volc/47/gshute/Desktop/cs5741/CS5741_Toolkit/src/appBase/AppBase.java
package appBase;

import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
import vfs.VFSRoot;

/**
 * Class AppBase is used for creating combined
 * applets and standalone applications.
 * This class implements two Template Methods:
 * the applet init() method and the standalone
 * application installApplication() method.
 * Concrete subclasses complete the implementation
 * by implementing the primitive operations getTitle()
 * and createContentPane(), and overriding the hook
 * operations createMenuBar() and the applet start()
 * method.
 * <p>
 * The AppBase class provides implemented helper
 * methods that can be used for constructing applets
 * and aplications: getResourceRoot(), getFontSize(),
 * setFontSize(), and createFontSizeAction().
 * The getResourceRoot() method provides access to
 * resources such as HTML files and icons.
 * The getFontSize() and setFontSize() methods can
 * be used to manage font size uniformly throughout
 * an applet or application.
 * They should not be called in any of the abstract
 * methods but may be called in the start() method.
 * The createFontSizeAction() method returns an
 * action that changes the font size.
 * It can be installed in menus and buttons for user
 * control of the font size.
 * </p>
 */
public abstract class AppBase
        extends JApplet {

  /**
   * createContentPane() is a primitive operation used
   * to create the content pane.
   *  
   * @return the content pane
   */
  protected abstract Container createContentPane();

  /**
   * getTitle() is a primitive operation used to set
   * the title for a standalone application.
   *  
   * @return the menu bar
   */
  protected abstract String getTitle();

  /**
   * createMenuBar() is a hook operation used
   * to create the menu bar.
   * The default returns null, resulting in no menu bar.
   *  
   * @return the menu bar
   */
  protected JMenuBar createMenuBar() {
    return null;
  }

  private boolean applet;

  private void initializeContext(boolean applt) {
    applet = applt;
  }

  /**
   * Install a content pane and menu bar into this
   * applet.
   */
  private void createAppletGUI() {
    getContentPane().add(createContentPane());
    JMenuBar menuBar = createMenuBar();
    if (menuBar != null) {
      setJMenuBar(menuBar);
    }
  }

  /**
   * Create an applet GUI, then transfer the content
   * pane and menu bar into a JFrame.
   */
  private void createApplicationGUI() {
    createAppletGUI();
    JFrame frame = new JFrame(getTitle());
    frame.setContentPane(getContentPane());
    if (getJMenuBar() != null) {
      frame.setJMenuBar(getJMenuBar());
    }
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocation(50, 50);
    frame.setVisible(true);
  }

  /**
   * app.isApplet() returns true if app is an applet,
   * false if it is a standalone application.
   * 
   * @return
   */
  public boolean isApplet() {
    return applet;
  }

  public VFSRoot getResourceRoot(String nm) {
    return new VFSRoot(this.getClass(), nm);
  }

  /**
   * Applet Entry point invoked by a browser.
   */
  public final void init() {
    initializeContext(true);
    // Create GUI on the event-dispatching thread.
    try {
      javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
          createAppletGUI();
        }

      });
    } catch (Exception ex) {
      System.err.println("createAppletGUI() failed");
      ex.printStackTrace(System.err);
    }
  }

  /**
   * Standalone application Entry point.
   */
  public final void installApplication() {
    initializeContext(false);
    // Create GUI on the event-dispatching thread.
    try {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
          createApplicationGUI();
          start();
        }

      });
    } catch (Exception e) {
      System.err.println("createApplicationGUI() failed");
    }
  }

  /**
   * app.setFontSize(sz) sets the font size for app to sz.
   * 
   * @param sz the new font size
   */
  public final void setFontSize(float sz) {
    FontManager.setFontSize(sz);
    SwingUtilities.updateComponentTreeUI(getContentPane());
    if (getJMenuBar() != null) {
      SwingUtilities.updateComponentTreeUI(getJMenuBar());
    }
  }

  /**
   * app.getFontSize() returns the font size of app.
   * 
   * @return the current font size
   */
  public final float getFontSize() {
    return getContentPane().getFont().getSize2D();
  }

  /**
   * app.getFontSize() returns the font size of app.
   * 
   * @return the current font size
   */
  public final Action createFontSizeAction(final float sz) {
    Action fontSizeAction = new AbstractAction("" + sz) {
      public void actionPerformed(ActionEvent ev) {
        setFontSize(sz);
      }
    };
    return fontSizeAction;
  }

}  // public class AppBase