package framework; import java.util.*; import javax.swing.*; /** * Super class of all classes playing the role of View in * the Model-View-Controller pattern. * * @author pearce * */ public class View extends JPanel implements Observer { private static final long serialVersionUID = 1L; /** * The title on the frame containing this panel */ protected String title; /** * this view is a view of this model */ protected Model theModel; /** * Protected so subclasses can add more menus */ protected JMenuBar menuBar; /** * the file controller handles file menu selections */ private FileController fc; public View(Model model) { theModel = model; theModel.addObserver(this); title = "MVC Framework"; menuBar = new JMenuBar(); fc = new FileController(this); JMenu fileMenu = GUIUtils.makeMenu("&File", new String[] {"&Open", "&Save", "Save&As", "&Exit"}, fc); menuBar.add(fileMenu); } /** * Initializes view to a new model */ public void initView() { theModel.setChanged(); theModel.setChanged(false); } /** * Creates a view frame for this view */ public void show() { initView(); JFrame frame = new JFrame(); frame.setTitle(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); frame.setJMenuBar(menuBar); frame.pack(); frame.setVisible(true); } @Override public void update(Observable o, Object arg) { } public Model getTheModel() { return theModel; } /** * called when there is a new model * @param theModel the new model */ public void setTheModel(Model theModel) { this.theModel = theModel; theModel.addObserver(this); initView(); } }