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


screenshot of GUI

Create a GUI that looks like this picture. (I have used a small window; your window should be resizable.)

The menu items under the File menu are:

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 will also terminate the program.)

The menu items under the Translate menu are:

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.
Wrap Lines
Wraps lines so that each line is no 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. 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.
Replaces each tab at the beginning of a line with four spaces.
Single space
Deletes all carriage return ('\r') characters, then replaces consecutive pairs of newline ('\n')characters with single newlines. Thus, one or two '\n's become one '\n', three or four '\n's become two '\n's, etc. For example, text\n\ntext becomes text\ntext, text\n\n\n\n\ntext becomes text\n\ntext, etc.
HTML to Wiki
Converts "<i>", "<em>", "</i>", and "</em>" in text to "//", and converts all "<b>", "<strong>", "</b>", and "</strong>" in text to "**". The quotation marks are not part of the strings. The first group indicates text that is to be italicized; the second group, text to be boldfaced.

Both HTML and Wiki markup are a lot more complicated than this, and wiki markup isn't very well standardized, but this is a good start.
Wiki to HTML
Converts strings "//text//" to "<em>text</em>", and strings "**text**" to "<strong>text</strong>". For simplicity, you can assume that italic and boldface text does not extend across line boundaries.
Zap Gremlins
Deletes "bad" characters. "Good" characters are the ASCII 32 to 126 (decimal), the newline character '\n', the carriage return character '\r', and the tab character '\t'.

Both the upper and the lower text areas are fully editable. When the user clicks the Translate button or chooses an translation from the Translate menu, the specified translation is applied to the text in the upper area, and the result replaces any previous text in the lower area.

Programming details:

Name your project Translator, your package translator, and your "main" class Translator.

Create an interface TranslatorInterface that declares the following methods:

String getName();
Returns the string to be used as a menu item.
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");

Each translator class you write should implement TranslatorInterface. All translator classes you write (yes, even including the identity translator!) should be fully JUnit tested. Include an test suite.

Your main class should have a method addTranslateItem(TranslatorInterface translator) that does all the work of adding a new translator to the GUI. That is, it creates a menu item with the correct name, adds it to the Translate menu, and adds a listener for this menu item that calls the new translator. In addition, the name of the translator is displayed in the GUI title bar, and the description of the translator is displayed just below the title bar (see the above picture for an example). In other words, you should be able to install a new translator to your program by adding a single line to your Translator class, for instance,

     addTranslateItem(new ZapGremlinsTranslator());

The addTranslateItem(TranslatorInterface translator) method is just a GUI building method. It doesn't do anything fancy--for example, it doesn't let the user add menu items to a running program! All it does is:

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

Here's what the "translate" listener does:

The program does only one kind of translation at a time. It does not do all four at once.

Here's the way translation should work.


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, December 2 . 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.