Assignment 13 - The JPad Text Editor
In this homework assignment you'll use Java's FileInputStream and FileOutputStream classes to build a simple Java text editor, named JPad. JPad works much like Windows' Notepad program, except that it stores its files in an encrypted form, so your nosey co-workers won't be able to see what you really think of them..
A Look at JPad
JPad is a Java application, not an applet. When you first start the program, it looks like this:
If you don't do anything, it continues to look like this forever or at least until you turn off your computer.
As you can see, the main JPad window contains:
- a TextArea where you can enter text
- a toolbar with four buttons--New, Open, Save, and Save As
- a menu with one choice, File
- a status area at the bottom that displays "Ready"
- a title bar that says JPad - Untitled

If you drop down the File menu, as shown here, you'll see the same selections that are available on the toolbar, along with the Exit menu item.
Opening Files
When you press the Open toolbar button, or select Open from JPad's menu, a file dialog displays all of the JPad files on your system. JPad files are encrypted and are stored with the extension jpad.
Displaying Files
When you open a JPad file, it is loaded into the TextArea as you can see here. Note that the status area reports that the file was read successfully, and that the title bar now gives the whole path to the file.
If a co-worker or an attorney attempts to open the same file in Notepad, they'll be in for a shock, as you can see here.
|
Legal Disclaimer
Since this product uses a proprietary 128-byte encryption method, it is illegal to export JPad outside of the United States. You may freely use this product inside the United States, but you should be aware that the encryption mechanism used is insecure in the hands of a ten-year-old of medium intelligence, or a Border Collie. Your documents will probably be safe from the prying eyes of your attorney, however, unless you attorney owns a Border Collie or knows a ten-year-old of medium intelligence. By using JPad, you agree not to sue me if all of your secret correspondence ends up posted on the Internet or broadcast live during a Senate hearing.
|
Step-By-Step Instructions
Step 1
Download the starter file, JPad.java to a new directory, compile and run it. If you can't get the program to run, don't go any further; post a message on the discussion board and someone hopefully will respond. [Remember that JPad is an application, not an applet.]
Step 2
Add the menu "hot-keys". Read the menu lesson [Lesson 12.6] if you don't remember how to create a MenuShortcut.
Add the following shortcuts to the menu. [Your changes will go in the menuBar() method].
- File|New - Ctrl+N
- File|Open - Ctrl
- File|Save - Ctrl+S
- File|Save As - Ctrl+A
- File|Exit - Ctrl+X.
To complete this portion of the exercise, you'll have to rewrite the method that creates the menu.
Step 3
Write the newFile() method. Follow the steps shown here:
- Set the field fname to "Untitled."
- Clear the TextArea named ta.
- Set the focus to the TextArea.
Step 4
Write the saveFile() method. Here are the different tasks you need to complete:
- Check to see if the field fname equals "Untitled."
- If it does, call the saveFileAs() method.
- If it does not, call the writeFile() method.
Step 5
Write the openFile() method. Here are the different tasks you need to complete:
- Construct and display a FileDialog object. You'll use this, FileDialog.LOAD, and "Open a JPad File" as arguments to the constructor. Set the file mask to "*.jpad" [Instructions on how to do this are in Lesson 13.2]
- If the user cancels the FileDialog, print a message in the status area [send the Label field status the setText() message], set focus back to the TextArea, and return.
- If the user did not cancel the FileDialog, assign the directory and the file name chosen by the user to the field fname, and call readFile().
Step 6
Write the saveFileAs() method. Follow these steps, which are mostly the same as those used for the openFile() method:
- Construct and display a FileDialog object. You'll use this, FileDialog.SAVE, and "Save a JPad File" as arguments to the constructor. Set the file mask to "*.jpad" [Instructions on how to do this are in Lesson 13.2]
- If the user cancels the FileDialog, print a message in the status area [send the Label field status the setText() message], set focus back to the TextArea, and return.
- If the user did not cancel the FileDialog, assign the directory and the file name chosen by the user to the field fname, and call writeFile()..
Step 7
Write the readFile() method. Here are the different tasks you need to complete:
- Check to see if the field fname is equal to "Untitled". If it is, then clear the TextArea, set the focus to the TextArea, and return.
- Create a FileInputStream using fname as the argument. To do this you first:
- Create a FileInputStream variable [fis] and set it to null.
- Start a try block, and call the FileInputStream constructor passing fname as the argument.
- Read the file. [This code goes inside the same try-block as the code in the previous step.] You can see several examples of this in Lesson 13.2. Here's a simple strategy:
- Create an empty [""] String variable and an int named ch.
- Write a while loop that calls fis.read() and assigns the results to ch, until fis.read() returns -1.
- Cast ch to a char, and append it to the end of your String.
- [This is where you'll come back and do the "decryption". For right now, leave this as plain text.]
- Store the results [that is, the String you created in the previous step] in the TextArea using setText(), set the status field to display "File read successfully", and set the title of your JPad window to "JPad - " + fname. [Use the setTitle() method].
- End the try-block, and write a catch-block that catches Exception. In the catch block, simply print a message in the statusLabel.
- Add a finally block to close the file. Look at Lesson 13.2 to see how this must be written..
Step 8
Write the writeFile() method. Here are the different tasks you need to complete:
- Create a FileOutputStream [fout] using fname. You follow the same steps you did to open your FileInputStream:
- Create a FileOutputStream variable [fout] and set it to null.
- Start a try block, and call the FileOutputStream constructor passing fname as the argument.
- Create a String variable [s] and retrieve the contents of the TextArea, using getText(). This code is in the try-block used to open the file.
- Write a for loop that goes from 0 to s.length() - 1. Inside the body of your loop, retrieve each character from your String using the charAt() method. Use the fout.write() method to write the character to the file. This code is also in the try block.
- Set the status Label to say "File written successfully", and set the title of your window to "JPad - " + fname.
- End the try block and follow it with a catch block that catches Exception. In the catch block, use the Label status to display an error message.
- Follow the catch block with a finally block that closes the stream. Look at Lesson 13.2 for an example of how this must be written.
Step 9
Add the Encryption. At this point, you should be able to read, edit, and write regular text files. It's time to add the encryption. Follow these steps:
- Locate the line in writeFile() that says fout.write(ch). Add 128 to ch when you write it.
- Locate the line in readFile() that says s += (char) ch. Change ch so that it uses the following expression: ((ch + 128) % 256).
Finishing Up
Submit your source code. Because this assignment is an application, there is nothing you can post on your Web page, [unless you want to post some pictures showing JPad at work].
|