CSCI 151 - Lab 7
Creating a Text Editor


Overview

In this lab, we will implement both an array and linked-list version of a data structure to create a text editor.

Materials

Description

We have been learning the internal representations of creating a List in Java. In this lab, we will explore these concepts in the context of creating a text editor.

To make an editor, we will need to store the sequence of entered text characters. We will also need the concept of a cursor, to help us locate where we want to be editing the sequence of text. This cursor will be between characters, and can be moved using the arrow keys. The cursor allows us to insert new characters at the cursor, and delete characters before the cursor with the backspace or delete key.

The text that you enter can then be saved to a file.

Step 1 - Setup

  1. Download the skeleton for this project.
  2. Unpack the code into a new Eclipse Java project.
  3. Add in the JUnit library to your project settings if necessary.
  4. Run the Editor.java file and verify that the GUI is displayed.

Step 2 - DoubleTextNode

The basic building block of our Linked Text View will be a DoubleTextNode. The skeleton of this class is provided for you in the editor.model.implementations package. Much of the manipulation of references can be found locally here in the Node class, simplifying the later implementation of the TextView.

Besides the get methods for each fields, included in the skeleton is an equals method to determine if two DoubleTextNode objects have the same internal data.

Also included is a method called isConsistent method, which will verify that when this node refers to another node with either its next or previous, that node also refers back to this node.

Step 2.1 - Implementation

Here are the methods you need to complete. Node that there is not a setNext or setPrev method included in this implementation. However, if you are inside a class, and talking to another element of the same class, you have the ability to modify the other element's private data members.

public void addAfter(DoubleTextNode other)

Given another node called other, inserts it immediately after this node in the chain of nodes that come next.

public void addBefore(DoubleTextNode other)

Given another node called other, inserts it immediately before this node in the chain of nodes that come previous.

public void removeAfter()

Removes the node that comes immediately after this node in the next chain.

public void removeBefore()

Removes the node that comes immediately before this node in the previous chain.

public DoubleTextNode copyBefore()

Returns a copy of the chain of nodes extending in the previous direction, including this node. This should be a deep copy of the nodes and their data.

public DoubleTextNode copyAfter()

Returns a copy of the chain of nodes extending in the next direction, including this node. This should be a deep copy of the nodes and their data.

public void mergeAfter(DoubleTextNode other)

Splices in the other node into chain after this node, dropping what is currently stored as next.

public void mergeBefore(DoubleTextNode other)

Splices in the other node into chain before this node, dropping what is currently stored as previous.

Step 2.2 - Testing

Run your code through the DoubleTextNodeTest suite to make sure your implementation has the correct behavior.

Step 3 - DoubleTextNodeView

You can now use your DoubleTextNode to implement the DocumentView interface. You can track the cursor implicitly by recording the node before and the node after the cursor, and these nodes will keep track of the rest of the sequence of characters.

Step 3.1 - Implementation

Here are the methods you need to complete. Feel free to add more helper methods if you feel they are necessary to completing your code.

public boolean isEmpty()

Returns true if there are no characters in the sequence.

public void insert(char c)

Adds a new character before the cursor.

public void removeBefore()

Removes the character immediately before the cursor.

public boolean atStart()

Returns true if there is no character before the cursor.

public boolean atEnd()

Returns true if there is no character after the cursor.

public void goRight()

Moves the cursor to the right.

public void goLeft()

Moves the cursor to the left.

public char peekAfter()

Returns the character after the cursor.

public char peekBefore()

Returns the character before the cursor.

public DoubleTextNodeView shallowCopy()

Returns a shallow copy of the DoubleTextNodeView object. No new DoubleTextNodes are created.

public DoubleTextNodeView deepCopy()

Returns a deep copy of the DoubleTextNodeView object. All internal DoubleTextNodes are copied.

public void removeAll()

Resets the sequence to have no characters.

Step 3.2 - Testing

Run your code through the DoubleTextNodeViewTest suite to make sure your implementation has the correct behavior.

Once you have passed the tests, you should be able to edit text in the GUI window.

Step 4 - ArrayView

The ArrayView class implements the same DocumentView interface, but with the central data structure of an array. You will also keep track of the index of the character before the cursor, and the size of the sequence held in the array.

Step 4.1 - Implementation

Proceed to implement the same methods as described in Step 3.1 for the array and associated indices/counters. Feel free to add more helper methods if you feel they are necessary to completing your code. You will find an empty remove method that you might find helpful to implement.

Step 4.2 - Testing

Run your code through the ArrayViewTest suite to make sure your implementation has the correct behavior.

Once you have passed the tests, you should be able to edit text in the GUI window.

Step 5 - Evaluation

Describe in your own words the strengths and weaknesses of your two implementations above. If on a future programming project you are asked to implement a list-like structure, which would you prefer to implement?

What to Hand In

Submit your DoubleTextNode.java, DoubleTextNodeView.java, and ArrayView.java implementations, along with a document for your evaluation in Step 5.

Grading


© Mark Goadrich, Hendrix College