Hint 1: It might be better to represent the text of a document using Java's StringBuffer class rather than the String class.
Hint 2: When a view is updated, it will need to know what happened. Was text inserted or deleted? Where did it happen? If text was inserted, what was the text? Fortunately, the Observer update() function allows an additional argument:
interface Observer {
void update(Observable o, Object arg);
}
We can use this extra parameter to pass a message to each observer. We begin by declaring a hierarchy of messages:
class Message { }
class InsertMessage extends Message {
int pos; // = position of insertion
String s; // = inserted text
// etc.
}
// etc.
Here's a sketch of how the Document code might look like:
class Document extends AppModel {
private StringBuffer text = new StringBuffer();
public void insert(int pos, String s) {
text.insert(pos,
s);
setChanged();
Message
msg = new InsertMessage(pos, s);
notifyObservers(this,
msg);
}
// etc.
}
Note that the s parameter in the insert method is a String rather than a single character. I figure this method can be used to insert single characters that are entered through the keyboard or to insert blocks of characters that are pasted into the document. By providing a replace method that calls StringBuffer.replace() you may be able to implement cut and paste.