Assignment 11: Simple Text Editor
Fall 2006, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Write a text editor.

Details:

Your editor should be a Swing GUI application. The GUI will consist of a window with a single scrollable JTextArea filling the entire window, and a single File menu. When you start the program, the window should have a reasonable initial size.

The File menu will have the following menu items: About, Load..., Save, Save As..., and either Quit or Exit (your choice). Choosing one of these menu items does all the usual things. However, since you may not have thought about what all the "usual things" are, here is a detailed description of what each menu item does.

Note 1: The ellipsis (...) after a menu command means that the command will ask for more information before it is completed, usually in a JDialog.

Note 2: A file is considered to be "dirty" if it has been edited but not saved--that is, if the version in memory is different from the version on disk. Your program should use a boolean dirty variable to keep track of this.

About
Opens a message dialog to tell the name of the program and who wrote it.
Load...
If the current file is dirty, opens a dialog to tell the user that the file has not been saved, and offers a chance to save it before proceeding. The usual options are Save, Don't Save, and Cancel. If the current file is not dirty, or if it is dirty and the user doesn't cancel the operation, the editor opens a JFileChooser to allow the user to choose a file. If successful, the contents of the new file will be displayed for editing, and the location of the file is remembered; but if an exception occurs, the editor displays a dialog telling the user about the error.
Save
Replaces the current file on disk (whose location was saved by the Load... command) with the edited contents and, if successful, sets the dirty flag to false. However, if the location is not known (this will happen if the user opens the editor and just starts typing, without first loading a file), this menu choice actually performs a Save As.... If any exceptions occur, use a dialog to inform the user.
Save As...
Opens a JFileChooser to allow the user to select a location in which to save the file. If a file already exists at that location, uses a dialog box to ask the user if the existing file should be replaced. If any exceptions occur, use a dialog to inform the user. If the operation was completed successfully, the chosen location should be saved for use by any subsequent Save commands, and the dirty flag should be set to false.
Exit (or Quit)
If the current file is dirty, opens a dialog to tell the user that the file has not been saved, and offers a chance to save it before proceeding. The usual options are Save, Don't Save, and Cancel. If the current file is not dirty, or if it is dirty and the user doesn't cancel the operation, the editor quits.
If the user clicks the window close button, this should behave exactly as selecting the Exit command (see java.awt.event.WindowAdapter.windowClosing).

The actual editing is done in the JTextArea. This component already supports basic editing capabilities; you don't need to add any features to it.

Any change to the display text should set the dirty flag to true; to accomplish this, see javax.swing.event.DocumentListener.

To add scroll bars (as needed) to a JTextArea, instead of
      myJPanel.add(myJTextArea);
do:
      JScrollPane myJScrollPane = new JScrollPane(myJTextArea);
      myJPanel.add(myJScrollPane);

For just about everything else, execute my SwingExamples.jar, or refer to the Java API.

Note: To prevent possible file corruption (if something happens in the middle of saving the file), the correct way to save a file is to save to a uniquely named temporary file, then replace the original file with the temporary file. You don't have to do this, but I wanted you to know about it.

Division of labor:

This assignment really requires only one class; please call it SimpleTextEditor. The GUI is very simple, so you should be able to get that set up (and the instance variables declared) in lab today. Since I would like everyone to get at least a little bit of experience with file I/O and exception handling, one of you should handle the load command and the other should handle the save commands.

Due date:

Please turn in, via Blackboard, an executable jar file (named SimpleTextEditor.jar) by midnight Sunday, December 10. The jar file should contain your source code as well as your class files.