CIT 591 Assignment 11: General-Purpose Text Translator
Fall 2011, 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.

This program has been designed to make it extremely easy to add new kinds of translations.

Since I want everyone to have some experience creating and working with GUIs, this is an individual assignment (not a team assignment).


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:

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.
Terminates the program. (Closing the window should also terminate the program.)

Put these menu items under the Translate menu:

Makes no changes to the input. This sounds useless, but in fact it is useful for debugging the GUI, and it also provides a template that you can use the next time you want to add a translator. (I have about 20 translators I've written at various times.)
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.
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.
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.
Double space
Replaces each newline in the input with two newlines
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). Consecutive nonblank lines should be joined (with a space in between), then wrapped to 72 characters, as in Wrap Text above. Blank lines separate paragraphs, and paragraphs are never joined. (Note: A "blank line" is any line that is visibly blank; it may contain spaces or tabs, which should be removed.)
Fix Indentation
Given a Java (or Java-like) program, indent it according to the following rules:

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. (I use this program a lot, and trust me, this really is a convenience.)

Only a single translation is performed at any time, and the text in the lower text area is replaced by a translation of the text in the upper text area. If you want to perform two consecutive translations, you would have to do the first translation, then copy the result from the lower text area, paste it into the upper text area, then do the second translation.

Programming details:

class TextTranslator extends JFrame (the “main” class)

The addTranslator method

The main class should have a void addTranslator(TranslatorInterface translator) method to do everything necessary to install the various translators. When setting up the GUI, addTranslator should be called once for each of the eight translators. Here's what the addTranslator method should do:

Adding a translator

For each translator you add to the program:

  1. Create a new class, appropriately named, that implements TranslatorInterface, and
  2. Add a call to addTranslator in your GUI building method (for example, addTranslator(new TextToPowerpoint());).

If you have to do more than this, you are doing it wrong.

class TranslateListener implements ActionListener

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


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

Submit your program, as a single .zip file, to Sakai, before 6am, December 9. Be sure your name is displayed in the Javadoc comments for each class.