package sudoku; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; /** * The SudokuHelper class currently creates the window for the Sudoku Helper progarm and inserts a * menu bar. It contains the main method and starts the SudokuPanel. It all menu items should work as advertised. * @author Kyle Wilson * @version 30 November 2006 * code for menu bar adapted from Scribble Program example by Xiaoping Jia Copyright 2002 * */ public class SudokuHelper extends JFrame { /** * Constructs a new Sudoku Helper. * */ public SudokuHelper() { setTitle("Sudoku Helper"); panel = new SudokuPanel(); getContentPane().setLayout(new BorderLayout()); getContentPane().add(createMenuBar(), BorderLayout.NORTH); getContentPane().add(panel, BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (exitAction != null) { exitAction.actionPerformed( new ActionEvent(SudokuHelper.this, 0, null)); } } }); } /** * Creates the menu bar. * @return - the menu bar */ protected JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu menu; JMenuItem mi; // File menu menu = new JMenu("File"); menuBar.add(menu); mi = new JMenuItem("New"); menu.add(mi); mi.addActionListener(new NewFileListener()); mi = new JMenuItem("Open"); menu.add(mi); mi.addActionListener(new OpenFileListener()); mi = new JMenuItem("Save"); menu.add(mi); mi.addActionListener(new SaveFileListener()); mi = new JMenuItem("Save As"); menu.add(mi); mi.addActionListener(new SaveAsFileListener()); menu.add(new JSeparator()); exitAction = new ExitListener(); mi = new JMenuItem("Exit"); menu.add(mi); mi.addActionListener(exitAction); // horizontal space menuBar.add(Box.createHorizontalGlue()); // Help menu menu = new JMenu("Help"); menuBar.add(menu); mi = new JMenuItem("User Guide"); menu.add(mi); mi.addActionListener(new GuideListener()); mi = new JMenuItem("About"); menu.add(mi); mi.addActionListener(new AboutListener()); return menuBar; } /** * Calls the SudokuPanel newFile method and resets the window title. * */ protected void newFile() { currentFilename = null; panel.newFile(); setTitle("Sudoku Helper"); } /** * Prompts the user for a filename, calls the SudokuPanel openFile method and passes the supplied filename. * @param filename - The supplied name of the file to open. */ protected void openFile(String filename) { currentFilename = filename; panel.openFile(filename); setTitle("Sudoku Helper [" + currentFilename + "]"); } /** * Calls the SudokuPanel saveFile method and passes either the default file name of "Untitled" * or the last used file name. * */ protected void saveFile() { if (currentFilename == null) { currentFilename = "Untitled"; } panel.saveFile(currentFilename); setTitle("Sudoku Helper [" + currentFilename + "]"); } /** * Prompts the user for a filename, calls the SudokuPanel saveFile method and passes the supplied filename. * @param filename - supplied file name to save the puzzle. */ protected void saveFileAs(String filename) { currentFilename = filename; panel.saveFile(filename); setTitle("Sudoku Helper [" + currentFilename + "]"); } class NewFileListener implements ActionListener { public void actionPerformed(ActionEvent e) { newFile(); } } /** * Listener for the Open File menu item * @author kyle * */ class OpenFileListener implements ActionListener { public void actionPerformed(ActionEvent e) { int retval = chooser.showDialog(null, "Open"); if (retval == JFileChooser.APPROVE_OPTION) { File theFile = chooser.getSelectedFile(); if (theFile != null) { if (theFile.isFile()) { String filename = chooser.getSelectedFile().getAbsolutePath(); openFile(filename); } } } } } /** * Listener for the Save File menu item * @author kyle * */ class SaveFileListener implements ActionListener { public void actionPerformed(ActionEvent e) { saveFile(); } } /** * Listener for the save as menu item. * @author kyle * */ class SaveAsFileListener implements ActionListener { public void actionPerformed(ActionEvent e) { int retval = chooser.showDialog(null, "Save As"); if (retval == JFileChooser.APPROVE_OPTION) { File theFile = chooser.getSelectedFile(); if (theFile != null) { if (!theFile.isDirectory()) { String filename = chooser.getSelectedFile().getAbsolutePath(); saveFileAs(filename); } } } } } /** * Listener for the Exit menu item. * @author kyle * */ class ExitListener implements ActionListener { public void actionPerformed(ActionEvent e) { int result = JOptionPane.showConfirmDialog(null, "Do you want to exit Sudoku Helper?", "Exit Sudoku Helper?", JOptionPane.YES_NO_OPTION); if (result == JOptionPane.YES_OPTION) { saveFile(); System.exit(0); } } } /** * Listener for the about help menu item. * @author kyle * */ class AboutListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Sudoku Helper v2006.11.30 (final)\nBy Kyle Wilson", "About", JOptionPane.INFORMATION_MESSAGE); } } /** * Listener for the help user guide menu item. * @author kyle * */ class GuideListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Welcome to Sudoku Helper!\n\nSudoku Helper keeps track of information to help you solve a sudoku puzzle.\nEach Row, Column, and 3x3 cluster contains every digit 1-9 without duplicates.\nFirst, enter all known numbers. Hit enter after each number to update Sudoku Helper.\nContinue entering numbers and hitting enter after each one.\nSudoku Helper will let you know if you make a mistake by highlighting a cell in red.\nSudoku Helper lets you save your sudoku puzzles by using the File>Save menu item.\nTo save, simply go to File>Save As and enter a file name and location.\n To open this puzzle at a later time, open Sudoku Helper and go to File>Open and select the file.\n\nWe hope you enjoy using Sudoku Helper!", "User Guide", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args) { int width = 800; int height = 810; JFrame frame = new SudokuHelper(); frame.setSize(width, height); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width/2 - width/2, screenSize.height/2 - height/2); frame.show(); } protected String currentFilename = null; //Current file name to keep track for save rather than save as protected SudokuPanel panel; // the SudokuPanel inside the main window protected ActionListener exitAction; // exit action listnere protected JFileChooser chooser = new JFileChooser("."); // to load a saved file }