editor
Class AbstractDocument

java.lang.Object
  |
  +--editor.AbstractDocument
Direct Known Subclasses:
EditorDocument

public abstract class AbstractDocument
extends Object
implements Document

This class provides a skeleton implementation of Document interface abstracting from pattern syntax and semantic and using listIterator method to iterate over document's characters, wrapped by java.lang.Character objects

Author:
Valter Crescenzi, Alessandro Masci, Gianni Mecca
See Also:
listIterator(int), Character

Constructor Summary
AbstractDocument()
           
 
Method Summary
 void clear()
          Clears document content.
 void copy()
          Overwrites the currently selected region to the clipboard.
 void copyAll()
          Overwrites all document's content to the clipboard.
 void cut()
          Removes the currently selected region and overwrites it to the clipboard.
 void cutAll()
          Cut all document's content and overwrites it to clipboard.
 boolean isEmpty()
          Returns true if document is empty.
protected abstract  ListIterator listIterator(int index)
          This method returns an iterator over document's characters wrapped by java.lang.Character objects.
 boolean loopSearch(String p)
          A version of search to be used in loop conditions.
 void open(Reader reader)
          Reads document content from a characters-stream Reader.
 void paste()
          Pastes the clipboard content to document, immediately following the current position.
 void put(String s)
          Inserts a string after current position.
 void remove()
          Removes current selection.
 boolean replace(String p, String s)
          Replaces the first occurrence of a pattern with a string.
 int replaceAll(String p, String s)
          Replaces all occurrences of a pattern with a string.
 void reset()
          Moves current selection to (0,0).
 void save(Writer writer)
          Saves document content into a characters-stream Writer.
 int size()
          Return the number of characters composing this document.
 String toString()
          Returns a String object representing document's content.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractDocument

public AbstractDocument()
Method Detail

cut

public void cut()
Removes the currently selected region and overwrites it to the clipboard.
Specified by:
cut in interface Document

cutAll

public void cutAll()
Cut all document's content and overwrites it to clipboard. The starting empty region becomes the new selected region.
See Also:
cut()

paste

public void paste()
Pastes the clipboard content to document, immediately following the current position. The region just pasted become the new currently selected region.
Specified by:
paste in interface Document

copy

public void copy()
Overwrites the currently selected region to the clipboard.
Specified by:
copy in interface Document

copyAll

public void copyAll()
Overwrites all document's content to the clipboard. The region comprising all document's content becomes the new selected region.
See Also:
copy()

replace

public boolean replace(String p,
                       String s)
Replaces the first occurrence of a pattern with a string.
Specified by:
replace in interface Document
Parameters:
pattern - pattern to search for and replace
s - string to replace the first occurrence of pattern with
Returns:
true if the pattern is found and replaced; false otherwise.

replaceAll

public int replaceAll(String p,
                      String s)
Replaces all occurrences of a pattern with a string.
Parameters:
pattern - pattern to search for and replace
s - string to replace the first occurrence of pattern with
Returns:
the number of occurrences replaced
See Also:
Document.replace(java.lang.String, java.lang.String)

put

public void put(String s)
Inserts a string after current position. The string just inserted becomes the new current selection.
Specified by:
put in interface Document
Parameters:
s - string to insert

remove

public void remove()
Removes current selection. Clipboard is unaffected by this method and current selection becomes empty.
Specified by:
remove in interface Document

reset

public void reset()
Moves current selection to (0,0).
Specified by:
reset in interface Document

clear

public void clear()
Clears document content. Current selection becomes (0,0)
Specified by:
clear in interface Document

loopSearch

public boolean loopSearch(String p)
A version of search to be used in loop conditions. The search method cannot be use to iterate a searching over document because of potentially indefinite loops. For example (semantic of pattern "b" is obvious):
   doc.clear();
   doc.put("aaaab");
   while (doc.search("b")) {
     System.out.println("found: "+doc.clipboard);
   }
 
will never end because after a successful searching, current position is at the end of document, so that next searching start again from position 0.

This method slightly modify search semantic returning false and moving current selection to (0,0) when invoked with current position set at the end of the document. Following code details new semantic:

   public boolean loopSearch(String p) {
     if (getPosition()==size()) {
       reset(); // Document circularity
       return false;
     }
     return search(p);
   } 
 
Specified by:
loopSearch in interface Document
Parameters:
p - pattern to search for
Returns:
true it the pattern occurrence has been found and selected as current region; false otherwise.
See Also:
Document.search(java.lang.String)

isEmpty

public boolean isEmpty()
Returns true if document is empty.
Specified by:
isEmpty in interface Document
Returns:
true if document is empty; false otherwise.

size

public int size()
Return the number of characters composing this document.
Specified by:
size in interface Document
Returns:
document's size

open

public void open(Reader reader)
          throws IOException
Reads document content from a characters-stream Reader. Old document content is discarded and new document's content is read from a characters stream Reader.
Specified by:
open in interface Document
Parameters:
reader - Reader from which new document's content is read
Throws:
IOException - if an I/O error occurred
See Also:
Document.save(java.io.Writer)

save

public void save(Writer writer)
          throws IOException
Saves document content into a characters-stream Writer.
Specified by:
save in interface Document
Parameters:
writer - Weader into which document's content is written
Throws:
IOException - if an I/O error occurred
See Also:
Document.open(java.io.Reader)

toString

public String toString()
Returns a String object representing document's content.
Specified by:
toString in interface Document
Returns:
a string representation of document's content.
Overrides:
toString in class Object

listIterator

protected abstract ListIterator listIterator(int index)
This method returns an iterator over document's characters wrapped by java.lang.Character objects.
See Also:
Character