package framework; import java.awt.event.*; import java.awt.*; import java.io.*; import javax.swing.*; /** The controller for all file menu items */ public class FileMenuController extends Controller { /** Gateway for doing the actual saves and reads */ private FileSystemGateway gateway; /** Used to remember where we were the last time a file dialog box was opened. This saves the user a lot of trouble navigating back to where his files are. */ private File currentDirectory; /** Initializes the gateway. @param aw the AppWindow that created this controller. */ public FileMenuController(AppWindow aw) { super(aw); gateway = new FileSystemGateway(); currentDirectory = new File("."); } /** Handles all commands coming from the file menu. @param e the action event fired by the selected menu item. */ public void actionPerformed(ActionEvent e) { try { if (e.getSource() instanceof JMenuItem) { String arg = e.getActionCommand(); if (arg.equals("New")) handleNew(); else if (arg.equals("Open")) handleOpen(); else if (arg.equals("Save")) handleSave(); else if (arg.equals("Save As")) handleSaveAs(); else if (arg.equals("Quit")) handleQuit(); } } catch (Exception x) { FrameworkUtils.error("Exception thrown: " + x); } } /** * Prompts user to save changes if necessary */ private void saveChanges() throws AppError { Model model = appWindow.getModel(); if (model.getUnsavedChanges()) { int response = JOptionPane.showConfirmDialog( null, "Save changes?", "Save Changes?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (response == 0) handleSave(); } } /** Prompts the user for the file name if necessary. @param open True if we are opening a file, false if saving. */ private String getFileName(boolean open) { JFileChooser fd = new JFileChooser(); fd.setCurrentDirectory(currentDirectory); if (open) { fd.showOpenDialog(appWindow); } else { fd.showSaveDialog(appWindow); } currentDirectory = fd.getCurrentDirectory(); return fd.getSelectedFile().getName(); } /** Called when the user selects File/New.
Uses reflection to create a new instance of Model.
Note that the new model will be the same type as the old model. Of course this could easily be made cooler by allowing different types of models to be created. But that makes things a bit more complicated elsewhere. */ public void handleNew() throws Exception { saveChanges(); Class c = appWindow.getModel().getClass(); appWindow.setModel((Model)c.newInstance()); } /** Called when the user selects File/Open */ public void handleOpen() throws AppError { saveChanges(); String fname = getFileName(true); if (fname != null) { appWindow.setModel(gateway.getModel(fname)); } } /** Called when the user selects File/SaveAs */ public void handleSaveAs() throws AppError { String fname = getFileName(false); Model model = appWindow.getModel(); model.setFname(fname); model.setUnsavedChanges(false); gateway.saveModelAs(model, fname); } /** Called when the user selects File/Save. This method calls saveAs if it's the first time the model is being saved. */ public void handleSave() throws AppError { Model model = appWindow.getModel(); String fname = model.getFname(); if (fname == null) { handleSaveAs(); } else { model.setUnsavedChanges(false); gateway.saveModel(model); } } /** Called when the user selects File/Quit. Saves changes and shuts down. */ protected void handleQuit() throws AppError { saveChanges(); System.exit(0); } }