CIT 590 Assignment 11: General-Purpose Text Translator
Fall 2009, David Matuszek

Purposes of this assignment:

General idea of the assignment:

I often have need to perform some simple translations on text--for example, copying text from a PowerPoint slide and getting rid of all the vertical tab characters. If I need to modify text in this way just once in a while, I can do it manually in a text editor; but if I have to do it frequently, I want a tool that does it for me. This assignment is to create just such a tool.

Details:

screenshot of GUI Create a GUI that looks like this picture. Your GUI should be resizable.

The menu items under the File menu should be:

Load...
Uses a JFileChooser to request a text file from the user, and displays the contents of this file in the upper text area.
Save As...
Uses a JFileChooser to ask the user where to save the file, then saves the contents of the lower text area in this location.
Quit
Terminates the program. (Closing the window should also terminate the program.)

Put these menu items under the Translate menu:

Identity
Makes no changes to the input. This is useful for debugging the GUI, but also provides a model that you can look at the next time you want to add a translator.
Detab
Replaces tabs at the beginning of each line. Each tab is replaced by four spaces. Tabs within the line (that is, after the first non-tab character) are not replaced.
Entab
Replaces each group of four spaces at the beginning of each line with a tab character. For example, if a line begins with ten spaces, the first eight spaces are replaced by two tabs, and the remaining two spaces are unaffected.
Use UNIX newlines
Replace all Windows newline sequences (carriage-return/linefeed, or "\015\012" in Java) with UNIX newlines ("\012").
Use Windows newlines
Replaces all UNIX newlines ("\012") with Windows newline sequences ("\015\012"). However, be careful not to modify existing Windows newlines--that is, do not change "\015\012" into "\015\015\012".
Single space
Replaces consecutive pairs of newlines with a single newline. For example, six consecutive newlines would be changed into three newlines; seven consecutive newlines would be changed into four newlines. All newlines in your output should be correct for the computer you are currently running on; you can find out what to use by calling System.getProperty("line.separator").
Double space
Replaces each newline in the input with two newlines. All newlines in your output should be the right kind for the computer you are currently running on
Wrap Text
Wraps lines so that no line is longer than 72 characters. Long lines should be broken at a space character, with the remainder put on a new line with the same indentation as the original. Very long lines will result in several shorter lines. If a line contains more than 72 consecutive non-space characters (as often happens with a URL), the long "word" is not broken, but is put on a line by itself. This method does not join lines, it only wraps long lines.
Flow Text
Flows text (the way web pages do). Lines longer than 72 characters should be wrapped (as in the Wrap Text translation), and short lines should be joined with the following line, if the following line is nonempty. Separate paragraphs, separated by a blank line, should be kept separate. (Note: A "blank line" is any line that is visibly blank; it may contain spaces or tabs, which should be removed.)
Zap Gremlins
Deletes "bad" characters. "Good" characters are the printable ASCII characters, the space character, the newline character '\n', the carriage return character '\r', and the tab character '\t'.

Both the upper and the lower text areas should be fully editable. When the user clicks the Translate button, the specified translation is applied to the text in the upper area, and the result replaces any previous text in the lower area. The program never changes any text in the upper area.

As a convenience, whenever a translation type is chosen from the Translate menu, the new translation should be automatically applied; the user should not have to explicitly click the Translate button.

Notes

Programming details:

class TextTranslator extends JFrame (the "main" class)

The main class should have an addTranslateItem(TranslatorInterface translator) method to install the various translators. It should be called once for each of the ten translators. Here's what the addTranslateItem method should do:

To add yet another translator (class that implements TranslatorInterface) to the program, all you should have to do is create a new translator of that type, and add a call to addTranslateItem in your GUI building method.

class TranslateListener

Here's what the actionPerformed method of the TranslateListener should do:

interface TranslatorInterface

Create an interface TranslatorInterface that declares the following methods:

String getName();
Returns the string to be used as a menu item and as the title of the GUI window..
String getDescription();
Returns a description of what this translator does.
String translate(String text);
Translates the input to the output. Note that the input is a single string, probably containing newlines, that represents the entire input, and the output is the same. Often this is more convenient. If it is easier for some methods to work with an array of lines, this can be done with
     String[] inputLines = text.split("\n");

Every translation class will implement this interface.

Structure of the assignment:

Grading:

All the usual style rules apply. Good Javadoc comments, JUnit tests for all computations, proper formatting and indentation, methods that fit on a single screen, good method and variable names, etc., etc. Use the names and method signatures specified, etc.

Due date:

Before midnight, Thursday, April 16 . Submit to Blackboard one copy of your project directory, properly zipped. Both your names should be in Javadoc comments in the classes. In addition, whichever of you turns the program in, should put your partner's name in the comments field in Blackboard.