CIT 591 Assignment 11: General-Purpose
Text Translator
Fall 2010, David Matuszek
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.
|
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:
The menu items under the Translate menu are:
'\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\ntexttext\n\ntext<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. //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. '\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.
Name your project Translator, your package translator, and your "main" class
Translator.
Create an interface TranslatorInterface that declares the following methods:
String getName();String getDescription();String translate(String text); 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 AllTests.java 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:
ActionListener. Each time you call addTranslateItem, it creates an new object of your ActionListener, and saves the parameter translator in an instance variable. When your ActionListener is used, it uses this instance variable to get the name and description and to do the translation. 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.