CPSC 225, Fall 2007
Lab 8: Text Files

For this lab, you will be reading and writing text files using Java's standard PrintWriter and Scanner classes.

In class on Friday, we agreed that this lab would be a two-person team effort. During the lab, you should work with your partner in a "pair programming" style. One person types code, while the other watches and comments, and both discuss any issues that arise. After a while, the members of the pair switch places. Before you leave the lab, make arrangements for completing the lab. Decide who will work on what and how. If you are going to use a shared CVS project, create your shared CVS repository before you leave the lab, as discussed below. (There was some information about using CVS in Lab 2.)

To begin the lab, one of you should log in and create a new Eclipse project. Copy the four files from the folder /classes/f07/cs225/files_for_lab_8 into that project.

This lab will be due next Wednesday, October 30. Be sure to let me know before then where to find the CVS repository that contains your project.

Part 1: Save and Open

The main() method for today's lab is in the class TextCollage. You should run the program to see what it does. The program opens a window that contains a large drawing area in the center, a menu bar at the top, and a text-input box at the bottom. When you click the mouse in the drawing area, a copy of the text from the box is added to the drawing area at the point where you click. (If the text box is empty, the string "Hello World!" is used.) There are commands in the Options menu for changing the text color and background color; there are commands in the Edit menu for removing strings that have been added and for restoring strings that have been removed; and there are commands in the File menu for saving and opening files and for saving the contents of the drawing area as an image. All of these commands are already implemented, except for the Save and Open commands.

Your first task is to implement the Save and Open commands. The implementation should go in the doMenuCommand() method at the end of the file DrawTextPanel.java. To implement the Save command, you should use a PrintWriter to output a textual representation of the contents of the drawing area to a file selected by the user. You can use the object named fileChooser, of type SimpleFileChooser, to get the file from the user. Most of the data that you need is stored in the ArrayList named strings. You will also need to save the background color, which can be obtained as canvas.getBackground(). You and your partner should design a representation that will be easy to read back into the program. The main difficulty here is that a string can contain spaces and so is not seen as a single "token" by a Scanner. (A string could, however, be read in by a scanner using the nextLine() command as long as the string is the last thing on a line. Note that the strings in this program do not contain any end-of-line characters.)

After getting the Save command to work, you should save a file and open it in a text editor, to make sure that you have successfully written all the data in the desired format.

Next, you should implement the Open command. This command allows the user to select a file, and it attempts to read the contents of the file back into the program and reconstruct the picture represented by the contents of the file. Of course, this will only succeed if the file is one that was previously saved from the program using the Save command (or if it follows exactly the same syntax as such a file). Note that if an error occurs while trying to read the file, then no change should be made to the current picture.

Note that both the Open and Save commands should report any errors to the user. The implementation of the Save Image command has a few features that you can imitate.

Part 2: A Random Text Collage

After you have Save and Open working, you can get to work on adding a completely new feature to the program: the ability to create a random text collage using strings selected at random from a text file specified by the user. Add a command for doing this to the File menu, and add an implementation of the command to the doMenuCommand() method.

To make a random text collage from a file, you want to pick either random lines or random tokens out of the file and place them at random points in the picture. You might or might not want to use random colors (or other properties) for the strings. You might or might not want to clear the picture before making the collage.

To implement this, I suggest that you use a Scanner to read the file selected by the user. Read the file either line-by-line or token-by-token, and place the strings that you read into a list of type ArrayList<String>. You can then easily select random items from the list. If you use lines of text, you probably want to discard empty lines. If the file contains no data, you would presumably want to treat that as an error.

To try out your work, you might use the file words.txt that can be found in the directory /classes/f07/cs225. This file contains a list of words that is used by a Linux spell-checker, with one word per line. (Warning: I notice that the list is unexpurgated.)

Part 3: Design And Add Another Feature

To complete your program, you and your partner should design at least one additional feature and add it to the program. You should consult with me about what would be appropriate and how to do it. Grading will be based in part on creativity, ambition, and degree of consultation. You will probably have to extend your file format to accommodate the new features; you should keep that in mind as you design the file format.

For example, you could add new options to control the appearance of strings. The DrawTextItem class, which is used to represent the strings that are drawn in the picture, has a variety of properties that can be applied to the strings. Some examples can be found, commented out, in the doMousePressed() method in the DrawTextPanel class. Another possibility is to provide some icons or images that can be "stamped" into the picture, similar to the icon images in Lab 2. Perhaps you can come up with other ideas.


Using CVS for Team Programming

Since you are working with a partner on this lab, you have the option of using a shared CVS repository. CVS is designed to support team programming, with many users having access to the same repository. The user who actually creates the original project can "Share" it, to place it into the repository. Other users can "Import" a copy of the project from CVS. Each user can "Update" their own copy of the project any time they want to get changes that have been made by other users. And each user can "Commit" their copy of the project, in order to copy the changes that they have made into the repository.

When several users are working on the same project, it is possible that two users might change the same part of the same file, and then both users might try to commit their changes. This can result in conflicting changes that cause the second commit to fail. In this case, the second user must modify his copy of the conflicting file until it becomes possible to commit it (or discard his changes, at least temporarily). Note that only incompatible changes to the same part of the same file will cause problems; CVS can handle multiple changes as long as they don't conflict in this way. In order to avoid this sort of problem, users can take responsibility for different parts of the program and avoid making changes to other users' parts. It is also a good idea to follow the following workflow when editing a project: Before making any changes, Update your copy, to be sure that you have the latest version. Then, after you have made some changes and have the program in a working state, Commit the changes. If you want to be really careful, you can Update the project again immediately before doing the Commit; this will uncover any incompatibilities that might have been introduced by another user while you were working.

Large CVS installations usually run a server (called the CVS pserver) to provide access to the projects in the repository. The server can be configured to control access to the various projects and even to allow read-only access for users who are only allowed to check out a project, but not make changes to it.

We are using a simpler access method (called extssh in Eclipse) that uses your regular Linux user name and password and uses standard file system permissions to control access to the projects. Fortunately, AFS, the network file system that use, makes it easy to grant users access to a repository, provided that you do it at the time that you create the repository directory. This means that you should make a new CVS repository that will allow access to both you and your partner (as well as me).

One of you should create the repository directory in your account with a command such as:

          mkdir  cvs-lab8

To grant access to the repository to both your partner and to me, use the commands, where aa0000 should be replaced with your partner's username, and cvs-lab8 should be replaced with whatever name you used for the directory:

          fs  setacl  cvs-lab8  -acl  eck  all
          fs  setacl  cvs-lab8  -acl  aa0000  all

Now, initialize the repository with the following command (replacing /home/zz9999/cvs-lab8 with the full path name of the directory):

          cvs  -d  /home/zz9999/cvs-lab8  init

You will never have to do anything with the repository again, except use it through Eclipse. The first time you want to Share or Import a project in Eclipse, you will have to configure the repository location. Just make sure that the box labeled "Create a new repository location" is checked in the first screen of the "Share Project" or "Checkout from CVS" dialog. When you click "Next", you'll get to the screen where you can enter the repository information.

For the "Host", enter math.hws.edu. For the "Repository Location", enter the full path name of the repository directory, from the "cvs -d ... init" command above. For the "User Name" and "Password" enter your own Linux user name and password (even if the repository is in your partner's account -- you do not have to give away your password to let your partner access the repository!). Make sure that the "Connection Type" is set to extssh. Then, clicking the "Next" button will take you on to the usual Share or Import screens.

David Eck, for CPSC 225