package framework; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.lang.reflect.*; import java.util.*; /** Base class for the application's main window.

Includes working File and Help menus.

Example:

      public class MyAppWindow extends AppWindow {
         public MyAppWindow(MyModel model) {
		    super(model);
		    // add custom views and windows here
	     }
	     public static void main(String[] args) {
			 javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
				   MyModel model = new MyModel();
                   AppWindow gui = new MyAppWindow(model);
                   gui.setVisible(true);
             }
        });
	  }
   }
   
*/ public class AppWindow extends JFrame { protected JMenu fileMenu; protected JMenu helpMenu; /** Subclasses can add more menus to this menu bar */ protected JMenuBar menuBar; /** Subclasses of AppView should be added to this set. */ protected Set views = new HashSet(); /** Change as desired or call Window.pack() */ public static final int FRAME_WIDTH = 700; /** Change as desired or call Window.pack() */ public static final int FRAME_HEIGHT = 400; /** The application model */ protected Model model; /** Used by controllers and views to fetch the current model. @return the current model */ public Model getModel() { return model; } /** Sets the current model to a new model and notifies all views @param m the new model */ public void setModel(Model m) { model = m; for(AppView view: views) { view.setModel(model); } model.notifyViews(); } /** Initializes look&feel, title, model, & menus @param m the initial model */ public AppWindow(Model m) { try { String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lookAndFeel); setDefaultLookAndFeelDecorated(true); } catch (Exception e) { } model = m; setTitle(model.getClass().getName()); setSize(FRAME_WIDTH, FRAME_HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // init menus: menuBar = new JMenuBar(); setJMenuBar(menuBar); fileMenu = FrameworkUtils.makeMenu("&File", new String[] {"&New", "&Save", "Sa&ve As", "&Open", "&Quit"}, new FileMenuController(this)); menuBar.add(fileMenu); helpMenu = FrameworkUtils.makeMenu("&Help", new String[] {"&About", "&Help"}, new HelpMenuController(this)); menuBar.add(helpMenu); } /** Adds a view to the views set. This should be called in subclasses to add new views.

This method also sets the model and adds the view to the JFrame, which I think works for Java 5.0. @param view The new view */ protected void addView(AppView view) { views.add(view); view.setModel(model); add(view); } /** Called when the user selects Help/Help. This method should be overridden by subclasses. */ public void help() { FrameworkUtils.error("Sorry, this item isn't implemented"); } /** Called when the user selects Help/About. */ protected void about() { JOptionPane.showMessageDialog(null, new String[] {"Application Framework", "Copyright(c) 2001", "All rights reserved"}, "About", JOptionPane.INFORMATION_MESSAGE); } }