/* TextEdit.java - a simple text editor - Matt Mahoney This program demonstrates simple text editing, menus, and load/save operations. The program displays a menu bar with one menu (File) with 5 menu items: New - clears the screen. Open - prompts the user to enter a file name and loads it. Save - saves the file using the name specified in the last "Open" or "Save As" command. If there is no such name, then prompt for a name as in "Save As". Save As - Prompts the user to enter a file name and saves the file. Exit - quits the program. "Open" and "Save As" use a dialog box that allows the user to navigate directories (a JFileChooser) to select a file. The user has the option to cancel, in which case the screen and disk are not modified and the current name of the file is unchanged. (The program is not exited). If the user specifies a nonexistent or unreadable file in "Open", then the operation is cancelled. If the user specifies an invalid file name or tries to write to a file without write permission in "Save" or "Save As", then the operation is cancelled. In both cases, an error dialog appears and the current name of the file (if any) is unchanged. Unlike most text editors, the program does not bother to ask the user if he/she wishes to save the screen content before "New", "Open" or "Exit" if it has been modified since the last "Save" or "Save As" operation. */ import java.io.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class TextEdit extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); private JMenu fileMenu = new JMenu("File"); private JMenuBar menuBar = new JMenuBar(); private JMenuItem newItem = new JMenuItem("New"); private JMenuItem openItem = new JMenuItem("Open"); private JMenuItem saveItem = new JMenuItem("Save"); private JMenuItem saveAsItem = new JMenuItem("Save As"); private JMenuItem exitItem = new JMenuItem("Exit"); private String filename = null; // set by "Open" or "Save As" public static void main(String args[]) { new TextEdit(); } // Constructor: create a text editor with a menu public TextEdit() { super("Text Editor"); // Create menu and add listeners fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(saveAsItem); fileMenu.add(exitItem); newItem.addActionListener(this); openItem.addActionListener(this); saveItem.addActionListener(this); saveAsItem.addActionListener(this); exitItem.addActionListener(this); menuBar.add(fileMenu); setJMenuBar(menuBar); // Create and display rest of GUI add(new JScrollPane(textArea)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); setVisible(true); } // Handle menu events public void actionPerformed(ActionEvent e) { if (e.getSource() == newItem) textArea.setText(""); else if (e.getSource() == openItem) loadFile(); else if (e.getSource() == saveItem) saveFile(filename); else if (e.getSource() == saveAsItem) saveFile(null); else if (e.getSource() == exitItem) System.exit(0); } // Prompt user to enter filename and load file. Allow user to cancel. // If file is not found, pop up an error and leave screen contents // and filename unchanged. private void loadFile() { JFileChooser fc = new JFileChooser(); String name = null; if (fc.showOpenDialog(null) != JFileChooser.CANCEL_OPTION) name = fc.getSelectedFile().getAbsolutePath(); else return; // user cancelled try { Scanner in = new Scanner(new File(name)); // might fail filename = name; textArea.setText(""); while (in.hasNext()) textArea.append(in.nextLine() + "\n"); in.close(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found: " + name, "Error", JOptionPane.ERROR_MESSAGE); } } // Save named file. If name is null, prompt user and assign to filename. // Allow user to cancel, leaving filename null. Tell user if save is // successful. private void saveFile(String name) { if (name == null) { // get filename from user JFileChooser fc = new JFileChooser(); if (fc.showSaveDialog(null) != JFileChooser.CANCEL_OPTION) name = fc.getSelectedFile().getAbsolutePath(); } if (name != null) { // else user cancelled try { Formatter out = new Formatter(new File(name)); // might fail filename = name; out.format("%s", textArea.getText()); out.close(); JOptionPane.showMessageDialog(null, "Saved to " + filename, "Save File", JOptionPane.PLAIN_MESSAGE); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "Cannot write to file: " + name, "Error", JOptionPane.ERROR_MESSAGE); } } } }