Homework 2: Simple Text Editor, part 1

Overview

For this assignment, you will write a simple text editor as a Swing application. There will be three major classes. One class will represent a text document. A second class will extend JComponent, and will represent the area upon which the document will be displayed. It will also handle events coming from the keyboard. A third class will extend JFrame. This will be the GUI itself. It will include an object of the previously mentioned JComponent subclass.

Representing a Text Document

This is the most complex of the three classes to implement. The document itself should be represented by a doubly linked list. Each node of the list will contain one character from the document. Your class should implement the Document interface.

You will need to create two inner classes. They should both be private. One of these inner classes should implement the Cursor interface. Objects of this class represent positions in the document that are between characters. The physically visible cursor will itself be represented by such an object, although you will find that they have other uses as well.

The other inner class will represent a node in the doubly linked list. It should have data members representing the next node, the previous node, and the character it represents.

There are many methods to be implemented for this part of the assignment. However, they should all be fairly short (no more than about 10 lines of code for each method).

The JComponent subclass

The JComponent subclass is responsible for drawing the document in a window. It will take advantage of the Cursor to iterate through the document and obtain characters from it. It will also display a physical cursor. It will handle keyboard events. This means that, while typing, any character inserted will be inserted into the document, and any character deleted will be deleted from the document. Both of the delete keys should be handled as well. Only the left and right arrow keys need to be handled. Make sure to implement the getPreferredSize() method.

Place the JComponent inside a JScrollPane. This will automatically handle out-of-bound characters and lines.

The JFrame subclass

This file will represent "glue code" to put the pieces together. It should include a "File" menu with the following options: Each option should be correctly implemented. When a new document is created, the user should be asked to save the previous document if it is not saved.

Testing

You will need to write a unit test for your implementation of the Document interface. You should be especially careful to test methods that are not otherwise used in your program. You should also write and submit a test plan for the GUI part of the program.